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