home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / sim / h8300 / writecode.c < prev   
Encoding:
C/C++ Source or Header  |  1993-08-17  |  15.9 KB  |  1,035 lines

  1. /* Code generator for the Hitachi H8/300 architecture simulator.
  2.  
  3.    Written by Steve Chamberlain of Cygnus Support.
  4.    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
  12.    makes no warranty with regard to the software or it's performance
  13.    and the user accepts the software "AS IS" with all faults.
  14.  
  15.    CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO
  16.    THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17.    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  
  19. */
  20.  
  21. /* This program reads the H8/300 opcode table and writes out
  22.    a large switch statement to understand the opcodes (with ifs if
  23.    there is more than one opcode per case) and code to do the stuff  */
  24.  
  25. #include "bfd.h"
  26. #include "sysdep.h"
  27.  
  28. #define DEFINE_TABLE
  29. #define INSIM
  30. #include"opcode/h8300.h"
  31.  
  32. #define MAXSAME 140
  33.  
  34. #define PTWO 256
  35. static struct h8_opcode *h8_opcodes_sorted[PTWO][MAXSAME];
  36.  
  37. char *cs = "/*";
  38. char *ce = "*/";
  39.  
  40. /* How to get at nibble n from the instruction */
  41. char *nibs[] =
  42. {
  43.   "foo",
  44.   "(b0&0xf)",
  45.   "((b1>>4)&0xf)",
  46.   "((b1)&0xf)",
  47.   "((pc[1]>>12)&0xf)",
  48.   "((pc[1]>>8)&0xf)",
  49.   "((pc[1]>>4)&0xf)",
  50.   "((pc[1])&0xf)",
  51.   0, 0};
  52.  
  53. /* how to get at the 3 bit immediate in the instruction */
  54. char *imm3[] =
  55. {"foo",
  56.  "foo",
  57.  "((b1>>4)&0x7)",
  58.  "foo",
  59.  "foo",
  60.  "foo",
  61.  "(pc[1]>>4)&0x7"};
  62.  
  63. /* How to get at a byte register from an index in the instruction at
  64.    nibble n */
  65. char *breg[] =
  66. {"foo",
  67.  "*(blow[b0])",
  68.  "*(bhigh[b1])",
  69.  "*(blow[b1])",
  70.  0, 0,
  71.  "*(bhigh[pc[1]>>8])"};
  72.  
  73. /* How to get at a word register from an index in the instruction at
  74.    nibble n */
  75.  
  76. char *wreg[] =
  77. {"foo",
  78.  "*(wlow[b0])",
  79.  "*(whigh[b1])",
  80.  "*(wlow[b1])"};
  81.  
  82. #define sorted_key noperands
  83.  
  84. /* sort the opcode table into h8_opcodes_sorted[0..255] */
  85. static void
  86. init ()
  87. {
  88.   unsigned int i;
  89.   struct h8_opcode *p;
  90.  
  91.   for (p = h8_opcodes; p->name; p++)
  92.     {
  93.       int n1 = 0;
  94.       int n2 = 0;
  95.       int j;
  96. #if 0
  97.       for (j = 0; p->data.nib[j] != E; j++)
  98.     {
  99.       if ((int) p->data.nib[j] == ABS16ORREL8SRC)
  100.         p->data.nib[j] = ABS16SRC;
  101.       if ((int) p->data.nib[j] == ABS16OR8SRC)
  102.         p->data.nib[j] = ABS16SRC;
  103.       if ((int) p->data.nib[j] == ABS16OR8DST)
  104.         p->data.nib[j] = ABS16DST;
  105.     }
  106. #endif
  107.  
  108.       if ((int) p->data.nib[0] < 16)
  109.     {
  110.       n1 = (int) p->data.nib[0];
  111.     }
  112.       else
  113.     n1 = 0;
  114.       if ((int) p->data.nib[1] < 16)
  115.     {
  116.       n2 = (int) p->data.nib[1];
  117.     }
  118.       else
  119.     n2 = 0;
  120.       for (i = 0; i < MAXSAME; i++)
  121.     {
  122.       int j = /* ((n3 >> 3) * 512) + ((n4 >> 3) * 256) + */ n1 * 16 + n2;
  123.  
  124.       if (h8_opcodes_sorted[j][i] == (struct h8_opcode *) NULL)
  125.         {
  126.           h8_opcodes_sorted[j][i] = p;
  127.           p->sorted_key = j;
  128.           break;
  129.         }
  130.     }
  131.  
  132.       if (i == MAXSAME)
  133.     abort ();
  134.  
  135.       /* Just make sure there are an even number of nibbles in it, and
  136.        that the count is the same s the length */
  137.       for (i = 0; p->data.nib[i] != E; i++)
  138.     /*EMPTY*/ ;
  139.       if (i & 1)
  140.     abort ();
  141.       p->length = i / 2;
  142.     }
  143.   for (i = 0; i < PTWO; i++)
  144.     {
  145.       if (h8_opcodes_sorted[i][0])
  146.     p = h8_opcodes_sorted[i][0];
  147.       else
  148.     h8_opcodes_sorted[i][0] = p;
  149.     }
  150. }
  151.  
  152. /* decode the lvalues, creating a pointer in real space to object -
  153.  remember if the thing has to be swapped out of where it is */
  154.  
  155.  
  156. int swap[2];
  157.  
  158. lval (p)
  159.      struct h8_opcode *p;
  160. {
  161.   int i;
  162.  
  163.   for (i = 0; p->data.nib[i] != E; i++)
  164.     {
  165.       int x = p->data.nib[i];
  166.       int size;
  167.       int op;
  168.       op = (x & DST) ? 1 : 0;
  169.  
  170.       switch (x & SIZE)
  171.     {
  172.     case L_32:
  173.       size = 32;
  174.       break;
  175.     case L_16:
  176.       size = 16;
  177.       break;
  178.     case L_8:
  179.       size = 8;
  180.       break;
  181.     default:
  182.       size = 1234;
  183.     }
  184.  
  185.       if (x & REG)
  186.     {
  187.       printf ("ir%d = GET_LVAL_%d_REG(%d);\n", op, size, i);
  188.     }
  189.       else if (x & IMM)
  190.     {
  191.       printf ("/* Imm has no lvalue */\n");
  192.     }
  193.  
  194.     }
  195.  
  196.  
  197.  
  198. }
  199.  
  200. void
  201. decode (p, fetch, size)
  202.      struct h8_opcode *p;
  203.      int fetch;
  204.      int size;
  205. {
  206.   if (fetch)
  207.     {
  208.       lval (p);
  209.     }
  210.  
  211. }
  212.  
  213.  
  214.  
  215. static void
  216. esleep ()
  217. {
  218.   printf ("saved_state.exception = SIGSTOP;\n");
  219. }
  220.  
  221. static void
  222. mov (p, s, sz)
  223.      struct h8_opcode *p;
  224.      char *s;
  225.      int sz;
  226. {
  227.   printf ("dst = srca;\n");
  228. }
  229.  
  230. static void
  231. andc (p)
  232.      struct h8_opcode *p;
  233. {
  234.   printf ("SET_CCR(GET_CCR() & srca);\n");
  235. }
  236.  
  237. static void
  238. addx (p)
  239.      struct h8_opcode *p;
  240. {
  241.   printf ("dst = srca + srcb+ (c != 0);\n");
  242. }
  243.  
  244. static void
  245. subx (p)
  246.      struct h8_opcode *p;
  247. {
  248.   printf ("dst = srcb - srca - (c != 0);\n");
  249. }
  250.  
  251. static void
  252. add (p, s, sz)
  253.      struct h8_opcode *p;
  254.      char *s;
  255.      int sz;
  256. {
  257.   printf ("%s;\n", s);
  258. }
  259.  
  260. static void
  261. adds (p, s)
  262.      struct h8_opcode *p;
  263.      char *s;
  264. {
  265.   printf ("%s;\n", s);
  266. }
  267.  
  268. static void
  269. bra (p, a)
  270.      struct h8_opcode *p;
  271.      char *a;
  272. {
  273.   printf ("if (%s) npc += ((char )b1)>>1;\n", a);
  274. }
  275.  
  276. static void
  277. bsr (p, a)
  278.      struct h8_opcode *p;
  279.      char *a;
  280. {
  281.   printf ("reg[7]-=2;\n");
  282.   printf ("tmp = reg[7];\n");
  283.   printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n");
  284.   printf ("npc += ((char)b1)>>1;\n");
  285. }
  286.  
  287. static void
  288. cmp (p, a, s)
  289.      struct h8_opcode *p;
  290.      char *a;
  291.      int s;
  292. {
  293.   decode (p, 1, s);
  294.   printf ("srca = -srca;\n");
  295.   printf ("dst = srca + srcb;\n");
  296. }
  297.  
  298. static
  299. void
  300. jsr (p, a, s)
  301.      struct h8_opcode *p;
  302.      char *a;
  303.      int s;
  304. {
  305.   printf ("if (b1 == 0xc4) {\n");
  306.   printf ("printf(\"%%c\", reg[2]);\n");
  307.   printf ("}\n");
  308.   printf ("else {\n");
  309.   printf ("reg[7]-=2;\n");
  310.   printf ("tmp = reg[7];\n");
  311.   printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n");
  312.   printf ("npc = (lval>>1) + saved_state.mem;\n");
  313.   printf ("}");
  314. }
  315.  
  316. static void
  317. jmp (p, a, s)
  318.      struct h8_opcode *p;
  319.      char *a;
  320.      int s;
  321. {
  322.   printf ("npc = (lval>>1) + saved_state.mem;\n");
  323. }
  324.  
  325. static void
  326. rts (p, a, s)
  327.      struct h8_opcode *p;
  328.      char *a;
  329.      int s;
  330. {
  331.   printf ("tmp = reg[7];\n");
  332.   printf ("reg[7]+=2;\n");
  333.   printf ("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n");
  334. }
  335.  
  336. static void
  337. rte (p, a, s)
  338.      struct h8_opcode *p;
  339.      char *a;
  340.      int s;
  341. {
  342.   printf ("reg[7]+=2;\n");
  343.   printf ("tmp = reg[7];\n");
  344.   printf ("reg[7]+=2;\n");
  345.   printf ("SET_CCR(tmp);\n");
  346.   printf ("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n");
  347. }
  348.  
  349. static void
  350. setf (p, a, s)
  351.      struct h8_opcode *p;
  352.      char *a;
  353.      int s;
  354. {
  355.   printf ("tmp = GET_CCR();\n");
  356.   printf ("tmp %s= srca;\n", a);
  357. }
  358.  
  359. static void
  360. bpt (p, a, s)
  361.      struct h8_opcode *p;
  362.      char *a;
  363.      int s;
  364. {
  365.   printf ("saved_state.exception = SIGTRAP;\n");
  366.   printf ("npc = pc;\n");
  367. }
  368.  
  369. static void
  370. log (p, a, s)
  371.      struct h8_opcode *p;
  372.      char *a;
  373.      int s;
  374. {
  375.   printf ("dst = srcb %s srca;\n", a);
  376. }
  377.  
  378. static void
  379. ulog (p, a, s)
  380.      struct h8_opcode *p;
  381.      char *a;
  382.      int s;
  383. {
  384.   printf ("dst = %s srcb ;\n", a);
  385. }
  386.  
  387. static void
  388. nop ()
  389. {
  390. }
  391.  
  392. static void
  393. rotl ()
  394. {
  395.   printf ("c = srcb & 0x80;\n");
  396.   printf ("dst = srcb << 1;\n");
  397.   printf ("if (c) dst|=1;\n");
  398. }
  399.  
  400. static void
  401. rotr ()
  402. {
  403.   printf ("c = srcb & 1;\n");
  404.   printf ("dst = srcb >> 1;\n");
  405.   printf ("if (c) dst|=0x80;\n");
  406. }
  407.  
  408. static void
  409. rotxl ()
  410. {
  411.   printf ("tmp = srcb & 0x80;\n");
  412.   printf ("dst = srcb << 1;\n");
  413.   printf ("if (c) dst|=1;\n");
  414.   printf ("c = tmp;\n");
  415. }
  416.  
  417. static void
  418. rotxr ()
  419. {
  420.   printf ("tmp = srcb & 1;\n");
  421.   printf ("dst = srcb >> 1;\n");
  422.   printf ("if (c) dst|=0x80;\n");
  423.   printf ("c = tmp;\n");
  424. }
  425.  
  426. static void
  427. shal ()
  428. {
  429.   printf ("c = srcb&0x80;\n");
  430.   printf ("dst = srcb << 1;\n");
  431. }
  432.  
  433. static
  434. void
  435. shar ()
  436. {
  437.   printf ("c = srcb&0x1;\n");
  438.   printf ("if (srcb&0x80) dst = (srcb>>1) | 0x80;\n");
  439.   printf ("else  dst = (srcb>>1) &~ 0x80;\n");
  440. }
  441.  
  442. static
  443. void
  444. shll ()
  445. {
  446.   printf ("c = srcb&0x80;\n");
  447.   printf ("dst = srcb << 1;\n");
  448. }
  449.  
  450. static
  451. void
  452. shlr ()
  453. {
  454.   printf ("c = srcb&0x1;\n");
  455.   printf ("dst = (srcb>>1) &~ 0x80;\n");
  456. }
  457.  
  458. static
  459. void
  460. divxu ()
  461. {
  462.   printf ("srca = %s;\n", breg[2]);
  463.   printf ("srcb = %s;\n", wreg[3]);
  464.   printf ("n = srca & 0x80;\n");
  465.   printf ("z = !srca;\n");
  466.   printf ("if (srca) dst = srcb / srca;tmp = srcb %% srca;\n");
  467.   printf ("%s = (dst & 0xff) | (tmp << 8);\n", wreg[3]);
  468. }
  469.  
  470. static
  471. void
  472. mulxu ()
  473. {
  474.   printf ("srca = %s;\n", breg[2]);
  475.   printf ("srcb = %s;\n", wreg[3]);
  476.  
  477.   printf ("dst = (srcb&0xff) * srca;\n");
  478.   printf ("%s = dst;\n", wreg[3]);
  479. }
  480.  
  481. static
  482. void
  483. inc ()
  484. {
  485.   printf ("dst = %s;\n", breg[3]);
  486.   printf ("v = (dst==0x7f);\n");
  487.   printf ("dst++;\n");
  488.   printf ("%s= dst;\n", breg[3]);
  489. }
  490.  
  491. static
  492. void
  493. bit (p, a, s)
  494.      struct h8_opcode *p;
  495.      char *a;
  496.      int s;
  497. {
  498.   printf ("%s\n", a);
  499. }
  500.  
  501. static
  502. void
  503. dec ()
  504. {
  505.   printf ("dst = %s;\n", breg[3]);
  506.   printf ("v = (dst==0x80);\n");
  507.   printf ("dst--;\n");
  508.   printf ("%s = dst;\n", breg[3]);
  509. }
  510.  
  511. char saf[] = "goto setflags;";
  512. char sf[] = "goto shiftflags;";
  513. char af8[] = "goto aluflags8;";
  514. char af16[] = "goto aluflags16;";
  515. char lf[] = "goto logflags;";
  516. char icf[] = "goto incflags;";
  517. char mf8[] = "goto movflags8;";
  518. char mf16[] = "goto movflags16;";
  519. char nx[] = "goto next;";
  520.  
  521. struct
  522. {
  523.   char *ftype;
  524.   int decode;
  525.   char *name;
  526.   void (*func) ();
  527.   char *arg;
  528.   int size;
  529.  
  530. }
  531.  
  532. table[] =
  533. {
  534.   {
  535.     nx, 1, "bld", bit, "dst = srcb; c = (srcb>>srca)&1;", 8
  536.   }
  537.   ,
  538.   {
  539.     nx, 1, "bild", bit, "dst = srcb; c = !((srcb>>srca)&1);", 8
  540.   }
  541.   ,
  542.   {
  543.     nx, 1, "band", bit, "dst = srcb; c = C &&((srcb>>srca)&1);", 8
  544.   }
  545.   ,
  546.   {
  547.     nx, 1, "biand", bit, "dst = srcb; c = C &&(!((srcb>>srca)&1));", 8
  548.   }
  549.   ,
  550.   {
  551.     nx, 1, "bior", bit, "dst = srcb; c = C ||(!((srcb>>srca)&1));", 8
  552.   }
  553.   ,
  554.   {
  555.     nx, 1, "bor", bit, "dst = srcb; c = C ||(((srcb>>srca)&1));", 8
  556.   }
  557.   ,
  558.   {
  559.     nx, 1, "bixor", bit, "dst = srcb; c = C ^(!((srcb>>srca)&1));", 8
  560.   }
  561.   ,
  562.   {
  563.     nx, 1, "bxor", bit, "dst = srcb; c = C ^(((srcb>>srca)&1));", 8
  564.   }
  565.   ,
  566.   {
  567.     nx, 1, "bnot", bit, "dst = srcb ^ (1<<srca);", 8
  568.   }
  569.   ,
  570.   {
  571.     nx, 1, "bclr", bit, "dst = srcb & ~(1<<srca);", 8
  572.   }
  573.   ,
  574.   {
  575.     nx, 1, "bset", bit, "dst = srcb | (1<<srca);", 8
  576.   }
  577.   ,
  578.   {
  579.     nx, 1, "bst", bit, "dst = (srcb & ~(1<<srca))| ((C)<<srca);", 8
  580.   }
  581.   ,
  582.   {
  583.     nx, 1, "bist", bit, "dst = (srcb & ~(1<<srca))| ((!C)<<srca);", 8
  584.   }
  585.   ,
  586.   {
  587.     nx, 1, "btst", bit, "dst = srcb; z = !((srcb>>srca)&1);", 8
  588.   }
  589.   ,
  590.   {
  591.     icf, 0, "dec", dec, 0, 0
  592.   }
  593.   ,
  594.   {
  595.     icf, 0, "inc", inc, 0, 0
  596.   }
  597.   ,
  598.   {
  599.     saf, 1, "orc", setf, "|", 0
  600.   }
  601.   ,
  602.   {
  603.     saf, 1, "xorc", setf, "^", 0
  604.   }
  605.   ,
  606.   {
  607.     saf, 1, "andc", setf, "&", 0
  608.   }
  609.   ,
  610.   {
  611.     nx, 1, "nop", nop, 0, 0
  612.   }
  613.   ,
  614.   {
  615.     nx, 1, "bra", bra, "1", 0
  616.   }
  617.   ,
  618.   {
  619.     nx, 1, "brn", bra, "0", 0
  620.   }
  621.   ,
  622.   {
  623.     nx, 1, "bhi", bra, "(C||Z)==0", 0
  624.   }
  625.   ,
  626.   {
  627.     nx, 1, "bls", bra, "(C||Z)==1", 0
  628.   }
  629.   ,
  630.   {
  631.     nx, 1, "bcs", bra, "C==1", 0
  632.   }
  633.   ,
  634.   {
  635.     nx, 1, "bcc", bra, "C==0", 0
  636.   }
  637.   ,
  638.   {
  639.     nx, 1, "bpl", bra, "N==0", 0
  640.   }
  641.   ,
  642.   {
  643.     nx, 1, "bmi", bra, "N==1", 0
  644.   }
  645.   ,
  646.   {
  647.     nx, 1, "bvs", bra, "V==1", 0
  648.   }
  649.   ,
  650.   {
  651.     nx, 1, "bvc", bra, "V==0", 0
  652.   }
  653.   ,
  654.   {
  655.     nx, 1, "bge", bra, "(N^V)==0", 0
  656.   }
  657.   ,
  658.   {
  659.     nx, 1, "bgt", bra, "(Z|(N^V))==0", 0
  660.   }
  661.   ,
  662.   {
  663.     nx, 1, "blt", bra, "(N^V)==1", 0
  664.   }
  665.   ,
  666.   {
  667.     nx, 1, "ble", bra, "(Z|(N^V))==1", 0
  668.   }
  669.   ,
  670.   {
  671.     nx, 1, "beq", bra, "Z==1", 0
  672.   }
  673.   ,
  674.   {
  675.     nx, 1, "bne", bra, "Z==0", 0
  676.   }
  677.   ,
  678.   {
  679.     nx, 1, "bsr", bsr, "", 0
  680.   }
  681.   ,
  682.   {
  683.     nx, 1, "jsr", jsr, 0, 0
  684.   }
  685.   ,
  686.   {
  687.     nx, 1, "jmp", jmp, 0, 0
  688.   }
  689.   ,
  690.   {
  691.     nx, 0, "rts", rts, 0, 0
  692.   }
  693.   ,
  694.   {
  695.     nx, 0, "rte", rte, 0, 0
  696.   }
  697.   ,
  698.   {
  699.     nx, 1, "andc", andc, 0, 0
  700.   }
  701.   ,
  702.   {
  703.     sf, 1, "shal", shal, 0, 0
  704.   }
  705.   ,
  706.   {
  707.     sf, 1, "shar", shar, 0, 0
  708.   }
  709.   ,
  710.   {
  711.     sf, 1, "shll", shll, 0, 0
  712.   }
  713.   ,
  714.   {
  715.     sf, 1, "shlr", shlr, 0, 0
  716.   }
  717.   ,
  718.   {
  719.     sf, 1, "rotxl", rotxl, 0, 0
  720.   }
  721.   ,
  722.   {
  723.     sf, 1, "rotxr", rotxr, 0, 0
  724.   }
  725.   ,
  726.   {
  727.     sf, 1, "rotl", rotl, 0, 0
  728.   }
  729.   ,
  730.   {
  731.     sf, 1, "rotr", rotr, 0, 0
  732.   }
  733.   ,
  734.   {
  735.     lf, 1, "xor", log, "^", 0
  736.   }
  737.   ,
  738.   {
  739.     lf, 1, "and", log, "&", 0
  740.   }
  741.   ,
  742.   {
  743.     lf, 1, "or", log, "|", 0
  744.   }
  745.   ,
  746.   {
  747.     lf, 1, "not", ulog, " ~", 0
  748.   }
  749.   ,
  750.   {
  751.     lf, 1, "neg", ulog, " - ", 0
  752.   }
  753.   ,
  754.   {
  755.     nx, 1, "adds", adds, "dst = srca + srcb", 0
  756.   }
  757.   ,
  758.   {
  759.     nx, 1, "subs", adds, "srca = -srca; dst = srcb + srca", 0
  760.   }
  761.   ,
  762.   {
  763.     af8, 1, "add.b", add, "dst = srca + srcb", 8
  764.   }
  765.   ,
  766.   {
  767.     af16, 1, "add.w", add, "dst = srca + srcb", 16
  768.   }
  769.   ,
  770.   {
  771.     af16, 1, "sub.w", add, "srca = -srca; dst = srcb + srca", 16
  772.   }
  773.   ,
  774.   {
  775.     af8, 1, "sub.b", add, "srca = -srca; dst = srcb + srca", 8
  776.   }
  777.   ,
  778.   {
  779.     af8, 1, "addx", addx, 0, 8
  780.   }
  781.   ,
  782.   {
  783.     af8, 1, "subx", subx, 0, 8
  784.   }
  785.   ,
  786.   {
  787.     af8, 0, "cmp.b", cmp, 0, 8
  788.   }
  789.   ,
  790.   {
  791.     af16, 0, "cmp.w", cmp, 0, 16
  792.   }
  793.   ,
  794.   {
  795.     nx, 1, "sleep", esleep, 0, 0
  796.   }
  797.   ,
  798.   {
  799.     nx, 0, "bpt", bpt, 0, 8
  800.   }
  801.   ,
  802.   {
  803.     nx, 0, "divxu", divxu, 0, 0
  804.   }
  805.   ,
  806.   {
  807.     nx, 0, "mulxu", mulxu, 0, 0
  808.   }
  809.   ,
  810.   {
  811.     mf8, 1, "mov.b", mov, 0, 8
  812.   }
  813.   ,
  814.   {
  815.     mf8, 1, "movtpe", mov, 0, 8
  816.   }
  817.   ,
  818.   {
  819.     mf8, 1, "movfpe", mov, 0, 8
  820.   }
  821.   ,
  822.   {
  823.     mf16, 1, "mov.w", mov, 0, 16
  824.   }
  825.   ,
  826.   {
  827.     0
  828.   }
  829. };
  830.  
  831. static
  832. void
  833. edo (p)
  834.      struct h8_opcode *p;
  835. {
  836.   int i;
  837.  
  838.   printf ("%s %s %s\n", cs, p->name, ce);
  839.  
  840.   for (i = 0; table[i].name; i++)
  841.     {
  842.       if (strcmp (table[i].name, p->name) == 0)
  843.     {
  844.       printf ("{\n");
  845.       if (table[i].decode)
  846.         decode (p, 1, table[i].size);
  847.       printf ("cycles += %d;\n", p->time);
  848.       printf ("npc = pc + %d;\n", p->length / 2);
  849.       table[i].func (p, table[i].arg, table[i].size);
  850.       if (table[i].decode)
  851.         decode (p, 0, table[i].size);
  852.       if (table[i].ftype)
  853.         printf (table[i].ftype);
  854.       else
  855.         printf ("goto next;\n");
  856.       printf ("}\n");
  857.       return;
  858.     }
  859.     }
  860.   printf ("%s not found %s\n", cs, ce);
  861.   printf ("saved_state.exception = SIGILL;\n");
  862.   printf ("break;\n");
  863. }
  864.  
  865. static
  866. int
  867. owrite (i)
  868.      int i;
  869. {
  870.   /* write if statements to select the right opcode */
  871.   struct h8_opcode **p;
  872.   int needand = 1;
  873.  
  874.   p = h8_opcodes_sorted[i];
  875.   printf ("case 0x%03x:\n", i);
  876.  
  877.   if (p[1] == 0)
  878.     {
  879.       /* See if the next few also match */
  880.       while (h8_opcodes_sorted[i + 1][0] == *p)
  881.     {
  882.       i++;
  883.       printf ("case 0x%03x:\n", i);
  884.     }
  885.  
  886.       /* Dont need any if's this is the only one */
  887.       edo (*p);
  888.     }
  889.   else
  890.     {
  891.       while (*p)
  892.     {
  893.       /* start two nibbles in since we know we match in the first byte */
  894.       int c;
  895.       int nib = 2;
  896.       int byte = 1;
  897.       int mask1[5];
  898.       int mask0[5];
  899.       int nibshift = 4;
  900.       int any = 0;
  901.  
  902.       for (c = 0; c < 5; c++)
  903.         {
  904.           mask1[c] = 0;
  905.           mask0[c] = 0;
  906.         }
  907.       printf ("%s %x%x", cs, (*p)->data.nib[0], (*p)->data.nib[1]);
  908.       while ((c = (*p)->data.nib[nib]) != E)
  909.         {
  910.           if (c & B30)
  911.         {
  912.           /* bit 3 must be zero */
  913.           mask0[byte] |= 0x8 << nibshift;
  914.           printf ("0");
  915.           any = 1;
  916.         }
  917.           else if (c & B31)
  918.         {
  919.           /* bit 3 must be one */
  920.           mask1[byte] |= 0x8 << nibshift;
  921.           printf ("8");
  922.           any = 1;
  923.         }
  924.           else if (c <= HexF)
  925.         {
  926.           mask0[byte] |= ((~c) & 0xf) << nibshift;
  927.           mask1[byte] |= (c & 0xf) << nibshift;
  928.           printf ("%x", c);
  929.           any = 1;
  930.         }
  931.           else
  932.         {
  933.           printf ("x");
  934.         }
  935.           nib++;
  936.           if (nibshift == 4)
  937.         {
  938.           nibshift = 0;
  939.         }
  940.           else
  941.         {
  942.           byte++;
  943.           nibshift = 4;
  944.         }
  945.         }
  946.       printf ("*/\n");
  947.       if (any)
  948.         {
  949.           printf ("if (");
  950.           needand = 0;
  951.           for (c = 1; c < byte; c++)
  952.         {
  953.           if (mask0[c] | mask1[c])
  954.             {
  955.               int sh;
  956.  
  957.               if (needand)
  958.             printf ("\n&&");
  959.               if (c & 1)
  960.             sh = 0;
  961.               else
  962.             sh = 8;
  963.               if (c / 2 == 0 && sh == 0)
  964.             printf ("((b1&0x%x)==0x%x)", mask0[c] | mask1[c],
  965.                 mask1[c]);
  966.               else
  967.             {
  968.               printf ("((pc[%d]&(0x%02x<<%d))==(0x%x<<%d))",
  969.                   c / 2, mask0[c] | mask1[c], sh,
  970.                   mask1[c], sh);
  971.             }
  972.  
  973.               needand = 1;
  974.             }
  975.         }
  976.           printf (")\n");
  977.         }
  978.       edo (*p);
  979.       p++;
  980.  
  981.     }
  982.     }
  983.   return i;
  984. }
  985.  
  986. static
  987. void
  988. remove_dups ()
  989. {
  990.   struct h8_opcode *s;
  991.   struct h8_opcode *d;
  992.  
  993.   for (d = s = h8_opcodes; s->name; s++)
  994.     {
  995.       int doit = 1;
  996.  
  997.       if (strcmp (s->name, "push") == 0)
  998.     doit = 0;
  999.       if (strcmp (s->name, "bhs") == 0)
  1000.     doit = 0;
  1001.       if (strcmp (s->name, "blo") == 0)
  1002.     doit = 0;
  1003.       if (strcmp (s->name, "bt") == 0)
  1004.     doit = 0;
  1005.       if (strcmp (s->name, "bf") == 0)
  1006.     doit = 0;
  1007.       if (strcmp (s->name, "pop") == 0)
  1008.     doit = 0;
  1009.       if (doit)
  1010.     {
  1011.       *d++ = *s;
  1012.     }
  1013.     }
  1014.   *d++ = *s++;
  1015. }
  1016.  
  1017. int
  1018. main ()
  1019. {
  1020.   int i;
  1021.  
  1022.   remove_dups ();
  1023.   init ();
  1024.  
  1025.   printf ("%s do the operation %s\n", cs, ce);
  1026.   printf ("switch (b0) \n{\n");
  1027.   for (i = 0; i < PTWO; i++)
  1028.     {
  1029.       i = owrite (i);
  1030.     }
  1031.   printf ("}\n");
  1032.  
  1033.   return 0;
  1034. }
  1035.