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