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