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