home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / sim / h8500 / compile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-09  |  50.8 KB  |  2,422 lines

  1. /* Simulator for the Hitachi H8/500 architecture.
  2.  
  3.    Written by Steve Chamberlain of Cygnus Support.
  4.    sac@cygnus.com
  5.  
  6.    This file is part of H8/500 sim
  7.  
  8.  
  9.         THIS SOFTWARE IS NOT COPYRIGHTED
  10.  
  11.    Cygnus offers the following for use in the public domain.  Cygnus
  12.    makes no warranty with regard to the software or it's performance
  13.    and the user accepts the software "AS IS" with all faults.
  14.  
  15.    CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO
  16.    THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17.    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  
  19. */
  20.  
  21. #include <signal.h>
  22. #include <sys/times.h>
  23. #include <sys/param.h>
  24. #include <setjmp.h>
  25. #include "ansidecl.h"
  26. #include "sysdep.h"
  27. #include "remote-sim.h"
  28.  
  29. #define O_RECOMPILE 85
  30. #define DEFINE_TABLE
  31. #define DISASSEMBLER_TABLE
  32.  
  33.  
  34. int debug;
  35.  
  36. /* This code can be compiled with any old C compiler, in which case
  37.    four or five switch statements will be executed for each
  38.    instruction simulated.  It can be compiled with GCC, then the
  39.    simulated instructions thread through the code fragments, and
  40.    everything goes much faster.
  41.  
  42.    These definitions make the code work either way
  43. */
  44. #ifdef __GNUC__
  45. #define DISPATCH(X) goto *(X); do
  46. #define LABEL(X) X##_L
  47. #define LABELN(X,N) X##_L##N
  48. #define LABEL_REF(X) &&X##_L
  49. #define LABEL_REFN(X,N) &&X##_L##N
  50. #define ENDDISPATCH while (0);
  51. #define fastref void *
  52.  
  53. #define DEFAULT ;
  54. #define INLINE __inline__
  55. #else
  56. #define DEFAULT default :
  57. #define DISPATCH(X) switch (X)
  58. #define LABEL(X) case X
  59. #define LABELN(X,N) case X
  60. #define LABEL_REF(X) X
  61. #define LABEL_REFN(X,N) X
  62. #define ENDDISPATCH
  63. #define fastref int
  64.  
  65.  
  66.  
  67. #define INLINE
  68. #define STORE_REG_B     1
  69. #define STORE_REG_W     2
  70. #define STORE_INC_B     3
  71. #define STORE_INC_W     4
  72. #define STORE_DEC_B     5
  73. #define STORE_DEC_W     6
  74. #define STORE_DISP_B     7
  75. #define STORE_DISP_W     8
  76. #define STORE_CRB     9
  77. #define STORE_CRW     10
  78. #define STORE_REG_L     11
  79. #define STORE_NOP     12
  80.  
  81. #define FETCH_NOP     9
  82. #define FETCH_REG_B     10
  83. #define FETCH_REG_W     11
  84. #define FETCH_INC_B     12
  85. #define FETCH_INC_W     13
  86. #define FETCH_DEC_B     14
  87. #define FETCH_DEC_W     15
  88. #define FETCH_DISP_B     16
  89. #define FETCH_DISP_W     17
  90. #define FETCH_IMM     18
  91. #define FETCH_CRB     19
  92. #define FETCH_CRW     20
  93. #define FETCH_LVAL     21
  94. #define FETCH_LVAL24     22
  95. #define FETCH_REG_L     23
  96.  
  97. #define FLAG_m         20
  98. #define FLAG_M         21
  99. #define FLAG_A         22
  100. #define FLAG_NONE     23
  101. #define FLAG_NOSTORE     24
  102. #define FLAG_CLEAR     25
  103. #define FLAG_a         26
  104. #define FLAG_BRANCH     27
  105. #define FLAG_special     28
  106.  
  107. #define FLAG_shiftword     29
  108. #define FLAG_shiftbyte     30
  109.  
  110. #define FLAG_multword     31
  111. #define FLAG_multbyte     32
  112. #endif
  113.  
  114.  
  115. #define h8500_table h8500_compile_table
  116. #include "../opcodes/h8500-opc.h"
  117.  
  118. #include "inst.h"
  119.  
  120. #define LOW_BYTE(x) ((x) & 0xff)
  121. #define HIGH_BYTE(x) (((x)>>8) & 0xff)
  122. #define NORMAL_CP ((cpu.regs[R_CP].c - cpu.memory)>>16)
  123. #define NORMAL_DP ((cpu.regs[R_DP].c - cpu.memory)>>16)
  124. #define NORMAL_EP ((cpu.regs[R_EP].c - cpu.memory)>>16)
  125. #define NORMAL_TP ((cpu.regs[R_TP].c - cpu.memory)>>16)
  126. #define SET_NORMREG(x,y) ((cpu.regs[x].l = (y)))
  127. #define GET_NORMREG(x) (cpu.regs[x].l)
  128. #define SET_SEGREG(x,y) { cpu.regs[x].c = ((y) & 0xff0000) + cpu.memory;}
  129. #define GET_SEGREG(x)  ( (cpu.regs[x].c  - cpu.memory ) >> 16)
  130. #define SET_NORMAL_CPPC(x) { pc = (x) & 0xffff; SET_SEGREG(R_CP, (x));}
  131. #define NORMAL_SR ((N<<3)|(Z<<2)|(V<<1)|(C))
  132. #define P(X,Y) ((X<<8) | Y)
  133.  
  134. #define BUILDSR()   cpu.regs[R_SR].s[LOW] = (N << 3) | (Z << 2) | (V<<1) | C;
  135.  
  136. #define GETSR()            \
  137.   C = (cpu.regs[R_SR].s[LOW] >> 0) & 1;\
  138.   V = (cpu.regs[R_SR].s[LOW] >> 1) & 1;\
  139.   Z = (cpu.regs[R_SR].s[LOW] >> 2) & 1;\
  140.   N = (cpu.regs[R_SR].s[LOW] >> 3) & 1;
  141.  
  142. #ifdef __CHAR_IS_SIGNED__
  143. #define SEXTCHAR(x) ((char)(x))
  144. #endif
  145.  
  146. #ifndef SEXTCHAR
  147. #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff):x)
  148. #endif
  149.  
  150. #define SEXTSHORT(x) ((short)(x))
  151.  
  152. /* Which segment registers go with which pointer registers */
  153. static unsigned char **segmap[R_LAST];
  154. static unsigned char *(regptr[R_LAST][3]);
  155. static unsigned char *(segregptr[R_LAST][3]);
  156. static cpu_state_type cpu;
  157.  
  158. static int segforreg[] = {R_DP, R_DP, R_DP, R_DP,
  159.                 R_EP, R_EP, R_TP, R_TP,
  160.                 R_DP, R_DP, R_DP, R_DP,
  161.                 R_EP, R_EP, R_TP, R_TP};
  162. int LOW;
  163. int HIGH;
  164.  
  165. /* routines for getting and storing args */
  166. #define elval(struct, lit) \
  167.  (((*(struct.reg.wptr) + lit) & 0xffff) + (*(struct.r2.segreg)))
  168.  
  169. #define displval(s) elval((s),(s).literal)
  170.  
  171. #define ireglval(struct) elval(struct, 0)
  172. #define wordat(x) (((x)[0] << 8) | (x)[1])
  173. #define longat(x) ((wordat((x))<<16)|(wordat((x)+2)))
  174. #define byteat(x) ((x)[0])
  175.  
  176. #define setwordat(x,y) {x[0] =( y)>>8; x[1] = y;}
  177. #define setbyteat(x,y) {x[0] = y;}
  178.  
  179. /*#define setalignedwordat(x,y) {((short *)x)[0] =y;}*/
  180. /*
  181. statics
  182. */
  183.  
  184. ea_type rd;
  185. ea_type rs;
  186. ea_type imm;
  187. ea_type cr;
  188. ea_type ea;
  189. ea_type nop;
  190. ea_type lval;
  191. ea_type lval24;
  192.  
  193. ea_type eavector[2];
  194.  
  195. int disp;
  196.  
  197. #define JBYTE 0
  198. #define JWORD 1
  199. #define JLONG 2
  200.  
  201. typedef union
  202. {
  203.   struct
  204.     {
  205.       fastref srcabyte;
  206.       fastref srcaword;
  207.       fastref srcalong;
  208.  
  209.       fastref srcbbyte;
  210.       fastref srcbword;
  211.       fastref srcblong;
  212.  
  213.       fastref dstbyte;
  214.       fastref dstword;
  215.       fastref dstlong;
  216.     } s;
  217.   struct
  218.     {
  219.       fastref byte;
  220.       fastref word;
  221.       fastref lon;
  222.     } a[3];
  223.  
  224.   fastref j[9];
  225. } size_ptr;
  226.  
  227. union
  228. {
  229.   struct ea_struct
  230.     {
  231.       size_ptr ea_nop;
  232.       size_ptr ea_reg;
  233.       size_ptr ea_inc;
  234.       size_ptr ea_dec;
  235.       size_ptr ea_disp;
  236.  
  237.       size_ptr ea_imm;
  238.       size_ptr ea_cr;
  239.       size_ptr ea_lval;
  240.       size_ptr ea_lval24;
  241.     } s;
  242. #define N_EATYPES (sizeof(struct ea_struct) / sizeof(size_ptr))
  243.   size_ptr a[N_EATYPES];
  244. } eas;
  245.  
  246. /* This function takes an ea structure filled in for the 1st source
  247.  operand and modifies it to be for either the 1st, 2nd or dst operand */
  248.  
  249. static void
  250. howto_workout (encoded, semiencoded, n)
  251.      ea_type *encoded;
  252.      ea_type *semiencoded;
  253.      int n;
  254. {
  255.   int i;
  256.   *encoded = *semiencoded;
  257.  
  258.   for (i = 0; i < N_EATYPES; i++)
  259.     {
  260.       if (encoded->type == eas.a[i].s.srcabyte)
  261.     {
  262.       encoded->type = eas.a[i].a[n].byte;
  263.       return;
  264.     }
  265.       else if (encoded->type == eas.a[i].s.srcaword)
  266.     {
  267.       encoded->type = eas.a[i].a[n].word;
  268.       return;
  269.     }
  270.       else if (encoded->type == eas.a[i].s.srcalong)
  271.     {
  272.       encoded->type = eas.a[i].a[n].lon;
  273.       return;
  274.     }
  275.     }
  276.  
  277.   abort ();
  278. }
  279.  
  280. fastref flag_shiftword;
  281. fastref flag_shiftbyte;
  282. fastref flag_multword;
  283. fastref flag_multbyte;
  284. fastref flag_mp;
  285. fastref flag_special;
  286. fastref flag_Mp;
  287. fastref flag_ap;
  288. fastref flag_Ap;
  289. fastref flag_nonep;
  290. fastref flag_nostorep;
  291. fastref flag_clearp;
  292. fastref flag_branch;
  293. fastref exec_dispatch[100];
  294.  
  295. static int
  296. get_now ()
  297. {
  298.   return time ((char *) 0);
  299. }
  300.  
  301. static int
  302. now_persec ()
  303. {
  304.   return 1;
  305. }
  306.  
  307. static void
  308. gotcr (ptr, n)
  309.      ea_type *ptr;
  310.      int n;
  311. {
  312.   int size;
  313.   n &= 0x7;
  314.   if (n == 0)
  315.     {
  316.       abort ();
  317.     }
  318.   else
  319.     {
  320.       ptr->type = eas.s.ea_cr.j[JBYTE];
  321.       ptr->reg.bptr = segregptr[n][JLONG];
  322.     }
  323. }
  324. static void
  325. gotreg (ptr, n, size)
  326.      ea_type *ptr;
  327.      int n;
  328.      int size;
  329. {
  330.   n &= 0x7;
  331.   ptr->type = eas.s.ea_reg.j[size];
  332.   ptr->reg.bptr = regptr[n][size];
  333. }
  334.  
  335. static void
  336. gotinc (ptr, n, inc, size)
  337.      ea_type *ptr;
  338.      int n;
  339.      int size;
  340. {
  341.   n &= 0x7;
  342.   if (inc > 0)
  343.     {
  344.       ptr->type = eas.s.ea_inc.j[size];
  345.     }
  346.   else
  347.     {
  348.       ptr->type = eas.s.ea_dec.j[size];
  349.     }
  350.   ptr->reg.bptr = regptr[n][JWORD];
  351.   ptr->r2.segreg = segmap[n];
  352. }
  353.  
  354.  
  355. static void
  356. gotabs (ptr, disp, reg, size)
  357.      ea_type *ptr;
  358.      int disp;
  359.      int reg;
  360.      int size;
  361. {
  362.   ptr->type = eas.s.ea_disp.j[size];
  363.   ptr->reg.bptr = regptr[reg][JWORD];
  364.   ptr->r2.segreg = segmap[reg];
  365.   ptr->literal = disp;
  366. }
  367.  
  368. static void
  369. gotind (ptr, disp, reg, size)
  370.      ea_type *ptr;
  371.      int disp;
  372.      int reg;
  373.      int size;
  374. {
  375.   gotabs (ptr, disp, reg & 0x7, size);
  376. }
  377.  
  378. static void
  379. gotimm (ptr, val)
  380.      ea_type *ptr;
  381.      int val;
  382. {
  383.   ptr->type = eas.s.ea_imm.j[0];
  384.   ptr->literal = val;
  385. }
  386.  
  387. static void
  388. indoff (ptr)
  389.      ea_type *ptr;
  390. {
  391.   int i;
  392.   for (i = 0; i < 6; i++)
  393.     {
  394.       if (ptr->type == eas.s.ea_disp.j[i])
  395.     {
  396.       ptr->type = eas.s.ea_lval.j[i];
  397.       return;
  398.     }
  399.     }
  400. }
  401.  
  402. thinkabout_shifts (d, bytesized)
  403.      decoded_inst *d;
  404.      int bytesized;
  405. {
  406.   if (bytesized)
  407.     {
  408.       /* Got a byte shift, fake up second arg */
  409.       d->srcb.type = eas.s.ea_imm.s.srcbword;
  410.       d->srcb.literal = 8;
  411.     }
  412.   else
  413.     {
  414.       /* got a word shift, fake up second arg */
  415.       d->srcb.type = eas.s.ea_imm.s.srcbword;
  416.       d->srcb.literal = 16;
  417.     }
  418. }
  419.  
  420. /* Calculate the number of cycles required to run this
  421.    instruction
  422. */
  423. static void
  424. compcycles (dst, opcode)
  425.      decoded_inst *dst;
  426.      h8500_opcode_info *opcode;
  427. {
  428.   int cycles = 0;
  429.   /* Guess for the time being - 1 cycle for the first two bytes in the
  430.      opcode - to fecth the operand, and 3 cycles for all the rest of
  431.      the bytes, since they mean that there is probably an operand to
  432.      fetch */
  433.  
  434.   switch (opcode->length)
  435.     {
  436.     case 1:
  437.     case 2:
  438.       cycles += opcode->length;
  439.       break;
  440.     default:
  441.       cycles += opcode->length * 3;
  442.       break;
  443.     }
  444.  
  445.   dst->cycles = cycles;
  446. }
  447.  
  448. static void
  449. translate (ptr, from, to)
  450.      ea_type *ptr;
  451.      fastref from;
  452.      fastref to;
  453. {
  454.   if (ptr->reg.wptr == &cpu.regs[7].s[LOW]
  455.       && ptr->type == from)
  456.     {
  457.       ptr->type = to;
  458.     }
  459. }
  460.  
  461. static
  462. void
  463. fix_incdecs (dst)
  464.      decoded_inst *dst;
  465. {
  466.   if (dst->dst.type == eas.s.ea_inc.s.dstbyte
  467.       && (dst->srca.type == eas.s.ea_inc.s.srcabyte
  468.       || dst->srcb.type == eas.s.ea_inc.s.srcbbyte))
  469.     {
  470.       dst->dst.type = eas.s.ea_disp.s.dstbyte;
  471.     }
  472.  
  473.   if (dst->dst.type == eas.s.ea_inc.s.dstword
  474.       && (dst->srca.type == eas.s.ea_inc.s.srcaword
  475.       || dst->srcb.type == eas.s.ea_inc.s.srcbword))
  476.     {
  477.       dst->dst.type = eas.s.ea_disp.s.dstword;
  478.     }
  479.  
  480.   if (dst->dst.type == eas.s.ea_dec.s.dstbyte
  481.       || dst->dst.type == eas.s.ea_dec.s.dstword)
  482.     {
  483.       if (dst->srca.type == eas.s.ea_dec.s.srcabyte)
  484.     {
  485.       dst->srca.type = eas.s.ea_disp.s.srcabyte;
  486.     }
  487.       else if (dst->srca.type == eas.s.ea_dec.s.srcaword)
  488.     {
  489.       dst->srca.type = eas.s.ea_disp.s.srcaword;
  490.     }
  491.       else if (dst->srcb.type == eas.s.ea_dec.s.srcbbyte)
  492.     {
  493.       dst->srcb.type = eas.s.ea_disp.s.srcbbyte;
  494.     }
  495.       else if (dst->srcb.type == eas.s.ea_dec.s.srcbword)
  496.     {
  497.       dst->srcb.type = eas.s.ea_disp.s.srcbword;
  498.     }
  499.     }
  500.  
  501.  
  502.   /* Turn a byte ops from the sp into word ops */
  503.   translate (&dst->dst, eas.s.ea_dec.s.dstbyte, eas.s.ea_dec.s.dstword);
  504.   translate (&dst->dst, eas.s.ea_inc.s.dstbyte, eas.s.ea_inc.s.dstword);
  505.  
  506.   translate (&dst->srca, eas.s.ea_dec.s.srcabyte, eas.s.ea_dec.s.srcaword);
  507.   translate (&dst->srca, eas.s.ea_inc.s.srcabyte, eas.s.ea_inc.s.srcaword);
  508.  
  509.   translate (&dst->srcb, eas.s.ea_dec.s.srcbbyte, eas.s.ea_dec.s.srcbword);
  510.   translate (&dst->srcb, eas.s.ea_inc.s.srcbbyte, eas.s.ea_inc.s.srcbword);
  511.  
  512.  
  513. }
  514.  
  515.  
  516. static void
  517. find (pc, buffer, dst)
  518.      int pc;
  519.      unsigned char *buffer;
  520.      decoded_inst *dst;
  521. {
  522.   h8500_opcode_info *opcode;
  523.   int i;
  524.   int idx;
  525.   int hadimm = 0;
  526.   dst->srca.reg.rptr = 0;
  527.  
  528.   /* Run down the table to find the one which matches */
  529.   for (opcode = h8500_table; opcode->name; opcode++)
  530.     {
  531.       int byte;
  532.       int rn;
  533.       int rd;
  534.       int rs;
  535.       int disp;
  536.       int abs;
  537.       int imm;
  538.       int pcrel;
  539.       int qim;
  540.       int i;
  541.       int cr;
  542.  
  543.  
  544.       dst->opcode = exec_dispatch[opcode->flavor & 0x7f];
  545.  
  546.       for (byte = 0; byte < opcode->length; byte++)
  547.     {
  548.       if ((buffer[byte] & opcode->bytes[byte].mask)
  549.           != (opcode->bytes[byte].contents))
  550.         {
  551.           goto next;
  552.         }
  553.       else
  554.         {
  555.           /* extract any info parts */
  556.           switch (opcode->bytes[byte].insert)
  557.         {
  558.         case 0:
  559.         case FP:
  560.           break;
  561.         default:
  562.           abort ();
  563.           break;
  564.         case RN:
  565.           rn = buffer[byte] & 0x7;
  566.           break;
  567.         case RS:
  568.           rs = buffer[byte] & 0x7;
  569.           break;
  570.         case CRB:
  571.           cr = buffer[byte] & 0x7;
  572.           if (cr == 0)
  573.             goto next;
  574.           break;
  575.         case CRW:
  576.           cr = buffer[byte] & 0x7;
  577.           if (cr != 0)
  578.             goto next;
  579.           break;
  580.         case DISP16:
  581.           disp = (buffer[byte] << 8) | (buffer[byte + 1]);
  582.           break;
  583.         case FPIND_D8:
  584.         case DISP8:
  585.           disp = ((char) (buffer[byte]));
  586.           break;
  587.         case RD:
  588.         case RDIND:
  589.           rd = buffer[byte] & 0x7;
  590.           break;
  591.         case ABS24:
  592.           abs =
  593.             (buffer[byte] << 16)
  594.             | (buffer[byte + 1] << 8)
  595.             | (buffer[byte + 2]);
  596.           break;
  597.         case ABS16:
  598.           abs = (buffer[byte] << 8) | (buffer[byte + 1]);
  599.           break;
  600.         case ABS8:
  601.           abs = (buffer[byte]);
  602.           break;
  603.         case IMM16:
  604.           imm = (buffer[byte] << 8) | (buffer[byte + 1]);
  605.           break;
  606.         case IMM4:
  607.           imm = (buffer[byte]) & 0xf;
  608.           break;
  609.         case IMM8:
  610.         case RLIST:
  611.           imm = SEXTCHAR (buffer[byte]);
  612.           break;
  613.         case PCREL16:
  614.           pcrel = SEXTSHORT ((buffer[byte] << 8) | (buffer[byte + 1]));
  615.           break;
  616.         case PCREL8:
  617.           pcrel = SEXTCHAR ((buffer[byte]));
  618.           break;
  619.         case QIM:
  620.           switch (buffer[byte] & 0x7)
  621.             {
  622.             case 0:
  623.               imm = 1;
  624.               break;
  625.             case 1:
  626.               imm = 2;
  627.               break;
  628.             case 4:
  629.               imm = -1;
  630.               break;
  631.             case 5:
  632.               imm = -2;
  633.               break;
  634.             }
  635.           break;
  636.  
  637.         }
  638.         }
  639.     }
  640.       if (opcode->flavor & O_BYTE)
  641.     {
  642.       idx = 0;
  643.       switch (opcode->flags)
  644.         {
  645.         case 'h':
  646.           dst->flags = flag_shiftbyte;
  647.           break;
  648.         case 'p':
  649.           dst->flags = flag_multbyte;
  650.           break;
  651.         case 'B':
  652.           dst->flags = flag_branch;
  653.           break;
  654.         case 'm':
  655.           dst->flags = flag_mp;
  656.           break;
  657.         case 'a':
  658.           dst->flags = flag_ap;
  659.           break;
  660.         case '-':
  661.           dst->flags = flag_nonep;
  662.           break;
  663.         case 0:
  664.           dst->flags = flag_nostorep;
  665.           break;
  666.         case 'c':
  667.           dst->flags = flag_clearp;
  668.           break;
  669.         case 's':
  670.           /* special */
  671.           dst->flags = flag_special;
  672.         }
  673.     }
  674.       else
  675.     {
  676.       idx = 1;
  677.       switch (opcode->flags)
  678.         {
  679.         case 'h':
  680.           dst->flags = flag_shiftword;
  681.           break;
  682.         case 'p':
  683.           dst->flags = flag_multword;
  684.           break;
  685.         case 'B':
  686.           dst->flags = flag_branch;
  687.           break;
  688.         case 'm':
  689.           dst->flags = flag_Mp;
  690.           break;
  691.         case 'a':
  692.           dst->flags = flag_Ap;
  693.           break;
  694.         case '-':
  695.           dst->flags = flag_nonep;
  696.           break;
  697.         case 0:
  698.           dst->flags = flag_nostorep;
  699.           break;
  700.         case 'c':
  701.           dst->flags = flag_clearp;
  702.           break;
  703.         case 's':
  704.           /* special */
  705.           dst->flags = flag_special;
  706.           break;
  707.         }
  708.     }
  709.  
  710.       for (i = 0; i < opcode->nargs; i++)
  711.     {
  712.       ea_type *p = eavector + i;
  713.  
  714.       switch (opcode->arg_type[i])
  715.         {
  716.         default:
  717.           abort ();
  718.  
  719.         case FP:
  720.           gotreg (p, 6, idx);
  721.           break;
  722.         case RNIND:
  723.           disp = 0;
  724.         case RNIND_D16:
  725.         case RNIND_D8:
  726.           gotind (p, disp, rn, idx);
  727.           break;
  728.           break;
  729.         case RDIND:
  730.           disp = 0;
  731.         case RDIND_D16:
  732.         case RDIND_D8:
  733.           gotind (p, disp, rd, idx);
  734.           break;
  735.         case FPIND_D8:
  736.           gotind (p, disp, 6, idx);
  737.           break;
  738.         case CRB:
  739.         case CRW:
  740.           gotcr (p, cr);
  741.           break;
  742.         case RN:
  743.           gotreg (p, rn, idx);
  744.           break;
  745.         case RD:
  746.           gotreg (p, rd, idx);
  747.           break;
  748.         case RS:
  749.           gotreg (p, rs, idx);
  750.           break;
  751.         case RNDEC:
  752.           gotinc (p, rn, -1, idx);
  753.           break;
  754.         case RNINC:
  755.           gotinc (p, rn, 1, idx);
  756.           break;
  757.         case SPINC:
  758.           gotinc (p, 7, 1, idx);
  759.           break;
  760.         case SPDEC:
  761.           gotinc (p, 7, -1, idx);
  762.           break;
  763.         case ABS24:
  764.         case ABS16:
  765.           gotabs (p, abs, R_HARD_0, idx);
  766.           break;
  767.         case ABS8:
  768.           gotabs (p, abs, R_HARD8_0, idx);
  769.           break;
  770.         case IMM16:
  771.         case RLIST:
  772.         case QIM:
  773.         case IMM4:
  774.         case IMM8:
  775.           gotimm (p, imm);
  776.           break;
  777.         case PCREL16:
  778.         case PCREL8:
  779.           gotimm (p,
  780.          ((pcrel + pc + opcode->length) & 0xffff) | (pc & 0xff0000),
  781.               R_HARD_0, JLONG);
  782.  
  783.         }
  784.     }
  785.  
  786.       /* Finished and done - turn from two operand stuff into three */
  787.  
  788.       dst->srca.type = eas.s.ea_nop.s.srcabyte;
  789.       dst->srcb.type = eas.s.ea_nop.s.srcbbyte;
  790.       dst->dst.type = eas.s.ea_nop.s.dstbyte;
  791.  
  792.       if (opcode->nargs)
  793.     {
  794.       switch (opcode->nargs)
  795.         {
  796.         case 1:
  797.           howto_workout (&dst->srca, &eavector[0], 0);
  798.           if (opcode->dst != '!')
  799.         howto_workout (&dst->dst, &eavector[0], 2);
  800.           break;
  801.         case 2:
  802.           if (opcode->src2 == '!')
  803.         {
  804.           howto_workout (&dst->srca, &eavector[0], 0);
  805.           howto_workout (&dst->dst, &eavector[1], 2);
  806.         }
  807.           else
  808.         {
  809.           howto_workout (&dst->srca, &eavector[0], 0);
  810.           howto_workout (&dst->srcb, &eavector[1], 1);
  811.           if (opcode->dst != '!')
  812.             {
  813.               howto_workout (&dst->dst, &eavector[1], 2);
  814.             }
  815.         }
  816.           break;
  817.         }
  818.  
  819.  
  820.  
  821.       /* Some extra stuff with pre inc and post dec,
  822.        make sure that if the same ea is there twice, only one of the
  823.        ops is auto inc/dec */
  824.  
  825.       fix_incdecs (dst);
  826.  
  827.  
  828.       /* Some special cases */
  829.       if (dst->opcode == exec_dispatch[O_PJSR]
  830.           || dst->opcode == exec_dispatch[O_PJMP])
  831.         {
  832.           /* Both the @abs:24 and @rn turn into a disp word,
  833.          chose the right a mode since  @abs:24 is 4 bytes
  834.          long */
  835.  
  836.           if (opcode->length == 4)
  837.         {
  838.           dst->srca.type = eas.s.ea_lval24.s.srcabyte;
  839.         }
  840.           else
  841.         {
  842.           dst->srca.type = eas.s.ea_reg.s.srcalong;
  843.         }
  844.  
  845.           dst->srca.r2.rptr = &cpu.regs[R_HARD_0];
  846.  
  847.           /* For [P]JSR, keep return address precomputed */
  848.           dst->srcb.literal = pc + opcode->length;
  849.           dst->srcb.type = eas.s.ea_imm.s.srcbword;
  850.         }
  851.  
  852.       if (dst->opcode == exec_dispatch[O_MULXU])
  853.         {
  854.           /* This is a multiply -fix the destination op */
  855.           if (dst->dst.type == eas.s.ea_reg.s.dstword)
  856.         {
  857.           dst->dst.type = eas.s.ea_reg.s.dstlong;
  858.         }
  859.           else
  860.         {
  861.           dst->dst.type = eas.s.ea_reg.s.dstword;
  862.         }
  863.           dst->dst.reg.bptr = regptr[rd][JWORD];
  864.         }
  865.       if (dst->opcode == exec_dispatch[O_DIVXU])
  866.         {
  867.           /* This is a wider than normal, fix the source operand */
  868.           dst->srcb.type
  869.         = (dst->srcb.type == eas.s.ea_reg.s.srcbword)
  870.         ? eas.s.ea_reg.s.srcblong
  871.         : eas.s.ea_reg.s.srcbword;
  872.  
  873.           dst->dst.type
  874.         = (dst->dst.type == eas.s.ea_reg.s.dstword)
  875.         ? eas.s.ea_reg.s.dstlong
  876.         : eas.s.ea_reg.s.dstword;
  877.  
  878.         }
  879.  
  880.       if (dst->opcode == exec_dispatch[O_LDM])
  881.         {
  882.           /* Turn of the stack ref */
  883.           dst->srca.type = eas.s.ea_nop.s.srcabyte;
  884.         }
  885.       if (dst->opcode == exec_dispatch[O_STM])
  886.         {
  887.           /* Turn of the stack ref */
  888.           dst->srcb.type = eas.s.ea_nop.s.srcbbyte;
  889.         }
  890.  
  891.  
  892.       /* extends read one size and write another */
  893.       if (dst->opcode == exec_dispatch[O_EXTS]
  894.           || dst->opcode == exec_dispatch[O_EXTU])
  895.         {
  896.           dst->dst.type = eas.s.ea_reg.s.dstword;
  897.           dst->dst.reg.bptr = regptr[rd][JWORD];
  898.           dst->flags = flag_Ap;
  899.         }
  900.  
  901.  
  902.       if (opcode->flags == 'h')
  903.         thinkabout_shifts (dst, opcode->flavor & O_BYTE);
  904.  
  905.  
  906.       /* For a branch, turn off one level of indirection */
  907.       if (opcode->src1 == 'B')
  908.         {
  909.           indoff (&dst->srca, 0);
  910.         }
  911.  
  912.     }
  913.       dst->next_pc = pc + opcode->length;
  914.  
  915.       compcycles (dst, opcode);
  916.  
  917.       return;
  918.  
  919.  
  920.     next:;
  921.     }
  922.  
  923.   /* Couldn't understand anything */
  924.   dst->opcode = exec_dispatch[O_TRAPA];
  925.   dst->next_pc = pc + 1;
  926.  
  927. }
  928.  
  929. compile (pc)
  930. {
  931.   int idx;
  932.  
  933.   /* find the next cache entry to use */
  934.  
  935.   idx = cpu.cache_top + 1;
  936.   cpu.compiles++;
  937.   if (idx >= cpu.csize)
  938.     {
  939.       idx = 1;
  940.     }
  941.   cpu.cache_top = idx;
  942.  
  943.   /* Throw away its old meaning */
  944.   cpu.cache_idx[cpu.cache[idx].oldpc] = 0;
  945.  
  946.   /* set to new address */
  947.   cpu.cache[idx].oldpc = pc;
  948.  
  949.   /* fill in instruction info */
  950.   find (pc, cpu.memory + pc, cpu.cache + idx);
  951.  
  952.   /* point to new cache entry */
  953.   cpu.cache_idx[pc] = idx;
  954. }
  955.  
  956. baddefault (x)
  957. {
  958.   printf ("bad default %d\n", x);
  959. }
  960.  
  961. static int fetch_l (arg)
  962.      ea_type *arg;
  963. {
  964.   int l, r;
  965.  
  966.   int h = *(arg->reg.wptr);
  967.   r = (union rtype *) (arg->reg.wptr) - &cpu.regs[0];
  968.   r++;
  969.  
  970.   l = cpu.regs[r].s[LOW];
  971.   return (h << 16) | l;
  972.  
  973. }
  974.  
  975. #define FETCH(dst, arg, n)  \
  976. { \
  977.  int r; unsigned char*lval; \
  978.  DISPATCH((arg).type) \
  979.  { LABELN(FETCH_NOP,n): \
  980.  dst= 0; \
  981.  break; \
  982.  DEFAULT baddefault((arg).type); break; \
  983.  LABELN(FETCH_LVAL,n):  \
  984.  dst = (*(((arg).reg.wptr)) + (arg.literal)) ; \
  985.  break; \
  986.  LABELN(FETCH_LVAL24,n):  \
  987.  dst = (*(((arg).reg.wptr)) + *(((arg).r2.wptr)) + (arg.literal)) &0xffffff; \
  988.  break; \
  989.  LABELN(FETCH_CRB,n):  \
  990.  dst = (*((arg).reg.segptr) - cpu.memory)>>16; \
  991.  break; \
  992.  LABELN(FETCH_CRW,n):  \
  993.   dst = BUILDSR();\
  994.  break; \
  995.  LABELN(FETCH_REG_B,n): \
  996.  dst = *((arg).reg.bptr); \
  997.  break; \
  998.  LABELN(FETCH_REG_W,n): \
  999.  dst = *((arg).reg.wptr); \
  1000.  break; \
  1001.  LABELN(FETCH_REG_L,n): \
  1002.  dst = fetch_l(&(arg));\
  1003.  break; \
  1004.  LABELN(FETCH_INC_B,n): \
  1005.  lval = elval ((arg), 0); \
  1006.  dst = byteat (lval); \
  1007.  (*((arg).reg.wptr))++; \
  1008.  break; \
  1009.  LABELN(FETCH_INC_W,n): \
  1010.  lval = elval ((arg), 0); \
  1011.  dst = wordat (lval); \
  1012.  (*(((arg).reg.wptr))) += 2; \
  1013.  break; \
  1014.  LABELN(FETCH_DEC_B, n): \
  1015.  (*(arg).reg.wptr)--; \
  1016.  lval = elval ((arg), 0); \
  1017.  r = byteat (lval); \
  1018.  dst = r; \
  1019.  break; \
  1020.  LABELN(FETCH_DEC_W, n): \
  1021.  (*((arg).reg.wptr)) -= 2; \
  1022.  lval = elval ((arg), 0); \
  1023.  r = wordat (lval); \
  1024.  dst = r; \
  1025.  break; \
  1026.  LABELN(FETCH_DISP_B,n): \
  1027.  lval = displval ((arg)); \
  1028.  dst = byteat (lval); \
  1029.  break; \
  1030.  LABELN(FETCH_DISP_W,n): \
  1031.  lval = displval ((arg)); \
  1032.  dst = wordat (lval); \
  1033.  break; \
  1034.  LABELN(FETCH_IMM, n): \
  1035.  dst = (arg).literal; \
  1036.  break; \
  1037.  } \
  1038.  ENDDISPATCH; \
  1039. }
  1040.  
  1041. static union
  1042. {
  1043.   short int i;
  1044.   struct
  1045.     {
  1046.       char low;
  1047.       char high;
  1048.     }
  1049.   u;
  1050. }
  1051.  
  1052. littleendian;
  1053.  
  1054. static
  1055. void
  1056. init_pointers ()
  1057. {
  1058.   static int init;
  1059.  
  1060.   if (!init)
  1061.     {
  1062.       int i;
  1063.  
  1064.       init = 1;
  1065.       littleendian.i = 1;
  1066.  
  1067.       for (i = 0; i < (int) R_LAST; i++)
  1068.     {
  1069.       if (littleendian.u.high)
  1070.         {
  1071.           /* big endian host */
  1072.  
  1073.  
  1074.           LOW = 1;
  1075.           HIGH = 0;
  1076.  
  1077.           regptr[i][0] = ((unsigned char *) (cpu.regs + i)) + 3;
  1078.           regptr[i][1] = ((unsigned char *) (cpu.regs + i)) + 2;
  1079.         }
  1080.       else
  1081.         {
  1082.           LOW = 0;
  1083.           HIGH = 1;
  1084.  
  1085.           regptr[i][0] = (unsigned char *) &(cpu.regs[i]);
  1086.           regptr[i][1] = (unsigned char *) (&(cpu.regs[i]));
  1087.         }
  1088.  
  1089.       regptr[i][2] = (unsigned char *) &(cpu.regs[i]);
  1090.     }
  1091.  
  1092.       memcpy (segregptr + 0, regptr + R_SR, sizeof (segregptr[0]));
  1093.       memcpy (segregptr + 1, regptr + R_TP, sizeof (segregptr[1]));
  1094.       memcpy (segregptr + 3, regptr + R_BR, sizeof (segregptr[3]));
  1095.       memcpy (segregptr + 4, regptr + R_EP, sizeof (segregptr[4]));
  1096.       memcpy (segregptr + 5, regptr + R_DP, sizeof (segregptr[5]));
  1097.       memcpy (segregptr + 6, regptr + R_CP, sizeof (segregptr[6]));
  1098.       memcpy (segregptr + 7, regptr + R_TP, sizeof (segregptr[7]));
  1099.  
  1100.       /* Pointers to into the cpu state for the seg registers */
  1101.  
  1102.       segmap[R0] = &cpu.regs[R_DP].c;
  1103.       segmap[R1] = &cpu.regs[R_DP].c;
  1104.       segmap[R2] = &cpu.regs[R_DP].c;
  1105.       segmap[R3] = &cpu.regs[R_DP].c;
  1106.       segmap[R4] = &cpu.regs[R_EP].c;
  1107.       segmap[R5] = &cpu.regs[R_EP].c;
  1108.       segmap[R6] = &cpu.regs[R_TP].c;
  1109.       segmap[R7] = &cpu.regs[R_TP].c;
  1110.       segmap[R_HARD_0] = &cpu.regs[R_DP].c;
  1111.       segmap[R_HARD8_0] = &cpu.regs[R_BP].c;
  1112.  
  1113.       cpu.memory = (unsigned char *) calloc (sizeof (char), MSIZE);
  1114.       cpu.cache_idx = (unsigned short *) calloc (sizeof (short), MSIZE);
  1115.  
  1116.       /* initialize the seg registers */
  1117.  
  1118.       cpu.regs[R_DP].c = cpu.memory;
  1119.       cpu.regs[R_TP].c = cpu.memory;
  1120.       cpu.regs[R_CP].c = cpu.memory;
  1121.       cpu.regs[R_BP].c = cpu.memory;
  1122.       cpu.regs[R_EP].c = cpu.memory;
  1123.       cpu.regs[R7].s[LOW] = 0xfffe;
  1124.       cpu.regs[R6].s[LOW] = 0xfffe;
  1125.       if (!cpu.cache)
  1126.     sim_csize (CSIZE);
  1127.     }
  1128. }
  1129.  
  1130. #define PUSHWORD(x)                \
  1131. {                        \
  1132.   int sp = cpu.regs[R7].s[LOW];            \
  1133.   unsigned char *p;                \
  1134.                         \
  1135.   sp -= 2;                    \
  1136.   p = (sp & 0xffff) + (cpu.regs[R_TP].c);    \
  1137.   cpu.regs[R7].s[LOW] = sp;            \
  1138.   setwordat (p, x);                \
  1139. }                        \
  1140.  
  1141. #define POPWORD(d)                \
  1142. {                        \
  1143.   int spx= cpu.regs[R7].s[LOW];            \
  1144.   unsigned char *p;                \
  1145.                             \
  1146.   p = (spx& 0xffff) + (cpu.regs[R_TP].c);    \
  1147.   spx+= 2;                    \
  1148.   cpu.regs[R7].s[LOW] = spx;                    \
  1149.   d = wordat (p);                \
  1150. }                        \
  1151.  
  1152. /* simulate a monitor trap */
  1153. trap ()
  1154. {
  1155.   switch (cpu.regs[R3].s[LOW] & 0xff)
  1156.     {
  1157.     case 33:
  1158.       /* exit */
  1159.       cpu.exception = SIGQUIT;
  1160.       break;
  1161.     case 6:
  1162.       /* print char in r0 */
  1163.       printf ("%c", cpu.regs[R0].s[LOW]);
  1164.       break;
  1165.     }
  1166. }
  1167. void
  1168. control_c (sig, code, scp, addr)
  1169.      int sig;
  1170.      int code;
  1171.      char *scp;
  1172.      char *addr;
  1173. {
  1174.   cpu.exception = SIGINT;
  1175. }
  1176.  
  1177. static jmp_buf jbuf;
  1178. static void
  1179. segv ()
  1180. {
  1181.   cpu.exception = SIGSEGV;
  1182.   longjmp (jbuf, 1);
  1183. }
  1184.  
  1185. void
  1186. sim_resume (step, siggnal)
  1187. {
  1188.   static int init1;
  1189.   int res;
  1190.   int tmp;
  1191.   int arga;
  1192.   int argb;
  1193.   int bit;
  1194.   int pc;
  1195.   int C, Z, V, N;
  1196.   int cycles = 0;
  1197.   int insts = 0;
  1198.   int tick_start = get_now ();
  1199.   void (*prev) ();
  1200.   void (*prev_seg) ();
  1201.  
  1202.   if (!init1)
  1203.     {
  1204.       int i;
  1205.  
  1206.       init1 = 1;
  1207.       init_pointers ();
  1208.  
  1209.       for (i = 0; i < N_EATYPES; i++)
  1210.     {
  1211.       eas.a[i].s.srcabyte = LABEL_REFN (FETCH_NOP, 0);
  1212.       eas.a[i].s.srcaword = LABEL_REFN (FETCH_NOP, 0);
  1213.       eas.a[i].s.srcalong = LABEL_REFN (FETCH_NOP, 0);
  1214.  
  1215.       eas.a[i].s.srcbbyte = LABEL_REFN (FETCH_NOP, 1);
  1216.       eas.a[i].s.srcbword = LABEL_REFN (FETCH_NOP, 1);
  1217.       eas.a[i].s.srcblong = LABEL_REFN (FETCH_NOP, 1);
  1218.  
  1219.       eas.a[i].s.dstbyte = LABEL_REF (STORE_NOP);
  1220.       eas.a[i].s.dstword = LABEL_REF (STORE_NOP);
  1221.       eas.a[i].s.dstlong = LABEL_REF (STORE_NOP);
  1222.     }
  1223.  
  1224.       eas.s.ea_lval.s.srcabyte = LABEL_REFN (FETCH_LVAL, 0);
  1225.       eas.s.ea_lval.s.srcaword = LABEL_REFN (FETCH_LVAL, 0);
  1226.       eas.s.ea_lval24.s.srcabyte = LABEL_REFN (FETCH_LVAL24, 0);
  1227.       eas.s.ea_lval24.s.srcaword = LABEL_REFN (FETCH_LVAL24, 0);
  1228.  
  1229.       eas.s.ea_nop.s.srcabyte = LABEL_REFN (FETCH_NOP, 0);
  1230.       eas.s.ea_nop.s.srcaword = LABEL_REFN (FETCH_NOP, 0);
  1231.       eas.s.ea_nop.s.srcbbyte = LABEL_REFN (FETCH_NOP, 1);
  1232.       eas.s.ea_nop.s.srcbword = LABEL_REFN (FETCH_NOP, 1);
  1233.       eas.s.ea_nop.s.dstbyte = LABEL_REF (STORE_NOP);
  1234.       eas.s.ea_nop.s.dstword = LABEL_REF (STORE_NOP);
  1235.  
  1236.       eas.s.ea_cr.s.srcabyte = LABEL_REFN (FETCH_CRB, 0);
  1237.       eas.s.ea_cr.s.srcaword = LABEL_REFN (FETCH_CRW, 0);
  1238.  
  1239.       eas.s.ea_cr.s.srcbbyte = LABEL_REFN (FETCH_CRB, 1);
  1240.       eas.s.ea_cr.s.srcbword = LABEL_REFN (FETCH_CRW, 1);
  1241.  
  1242.       eas.s.ea_cr.s.dstbyte = LABEL_REF (STORE_CRB);
  1243.       eas.s.ea_cr.s.dstword = LABEL_REF (STORE_CRW);
  1244.  
  1245.       eas.s.ea_reg.s.srcabyte = LABEL_REFN (FETCH_REG_B, 0);
  1246.       eas.s.ea_reg.s.srcaword = LABEL_REFN (FETCH_REG_W, 0);
  1247.       eas.s.ea_reg.s.srcalong = LABEL_REFN (FETCH_REG_L, 0);
  1248.  
  1249.       eas.s.ea_reg.s.srcbbyte = LABEL_REFN (FETCH_REG_B, 1);
  1250.       eas.s.ea_reg.s.srcbword = LABEL_REFN (FETCH_REG_W, 1);
  1251.       eas.s.ea_reg.s.srcblong = LABEL_REFN (FETCH_REG_L, 1);
  1252.  
  1253.       eas.s.ea_reg.s.dstbyte = LABEL_REF (STORE_REG_B);
  1254.       eas.s.ea_reg.s.dstword = LABEL_REF (STORE_REG_W);
  1255.       eas.s.ea_reg.s.dstlong = LABEL_REF (STORE_REG_L);
  1256.  
  1257.       eas.s.ea_inc.s.srcabyte = LABEL_REFN (FETCH_INC_B, 0);
  1258.       eas.s.ea_inc.s.srcaword = LABEL_REFN (FETCH_INC_W, 0);
  1259.       eas.s.ea_inc.s.srcbbyte = LABEL_REFN (FETCH_INC_B, 1);
  1260.       eas.s.ea_inc.s.srcbword = LABEL_REFN (FETCH_INC_W, 1);
  1261.       eas.s.ea_inc.s.dstbyte = LABEL_REF (STORE_INC_B);
  1262.       eas.s.ea_inc.s.dstword = LABEL_REF (STORE_INC_W);
  1263.  
  1264.       eas.s.ea_dec.s.srcabyte = LABEL_REFN (FETCH_DEC_B, 0);
  1265.       eas.s.ea_dec.s.srcaword = LABEL_REFN (FETCH_DEC_W, 0);
  1266.       eas.s.ea_dec.s.srcbbyte = LABEL_REFN (FETCH_DEC_B, 1);
  1267.       eas.s.ea_dec.s.srcbword = LABEL_REFN (FETCH_DEC_W, 1);
  1268.       eas.s.ea_dec.s.dstbyte = LABEL_REF (STORE_DEC_B);
  1269.       eas.s.ea_dec.s.dstword = LABEL_REF (STORE_DEC_W);
  1270.  
  1271.       eas.s.ea_disp.s.srcabyte = LABEL_REFN (FETCH_DISP_B, 0);
  1272.       eas.s.ea_disp.s.srcaword = LABEL_REFN (FETCH_DISP_W, 0);
  1273.       eas.s.ea_disp.s.srcbbyte = LABEL_REFN (FETCH_DISP_B, 1);
  1274.       eas.s.ea_disp.s.srcbword = LABEL_REFN (FETCH_DISP_W, 1);
  1275.       eas.s.ea_disp.s.dstbyte = LABEL_REF (STORE_DISP_B);
  1276.       eas.s.ea_disp.s.dstword = LABEL_REF (STORE_DISP_W);
  1277.  
  1278.       eas.s.ea_imm.s.srcabyte = LABEL_REFN (FETCH_IMM, 0);
  1279.       eas.s.ea_imm.s.srcaword = LABEL_REFN (FETCH_IMM, 0);
  1280.       eas.s.ea_imm.s.srcbbyte = LABEL_REFN (FETCH_IMM, 1);
  1281.       eas.s.ea_imm.s.srcbword = LABEL_REFN (FETCH_IMM, 1);
  1282.  
  1283.       flag_special = LABEL_REF (FLAG_special);
  1284.       flag_mp = LABEL_REF (FLAG_m);
  1285.       flag_Mp = LABEL_REF (FLAG_M);
  1286.       flag_ap = LABEL_REF (FLAG_a);
  1287.       flag_Ap = LABEL_REF (FLAG_A);
  1288.       flag_nonep = LABEL_REF (FLAG_NONE);
  1289.       flag_nostorep = LABEL_REF (FLAG_NOSTORE);
  1290.       flag_clearp = LABEL_REF (FLAG_CLEAR);
  1291.       flag_shiftbyte = LABEL_REF (FLAG_shiftbyte);
  1292.       flag_shiftword = LABEL_REF (FLAG_shiftword);
  1293.       flag_multbyte = LABEL_REF (FLAG_multbyte);
  1294.       flag_multword = LABEL_REF (FLAG_multword);
  1295.  
  1296.  
  1297.       exec_dispatch[O_ADDS] = LABEL_REF (O_ADDS);
  1298.       exec_dispatch[O_ADDX] = LABEL_REF (O_ADDX);
  1299.       exec_dispatch[O_ADD] = LABEL_REF (O_ADD);
  1300.       exec_dispatch[O_ANDC] = LABEL_REF (O_ANDC);
  1301.       exec_dispatch[O_AND] = LABEL_REF (O_AND);
  1302.       exec_dispatch[O_BCC] = LABEL_REF (O_BCC);
  1303.       exec_dispatch[O_BCLR] = LABEL_REF (O_BCLR);
  1304.       exec_dispatch[O_BCS] = LABEL_REF (O_BCS);
  1305.       exec_dispatch[O_BEQ] = LABEL_REF (O_BEQ);
  1306.       exec_dispatch[O_BF] = LABEL_REF (O_BF);
  1307.       exec_dispatch[O_BGE] = LABEL_REF (O_BGE);
  1308.       exec_dispatch[O_BGT] = LABEL_REF (O_BGT);
  1309.       exec_dispatch[O_BHI] = LABEL_REF (O_BHI);
  1310.       exec_dispatch[O_BHS] = LABEL_REF (O_BHS);
  1311.       exec_dispatch[O_BLE] = LABEL_REF (O_BLE);
  1312.       exec_dispatch[O_BLO] = LABEL_REF (O_BLO);
  1313.       exec_dispatch[O_BLS] = LABEL_REF (O_BLS);
  1314.       exec_dispatch[O_BLT] = LABEL_REF (O_BLT);
  1315.       exec_dispatch[O_BMI] = LABEL_REF (O_BMI);
  1316.       exec_dispatch[O_BNE] = LABEL_REF (O_BNE);
  1317.       exec_dispatch[O_BNOT] = LABEL_REF (O_BNOT);
  1318.       exec_dispatch[O_BPL] = LABEL_REF (O_BPL);
  1319.       exec_dispatch[O_BPT] = LABEL_REF (O_BPT);
  1320.       exec_dispatch[O_BRA] = LABEL_REF (O_BRA);
  1321.       exec_dispatch[O_BRN] = LABEL_REF (O_BRN);
  1322.       exec_dispatch[O_BSET] = LABEL_REF (O_BSET);
  1323.       exec_dispatch[O_BSR] = LABEL_REF (O_BSR);
  1324.       exec_dispatch[O_BTST] = LABEL_REF (O_BTST);
  1325.       exec_dispatch[O_BT] = LABEL_REF (O_BT);
  1326.       exec_dispatch[O_BVC] = LABEL_REF (O_BVC);
  1327.       exec_dispatch[O_BVS] = LABEL_REF (O_BVS);
  1328.       exec_dispatch[O_CLR] = LABEL_REF (O_CLR);
  1329.       exec_dispatch[O_CMP] = LABEL_REF (O_CMP);
  1330.       exec_dispatch[O_DADD] = LABEL_REF (O_DADD);
  1331.       exec_dispatch[O_DIVXU] = LABEL_REF (O_DIVXU);
  1332.       exec_dispatch[O_DSUB] = LABEL_REF (O_DSUB);
  1333.       exec_dispatch[O_EXTS] = LABEL_REF (O_EXTS);
  1334.       exec_dispatch[O_EXTU] = LABEL_REF (O_EXTU);
  1335.       exec_dispatch[O_JMP] = LABEL_REF (O_JMP);
  1336.       exec_dispatch[O_JSR] = LABEL_REF (O_JSR);
  1337.       exec_dispatch[O_LDC] = LABEL_REF (O_LDC);
  1338.       exec_dispatch[O_LDM] = LABEL_REF (O_LDM);
  1339.       exec_dispatch[O_LINK] = LABEL_REF (O_LINK);
  1340.       exec_dispatch[O_MOVFPE] = LABEL_REF (O_MOVFPE);
  1341.       exec_dispatch[O_MOVTPE] = LABEL_REF (O_MOVTPE);
  1342.       exec_dispatch[O_MOV] = LABEL_REF (O_MOV);
  1343.       exec_dispatch[O_MULXU] = LABEL_REF (O_MULXU);
  1344.       exec_dispatch[O_NEG] = LABEL_REF (O_NEG);
  1345.       exec_dispatch[O_NOP] = LABEL_REF (O_NOP);
  1346.       exec_dispatch[O_NOT] = LABEL_REF (O_NOT);
  1347.       exec_dispatch[O_ORC] = LABEL_REF (O_ORC);
  1348.       exec_dispatch[O_OR] = LABEL_REF (O_OR);
  1349.       exec_dispatch[O_PJMP] = LABEL_REF (O_PJMP);
  1350.       exec_dispatch[O_PJSR] = LABEL_REF (O_PJSR);
  1351.       exec_dispatch[O_PRTD] = LABEL_REF (O_PRTD);
  1352.       exec_dispatch[O_PRTS] = LABEL_REF (O_PRTS);
  1353.       exec_dispatch[O_RECOMPILE] = LABEL_REF (O_RECOMPILE);
  1354.  
  1355.       exec_dispatch[O_ROTL] = LABEL_REF (O_ROTL);
  1356.       exec_dispatch[O_ROTR] = LABEL_REF (O_ROTR);
  1357.       exec_dispatch[O_ROTXL] = LABEL_REF (O_ROTXL);
  1358.       exec_dispatch[O_ROTXR] = LABEL_REF (O_ROTXR);
  1359.  
  1360.       exec_dispatch[O_RTD] = LABEL_REF (O_RTD);
  1361.       exec_dispatch[O_RTS] = LABEL_REF (O_RTS);
  1362.       exec_dispatch[O_SCB_EQ] = LABEL_REF (O_SCB_EQ);
  1363.       exec_dispatch[O_SCB_F] = LABEL_REF (O_SCB_F);
  1364.       exec_dispatch[O_SCB_NE] = LABEL_REF (O_SCB_NE);
  1365.       exec_dispatch[O_SHAL] = LABEL_REF (O_SHAL);
  1366.       exec_dispatch[O_SHAR] = LABEL_REF (O_SHAR);
  1367.       exec_dispatch[O_SHLL] = LABEL_REF (O_SHLL);
  1368.       exec_dispatch[O_SHLR] = LABEL_REF (O_SHLR);
  1369.  
  1370.       exec_dispatch[O_SLEEP] = LABEL_REF (O_SLEEP);
  1371.       exec_dispatch[O_STC] = LABEL_REF (O_STC);
  1372.       exec_dispatch[O_STM] = LABEL_REF (O_STM);
  1373.       exec_dispatch[O_SUBS] = LABEL_REF (O_SUBS);
  1374.       exec_dispatch[O_SUBX] = LABEL_REF (O_SUBX);
  1375.       exec_dispatch[O_SUB] = LABEL_REF (O_SUB);
  1376.       exec_dispatch[O_SWAP] = LABEL_REF (O_SWAP);
  1377.       exec_dispatch[O_TAS] = LABEL_REF (O_TAS);
  1378.       exec_dispatch[O_TRAPA] = LABEL_REF (O_TRAPA);
  1379.       exec_dispatch[O_TRAP_VS] = LABEL_REF (O_TRAP_VS);
  1380.       exec_dispatch[O_TST] = LABEL_REF (O_TST);
  1381.       exec_dispatch[O_UNLK] = LABEL_REF (O_UNLK);
  1382.       exec_dispatch[O_XCH] = LABEL_REF (O_XCH);
  1383.       exec_dispatch[O_XORC] = LABEL_REF (O_XORC);
  1384.       exec_dispatch[O_XOR] = LABEL_REF (O_XOR);
  1385.       nop.type = eas.s.ea_nop.s.srcabyte;
  1386.       cpu.cache[0].opcode = exec_dispatch[O_RECOMPILE];
  1387.       cpu.cache[0].srca.type = eas.s.ea_nop.s.srcabyte;
  1388.       cpu.cache[0].srcb.type = eas.s.ea_nop.s.srcbbyte;
  1389.     }
  1390.  
  1391.   prev = signal (SIGINT, control_c);
  1392.   prev_seg = signal (SIGSEGV, segv);
  1393.  
  1394.   if (step)
  1395.     {
  1396.       cpu.exception = SIGTRAP;
  1397.     }
  1398.   else
  1399.     {
  1400.       cpu.exception = 0;
  1401.     }
  1402.  
  1403.   pc = cpu.regs[R_PC].s[LOW] + (NORMAL_CP << 16);
  1404.  
  1405.   GETSR ();
  1406.  
  1407.   if (setjmp (jbuf) == 0) {
  1408.     do
  1409.       {
  1410.     int cidx;
  1411.     decoded_inst *code;
  1412.  
  1413.       top:
  1414.     cidx = cpu.cache_idx[pc];
  1415.     code = cpu.cache + cidx;
  1416.  
  1417.     FETCH (arga, code->srca, 0);
  1418.     FETCH (argb, code->srcb, 1);
  1419.  
  1420.  
  1421.     
  1422. #ifdef DEBUG
  1423.     if (debug)
  1424.       {
  1425.         printf ("%x %d %s\n", pc, code->opcode,
  1426.             code->op ? code->op->name : "**");
  1427.       }
  1428. #endif
  1429.  
  1430.     cycles += code->cycles;
  1431.     insts++;
  1432.     DISPATCH (code->opcode)
  1433.       {
  1434.         LABEL (O_RECOMPILE):
  1435.         /* This opcode is a fake for when we get to an instruction which
  1436.            hasn't been compiled */
  1437.         compile (pc);
  1438.         goto top;
  1439.         break;
  1440.         LABEL (O_NEG):
  1441.         arga = -arga;
  1442.         argb = 0;
  1443.         res = arga + argb;
  1444.         break;
  1445.         LABEL (O_SUBX):
  1446.         arga += C;
  1447.         LABEL (O_SUB):
  1448.         LABEL (O_SUBS):
  1449.         arga = -arga;
  1450.         LABEL (O_ADD):
  1451.         LABEL (O_ADDS):
  1452.         res = arga + argb;
  1453.         break;
  1454.  
  1455.         LABEL (O_ADDX):
  1456.         res = arga + argb + C;
  1457.         break;
  1458.  
  1459.         LABEL (O_AND):
  1460.         LABEL (O_ANDC):
  1461.         res = arga & argb;
  1462.         break;
  1463.         break;
  1464.  
  1465.         LABEL (O_BCLR):
  1466.         arga &= 0xf;
  1467.         bit = (argb & (1 << arga));
  1468.         res = argb & ~(1 << arga);
  1469.         goto bitop;
  1470.  
  1471.  
  1472.         LABEL (O_BRA):
  1473.         LABEL (O_BT):
  1474.         if (1)
  1475.           goto condtrue;
  1476.  
  1477.         LABEL (O_BRN):
  1478.         LABEL (O_BF):
  1479.         if (0)
  1480.           goto condtrue;
  1481.         break;
  1482.  
  1483.         LABEL (O_BHI):
  1484.         if ((C || Z) == 0)
  1485.           goto condtrue;
  1486.         break;
  1487.  
  1488.         LABEL (O_BLS):
  1489.         if ((C || Z))
  1490.           goto condtrue;
  1491.         break;
  1492.  
  1493.         LABEL (O_BCS):
  1494.         LABEL (O_BLO):
  1495.         if ((C == 1))
  1496.           goto condtrue;
  1497.         break;
  1498.  
  1499.         LABEL (O_BCC):
  1500.         LABEL (O_BHS):
  1501.         if ((C == 0))
  1502.           goto condtrue;
  1503.         break;
  1504.  
  1505.         LABEL (O_BEQ):
  1506.         if (Z)
  1507.           goto condtrue;
  1508.         break;
  1509.         LABEL (O_BGT):
  1510.         if (((Z || (N ^ V)) == 0))
  1511.           goto condtrue;
  1512.         break;
  1513.  
  1514.  
  1515.         LABEL (O_BLE):
  1516.         if (((Z || (N ^ V)) == 1))
  1517.           goto condtrue;
  1518.         break;
  1519.  
  1520.         LABEL (O_BGE):
  1521.         if ((N ^ V) == 0)
  1522.           goto condtrue;
  1523.         break;
  1524.         LABEL (O_BLT):
  1525.         if ((N ^ V))
  1526.           goto condtrue;
  1527.         break;
  1528.         LABEL (O_BMI):
  1529.         if ((N))
  1530.           goto condtrue;
  1531.         break;
  1532.         LABEL (O_BNE):
  1533.         if ((Z == 0))
  1534.           goto condtrue;
  1535.         break;
  1536.         LABEL (O_BPL):
  1537.         if (N == 0)
  1538.           goto condtrue;
  1539.         break;
  1540.         break;
  1541.         LABEL (O_BVC):
  1542.         if ((V == 0))
  1543.           goto condtrue;
  1544.         break;
  1545.         LABEL (O_BVS):
  1546.         if ((V == 1))
  1547.           goto condtrue;
  1548.         break;
  1549.  
  1550.         LABEL (O_BNOT):
  1551.         bit = argb & (1<<(arga & 0xf));
  1552.         res = argb ^ (1<<(arga & 0xf));
  1553.         goto bitop;
  1554.         break;
  1555.  
  1556.         LABEL (O_BSET):
  1557.         arga = 1 << (arga & 0xf);
  1558.         bit = argb & arga;
  1559.         res = argb | arga;
  1560.         goto bitop;
  1561.         break;
  1562.  
  1563.         LABEL (O_PJMP):
  1564.         pc = arga;
  1565.         goto next;
  1566.  
  1567.         LABEL (O_UNLK):
  1568.         {
  1569.           int t;
  1570.           SET_NORMREG (R7, GET_NORMREG (R6));
  1571.           POPWORD (t);
  1572.           SET_NORMREG (R6, t);
  1573.           pc = code->next_pc;
  1574.           goto next;
  1575.         }
  1576.  
  1577.         LABEL (O_RTS):
  1578.         {
  1579.           int cp = pc & 0xff0000;
  1580.           POPWORD (pc);
  1581.           pc |= cp;
  1582.           goto next;
  1583.         }
  1584.         break;
  1585.  
  1586.         LABEL (O_PRTS):
  1587.         {
  1588.           int cp;
  1589.           int off;
  1590.           POPWORD (cp);
  1591.           POPWORD (off);
  1592.           cp <<= 16;
  1593.           SET_SEGREG (R_CP, cp);
  1594.           pc = cp + off;
  1595.         }
  1596.         goto next;
  1597.  
  1598.         LABEL (O_PJSR):
  1599.         PUSHWORD (argb & 0xffff);
  1600.         PUSHWORD (argb >> 16);
  1601.         pc = (arga & 0xffffff);
  1602.         goto next;
  1603.  
  1604.         LABEL (O_BSR):
  1605.         LABEL (O_JSR):
  1606.         PUSHWORD (code->next_pc);
  1607.         pc = arga;
  1608.         goto next;
  1609.  
  1610.         LABEL (O_BTST):
  1611.         Z = (((argb >> (arga & 0xf)) & 1) == 0);
  1612.         pc = code->next_pc;
  1613.         goto next;
  1614.  
  1615.         LABEL (O_CLR):
  1616.         res = 0;
  1617.         break;
  1618.  
  1619.         LABEL (O_CMP):
  1620.         arga = -arga;
  1621.         res = arga + argb;
  1622.         break;
  1623.  
  1624.         LABEL (O_DADD):
  1625.         res = arga + argb + C;
  1626.         if (res > 99)
  1627.           {
  1628.         res -= 100;
  1629.         C = 1;
  1630.           }
  1631.         else
  1632.           {
  1633.         C = 0;
  1634.           }
  1635.         Z = Z && (res == 0);
  1636.         break;
  1637.  
  1638.  
  1639.         LABEL (O_DSUB):
  1640.         res = argb - arga - C;
  1641.         if (res < 0)
  1642.           {
  1643.         res += 100;
  1644.         C = 1;
  1645.           }
  1646.         else
  1647.           {
  1648.         C = 0;
  1649.           }
  1650.         Z = Z && (res == 0);
  1651.         break;
  1652.  
  1653.         LABEL (O_EXTS):
  1654.         res = SEXTCHAR (arga);
  1655.         break;
  1656.  
  1657.         LABEL (O_EXTU):
  1658.         res = (unsigned char) arga;
  1659.         break;
  1660.  
  1661.         LABEL (O_JMP):
  1662.         pc = arga | (pc & 0xff0000);
  1663.         goto next;
  1664.         break;
  1665.  
  1666.         LABEL (O_LDM):
  1667.  
  1668.         for (tmp = 0; tmp < 7; tmp++)
  1669.           {
  1670.         if (argb & (1 << tmp))
  1671.           {
  1672.             POPWORD (cpu.regs[tmp].s[LOW]);
  1673.           }
  1674.           }
  1675.         if (argb & 0x80)
  1676.           POPWORD (tmp);    /* dummy ready for sp */
  1677.         goto nextpc;
  1678.         break;
  1679.  
  1680.         LABEL (O_LINK):
  1681.         PUSHWORD (cpu.regs[R6].s[LOW]);
  1682.         cpu.regs[R6].s[LOW] = cpu.regs[R7].s[LOW];
  1683.         cpu.regs[R7].s[LOW] += argb;
  1684.         goto nextpc;
  1685.  
  1686.         LABEL (O_STC):
  1687.         LABEL (O_LDC):
  1688.         LABEL (O_MOVFPE):
  1689.         LABEL (O_MOVTPE):
  1690.         LABEL (O_MOV):
  1691.         LABEL (O_TST):
  1692.         res = arga;
  1693.         break;
  1694.  
  1695.         LABEL (O_TRAPA):
  1696.         if (arga == 15)
  1697.           {
  1698.         trap ();
  1699.           }
  1700.         else
  1701.           {
  1702.         PUSHWORD (pc & 0xffff);
  1703.         if (cpu.maximum)
  1704.           {
  1705.             PUSHWORD (NORMAL_CP);
  1706.           }
  1707.         PUSHWORD (NORMAL_SR);
  1708.         if (cpu.maximum)
  1709.           {
  1710.             arga = arga * 4 + 0x40;
  1711.             SET_NORMAL_CPPC (longat (cpu.memory + arga));
  1712.           }
  1713.         else
  1714.           {
  1715.             arga = arga * 2 + 0x20;
  1716.             SET_NORMAL_CPPC (wordat (cpu.memory + arga));
  1717.           }
  1718.           }
  1719.         break;
  1720.  
  1721.         LABEL (O_OR):
  1722.         LABEL (O_ORC):
  1723.         res = arga | argb;
  1724.         break;
  1725.  
  1726.         LABEL (O_XOR):
  1727.         LABEL (O_XORC):
  1728.         res = arga ^ argb;
  1729.         break;
  1730.  
  1731.         LABEL (O_SCB_F):
  1732.         {
  1733.         scb_f:
  1734.           res = arga - 1;
  1735.           code->srca.reg.wptr[0] = res;
  1736.           if (res != -1)
  1737.         {
  1738.           pc = argb;
  1739.           goto next;
  1740.         }
  1741.         }
  1742.         break;
  1743.  
  1744.         LABEL (O_SCB_EQ):
  1745.         if (Z == 1)
  1746.           break;
  1747.         else
  1748.           goto scb_f;
  1749.  
  1750.         LABEL (O_SCB_NE):
  1751.         if (Z == 0)
  1752.           break;
  1753.         else
  1754.           goto scb_f;
  1755.  
  1756.         LABEL (O_NOP):
  1757.         /* If only they were all as simple as this */
  1758.         break;
  1759.  
  1760.         LABEL (O_ROTL):
  1761.         res = arga << 1;
  1762.         C = (res >> argb) & 1;
  1763.         res |= C;
  1764.         break;
  1765.  
  1766.  
  1767.         LABEL (O_ROTR):
  1768.         C = arga & 1;
  1769.         res = arga >> 1;
  1770.         res |= (C << (argb - 1));
  1771.         break;
  1772.  
  1773.         LABEL (O_ROTXL):
  1774.         res = arga << 1;
  1775.         res |= C;
  1776.         C = (res >> argb) & 1;
  1777.         break;
  1778.  
  1779.         LABEL (O_ROTXR):
  1780.         res = arga >> 1;
  1781.         res |= (C << (argb - 1));
  1782.         C = arga & 1;
  1783.         break;
  1784.  
  1785.         LABEL (O_SHAL):
  1786.         res = arga << 1;
  1787.         if (argb == 16)
  1788.           {
  1789.         C = (res >> (16)) & 1;
  1790.         Z = ((res & 0xffff) == 0);
  1791.         N = ((res & 0x8000) != 0);
  1792.           }
  1793.  
  1794.         else
  1795.           {
  1796.         C = (res >> (8)) & 1;
  1797.         Z = ((res & 0xff) == 0);
  1798.         N = ((res & 0x80) != 0);
  1799.  
  1800.           }
  1801.         V = C ^ N;
  1802.         goto none;
  1803.  
  1804.         LABEL (O_SHAR):
  1805.         C = arga & 1;
  1806.         if (argb == 16)
  1807.           {
  1808.         res = ((short) arga) >> 1;
  1809.           }
  1810.         else
  1811.           {
  1812.         res = (SEXTCHAR (arga)) >> 1;
  1813.           }
  1814.         break;
  1815.  
  1816.         LABEL (O_SHLL):
  1817.         res = arga << 1;
  1818.         C = (res >> argb) & 1;
  1819.         break;
  1820.  
  1821.         LABEL (O_SHLR):
  1822.         C = arga & 1;
  1823.         res = arga >> 1;
  1824.         break;
  1825.  
  1826.         LABEL (O_DIVXU):
  1827.         if (arga == 0)
  1828.           {
  1829.         N = V = C = 0;
  1830.         Z = 1;
  1831.         cpu.exception = SIGILL;
  1832.           }
  1833.         else
  1834.           {
  1835.         int d = argb / arga;
  1836.         int m = argb % arga;
  1837.         if (code->dst.type == eas.s.ea_reg.s.dstlong)
  1838.           {
  1839.             res = (m << 16) | (d & 0xffff);
  1840.           }
  1841.         else
  1842.           {
  1843.             res = (m << 8) | (d & 0xff);
  1844.           }
  1845.  
  1846.           }
  1847.         break;
  1848.  
  1849.         LABEL (O_MULXU):
  1850.         res = arga * argb;
  1851.         break;
  1852.  
  1853.         LABEL (O_NOT):
  1854.         res = ~arga;
  1855.         break;
  1856.  
  1857.         LABEL (O_SWAP):
  1858.         res = ((arga >> 8) & 0xff) | ((arga << 8) & 0xff00);
  1859.         break;
  1860.  
  1861.  
  1862.         LABEL (O_STM):
  1863.         for (tmp = 7; tmp >= 0; tmp--)
  1864.           {
  1865.         if (arga & (1 << tmp))
  1866.           {
  1867.             PUSHWORD (cpu.regs[tmp].s[LOW]);
  1868.           }
  1869.           }
  1870.         goto nextpc;
  1871.  
  1872.         LABEL (O_TAS):
  1873.         C = 0;
  1874.         V = 0;
  1875.         Z = arga == 0;
  1876.         N = arga < 0;
  1877.         res = arga | 0x80;
  1878.         goto none;
  1879.  
  1880.         LABEL (O_PRTD):
  1881.         LABEL (O_XCH):
  1882.         LABEL (O_RTD):
  1883.         cpu.exception = SIGILL;
  1884.         goto next;
  1885.  
  1886.         LABEL (O_TRAP_VS):
  1887.         LABEL (O_SLEEP):
  1888.         LABEL (O_BPT):
  1889.         cpu.exception = SIGTRAP;
  1890.         goto next;
  1891.         break;
  1892.       }
  1893.  
  1894.     ENDDISPATCH;
  1895.  
  1896.     DISPATCH (code->flags)
  1897.       {
  1898.       bitop:
  1899.         Z = (res & bit) == 0;
  1900.         pc = code->next_pc;
  1901.         break;
  1902.         LABEL (FLAG_multword):
  1903.         Z = (res & 0xffff) == 0;
  1904.         N = (res & 0x8000) != 0;
  1905.         V = 0;
  1906.         C = 0;
  1907.         pc = code->next_pc;
  1908.         break;
  1909.  
  1910.         LABEL (FLAG_multbyte):
  1911.         /* 8*8 -> 16 */
  1912.         Z = (res & 0xff) == 0;
  1913.         N = (res & 0x80) != 0;
  1914.         V = 0;
  1915.         C = 0;
  1916.         pc = code->next_pc;
  1917.         break;
  1918.  
  1919.         LABEL (FLAG_shiftword):
  1920.         N = (res & 0x8000) != 0;
  1921.         Z = (res & 0xffff) == 0;
  1922.         V = 0;
  1923.         pc = code->next_pc;
  1924.         break;
  1925.  
  1926.         LABEL (FLAG_shiftbyte):
  1927.         N = (res & 0x80) != 0;
  1928.         Z = (res & 0xff) == 0;
  1929.         V = 0;
  1930.         pc = code->next_pc;
  1931.         break;
  1932.  
  1933.         LABEL (FLAG_special):
  1934.         pc = code->next_pc;
  1935.         break;
  1936.  
  1937.         LABEL (FLAG_m):
  1938.         /* Move byte flags */
  1939.         /* after a logical instruction */
  1940.         N = (res & 0x80) != 0;
  1941.         Z = (res & 0xff) == 0;
  1942.         V = (((~arga & ~argb & res) | (arga & argb & ~res)) & 0x80) != 0;
  1943.         pc = code->next_pc;
  1944.         break;
  1945.  
  1946.         LABEL (FLAG_M):
  1947.         /* Move word flags */
  1948.         /* after a logical instruction */
  1949.         N = (res & 0x8000) != 0;
  1950.         Z = (res & 0xffff) == 0;
  1951.         V = (((~arga & ~argb & res) | (arga & argb & ~res)) & 0x8000) != 0;
  1952.         pc = code->next_pc;
  1953.         break;
  1954.  
  1955.         LABEL (FLAG_a):
  1956.         /* after byte sized arith */
  1957.         C = (res & 0x100) != 0;
  1958.         N = (res & 0x80) != 0;
  1959.         Z = (res & 0xff) == 0;
  1960.         V = (((~arga & ~argb & res) | (arga & argb & ~res)) & 0x80) != 0;
  1961.         pc = code->next_pc;
  1962.         break;
  1963.  
  1964.         LABEL (FLAG_A):
  1965.         /* after word sized arith */
  1966.         C = (res & 0x10000) != 0;
  1967.         N = (res & 0x8000) != 0;
  1968.         Z = (res & 0xffff) == 0;
  1969.         V = (((~arga & ~argb & res) | (arga & argb & ~res)) & 0x8000) != 0;
  1970.         pc = code->next_pc;
  1971.         break;
  1972.  
  1973.         LABEL (FLAG_NONE):
  1974.       none:;
  1975.         /* no flags but store */
  1976.         pc = code->next_pc;
  1977.         break;
  1978.         LABEL (FLAG_NOSTORE):
  1979.         /* no flags and no store */
  1980.         pc = code->next_pc;
  1981.         break;
  1982.         LABEL (FLAG_CLEAR):
  1983.         /* clear flags */
  1984.         N = 0;
  1985.         Z = 1;
  1986.         V = 0;
  1987.         C = 0;
  1988.         pc = code->next_pc;
  1989.         break;
  1990.       condtrue:
  1991.         pc = arga;
  1992.         goto next;
  1993.       }
  1994.     ENDDISPATCH;
  1995.  
  1996.     DISPATCH (code->dst.type)
  1997.       {
  1998.         unsigned char *lval;
  1999.  
  2000.         LABEL (STORE_CRB):
  2001.         (*(code->dst.reg.segptr)) = cpu.memory + (res << 16);
  2002.         break;
  2003.  
  2004.         LABEL (STORE_NOP):
  2005.         break;
  2006.  
  2007.         LABEL (STORE_REG_B):
  2008.         (*(code->dst.reg.bptr)) = res;
  2009.         break;
  2010.  
  2011.         LABEL (STORE_REG_W):
  2012.         (*(code->dst.reg.wptr)) = res;
  2013.         break;
  2014.  
  2015.         LABEL (STORE_REG_L):
  2016.         {
  2017.           int l, r;
  2018.  
  2019.           r = (union rtype *) (code->dst.reg.wptr) - &cpu.regs[0];
  2020.           r++;
  2021.           *(code->dst.reg.wptr) = res >> 16;
  2022.           cpu.regs[r].s[LOW] = res & 0xffff;
  2023.  
  2024.         }
  2025.  
  2026.         break;
  2027.  
  2028.         LABEL (STORE_DISP_W):
  2029.         lval = displval (code->dst);
  2030.         setwordat (lval, res);
  2031.         break;
  2032.  
  2033.         LABEL (STORE_DISP_B):
  2034.         lval = displval (code->dst);
  2035.         setbyteat (lval, res);
  2036.         break;
  2037.  
  2038.         LABEL (STORE_INC_B):
  2039.         lval = elval (code->dst, 0);
  2040.         setbyteat (lval, res);
  2041.         (*(code->dst.reg.wptr))++;
  2042.         break;
  2043.  
  2044.         LABEL (STORE_INC_W):
  2045.         lval = elval (code->dst, 0);
  2046.         setwordat (lval, res);
  2047.         (*(code->dst.reg.wptr)) += 2;
  2048.         break;
  2049.  
  2050.         LABEL (STORE_DEC_B):
  2051.         (*(code->dst.reg.wptr))--;
  2052.         lval = elval (code->dst, 0);
  2053.         setbyteat (lval, res);
  2054.         break;
  2055.  
  2056.         LABEL (STORE_CRW):
  2057.         /* Make an up to date sr from the flag state */
  2058.         cpu.regs[R_SR].s[LOW] = res;
  2059.         GETSR ();
  2060.         break;
  2061.  
  2062.         LABEL (STORE_DEC_W):
  2063.         (*(code->dst.reg.wptr)) -= 2;
  2064.         lval = elval (code->dst, 0);
  2065.         setwordat (lval, res);
  2066.  
  2067.         break;
  2068.  
  2069.       nextpc:
  2070.         pc = code->next_pc;
  2071.  
  2072.       }
  2073.     ENDDISPATCH;
  2074.       next:;
  2075.       }
  2076.     while (!cpu.exception);
  2077.   }
  2078.  
  2079.   cpu.ticks += get_now () - tick_start;
  2080.   cpu.cycles += cycles;
  2081.   cpu.insts += insts;
  2082.   cpu.regs[R_PC].s[LOW] = pc;
  2083.   BUILDSR ();
  2084.  
  2085.   signal (SIGINT, prev);
  2086.   signal (SIGSEGV, prev_seg);
  2087. }
  2088.  
  2089.  
  2090.  
  2091.  
  2092. int
  2093. sim_write (addr, buffer, size)
  2094.      SIM_ADDR addr;
  2095.      unsigned char *buffer;
  2096.      int size;
  2097. {
  2098.   int i;
  2099.  
  2100.   init_pointers ();
  2101.   if (addr < 0 || addr + size > MSIZE)
  2102.     return 0;
  2103.   for (i = 0; i < size; i++)
  2104.     {
  2105.       cpu.memory[addr + i] = buffer[i];
  2106.       cpu.cache_idx[addr + i] = 0;
  2107.     }
  2108.   return size;
  2109. }
  2110.  
  2111. int
  2112. sim_read (addr, buffer, size)
  2113.      SIM_ADDR addr;
  2114.      unsigned char *buffer;
  2115.      int size;
  2116. {
  2117.   init_pointers ();
  2118.   if (addr < 0 || addr + size > MSIZE)
  2119.     return 0;
  2120.   memcpy (buffer, cpu.memory + addr, size);
  2121.   return size;
  2122. }
  2123.  
  2124. /* Ripped off from tm-h8500.h */
  2125.  
  2126. #define R0_REGNUM    0
  2127. #define R1_REGNUM    1
  2128. #define R2_REGNUM    2
  2129. #define R3_REGNUM    3
  2130. #define R4_REGNUM    4
  2131. #define R5_REGNUM    5
  2132. #define R6_REGNUM    6
  2133. #define R7_REGNUM    7
  2134.  
  2135. /* As above, but with correct seg register glued on */
  2136. #define PR0_REGNUM    8
  2137. #define PR1_REGNUM    9
  2138. #define PR2_REGNUM    10
  2139. #define PR3_REGNUM    11
  2140. #define PR4_REGNUM    12
  2141. #define PR5_REGNUM    13
  2142. #define PR6_REGNUM    14
  2143. #define PR7_REGNUM    15
  2144.  
  2145. #define SP_REGNUM       PR7_REGNUM    /* Contains address of top of stack */
  2146. #define FP_REGNUM       PR6_REGNUM    /* Contains address of executing stack frame */
  2147.  
  2148.  
  2149. #define SEG_C_REGNUM    16    /* Segment registers */
  2150. #define SEG_D_REGNUM    17
  2151. #define SEG_E_REGNUM    18
  2152. #define SEG_T_REGNUM    19
  2153.  
  2154. #define CCR_REGNUM      20    /* Contains processor status */
  2155. #define PC_REGNUM       21    /* Contains program counter */
  2156.  
  2157. #define CYCLE_REGNUM    22
  2158. #define INST_REGNUM     23
  2159. #define TICK_REGNUM     24
  2160.  
  2161. void
  2162. sim_store_register (rn, value)
  2163.      int rn;
  2164.      unsigned char *value;
  2165. {
  2166.   int seg = 0;
  2167.   int reg = -1;
  2168.  
  2169.   init_pointers ();
  2170.   switch (rn)
  2171.     {
  2172.     case PC_REGNUM:
  2173.       SET_SEGREG (R_CP, (value[1]<<16));
  2174.       cpu.regs[R_PC].s[LOW] = (value[2] << 8) | value[3];
  2175.       break;
  2176.     case SEG_C_REGNUM:
  2177.     case SEG_D_REGNUM:
  2178.     case SEG_E_REGNUM:
  2179.     case SEG_T_REGNUM:
  2180.       seg = rn - SEG_C_REGNUM + R_CP;
  2181.       reg = -1;
  2182.       break;
  2183.     default:
  2184.       abort ();
  2185.     case R0_REGNUM:
  2186.     case R1_REGNUM:
  2187.     case R2_REGNUM:
  2188.     case R3_REGNUM:
  2189.     case R4_REGNUM:
  2190.     case R5_REGNUM:
  2191.     case R6_REGNUM:
  2192.     case R7_REGNUM:
  2193.       seg = 0;
  2194.       reg = rn - R0_REGNUM;
  2195.       break;
  2196.     case CCR_REGNUM:
  2197.       seg = 0;
  2198.       reg = R_SR;
  2199.       break;
  2200.     case CYCLE_REGNUM:
  2201.       cpu.cycles = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
  2202.       return;
  2203.     case INST_REGNUM:
  2204.       cpu.insts = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
  2205.       return;
  2206.     case TICK_REGNUM:
  2207.       cpu.ticks = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
  2208.       return;
  2209.     case PR0_REGNUM:
  2210.     case PR1_REGNUM:
  2211.     case PR2_REGNUM:
  2212.     case PR3_REGNUM:
  2213.     case PR4_REGNUM:
  2214.     case PR5_REGNUM:
  2215.     case PR6_REGNUM:
  2216.     case PR7_REGNUM:
  2217.       SET_SEGREG (segforreg[rn], value[1]);
  2218.       reg = rn - PR0_REGNUM;      
  2219.       cpu.regs[reg].s[LOW] = (value[2] << 8) | value[3];
  2220.       return;
  2221.     }
  2222.  
  2223.   if (seg)
  2224.     SET_SEGREG (seg, value[0] << 16);
  2225.  
  2226.   if (reg > 0)
  2227.     {
  2228.       cpu.regs[reg].s[LOW] = (value[0] << 8) | value[1];
  2229.     }
  2230. }
  2231.  
  2232. void
  2233. sim_fetch_register (rn, buf)
  2234.      int rn;
  2235.      unsigned char *buf;
  2236. {
  2237.   init_pointers ();
  2238.  
  2239.   switch (rn)
  2240.     {
  2241.     default:
  2242.       abort ();
  2243.     case SEG_C_REGNUM:
  2244.     case SEG_D_REGNUM:
  2245.     case SEG_E_REGNUM:
  2246.     case SEG_T_REGNUM:
  2247.       buf[0] = GET_SEGREG(rn - SEG_C_REGNUM + R_CP);
  2248.       break;
  2249.     case CCR_REGNUM:
  2250.       buf[0] = cpu.regs[R_SR].s[HIGH];
  2251.       buf[1] = cpu.regs[R_SR].s[LOW];
  2252.       break;
  2253.     case PC_REGNUM:
  2254.       buf[0] = 0;
  2255.       buf[1] = GET_SEGREG(R_CP);
  2256.       buf[2] = HIGH_BYTE (cpu.regs[R_PC].s[LOW]);
  2257.       buf[3] = LOW_BYTE (cpu.regs[R_PC].s[LOW]);
  2258.       break;
  2259.  
  2260.     case PR0_REGNUM:
  2261.     case PR1_REGNUM:
  2262.     case PR2_REGNUM:
  2263.     case PR3_REGNUM:
  2264.     case PR4_REGNUM:
  2265.     case PR5_REGNUM:
  2266.     case PR6_REGNUM:
  2267.     case PR7_REGNUM:
  2268.       rn -= PR0_REGNUM;
  2269.       buf[0] = 0;
  2270.       buf[1] = GET_SEGREG(segforreg[rn]);
  2271.       buf[2] = HIGH_BYTE (cpu.regs[rn].s[LOW]);
  2272.       buf[3] = LOW_BYTE (cpu.regs[rn].s[LOW]);
  2273.       break;
  2274.     case R0_REGNUM:
  2275.     case R1_REGNUM:
  2276.     case R2_REGNUM:
  2277.     case R3_REGNUM:
  2278.     case R4_REGNUM:
  2279.     case R5_REGNUM:
  2280.     case R6_REGNUM:
  2281.     case R7_REGNUM:
  2282.       buf[0] = HIGH_BYTE (cpu.regs[rn].s[LOW]);
  2283.       buf[1] = LOW_BYTE (cpu.regs[rn].s[LOW]);
  2284.       break;
  2285.     case CYCLE_REGNUM:
  2286.       buf[0] = cpu.cycles >> 24;
  2287.       buf[1] = cpu.cycles >> 16;
  2288.       buf[2] = cpu.cycles >> 8;
  2289.       buf[3] = cpu.cycles >> 0;
  2290.       break;
  2291.  
  2292.     case TICK_REGNUM:
  2293.       buf[0] = cpu.ticks >> 24;
  2294.       buf[1] = cpu.ticks >> 16;
  2295.       buf[2] = cpu.ticks >> 8;
  2296.       buf[3] = cpu.ticks >> 0;
  2297.       break;
  2298.  
  2299.     case INST_REGNUM:
  2300.       buf[0] = cpu.insts >> 24;
  2301.       buf[1] = cpu.insts >> 16;
  2302.       buf[2] = cpu.insts >> 8;
  2303.       buf[3] = cpu.insts >> 0;
  2304.       break;
  2305.     }
  2306. }
  2307.  
  2308. int
  2309. sim_trace ()
  2310. {
  2311.  
  2312.   int i;
  2313.  
  2314.   for (i = 0; i < 12; i += 2)
  2315.     {
  2316.       unsigned char *p = cpu.regs[R_TP].c + ((cpu.regs[R6].s[LOW] + i) & 0xffff);
  2317.       unsigned short *j = (unsigned short *) p;
  2318.  
  2319.       printf ("%04x ", *j);
  2320.     }
  2321.   printf ("\n");
  2322.   printf ("%02x %02x %02x %02x:%04x %04x %04x %04x %04x %04x %04x %04x %04x\n",
  2323.       NORMAL_DP,
  2324.       NORMAL_EP,
  2325.       NORMAL_TP,
  2326.       NORMAL_CP,
  2327.       cpu.regs[R_PC].s[LOW],
  2328.       cpu.regs[0].s[LOW],
  2329.       cpu.regs[1].s[LOW],
  2330.       cpu.regs[2].s[LOW],
  2331.       cpu.regs[3].s[LOW],
  2332.       cpu.regs[4].s[LOW],
  2333.       cpu.regs[5].s[LOW],
  2334.       cpu.regs[6].s[LOW],
  2335.       cpu.regs[7].s[LOW]);
  2336.   sim_resume (1, 0);
  2337.   return 0;
  2338. }
  2339.  
  2340. void
  2341. sim_stop_reason (reason, sigrc)
  2342.      enum sim_stop *reason;
  2343.      int *sigrc;
  2344. {
  2345.   *reason = sim_stopped;
  2346.   *sigrc = cpu.exception;
  2347. }
  2348.  
  2349.  
  2350. sim_csize (n)
  2351. {
  2352.   if (cpu.cache)
  2353.     free (cpu.cache);
  2354.   if (n < 2)
  2355.     n = 2;
  2356.   cpu.cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n);
  2357.   cpu.csize = n;
  2358. }
  2359.  
  2360.  
  2361. void
  2362. sim_info (verbose)
  2363.      int verbose;
  2364. {
  2365.   double timetaken = (double) cpu.ticks / (double) now_persec ();
  2366.   double virttime = cpu.cycles / 10.0e6;
  2367.  
  2368.   printf_filtered ("\n\ninstructions executed  %10d\n", cpu.insts);
  2369.   printf_filtered ("cycles (v approximate) %10d\n", cpu.cycles);
  2370.   printf_filtered ("real time taken        %10.4f\n", timetaken);
  2371.   printf_filtered ("virtual time taked     %10.4f\n", virttime);
  2372.   if (timetaken) 
  2373.     {
  2374.       printf_filtered ("simulation ratio       %10.4f\n", virttime / timetaken);
  2375.     }
  2376.   
  2377.   printf_filtered ("compiles               %10d\n", cpu.compiles);
  2378.   printf_filtered ("cache size             %10d\n", cpu.csize);
  2379. }
  2380.  
  2381. void
  2382. sim_kill()
  2383. {
  2384.   /* nothing to do */
  2385. }
  2386.  
  2387. void
  2388. sim_open (args)
  2389.      char *args;
  2390. {
  2391.   /* nothing to do */
  2392. }
  2393.  
  2394. void
  2395. sim_close (quitting)
  2396.      int quitting;
  2397. {
  2398.   /* nothing to do */
  2399. }
  2400.  
  2401. int
  2402. sim_load (prog, from_tty)
  2403.      char *prog;
  2404.      int from_tty;
  2405. {
  2406.   /* Return nonzero so gdb will handle it.  */
  2407.   return 1;
  2408. }
  2409.  
  2410. void
  2411. sim_create_inferior (start_address, argv, env)
  2412.      SIM_ADDR start_address;
  2413.      char **argv;
  2414.      char **env;
  2415. {
  2416.   /* ??? We assume this is a 4 byte quantity.  */
  2417.   int pc;
  2418.  
  2419.   pc = start_address;
  2420.   sim_store_register (PC_REGNUM, (unsigned char *) &pc);
  2421. }
  2422.