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 / h8300 / compile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-18  |  32.9 KB  |  1,805 lines

  1. /*
  2.  * Simulator for the Hitachi H8/300 architecture.
  3.  *
  4.  * Written by Steve Chamberlain of Cygnus Support. sac@cygnus.com
  5.  *
  6.  * This file is part of H8/300 sim
  7.  *
  8.  *
  9.  * THIS SOFTWARE IS NOT COPYRIGHTED
  10.  *
  11.  * Cygnus offers the following for use in the public domain.  Cygnus makes no
  12.  * warranty with regard to the software or its performance and the user
  13.  * accepts the software "AS IS" with all faults.
  14.  *
  15.  * CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS
  16.  * SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #include <signal.h>
  21. #include <sys/times.h>
  22. #include <sys/param.h>
  23. #include "ansidecl.h"
  24. #include "sysdep.h"
  25. #include "remote-sim.h"
  26. #include "bfd.h"
  27.  
  28. int debug;
  29.  
  30.  
  31. #define X(op, size)  op*4+size
  32.  
  33. #define SP (h8300hmode ? SL:SW)
  34. #define SB 0
  35. #define SW 1
  36. #define SL 2
  37. #define OP_REG 1
  38. #define OP_DEC 2
  39. #define OP_DISP 3
  40. #define OP_INC 4
  41. #define OP_PCREL 5
  42. #define OP_MEM 6
  43. #define OP_CCR 7
  44. #define OP_IMM 8
  45. #define OP_ABS 10
  46. #define h8_opcodes ops
  47. #define DEFINE_TABLE
  48. #include "opcode/h8300.h"
  49.  
  50. #include "inst.h"
  51.  
  52. #define LOW_BYTE(x) ((x) & 0xff)
  53. #define HIGH_BYTE(x) (((x)>>8) & 0xff)
  54. #define P(X,Y) ((X<<8) | Y)
  55.  
  56. #define BUILDSR()   cpu.ccr = (N << 3) | (Z << 2) | (V<<1) | C;
  57.  
  58. #define GETSR()            \
  59.   c = (cpu.ccr >> 0) & 1;\
  60.   v = (cpu.ccr >> 1) & 1;\
  61.   nz = !((cpu.ccr >> 2) & 1);\
  62.   n = (cpu.ccr >> 3) & 1;
  63.  
  64. #ifdef __CHAR_IS_SIGNED__
  65. #define SEXTCHAR(x) ((char)(x))
  66. #endif
  67.  
  68. #ifndef SEXTCHAR
  69. #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff):x)
  70. #endif
  71.  
  72. #define UEXTCHAR(x) ((x) & 0xff)
  73. #define UEXTSHORT(x) ((x) & 0xffff)
  74. #define SEXTSHORT(x) ((short)(x))
  75.  
  76. static cpu_state_type cpu;
  77.  
  78. int h8300hmode = 0;
  79.  
  80.  
  81. static int
  82. get_now ()
  83. {
  84.   struct tms b;
  85.  
  86.   return time (0);
  87. #if 0
  88.   times (&b);
  89.   return b.tms_utime + b.tms_stime;
  90. #endif
  91. }
  92.  
  93. static int
  94. now_persec ()
  95. {
  96.   return 1;
  97. }
  98.  
  99.  
  100. static int
  101. bitfrom (x)
  102. {
  103.   switch (x & SIZE)
  104.     {
  105.     case L_8:
  106.       return SB;
  107.     case L_16:
  108.       return SW;
  109.     case L_32:
  110.       return SL;
  111.     case L_P:
  112.       return h8300hmode ? SL : SW;
  113.     }
  114. }
  115.  
  116. static
  117. unsigned int
  118. lvalue (x, rn)
  119. {
  120.   switch (x / 4)
  121.     {
  122.     case OP_DISP:
  123.       if (rn == 8)
  124.     {
  125.       return X (OP_IMM, SP);
  126.     }
  127.       return X (OP_REG, SP);
  128.  
  129.     case OP_MEM:
  130.  
  131.       return X (OP_MEM, SP);
  132.     default:
  133.       abort ();
  134.     }
  135. }
  136.  
  137. static unsigned int
  138. decode (addr, data, dst)
  139.      int addr;
  140.      unsigned char *data;
  141.      decoded_inst *dst;
  142.  
  143. {
  144.   int rs = 0;
  145.   int rd = 0;
  146.   int rdisp = 0;
  147.   int abs = 0;
  148.   int plen = 0;
  149.  
  150.   struct h8_opcode *q = h8_opcodes;
  151.   int size = 0;
  152.   dst->dst.type = -1;
  153.   dst->src.type = -1;
  154.   /* Find the exact opcode/arg combo */
  155.   while (q->name)
  156.     {
  157.       op_type *nib;
  158.       unsigned int len = 0;
  159.  
  160.       nib = q->data.nib;
  161.  
  162.       while (1)
  163.     {
  164.       op_type looking_for = *nib;
  165.       int thisnib = data[len >> 1];
  166.  
  167.       thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
  168.  
  169.       if (looking_for < 16 && looking_for >= 0)
  170.         {
  171.           if (looking_for != thisnib)
  172.         goto fail;
  173.         }
  174.       else
  175.         {
  176.           if ((int) looking_for & (int) B31)
  177.         {
  178.           if (!(((int) thisnib & 0x8) != 0))
  179.             goto fail;
  180.           looking_for = (op_type) ((int) looking_for & ~(int)
  181.                        B31);
  182.           thisnib &= 0x7;
  183.         }
  184.           if ((int) looking_for & (int) B30)
  185.         {
  186.           if (!(((int) thisnib & 0x8) == 0))
  187.             goto fail;
  188.           looking_for = (op_type) ((int) looking_for & ~(int) B30);
  189.         }
  190.           if (looking_for & DBIT)
  191.         {
  192.           if ((looking_for & 5) != (thisnib & 5))
  193.             goto fail;
  194.           abs = (thisnib & 0x8) ? 2 : 1;
  195.         }
  196.           else if (looking_for & (REG | IND | INC | DEC))
  197.         {
  198.           if (looking_for & REG)
  199.             {
  200.               /*
  201.                * Can work out size from the
  202.                * register
  203.                */
  204.               size = bitfrom (looking_for);
  205.             }
  206.           if (looking_for & SRC)
  207.             {
  208.               rs = thisnib;
  209.             }
  210.           else
  211.             {
  212.               rd = thisnib;
  213.             }
  214.         }
  215.           else if (looking_for & L_16)
  216.         {
  217.           abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
  218.           plen = 16;
  219.           if (looking_for & (PCREL | DISP))
  220.             {
  221.               abs = (short) (abs);
  222.             }
  223.         }
  224.           else if (looking_for & ABSJMP)
  225.         {
  226.           abs =
  227.             (data[1] << 16)
  228.             | (data[2] << 8)
  229.             | (data[3]);
  230.         }
  231.           else if (looking_for & MEMIND)
  232.         {
  233.           abs = data[1];
  234.         }
  235.           else if (looking_for & L_32)
  236.         {
  237.           int i = len >> 1;
  238.           abs = (data[i] << 24)
  239.             | (data[i + 1] << 16)
  240.             | (data[i + 2] << 8)
  241.             | (data[i + 3]);
  242.  
  243.           plen = 32;
  244.         }
  245.           else if (looking_for & L_24)
  246.         {
  247.           int i = len >> 1;
  248.           abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]);
  249.           plen = 24;
  250.         }
  251.           else if (looking_for & IGNORE)
  252.         {
  253.           /* nothing to do */
  254.         }
  255.           else if (looking_for & DISPREG)
  256.         {
  257.           rdisp = thisnib & 0x7;
  258.         }
  259.           else if (looking_for & KBIT)
  260.         {
  261.           switch (thisnib)
  262.             {
  263.             case 9:
  264.               abs = 4;
  265.               break;
  266.             case 8:
  267.               abs = 2;
  268.               break;
  269.             case 0:
  270.               abs = 1;
  271.               break;
  272.             }
  273.         }
  274.           else if (looking_for & L_8)
  275.         {
  276.           plen = 8;
  277.  
  278.           if (looking_for & PCREL)
  279.             {
  280.               abs = SEXTCHAR (data[len >> 1]);
  281.             }
  282.           else
  283.             {
  284.               abs = data[len >> 1] & 0xff;
  285.             }
  286.         }
  287.           else if (looking_for & L_3)
  288.         {
  289.           plen = 3;
  290.  
  291.           abs = thisnib;
  292.         }
  293.           else if (looking_for == E)
  294.         {
  295.           dst->op = q;
  296.  
  297.           /* Fill in the args */
  298.           {
  299.             op_type *args = q->args.nib;
  300.             int hadone = 0;
  301.  
  302.             while (*args != E)
  303.               {
  304.             int x = *args;
  305.             int rn = (x & DST) ? rd : rs;
  306.             ea_type *p;
  307.  
  308.             if (x & DST)
  309.               {
  310.                 p = &(dst->dst);
  311.               }
  312.             else
  313.               {
  314.                 p = &(dst->src);
  315.               }
  316.  
  317.             if (x & (IMM | KBIT | DBIT))
  318.               {
  319.                 p->type = X (OP_IMM, size);
  320.                 p->literal = abs;
  321.               }
  322.             else if (x & REG)
  323.               {
  324.                 /* Reset the size, some
  325.                    ops (like mul) have two sizes */
  326.  
  327.                 size = bitfrom (x);
  328.                 p->type = X (OP_REG, size);
  329.                 p->reg = rn;
  330.               }
  331.             else if (x & INC)
  332.               {
  333.                 p->type = X (OP_INC, size);
  334.                 p->reg = rn & 0x7;
  335.               }
  336.             else if (x & DEC)
  337.               {
  338.                 p->type = X (OP_DEC, size);
  339.                 p->reg = rn & 0x7;
  340.               }
  341.             else if (x & IND)
  342.               {
  343.                 p->type = X (OP_DISP, size);
  344.                 p->reg = rn & 0x7;
  345.                 p->literal = 0;
  346.               }
  347.             else if (x & (ABS | ABSJMP | ABSMOV))
  348.               {
  349.                 p->type = X (OP_DISP, size);
  350.                 p->literal = abs;
  351.                 p->reg = 8;
  352.               }
  353.             else if (x & MEMIND)
  354.               {
  355.                 p->type = X (OP_MEM, size);
  356.                 p->literal = abs;
  357.               }
  358.             else if (x & PCREL)
  359.               {
  360.                 p->type = X (OP_PCREL, size);
  361.                 p->literal = abs + addr + 2;
  362.                 if (x & L_16)
  363.                   p->literal += 2;
  364.               }
  365.             else if (x & ABSJMP)
  366.               {
  367.                 p->type = X (OP_IMM, SP);
  368.                 p->literal = abs;
  369.               }
  370.             else if (x & DISP)
  371.               {
  372.                 p->type = X (OP_DISP, size);
  373.                 p->literal = abs;
  374.                 p->reg = rdisp & 0x7;
  375.               }
  376.             else if (x & CCR)
  377.               {
  378.                 p->type = OP_CCR;
  379.               }
  380.             else
  381.               printf ("Hmmmm %x", x);
  382.  
  383.             args++;
  384.               }
  385.           }
  386.  
  387.           /*
  388.              * But a jmp or a jsr gets
  389.              * automagically lvalued, since we
  390.              * branch to their address not their
  391.              * contents
  392.            */
  393.           if (q->how == O (O_JSR, SB)
  394.               || q->how == O (O_JMP, SB))
  395.             {
  396.               dst->src.type = lvalue (dst->src.type, dst->src.reg);
  397.             }
  398.  
  399.           if (dst->dst.type == -1)
  400.             dst->dst = dst->src;
  401.  
  402.           dst->opcode = q->how;
  403.           dst->cycles = q->time;
  404.  
  405.           /* And a jsr to 0xc4 is turned into a magic trap */
  406.  
  407.           if (dst->opcode == O (O_JSR, SB))
  408.             {
  409.               if (dst->src.literal == 0xc4)
  410.             {
  411.               dst->opcode = O (O_SYSCALL, SB);
  412.             }
  413.             }
  414.  
  415.           dst->next_pc = addr + len / 2;
  416.           return;
  417.         }
  418.           else
  419.         {
  420.           printf ("Dont understand %x \n", looking_for);
  421.         }
  422.         }
  423.  
  424.       len++;
  425.       nib++;
  426.     }
  427.  
  428.     fail:
  429.       q++;
  430.     }
  431.  
  432.   dst->opcode = O (O_ILL, SB);
  433. }
  434.  
  435.  
  436. static void
  437. compile (pc)
  438. {
  439.   int idx;
  440.  
  441.   /* find the next cache entry to use */
  442.  
  443.   idx = cpu.cache_top + 1;
  444.   cpu.compiles++;
  445.   if (idx >= cpu.csize)
  446.     {
  447.       idx = 1;
  448.     }
  449.   cpu.cache_top = idx;
  450.  
  451.   /* Throw away its old meaning */
  452.   cpu.cache_idx[cpu.cache[idx].oldpc] = 0;
  453.  
  454.   /* set to new address */
  455.   cpu.cache[idx].oldpc = pc;
  456.  
  457.   /* fill in instruction info */
  458.   decode (pc, cpu.memory + pc, cpu.cache + idx);
  459.  
  460.   /* point to new cache entry */
  461.   cpu.cache_idx[pc] = idx;
  462. }
  463.  
  464.  
  465. static unsigned char *breg[18];
  466. static unsigned short *wreg[18];
  467. static unsigned int *lreg[18];
  468.  
  469. #define GET_B_REG(x) *(breg[x])
  470. #define SET_B_REG(x,y) (*(breg[x])) = (y)
  471. #define GET_W_REG(x) *(wreg[x])
  472. #define SET_W_REG(x,y) (*(wreg[x])) = (y)
  473.  
  474. #define GET_L_REG(x) *(lreg[x])
  475. #define SET_L_REG(x,y) (*(lreg[x])) = (y)
  476.  
  477. #define GET_MEMORY_L(x) \
  478.   ((cpu.memory[x+0] << 24) | (cpu.memory[x+1] << 16) | (cpu.memory[x+2] << 8) | cpu.memory[x+3])
  479.  
  480. #define GET_MEMORY_W(x) \
  481.   ((cpu.memory[x+0] << 8) | (cpu.memory[x+1] << 0))
  482.  
  483.  
  484. #define SET_MEMORY_B(x,y) \
  485.   (cpu.memory[(x)] = y)
  486.  
  487. #define SET_MEMORY_W(x,y) \
  488. {register unsigned char *_p = cpu.memory+x;\
  489.    register int __y = y;\
  490.      _p[0] = (__y)>>8;\
  491.        _p[1] =(__y);     }
  492.  
  493. #define SET_MEMORY_L(x,y)  \
  494. {register unsigned char *_p = cpu.memory+x;register int __y = y;\
  495.    _p[0] = (__y)>>24;     _p[1] = (__y)>>16;     _p[2] = (__y)>>8;     _p[3] = (__y)>>0;}
  496.  
  497. #define GET_MEMORY_B(x)  (cpu.memory[x])
  498.  
  499. int
  500. fetch (arg, n)
  501.      ea_type *arg;
  502. {
  503.   int rn = arg->reg;
  504.   int abs = arg->literal;
  505.   int r;
  506.   int t;
  507.  
  508.   switch (arg->type)
  509.     {
  510.     case X (OP_REG, SB):
  511.       return GET_B_REG (rn);
  512.     case X (OP_REG, SW):
  513.       return GET_W_REG (rn);
  514.     case X (OP_REG, SL):
  515.       return GET_L_REG (rn);
  516.     case X (OP_IMM, SB):
  517.     case X (OP_IMM, SW):
  518.     case X (OP_IMM, SL):
  519.       return abs;
  520.     case X (OP_DEC, SB):
  521.       abort ();
  522.  
  523.     case X (OP_INC, SB):
  524.       t = GET_L_REG (rn);
  525.       t &= cpu.mask;
  526.       r = GET_MEMORY_B (t);
  527.       t++;
  528.       t = t & cpu.mask;
  529.       SET_L_REG (rn, t);
  530.       return r;
  531.       break;
  532.     case X (OP_INC, SW):
  533.       t = GET_L_REG (rn);
  534.       t &= cpu.mask;
  535.       r = GET_MEMORY_W (t);
  536.       t += 2;
  537.       t = t & cpu.mask;
  538.       SET_L_REG (rn, t);
  539.       return r;
  540.     case X (OP_INC, SL):
  541.       t = GET_L_REG (rn);
  542.       t &= cpu.mask;
  543.       r = GET_MEMORY_L (t);
  544.  
  545.       t += 4;
  546.       t = t & cpu.mask;
  547.       SET_L_REG (rn, t);
  548.       return r;
  549.  
  550.     case X (OP_DISP, SB):
  551.       t = GET_L_REG (rn) + abs;
  552.       t &= cpu.mask;
  553.       return GET_MEMORY_B (t);
  554.  
  555.     case X (OP_DISP, SW):
  556.       t = GET_L_REG (rn) + abs;
  557.       t &= cpu.mask;
  558.       return GET_MEMORY_W (t);
  559.  
  560.     case X (OP_DISP, SL):
  561.       t = GET_L_REG (rn) + abs;
  562.       t &= cpu.mask;
  563.       return GET_MEMORY_L (t);
  564.  
  565.     case X (OP_MEM, SL):
  566.       t = GET_MEMORY_L (abs);
  567.       t &= cpu.mask;
  568.       return t;
  569.  
  570.     default:
  571.       abort ();
  572.  
  573.     }
  574. }
  575.  
  576.  
  577. static
  578. void
  579. store (arg, n)
  580.      ea_type *arg;
  581.      int n;
  582. {
  583.   int rn = arg->reg;
  584.   int abs = arg->literal;
  585.   int t;
  586.  
  587.   switch (arg->type)
  588.     {
  589.     case X (OP_REG, SB):
  590.       SET_B_REG (rn, n);
  591.       break;
  592.     case X (OP_REG, SW):
  593.       SET_W_REG (rn, n);
  594.       break;
  595.     case X (OP_REG, SL):
  596.       SET_L_REG (rn, n);
  597.       break;
  598.  
  599.     case X (OP_DEC, SB):
  600.       t = GET_L_REG (rn) - 1;
  601.       t &= cpu.mask;
  602.       SET_L_REG (rn, t);
  603.       SET_MEMORY_B (t, n);
  604.  
  605.       break;
  606.     case X (OP_DEC, SW):
  607.       t = (GET_L_REG (rn) - 2) & cpu.mask;
  608.       SET_L_REG (rn, t);
  609.       SET_MEMORY_W (t, n);
  610.       break;
  611.  
  612.     case X (OP_DEC, SL):
  613.       t = (GET_L_REG (rn) - 4) & cpu.mask;
  614.       SET_L_REG (rn, t);
  615.       SET_MEMORY_L (t, n);
  616.       break;
  617.  
  618.     case X (OP_DISP, SB):
  619.       t = GET_L_REG (rn) + abs;
  620.       t &= cpu.mask;
  621.       SET_MEMORY_B (t, n);
  622.       break;
  623.  
  624.     case X (OP_DISP, SW):
  625.       t = GET_L_REG (rn) + abs;
  626.       t &= cpu.mask;
  627.       SET_MEMORY_W (t, n);
  628.       break;
  629.  
  630.     case X (OP_DISP, SL):
  631.       t = GET_L_REG (rn) + abs;
  632.       t &= cpu.mask;
  633.       SET_MEMORY_L (t, n);
  634.       break;
  635.     default:
  636.       abort ();
  637.     }
  638. }
  639.  
  640.  
  641. static union
  642. {
  643.   short int i;
  644.   struct
  645.     {
  646.       char low;
  647.       char high;
  648.     }
  649.   u;
  650. }
  651.  
  652. littleendian;
  653.  
  654. static
  655. void
  656. init_pointers ()
  657. {
  658.   static int init;
  659.  
  660.   if (!init)
  661.     {
  662.       int i;
  663.  
  664.       init = 1;
  665.       littleendian.i = 1;
  666.  
  667.       cpu.memory = (unsigned char *) calloc (sizeof (char), MSIZE);
  668.       cpu.cache_idx = (unsigned short *) calloc (sizeof (short), MSIZE);
  669.       cpu.mask = (1 << MPOWER) - 1;
  670.  
  671.       for (i = 0; i < 9; i++)
  672.     {
  673.       cpu.regs[i] = 0;
  674.     }
  675.  
  676.       for (i = 0; i < 8; i++)
  677.     {
  678.       unsigned char *p = (unsigned char *) (cpu.regs + i);
  679.       unsigned char *e = (unsigned char *) (cpu.regs + i + 1);
  680.       unsigned short *q = (unsigned short *) (cpu.regs + i);
  681.       unsigned short *u = (unsigned short *) (cpu.regs + i + 1);
  682.       cpu.regs[i] = 0x00112233;
  683.       while (p < e)
  684.         {
  685.           if (*p == 0x22)
  686.         {
  687.           breg[i] = p;
  688.         }
  689.           if (*p == 0x33)
  690.         {
  691.           breg[i + 8] = p;
  692.         }
  693.           p++;
  694.         }
  695.       while (q < u)
  696.         {
  697.           if (*q == 0x2233)
  698.         {
  699.           wreg[i] = q;
  700.         }
  701.           if (*q == 0x0011)
  702.         {
  703.           wreg[i + 8] = q;
  704.         }
  705.           q++;
  706.         }
  707.       cpu.regs[i] = 0;
  708.       lreg[i] = &cpu.regs[i];
  709.     }
  710.  
  711.       lreg[8] = &cpu.regs[8];
  712.  
  713.       /* initialize the seg registers */
  714.       if (!cpu.cache)
  715.     sim_csize (CSIZE);
  716.     }
  717. }
  718.  
  719. static void
  720. control_c (sig, code, scp, addr)
  721.      int sig;
  722.      int code;
  723.      char *scp;
  724.      char *addr;
  725. {
  726.   cpu.exception = SIGINT;
  727. }
  728.  
  729. #define C (c != 0)
  730. #define Z (nz == 0)
  731. #define V (v != 0)
  732. #define N (n != 0)
  733.  
  734. static int
  735. mop (code, bsize, sign)
  736.      decoded_inst *code;
  737.      int bsize;
  738.      int sign;
  739. {
  740.   int multiplier;
  741.   int multiplicand;
  742.   int result;
  743.   int n, nz;
  744.  
  745.   if (sign)
  746.     {
  747.       multiplicand =
  748.     bsize ? SEXTCHAR (GET_W_REG (code->dst.reg)) :
  749.     SEXTSHORT (GET_W_REG (code->dst.reg));
  750.       multiplier =
  751.     bsize ? SEXTCHAR (GET_B_REG (code->src.reg)) :
  752.     SEXTSHORT (GET_W_REG (code->src.reg));
  753.     }
  754.   else
  755.     {
  756.       multiplicand = bsize ? UEXTCHAR (GET_W_REG (code->dst.reg)) :
  757.     UEXTSHORT (GET_W_REG (code->dst.reg));
  758.       multiplier =
  759.     bsize ? UEXTCHAR (GET_B_REG (code->src.reg)) :
  760.     UEXTSHORT (GET_W_REG (code->src.reg));
  761.  
  762.     }
  763.   result = multiplier * multiplicand;
  764.  
  765.   if (sign)
  766.     {
  767.       n = result & (bsize ? 0x8000 : 0x80000000);
  768.       nz = result & (bsize ? 0xffff : 0xffffffff);
  769.     }
  770.   if (bsize)
  771.     {
  772.       SET_W_REG (code->dst.reg, result);
  773.     }
  774.   else
  775.     {
  776.       SET_L_REG (code->dst.reg, result);
  777.     }
  778. /*  return ((n==1) << 1) | (nz==1); */
  779.  
  780. }
  781.  
  782. #define OSHIFTS(name, how) \
  783. case O(name, SB):                \
  784. {                        \
  785.   int t;                    \
  786.   int hm = 0x80;                \
  787.   rd = GET_B_REG (code->src.reg);        \
  788.   how;                         \
  789.   goto shift8;                    \
  790. }                         \
  791. case O(name, SW):                \
  792. {                         \
  793.   int t;                    \
  794.   int hm = 0x8000;                \
  795.   rd = GET_W_REG (code->src.reg);         \
  796.   how;                         \
  797.   goto shift16;                    \
  798. }                         \
  799. case O(name, SL):                \
  800. {                        \
  801.   int t;                    \
  802.   int hm = 0x80000000;                 \
  803.   rd = GET_L_REG (code->src.reg);        \
  804.   how;                         \
  805.   goto shift32;                    \
  806. }
  807.  
  808. #define OBITOP(name,f, s, op)             \
  809. case  O(name, SB):                \
  810. {                        \
  811.   int m;                    \
  812.   int b;                     \
  813.   if (f) ea = fetch (&code->dst);        \
  814.   m=1<< fetch(&code->src);            \
  815.   op;                        \
  816.   if(s) store (&code->dst,ea); goto next;    \
  817. }
  818.  
  819. void
  820. sim_resume (step, siggnal)
  821. {
  822.   static int init1;
  823.   int cycles = 0;
  824.   int insts = 0;
  825.   int tick_start = get_now ();
  826.   void (*prev) ();
  827.   int poll_count = 0;
  828.   int res;
  829.   int tmp;
  830.   int rd;
  831.   int ea;
  832.   int bit;
  833.   int pc;
  834.   int c, nz, v, n;
  835.  
  836.   init_pointers ();
  837.  
  838.   prev = signal (SIGINT, control_c);
  839.  
  840.   if (step)
  841.     {
  842.       cpu.exception = SIGTRAP;
  843.     }
  844.   else
  845.     {
  846.       cpu.exception = 0;
  847.     }
  848.  
  849.   pc = cpu.pc;
  850.  
  851.   GETSR ();
  852.  
  853.   do
  854.     {
  855.       int cidx;
  856.       decoded_inst *code;
  857.  
  858.     top:
  859.       cidx = cpu.cache_idx[pc];
  860.       code = cpu.cache + cidx;
  861.  
  862.  
  863. #define ALUOP(STORE, NAME, HOW) \
  864.     case O(NAME,SB):  HOW; if(STORE)goto alu8;else goto just_flags_alu8;  \
  865.     case O(NAME, SW): HOW; if(STORE)goto alu16;else goto just_flags_alu16; \
  866.     case O(NAME,SL):  HOW; if(STORE)goto alu32;else goto just_flags_alu32;
  867.  
  868.  
  869. #define LOGOP(NAME, HOW) \
  870.     case O(NAME,SB): HOW; goto log8;\
  871.     case O(NAME, SW): HOW; goto log16;\
  872.     case O(NAME,SL): HOW; goto log32;
  873.  
  874.  
  875.  
  876. #if ADEBUG
  877.       if (debug)
  878.     {
  879.       printf ("%x %d %s\n", pc, code->opcode,
  880.           code->op ? code->op->name : "**");
  881.     }
  882.       cpu.stats[code->opcode]++;
  883.  
  884. #endif
  885.  
  886.       cycles += code->cycles;
  887.       insts++;
  888.       switch (code->opcode)
  889.     {
  890.     case 0:
  891.       /*
  892.        * This opcode is a fake for when we get to an
  893.        * instruction which hasnt been compiled
  894.        */
  895.       compile (pc);
  896.       goto top;
  897.       break;
  898.  
  899.  
  900.     case O (O_SUBX, SB):
  901.       rd = fetch (&code->dst);
  902.       ea = fetch (&code->src);
  903.       ea = -(ea + C);
  904.       res = rd + ea;
  905.       goto alu8;
  906.  
  907.     case O (O_ADDX, SB):
  908.       rd = fetch (&code->dst);
  909.       ea = fetch (&code->src);
  910.       ea = C + ea;
  911.       res = rd + ea;
  912.       goto alu8;
  913.  
  914. #define EA    ea = fetch(&code->src);
  915. #define RD_EA ea = fetch(&code->src); rd = fetch(&code->dst);
  916.  
  917.       ALUOP (1, O_SUB, RD_EA;
  918.          ea = -ea;
  919.          res = rd + ea);
  920.       ALUOP (1, O_NEG, EA;
  921.          ea = -ea;
  922.          rd = 0;
  923.          res = rd + ea);
  924.  
  925.     case O (O_ADD, SB):
  926.       rd = GET_B_REG (code->dst.reg);
  927.       ea = fetch (&code->src);
  928.       res = rd + ea;
  929.       goto alu8;
  930.     case O (O_ADD, SW):
  931.       rd = GET_W_REG (code->dst.reg);
  932.       ea = fetch (&code->src);
  933.       res = rd + ea;
  934.       goto alu16;
  935.     case O (O_ADD, SL):
  936.       rd = GET_L_REG (code->dst.reg);
  937.       ea = fetch (&code->src);
  938.       res = rd + ea;
  939.       goto alu32;
  940.  
  941.  
  942.       LOGOP (O_AND, RD_EA;
  943.          res = rd & ea);
  944.  
  945.       LOGOP (O_OR, RD_EA;
  946.          res = rd | ea);
  947.  
  948.       LOGOP (O_XOR, RD_EA;
  949.          res = rd ^ ea);
  950.  
  951.  
  952.     case O (O_MOV_TO_MEM, SB):
  953.       res = GET_B_REG (code->src.reg);
  954.       goto log8;
  955.     case O (O_MOV_TO_MEM, SW):
  956.       res = GET_W_REG (code->src.reg);
  957.       goto log16;
  958.     case O (O_MOV_TO_MEM, SL):
  959.       res = GET_L_REG (code->src.reg);
  960.       goto log32;
  961.  
  962.  
  963.     case O (O_MOV_TO_REG, SB):
  964.       res = fetch (&code->src);
  965.       SET_B_REG (code->dst.reg, res);
  966.       goto just_flags_log8;
  967.     case O (O_MOV_TO_REG, SW):
  968.       res = fetch (&code->src);
  969.       SET_W_REG (code->dst.reg, res);
  970.       goto just_flags_log16;
  971.     case O (O_MOV_TO_REG, SL):
  972.       res = fetch (&code->src);
  973.       SET_L_REG (code->dst.reg, res);
  974.       goto just_flags_log32;
  975.  
  976.  
  977.     case O (O_ADDS, SL):
  978.       SET_L_REG (code->dst.reg,
  979.              GET_L_REG (code->dst.reg)
  980.              + code->src.literal);
  981.  
  982.       goto next;
  983.  
  984.     case O (O_SUBS, SL):
  985.       SET_L_REG (code->dst.reg,
  986.              GET_L_REG (code->dst.reg)
  987.              - code->src.literal);
  988.       goto next;
  989.  
  990.     case O (O_CMP, SB):
  991.       rd = fetch (&code->dst);
  992.       ea = fetch (&code->src);
  993.       ea = -ea;
  994.       res = rd + ea;
  995.       goto just_flags_alu8;
  996.  
  997.     case O (O_CMP, SW):
  998.       rd = fetch (&code->dst);
  999.       ea = fetch (&code->src);
  1000.       ea = -ea;
  1001.       res = rd + ea;
  1002.       goto just_flags_alu16;
  1003.  
  1004.     case O (O_CMP, SL):
  1005.       rd = fetch (&code->dst);
  1006.       ea = fetch (&code->src);
  1007.       ea = -ea;
  1008.       res = rd + ea;
  1009.       goto just_flags_alu32;
  1010.  
  1011.  
  1012.     case O (O_DEC, SB):
  1013.       rd = GET_B_REG (code->src.reg);
  1014.       ea = -1;
  1015.       res = rd + ea;
  1016.       SET_B_REG (code->src.reg, res);
  1017.       goto just_flags_inc8;
  1018.  
  1019.     case O (O_DEC, SW):
  1020.       rd = GET_W_REG (code->dst.reg);
  1021.       ea = -code->src.literal;
  1022.       res = rd + ea;
  1023.       SET_W_REG (code->dst.reg, res);
  1024.       goto just_flags_inc16;
  1025.  
  1026.     case O (O_DEC, SL):
  1027.       rd = GET_L_REG (code->dst.reg);
  1028.       ea = -code->src.literal;
  1029.       res = rd + ea;
  1030.       SET_L_REG (code->dst.reg, res);
  1031.       goto just_flags_inc32;
  1032.  
  1033.  
  1034.     case O (O_INC, SB):
  1035.       rd = GET_B_REG (code->src.reg);
  1036.       ea = 1;
  1037.       res = rd + ea;
  1038.       SET_B_REG (code->src.reg, res);
  1039.       goto just_flags_inc8;
  1040.  
  1041.     case O (O_INC, SW):
  1042.       rd = GET_W_REG (code->dst.reg);
  1043.       ea = code->src.literal;
  1044.       res = rd + ea;
  1045.       SET_W_REG (code->dst.reg, res);
  1046.       goto just_flags_inc16;
  1047.  
  1048.     case O (O_INC, SL):
  1049.       rd = GET_L_REG (code->dst.reg);
  1050.       ea = code->src.literal;
  1051.       res = rd + ea;
  1052.       SET_L_REG (code->dst.reg, res);
  1053.       goto just_flags_inc32;
  1054.  
  1055.  
  1056. #define GET_CCR(x) BUILDSR();x = cpu.ccr
  1057.  
  1058.     case O (O_ANDC, SB):
  1059.       GET_CCR (rd);
  1060.       ea = code->src.literal;
  1061.       res = rd & ea;
  1062.       goto setc;
  1063.  
  1064.     case O (O_ORC, SB):
  1065.       GET_CCR (rd);
  1066.       ea = code->src.literal;
  1067.       res = rd | ea;
  1068.       goto setc;
  1069.  
  1070.     case O (O_XORC, SB):
  1071.       GET_CCR (rd);
  1072.       ea = code->src.literal;
  1073.       res = rd ^ ea;
  1074.       goto setc;
  1075.  
  1076.  
  1077.     case O (O_BRA, SB):
  1078.       if (1)
  1079.         goto condtrue;
  1080.       goto next;
  1081.  
  1082.     case O (O_BRN, SB):
  1083.       if (0)
  1084.         goto condtrue;
  1085.       goto next;
  1086.  
  1087.     case O (O_BHI, SB):
  1088.       if ((C || Z) == 0)
  1089.         goto condtrue;
  1090.       goto next;
  1091.  
  1092.  
  1093.     case O (O_BLS, SB):
  1094.       if ((C || Z))
  1095.         goto condtrue;
  1096.       goto next;
  1097.  
  1098.     case O (O_BCS, SB):
  1099.       if ((C == 1))
  1100.         goto condtrue;
  1101.       goto next;
  1102.  
  1103.     case O (O_BCC, SB):
  1104.       if ((C == 0))
  1105.         goto condtrue;
  1106.       goto next;
  1107.  
  1108.     case O (O_BEQ, SB):
  1109.       if (Z)
  1110.         goto condtrue;
  1111.       goto next;
  1112.     case O (O_BGT, SB):
  1113.       if (((Z || (N ^ V)) == 0))
  1114.         goto condtrue;
  1115.       goto next;
  1116.  
  1117.  
  1118.     case O (O_BLE, SB):
  1119.       if (((Z || (N ^ V)) == 1))
  1120.         goto condtrue;
  1121.       goto next;
  1122.  
  1123.     case O (O_BGE, SB):
  1124.       if ((N ^ V) == 0)
  1125.         goto condtrue;
  1126.       goto next;
  1127.     case O (O_BLT, SB):
  1128.       if ((N ^ V))
  1129.         goto condtrue;
  1130.       goto next;
  1131.     case O (O_BMI, SB):
  1132.       if ((N))
  1133.         goto condtrue;
  1134.       goto next;
  1135.     case O (O_BNE, SB):
  1136.       if ((Z == 0))
  1137.         goto condtrue;
  1138.       goto next;
  1139.  
  1140.     case O (O_BPL, SB):
  1141.       if (N == 0)
  1142.         goto condtrue;
  1143.       goto next;
  1144.     case O (O_BVC, SB):
  1145.       if ((V == 0))
  1146.         goto condtrue;
  1147.       goto next;
  1148.     case O (O_BVS, SB):
  1149.       if ((V == 1))
  1150.         goto condtrue;
  1151.       goto next;
  1152.  
  1153.     case O (O_SYSCALL, SB):
  1154.       printf ("%c", cpu.regs[2]);
  1155.       goto next;
  1156.  
  1157.       OSHIFTS (O_NOT, rd = ~rd);
  1158.       OSHIFTS (O_SHLL, c = rd & hm;
  1159.            rd <<= 1);
  1160.       OSHIFTS (O_SHLR, c = rd & 1;
  1161.            rd = (unsigned int) rd >> 1);
  1162.       OSHIFTS (O_SHAL, c = rd & hm;
  1163.            rd <<= 1);
  1164.       OSHIFTS (O_SHAR, t = rd & hm;
  1165.            c = rd & 1;
  1166.            rd >>= 1;
  1167.            rd |= t;
  1168.         );
  1169.       OSHIFTS (O_ROTL, c = rd & hm;
  1170.            rd <<= 1;
  1171.            rd |= C);
  1172.       OSHIFTS (O_ROTR, c = rd & 1;
  1173.            rd = (unsigned int) rd >> 1;
  1174.            if (c) rd |= hm;);
  1175.       OSHIFTS (O_ROTXL, t = rd & hm;
  1176.            rd <<= 1;
  1177.            rd |= C;
  1178.            c = t;
  1179.         );
  1180.       OSHIFTS (O_ROTXR, t = rd & 1;
  1181.            rd = (unsigned int) rd >> 1;
  1182.            if (C) rd |= hm; c = t;);
  1183.  
  1184.     case O (O_JMP, SB):
  1185.       {
  1186.         pc = fetch (&code->src);
  1187.         goto end;
  1188.  
  1189.       }
  1190.  
  1191.     case O (O_JSR, SB):
  1192.       {
  1193.         int tmp;
  1194.         pc = fetch (&code->src);
  1195.       call:
  1196.         tmp = cpu.regs[7];
  1197.  
  1198.         if (h8300hmode)
  1199.           {
  1200.         tmp -= 4;
  1201.         SET_MEMORY_L (tmp, code->next_pc);
  1202.           }
  1203.         else
  1204.           {
  1205.         tmp -= 2;
  1206.         SET_MEMORY_W (tmp, code->next_pc);
  1207.           }
  1208.         cpu.regs[7] = tmp;
  1209.  
  1210.         goto end;
  1211.       }
  1212.     case O (O_BSR, SB):
  1213.       pc = code->src.literal;
  1214.       goto call;
  1215.  
  1216.     case O (O_RTS, SB):
  1217.       {
  1218.         int tmp;
  1219.  
  1220.         tmp = cpu.regs[7];
  1221.  
  1222.         if (h8300hmode)
  1223.           {
  1224.         pc = GET_MEMORY_L (tmp);
  1225.         tmp += 4;
  1226.           }
  1227.         else
  1228.           {
  1229.         pc = GET_MEMORY_W (tmp);
  1230.         tmp += 2;
  1231.           }
  1232.  
  1233.         cpu.regs[7] = tmp;
  1234.         goto end;
  1235.       }
  1236.  
  1237.     case O (O_ILL, SB):
  1238.       cpu.exception = SIGILL;
  1239.       goto end;
  1240.     case O (O_SLEEP, SB):
  1241.     case O (O_BPT, SB):
  1242.       cpu.exception = SIGTRAP;
  1243.       goto end;
  1244.  
  1245.       OBITOP (O_BNOT, 1, 1, ea ^= m);
  1246.       OBITOP (O_BTST, 1, 0, nz = ea & m);
  1247.       OBITOP (O_BCLR, 1, 1, ea &= ~m);
  1248.       OBITOP (O_BSET, 1, 1, ea |= m);    
  1249.       OBITOP (O_BLD, 1, 0, c = ea & m);
  1250.       OBITOP (O_BILD, 1, 0, c = !(ea & m));
  1251.       OBITOP (O_BST, 1, 1, ea &= ~m;
  1252.           if (C) ea |= m);
  1253.       OBITOP (O_BIST, 1, 1, ea &= ~m;
  1254.           if (!C) ea |= m);
  1255.       OBITOP (O_BAND, 1, 0, c = (ea & m) && C);
  1256.       OBITOP (O_BIAND, 1, 0, c = !(ea & m) && C);
  1257.       OBITOP (O_BOR, 1, 0, c = (ea & m) || C);
  1258.       OBITOP (O_BIOR, 1, 0, c = !(ea & m) || C);
  1259.       OBITOP (O_BXOR, 1, 0, c = (ea & m) != C);
  1260.       OBITOP (O_BIXOR, 1, 0, c = !(ea & m) != C);
  1261.  
  1262.  
  1263. #define MOP(bsize, signed) mop(code, bsize,signed); goto next;
  1264.  
  1265.     case O (O_MULS, SB):
  1266.       MOP (1, 1);
  1267.       break;
  1268.     case O (O_MULS, SW):
  1269.       MOP (0, 1);
  1270.       break;
  1271.     case O (O_MULU, SB):
  1272.       MOP (1, 0);
  1273.       break;
  1274.     case O (O_MULU, SW):
  1275.       MOP (0, 0);
  1276.       break;
  1277.  
  1278.  
  1279.     case O (O_DIVU, SB):
  1280.       {
  1281.         rd = GET_W_REG (code->dst.reg);
  1282.         ea = GET_B_REG (code->src.reg);
  1283.         if (ea)
  1284.           {
  1285.         tmp = rd % ea;
  1286.         rd = rd / ea;
  1287.           }
  1288.         SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8));
  1289.         n = ea & 0x80;
  1290.         nz = ea & 0xff;
  1291.  
  1292.         goto next;
  1293.       }
  1294.     case O (O_DIVU, SW):
  1295.       {
  1296.         rd = GET_L_REG (code->dst.reg);
  1297.         ea = GET_W_REG (code->src.reg);
  1298.         n = ea & 0x8000;
  1299.         nz = ea & 0xffff;
  1300.         if (ea)
  1301.           {
  1302.         tmp = rd % ea;
  1303.         rd = rd / ea;
  1304.           }
  1305.         SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16));
  1306.         goto next;
  1307.       }
  1308.  
  1309.     case O (O_DIVS, SB):
  1310.       {
  1311.  
  1312.         rd = SEXTSHORT (GET_W_REG (code->dst.reg));
  1313.         ea = SEXTCHAR (GET_B_REG (code->src.reg));
  1314.         if (ea)
  1315.           {
  1316.         tmp = (int) rd % (int) ea;
  1317.         rd = (int) rd / (int) ea;
  1318.         n = rd & 0x8000;
  1319.         nz = 1;
  1320.           }
  1321.         else
  1322.           nz = 0;
  1323.         SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8));
  1324.         goto next;
  1325.       }
  1326.     case O (O_DIVS, SW):
  1327.       {
  1328.         rd = GET_L_REG (code->dst.reg);
  1329.         ea = SEXTSHORT (GET_W_REG (code->src.reg));
  1330.         if (ea)
  1331.           {
  1332.         tmp = (int) rd % (int) ea;
  1333.         rd = (int) rd / (int) ea;
  1334.         n = rd & 0x80000000;
  1335.         nz = 1;
  1336.           }
  1337.         else
  1338.           nz = 0;
  1339.         SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16));
  1340.         goto next;
  1341.       }
  1342.     case O (O_EXTS, SW):
  1343.       rd = GET_B_REG (code->src.reg + 8) & 0xff;    /* Yes, src, not dst.  */
  1344.       ea = rd & 0x80 ? -256 : 0;
  1345.       res = rd + ea;
  1346.       goto log16;
  1347.     case O (O_EXTS, SL):
  1348.       rd = GET_W_REG (code->src.reg) & 0xffff;
  1349.       ea = rd & 0x8000 ? -65536 : 0;
  1350.       res = rd + ea;
  1351.       goto log32;
  1352.     case O (O_EXTU, SW):
  1353.       rd = GET_B_REG (code->src.reg + 8) & 0xff;
  1354.       ea = 0;
  1355.       res = rd + ea;
  1356.       goto log16;
  1357.     case O (O_EXTU, SL):
  1358.       rd = GET_W_REG (code->src.reg) & 0xffff;
  1359.       ea = 0;
  1360.       res = rd + ea;
  1361.       goto log32;
  1362.  
  1363.     case O (O_NOP, SB):
  1364.       goto next;
  1365.  
  1366.     default:
  1367.       cpu.exception = SIGILL;
  1368.       goto end;
  1369.  
  1370.     }
  1371.       abort ();
  1372.  
  1373.     setc:
  1374.       cpu.ccr = res;
  1375.       GETSR ();
  1376.       goto next;
  1377.  
  1378.     condtrue:
  1379.       /* When a branch works */
  1380.       pc = code->src.literal;
  1381.       goto end;
  1382.  
  1383.       /* Set the cond codes from res */
  1384.     bitop:
  1385.  
  1386.       /* Set the flags after an 8 bit inc/dec operation */
  1387.     just_flags_inc8:
  1388.       n = res & 0x80;
  1389.       nz = res & 0xff;
  1390.       v = (rd & 0x7f) == 0x7f;
  1391.       goto next;
  1392.  
  1393.  
  1394.       /* Set the flags after an 16 bit inc/dec operation */
  1395.     just_flags_inc16:
  1396.       n = res & 0x8000;
  1397.       nz = res & 0xffff;
  1398.       v = (rd & 0x7fff) == 0x7fff;
  1399.       goto next;
  1400.  
  1401.  
  1402.       /* Set the flags after an 32 bit inc/dec operation */
  1403.     just_flags_inc32:
  1404.       n = res & 0x80000000;
  1405.       nz = res & 0xffffffff;
  1406.       v = (rd & 0x7fffffff) == 0x7fffffff;
  1407.       goto next;
  1408.  
  1409.  
  1410.     shift8:
  1411.       /* Set flags after an 8 bit shift op, carry set in insn */
  1412.       n = (rd & 0x80);
  1413.       v = 0;
  1414.       nz = rd & 0xff;
  1415.       SET_B_REG (code->src.reg, rd);
  1416.       goto next;
  1417.  
  1418.  
  1419.     shift16:
  1420.       /* Set flags after an 16 bit shift op, carry set in insn */
  1421.       n = (rd & 0x8000);
  1422.       v = 0;
  1423.       nz = rd & 0xffff;
  1424.  
  1425.       SET_W_REG (code->src.reg, rd);
  1426.       goto next;
  1427.  
  1428.     shift32:
  1429.       /* Set flags after an 32 bit shift op, carry set in insn */
  1430.       n = (rd & 0x80000000);
  1431.       v = 0;
  1432.       nz = rd & 0xffffffff;
  1433.       SET_L_REG (code->src.reg, rd);
  1434.       goto next;
  1435.  
  1436.     log32:
  1437.       store (&code->dst, res);
  1438.     just_flags_log32:
  1439.       /* flags after a 32bit logical operation */
  1440.       n = res & 0x80000000;
  1441.       nz = res & 0xffffffff;
  1442.       v = 0;
  1443.       goto next;
  1444.  
  1445.     log16:
  1446.       store (&code->dst, res);
  1447.     just_flags_log16:
  1448.       /* flags after a 16bit logical operation */
  1449.       n = res & 0x8000;
  1450.       nz = res & 0xffff;
  1451.       v = 0;
  1452.       goto next;
  1453.  
  1454.  
  1455.     log8:
  1456.       store (&code->dst, res);
  1457.     just_flags_log8:
  1458.       n = res & 0x80;
  1459.       nz = res & 0xff;
  1460.       v = 0;
  1461.       goto next;
  1462.  
  1463.     alu8:
  1464.       SET_B_REG (code->dst.reg, res);
  1465.     just_flags_alu8:
  1466.       n = res & 0x80;
  1467.       nz = res & 0xff;
  1468.       v = ((ea & 0x80) == (rd & 0x80)) && ((ea & 0x80) != (res & 0x80));
  1469.       c = (res & 0x100);
  1470.       goto next;
  1471.  
  1472.     alu16:
  1473.       SET_W_REG (code->dst.reg, res);
  1474.     just_flags_alu16:
  1475.       n = res & 0x8000;
  1476.       nz = res & 0xffff;
  1477.       v = ((ea & 0x8000) == (rd & 0x8000)) && ((ea & 0x8000) != (res & 0x8000));
  1478.       c = (res & 0x10000);
  1479.       goto next;
  1480.  
  1481.     alu32:
  1482.       SET_L_REG (code->dst.reg, res);
  1483.     just_flags_alu32:
  1484.       n = res & 0x80000000;
  1485.       nz = res & 0xffffffff;
  1486.       v = ((ea & 0x80000000) == (rd & 0x80000000))
  1487.     && ((ea & 0x80000000) != (res & 0x80000000));
  1488.       switch (code->opcode / 4)
  1489.     {
  1490.     case O_ADD:
  1491.       c = ((unsigned) res < (unsigned) rd) || ((unsigned) res < (unsigned) ea);
  1492.       break;
  1493.     case O_SUB:
  1494.     case O_CMP:
  1495.       c = (unsigned) rd < (unsigned) -ea;
  1496.       break;
  1497.     case O_NEG:
  1498.       c = res != 0;
  1499.       break;
  1500.     }
  1501.       goto next;
  1502.  
  1503.     next:;
  1504.       pc = code->next_pc;
  1505.  
  1506.     end:
  1507.       ;
  1508.       /*      if (cpu.regs[8] ) abort(); */
  1509.  
  1510. #ifdef __GO32__
  1511.       /* Poll after every 100th insn, */
  1512.       if (poll_count++ > 100)
  1513.     {
  1514.       poll_count = 0;
  1515.       if (kbhit ())
  1516.         {
  1517.           int c = getkey ();
  1518.           control_c ();
  1519.         }
  1520.     }
  1521. #endif
  1522.  
  1523.     }
  1524.   while (!cpu.exception);
  1525.   cpu.ticks += get_now () - tick_start;
  1526.   cpu.cycles += cycles;
  1527.   cpu.insts += insts;
  1528.  
  1529.   cpu.pc = pc;
  1530.   BUILDSR ();
  1531.  
  1532.   signal (SIGINT, prev);
  1533. }
  1534.  
  1535.  
  1536. int
  1537. sim_write (addr, buffer, size)
  1538.      SIM_ADDR addr;
  1539.      unsigned char *buffer;
  1540.      int size;
  1541. {
  1542.   int i;
  1543.  
  1544.   init_pointers ();
  1545.   if (addr < 0 || addr + size > MSIZE)
  1546.     return 0;
  1547.   for (i = 0; i < size; i++)
  1548.     {
  1549.       cpu.memory[addr + i] = buffer[i];
  1550.       cpu.cache_idx[addr + i] = 0;
  1551.     }
  1552.   return size;
  1553. }
  1554.  
  1555. int
  1556. sim_read (addr, buffer, size)
  1557.      SIM_ADDR addr;
  1558.      unsigned char *buffer;
  1559.      int size;
  1560. {
  1561.   init_pointers ();
  1562.   if (addr < 0 || addr + size > MSIZE)
  1563.     return 0;
  1564.   memcpy (buffer, cpu.memory + addr, size);
  1565.   return size;
  1566. }
  1567.  
  1568.  
  1569. #define R0_REGNUM    0
  1570. #define R1_REGNUM    1
  1571. #define R2_REGNUM    2
  1572. #define R3_REGNUM    3
  1573. #define R4_REGNUM    4
  1574. #define R5_REGNUM    5
  1575. #define R6_REGNUM    6
  1576. #define R7_REGNUM    7
  1577.  
  1578. #define SP_REGNUM       R7_REGNUM    /* Contains address of top of stack */
  1579. #define FP_REGNUM       R6_REGNUM    /* Contains address of executing
  1580.                        * stack frame */
  1581.  
  1582. #define CCR_REGNUM      8    /* Contains processor status */
  1583. #define PC_REGNUM       9    /* Contains program counter */
  1584.  
  1585. #define CYCLE_REGNUM    10
  1586. #define INST_REGNUM     11
  1587. #define TICK_REGNUM     12
  1588.  
  1589.  
  1590. void
  1591. sim_store_register (rn, value)
  1592.      int rn;
  1593.      unsigned char *value;
  1594. {
  1595.   int longval;
  1596.   int shortval;
  1597.   int intval;
  1598.   longval = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
  1599.   shortval = (value[0] << 8) | (value[1]);
  1600.   intval = h8300hmode ? longval : shortval;
  1601.  
  1602.   init_pointers ();
  1603.   switch (rn)
  1604.     {
  1605.     case PC_REGNUM:
  1606.       cpu.pc = intval;
  1607.       break;
  1608.     default:
  1609.       abort ();
  1610.     case R0_REGNUM:
  1611.     case R1_REGNUM:
  1612.     case R2_REGNUM:
  1613.     case R3_REGNUM:
  1614.     case R4_REGNUM:
  1615.     case R5_REGNUM:
  1616.     case R6_REGNUM:
  1617.     case R7_REGNUM:
  1618.       cpu.regs[rn] = intval;
  1619.       break;
  1620.     case CCR_REGNUM:
  1621.       cpu.ccr = intval;
  1622.       break;
  1623.     case CYCLE_REGNUM:
  1624.       cpu.cycles = longval;
  1625.       break;
  1626.  
  1627.     case INST_REGNUM:
  1628.       cpu.insts = longval;
  1629.       break;
  1630.  
  1631.     case TICK_REGNUM:
  1632.       cpu.ticks = longval;
  1633.       break;
  1634.     }
  1635. }
  1636.  
  1637. void
  1638. sim_fetch_register (rn, buf)
  1639.      int rn;
  1640.      unsigned char *buf;
  1641. {
  1642.   int v;
  1643.   int longreg = 0;
  1644.  
  1645.   init_pointers ();
  1646.  
  1647.   switch (rn)
  1648.     {
  1649.     default:
  1650.       abort ();
  1651.     case 8:
  1652.       v = cpu.ccr;
  1653.       break;
  1654.     case 9:
  1655.       v = cpu.pc;
  1656.       break;
  1657.     case R0_REGNUM:
  1658.     case R1_REGNUM:
  1659.     case R2_REGNUM:
  1660.     case R3_REGNUM:
  1661.     case R4_REGNUM:
  1662.     case R5_REGNUM:
  1663.     case R6_REGNUM:
  1664.     case R7_REGNUM:
  1665.       v = cpu.regs[rn];
  1666.       break;
  1667.     case 10:
  1668.       v = cpu.cycles;
  1669.       longreg = 1;
  1670.       break;
  1671.     case 11:
  1672.       v = cpu.ticks;
  1673.       longreg = 1;
  1674.       break;
  1675.     case 12:
  1676.       v = cpu.insts;
  1677.       longreg = 1;
  1678.       break;
  1679.     }
  1680.   if (h8300hmode || longreg)
  1681.     {
  1682.       buf[0] = v >> 24;
  1683.       buf[1] = v >> 16;
  1684.       buf[2] = v >> 8;
  1685.       buf[3] = v >> 0;
  1686.     }
  1687.   else
  1688.     {
  1689.       buf[0] = v >> 8;
  1690.       buf[1] = v;
  1691.     }
  1692. }
  1693.  
  1694. void
  1695. sim_stop_reason (reason, sigrc)
  1696.      enum sim_stop *reason;
  1697.      int *sigrc;
  1698. {
  1699.   *reason = sim_stopped;
  1700.   *sigrc = cpu.exception;
  1701. }
  1702.  
  1703. sim_csize (n)
  1704. {
  1705.   if (cpu.cache)
  1706.     free (cpu.cache);
  1707.   if (n < 2)
  1708.     n = 2;
  1709.   cpu.cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n);
  1710.   memset (cpu.cache, 0, sizeof (decoded_inst) * n);
  1711.   cpu.csize = n;
  1712. }
  1713.  
  1714.  
  1715. void
  1716. sim_info (verbose)
  1717.      int verbose;
  1718. {
  1719.   double timetaken = (double) cpu.ticks / (double) now_persec ();
  1720.   double virttime = cpu.cycles / 10.0e6;
  1721.  
  1722.   printf_filtered ("\n\n#instructions executed  %10d\n", cpu.insts);
  1723.   printf_filtered ("#cycles (v approximate) %10d\n", cpu.cycles);
  1724.   printf_filtered ("#real time taken        %10.4f\n", timetaken);
  1725.   printf_filtered ("#virtual time taked     %10.4f\n", virttime);
  1726.   if (timetaken != 0.0)
  1727.     printf_filtered ("#simulation ratio       %10.4f\n", virttime / timetaken);
  1728.   printf_filtered ("#compiles               %10d\n", cpu.compiles);
  1729.   printf_filtered ("#cache size             %10d\n", cpu.csize);
  1730.  
  1731. #ifdef ADEBUG
  1732.   if (verbose)
  1733.     {
  1734.       int i;
  1735.       for (i = 0; i < O_LAST; i++)
  1736.     {
  1737.       if (cpu.stats[i])
  1738.         printf_filtered ("%d: %d\n", i, cpu.stats[i]);
  1739.     }
  1740.     }
  1741. #endif
  1742. }
  1743.  
  1744. /* Indicate whether the cpu is an h8/300 or h8/300h.
  1745.    FLAG is non-zero for the h8/300h.  */
  1746.  
  1747. void
  1748. set_h8300h (flag)
  1749.      int flag;
  1750. {
  1751.   h8300hmode = flag;
  1752. }
  1753.  
  1754. void
  1755. sim_kill ()
  1756. {
  1757.   /* nothing to do */
  1758. }
  1759.  
  1760. void
  1761. sim_open (args)
  1762.      char *args;
  1763. {
  1764.   /* nothing to do */
  1765. }
  1766.  
  1767. void
  1768. sim_close (quitting)
  1769.      int quitting;
  1770. {
  1771.   /* nothing to do */
  1772. }
  1773.  
  1774. /* Called by gdb to load a program into memory.  */
  1775.  
  1776. int
  1777. sim_load (prog, from_tty)
  1778.      char *prog;
  1779.      int from_tty;
  1780. {
  1781.   bfd *abfd;
  1782.  
  1783.   /* See if the file is for the h8/300 or h8/300h.  */
  1784.   /* ??? This may not be the most efficient way.  The z8k simulator
  1785.      does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO).  */
  1786.   if ((abfd = bfd_openr (prog, "coff-h8300")) != 0)
  1787.     {
  1788.       if (bfd_check_format (abfd, bfd_object)) 
  1789.     set_h8300h (abfd->arch_info->mach == bfd_mach_h8300h);
  1790.       bfd_close (abfd);
  1791.     }
  1792.  
  1793.   /* Return non-zero so gdb will handle it.  */
  1794.   return 1;
  1795. }
  1796.  
  1797. void
  1798. sim_create_inferior (start_address, argv, env)
  1799.      SIM_ADDR start_address;
  1800.      char **argv;
  1801.      char **env;
  1802. {
  1803.   cpu.pc = start_address;
  1804. }
  1805.