]> Git Repo - binutils.git/blob - sim/h8300/writecode.c
* compile.c (sim_resume): Fix overflow checks for ALU insns.
[binutils.git] / sim / h8300 / writecode.c
1 /* code generator for the Hitachi H8/300 architecture simulator.
2    Copyright (C) 1993 Free Software Foundation, Inc.
3    Hacked by Steve Chamberlain of Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
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 14
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
71 /* How to get at a word register from an index in the instruction at
72    nibble n */
73
74 char *wreg[] =
75 {"foo",
76  "*(wlow[b0])",
77  "*(whigh[b1])",
78  "*(wlow[b1])"};
79
80 #define sorted_key noperands
81
82 /* sort the opcode table into h8_opcodes_sorted[0..255] */
83 static void
84 init ()
85 {
86   unsigned int i;
87   struct h8_opcode *p;
88
89   for (p = h8_opcodes; p->name; p++)
90     {
91       int n1 = 0;
92       int n2 = 0;
93       int j;
94
95       for (j = 0; p->data.nib[j] != E; j++)
96         {
97           if ((int) p->data.nib[j] == ABS16ORREL8SRC)
98             p->data.nib[j] = ABS16SRC;
99           if ((int) p->data.nib[j] == ABS16OR8SRC)
100             p->data.nib[j] = ABS16SRC;
101           if ((int) p->data.nib[j] == ABS16OR8DST)
102             p->data.nib[j] = ABS16DST;
103         }
104       if ((int) p->data.nib[0] < 16)
105         {
106           n1 = (int) p->data.nib[0];
107         }
108       else
109         n1 = 0;
110       if ((int) p->data.nib[1] < 16)
111         {
112           n2 = (int) p->data.nib[1];
113         }
114       else
115         n2 = 0;
116       for (i = 0; i < MAXSAME; i++)
117         {
118           int j = /* ((n3 >> 3) * 512) + ((n4 >> 3) * 256) + */ n1 * 16 + n2;
119
120           if (h8_opcodes_sorted[j][i] == (struct h8_opcode *) NULL)
121             {
122               h8_opcodes_sorted[j][i] = p;
123               p->sorted_key = j;
124               break;
125             }
126         }
127
128       if (i == MAXSAME)
129         abort ();
130
131       /* Just make sure there are an even number of nibbles in it, and
132        that the count is the same s the length */
133       for (i = 0; p->data.nib[i] != E; i++)
134         /*EMPTY*/ ;
135       if (i & 1)
136         abort ();
137       p->length = i / 2;
138     }
139   for (i = 0; i < PTWO; i++)
140     {
141       if (h8_opcodes_sorted[i][0])
142         p = h8_opcodes_sorted[i][0];
143       else
144         h8_opcodes_sorted[i][0] = p;
145     }
146 }
147
148 /* either fetch srca&srcb or store dst */
149
150 void
151 decode (p, fetch, size)
152      struct h8_opcode *p;
153      int fetch;
154      int size;
155 {
156   int i;
157   char *ss = size == 8 ? "BYTE" : "WORD";
158
159   for (i = 0; p->data.nib[i] != E; i++)
160     {
161       switch (p->data.nib[i])
162         {
163         case RS8:
164           if (fetch)
165             printf ("srca = %s;\n", breg[i]);
166           break;
167         case RS16 | B30:
168         case RS16 | B31:
169         case RS16:
170           if (fetch)
171             printf ("srca = %s;\n", wreg[i]);
172           break;
173         case RD8:
174           if (fetch)
175             printf ("srcb = %s;\n", breg[i]);
176           else
177             printf ("%s = dst;\n", breg[i]);
178           break;
179         case RD16 | B30:
180         case RD16 | B31:
181         case RD16:
182           if (fetch)
183             printf ("srcb = %s;\n", wreg[i]);
184           else
185             printf ("%s =dst;\n", wreg[i]);
186           break;
187         case IMM8:
188           if (fetch)
189             printf ("srca = b1;\n");
190           break;
191         case RSINC:
192         case RSINC | B30:
193         case RSINC | B31:
194
195           if (fetch)
196             {
197               printf ("srca = %s_MEM(%s);\n", ss, wreg[i]);
198               printf ("%s+=%d;\n", wreg[i], size / 8);
199             }
200           break;
201         case RSIND:
202         case RSIND | B30:
203         case RSIND | B31:
204           if (fetch)
205             {
206               printf ("lval = %s;\n", wreg[i]);
207               printf ("srca = %s_MEM(lval);\n", ss);
208             }
209           break;
210
211         case RDIND:
212         case RDIND | B30:
213         case RDIND | B31:
214           if (fetch)
215             {
216               printf ("lval = %s;\n", wreg[i]);
217               printf ("srcb = %s_MEM(lval);\n", ss);
218             }
219           else
220             {
221               printf ("SET_%s_MEM(lval,dst);\n", ss);
222             }
223           break;
224
225         case MEMIND:
226           if (fetch)
227             {
228               printf ("lval = pc[1];\n");
229             }
230           break;
231         case RDDEC:
232         case RDDEC | B30:
233         case RDDEC | B31:
234           if (!fetch)
235             {
236               printf ("%s -=%d;\n", wreg[i], size / 8);
237               printf ("SET_%s_MEM(%s, dst);\n", ss, wreg[i]);
238             }
239           break;
240         case IMM3:
241         case IMM3 | B31:
242         case IMM3 | B30:
243
244           if (fetch)
245             printf ("srca = %s;\n", imm3[i]);
246           break;
247         case IMM16:
248           if (fetch)
249             printf ("srca =( pc[1]);\n");
250           break;
251         case ABS8SRC:
252           if (fetch)
253             {
254
255               printf ("lval = (0xff00) + b1;\n");
256               printf ("srca = BYTE_MEM(lval);\n");
257             }
258
259           break;
260         case ABS8DST:
261           if (fetch)
262             {
263               printf ("lval = (0xff00) + b1;\n");
264               printf ("srcb = BYTE_MEM(lval);\n");
265             }
266           else
267             {
268               printf ("SET_BYTE_MEM(lval,dst);\n");
269             }
270           break;
271         case KBIT:
272           if (fetch)
273             printf ("srca = ((b1&0x80)?2:1);\n");
274           break;
275         case ABS16ORREL8SRC:
276         case ABS16SRC:
277           if (fetch)
278             {
279               printf ("lval = pc[1];\n");
280               printf ("srca = %s_MEM(lval);\n", size == 8 ? "BYTE" : "WORD");
281             }
282           break;
283         case DISPREG | B30:
284         case DISPREG | B31:
285         case DISPREG:
286           printf ("rn = %s & 0x7;\n", nibs[i]);
287           break;
288         case DISPSRC:
289           if (fetch)
290             {
291               printf ("lval = 0xffff&(pc[1] +reg[rn]);\n");
292               printf ("srca = %s_MEM(lval);\n", size == 8 ? "BYTE" : "WORD");
293             }
294           break;
295         case DISPDST:
296           if (fetch)
297             {
298               printf ("lval = 0xffff&(pc[1] +reg[rn]);\n");
299               printf ("srcb = %s_MEM(lval);\n", size == 8 ? "BYTE" : "WORD");
300             }
301           else
302             {
303               printf ("SET_%s_MEM(lval,dst);\n", ss);
304             }
305           break;
306         case ABS16DST:
307           if (fetch)
308             {
309               printf ("lval = (pc[1]);\n");
310               printf ("srcb = %s_MEM(lval);\n", ss);
311             }
312           else
313             {
314               printf ("SET_%s_MEM(lval,dst);\n", ss);
315             }
316           break;
317         case IGNORE:
318           break;
319         case DISP8:
320           printf ("                     /* DISP8 handled in opcode */\n");
321           break;
322         default:
323           if (p->data.nib[i] > HexF)
324             {
325               printf ("saved_state.exception = SIGILL;\n");
326             }
327         }
328     }
329 }
330
331 static void
332 esleep ()
333 {
334   printf ("saved_state.exception = SIGSTOP;\n");
335 }
336
337 static void
338 mov (p, s, sz)
339      struct h8_opcode *p;
340      char *s;
341      int sz;
342 {
343   printf ("dst = srca;\n");
344 }
345
346 static void
347 andc (p)
348      struct h8_opcode *p;
349 {
350   printf ("SET_CCR(GET_CCR() & srca);\n");
351 }
352
353 static void
354 addx (p)
355      struct h8_opcode *p;
356 {
357   printf ("dst = srca + srcb+ (c != 0);\n");
358 }
359
360 static void
361 subx (p)
362      struct h8_opcode *p;
363 {
364   printf ("dst = srcb - srca - (c != 0);\n");
365 }
366
367 static void
368 add (p, s, sz)
369      struct h8_opcode *p;
370      char *s;
371      int sz;
372 {
373   printf ("%s;\n", s);
374 }
375
376 static void
377 adds (p, s)
378      struct h8_opcode *p;
379      char *s;
380 {
381   printf ("%s;\n", s);
382 }
383
384 static void
385 bra (p, a)
386      struct h8_opcode *p;
387      char *a;
388 {
389   printf ("if (%s) npc += ((char )b1)>>1;\n", a);
390 }
391
392 static void
393 bsr (p, a)
394      struct h8_opcode *p;
395      char *a;
396 {
397   printf ("reg[7]-=2;\n");
398   printf ("tmp = reg[7];\n");
399   printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n");
400   printf ("npc += ((char)b1)>>1;\n");
401 }
402
403 static void
404 cmp (p, a, s)
405      struct h8_opcode *p;
406      char *a;
407      int s;
408 {
409   decode (p, 1, s);
410   printf ("srca = -srca;\n");
411   printf ("dst = srca + srcb;\n");
412 }
413
414 static
415 void
416 jsr (p, a, s)
417      struct h8_opcode *p;
418      char *a;
419      int s;
420 {
421   printf ("if (b1 == 0xc4) {\n");
422   printf ("printf(\"%%c\", reg[2]);\n");
423   printf ("}\n");
424   printf ("else {\n");
425   printf ("reg[7]-=2;\n");
426   printf ("tmp = reg[7];\n");
427   printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n");
428   printf ("npc = (lval>>1) + saved_state.mem;\n");
429   printf ("}");
430 }
431
432 static void
433 jmp (p, a, s)
434      struct h8_opcode *p;
435      char *a;
436      int s;
437 {
438   printf ("npc = (lval>>1) + saved_state.mem;\n");
439 }
440
441 static void
442 rts (p, a, s)
443      struct h8_opcode *p;
444      char *a;
445      int s;
446 {
447   printf ("tmp = reg[7];\n");
448   printf ("reg[7]+=2;\n");
449   printf ("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n");
450 }
451
452 static void
453 rte (p, a, s)
454      struct h8_opcode *p;
455      char *a;
456      int s;
457 {
458   printf ("reg[7]+=2;\n");
459   printf ("tmp = reg[7];\n");
460   printf ("reg[7]+=2;\n");
461   printf ("SET_CCR(tmp);\n");
462   printf ("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n");
463 }
464
465 static void
466 setf (p, a, s)
467      struct h8_opcode *p;
468      char *a;
469      int s;
470 {
471   printf ("tmp = GET_CCR();\n");
472   printf ("tmp %s= srca;\n", a);
473 }
474
475 static void
476 bpt (p, a, s)
477      struct h8_opcode *p;
478      char *a;
479      int s;
480 {
481   printf ("saved_state.exception = SIGTRAP;\n");
482   printf ("npc = pc;\n");
483 }
484
485 static void
486 log (p, a, s)
487      struct h8_opcode *p;
488      char *a;
489      int s;
490 {
491   printf ("dst = srcb %s srca;\n", a);
492 }
493
494 static void
495 ulog (p, a, s)
496      struct h8_opcode *p;
497      char *a;
498      int s;
499 {
500   printf ("dst = %s srcb ;\n", a);
501 }
502
503 static void
504 nop ()
505 {
506 }
507
508 static void
509 rotl ()
510 {
511   printf ("c = srcb & 0x80;\n");
512   printf ("dst = srcb << 1;\n");
513   printf ("if (c) dst|=1;\n");
514 }
515
516 static void
517 rotr ()
518 {
519   printf ("c = srcb & 1;\n");
520   printf ("dst = srcb >> 1;\n");
521   printf ("if (c) dst|=0x80;\n");
522 }
523
524 static void
525 rotxl ()
526 {
527   printf ("tmp = srcb & 0x80;\n");
528   printf ("dst = srcb << 1;\n");
529   printf ("if (c) dst|=1;\n");
530   printf ("c = tmp;\n");
531 }
532
533 static void
534 rotxr ()
535 {
536   printf ("tmp = srcb & 1;\n");
537   printf ("dst = srcb >> 1;\n");
538   printf ("if (c) dst|=0x80;\n");
539   printf ("c = tmp;\n");
540 }
541
542 static void
543 shal ()
544 {
545   printf ("c = srcb&0x80;\n");
546   printf ("dst = srcb << 1;\n");
547 }
548
549 static
550 void
551 shar ()
552 {
553   printf ("c = srcb&0x1;\n");
554   printf ("if (srcb&0x80) dst = (srcb>>1) | 0x80;\n");
555   printf ("else  dst = (srcb>>1) &~ 0x80;\n");
556 }
557
558 static
559 void
560 shll ()
561 {
562   printf ("c = srcb&0x80;\n");
563   printf ("dst = srcb << 1;\n");
564 }
565
566 static
567 void
568 shlr ()
569 {
570   printf ("c = srcb&0x1;\n");
571   printf ("dst = (srcb>>1) &~ 0x80;\n");
572 }
573
574 static
575 void
576 divxu ()
577 {
578   printf ("srca = %s;\n", breg[2]);
579   printf ("srcb = %s;\n", wreg[3]);
580   printf ("n = srca & 0x80;\n");
581   printf ("z = !srca;\n");
582   printf ("if (srca) dst = srcb / srca;tmp = srcb %% srca;\n");
583   printf ("%s = (dst & 0xff) | (tmp << 8);\n", wreg[3]);
584 }
585
586 static
587 void
588 mulxu ()
589 {
590   printf ("srca = %s;\n", breg[2]);
591   printf ("srcb = %s;\n", wreg[3]);
592
593   printf ("dst = (srcb&0xff) * srca;\n");
594   printf ("%s = dst;\n", wreg[3]);
595 }
596
597 static
598 void
599 inc ()
600 {
601   printf ("dst = %s;\n", breg[3]);
602   printf ("v = (dst==0x7f);\n");
603   printf ("dst++;\n");
604   printf ("%s= dst;\n", breg[3]);
605 }
606
607 static
608 void
609 bit (p, a, s)
610      struct h8_opcode *p;
611      char *a;
612      int s;
613 {
614   printf ("%s\n", a);
615 }
616
617 static
618 void
619 dec ()
620 {
621   printf ("dst = %s;\n", breg[3]);
622   printf ("v = (dst==0x80);\n");
623   printf ("dst--;\n");
624   printf ("%s = dst;\n", breg[3]);
625 }
626
627 char saf[] = "goto setflags;";
628 char sf[] = "goto shiftflags;";
629 char af8[] = "goto aluflags8;";
630 char af16[] = "goto aluflags16;";
631 char lf[] = "goto logflags;";
632 char icf[] = "goto incflags;";
633 char mf8[] = "goto movflags8;";
634 char mf16[] = "goto movflags16;";
635 char nx[] = "goto next;";
636
637 struct
638 {
639   char *ftype;
640   int decode;
641   char *name;
642   void (*func) ();
643   char *arg;
644   int size;
645
646 }
647
648 table  [] =
649 {
650   {
651     nx, 1, "bld", bit, "dst = srcb; c = (srcb>>srca)&1;", 8
652   }
653   ,
654   {
655     nx, 1, "bild", bit, "dst = srcb; c = !((srcb>>srca)&1);", 8
656   }
657   ,
658   {
659     nx, 1, "band", bit, "dst = srcb; c = C &&((srcb>>srca)&1);", 8
660   }
661   ,
662   {
663     nx, 1, "biand", bit, "dst = srcb; c = C &&(!((srcb>>srca)&1));", 8
664   }
665   ,
666   {
667     nx, 1, "bior", bit, "dst = srcb; c = C ||(!((srcb>>srca)&1));", 8
668   }
669   ,
670   {
671     nx, 1, "bor", bit, "dst = srcb; c = C ||(((srcb>>srca)&1));", 8
672   }
673   ,
674   {
675     nx, 1, "bixor", bit, "dst = srcb; c = C ^(!((srcb>>srca)&1));", 8
676   }
677   ,
678   {
679     nx, 1, "bxor", bit, "dst = srcb; c = C ^(((srcb>>srca)&1));", 8
680   }
681   ,
682   {
683     nx, 1, "bnot", bit, "dst = srcb ^ (1<<srca);", 8
684   }
685   ,
686   {
687     nx, 1, "bclr", bit, "dst = srcb & ~(1<<srca);", 8
688   }
689   ,
690   {
691     nx, 1, "bset", bit, "dst = srcb | (1<<srca);", 8
692   }
693   ,
694   {
695     nx, 1, "bst", bit, "dst = (srcb & ~(1<<srca))| ((C)<<srca);", 8
696   }
697   ,
698   {
699     nx, 1, "bist", bit, "dst = (srcb & ~(1<<srca))| ((!C)<<srca);", 8
700   }
701   ,
702   {
703     nx, 1, "btst", bit, "dst = srcb; z = !((srcb>>srca)&1);", 8
704   }
705   ,
706   {
707     icf, 0, "dec", dec, 0, 0
708   }
709   ,
710   {
711     icf, 0, "inc", inc, 0, 0
712   }
713   ,
714   {
715     saf, 1, "orc", setf, "|", 0
716   }
717   ,
718   {
719     saf, 1, "xorc", setf, "^", 0
720   }
721   ,
722   {
723     saf, 1, "andc", setf, "&", 0
724   }
725   ,
726   {
727     nx, 1, "nop", nop, 0, 0
728   }
729   ,
730   {
731     nx, 1, "bra", bra, "1", 0
732   }
733   ,
734   {
735     nx, 1, "brn", bra, "0", 0
736   }
737   ,
738   {
739     nx, 1, "bhi", bra, "(C||Z)==0", 0
740   }
741   ,
742   {
743     nx, 1, "bls", bra, "(C||Z)==1", 0
744   }
745   ,
746   {
747     nx, 1, "bcs", bra, "C==1", 0
748   }
749   ,
750   {
751     nx, 1, "bcc", bra, "C==0", 0
752   }
753   ,
754   {
755     nx, 1, "bpl", bra, "N==0", 0
756   }
757   ,
758   {
759     nx, 1, "bmi", bra, "N==1", 0
760   }
761   ,
762   {
763     nx, 1, "bvs", bra, "V==1", 0
764   }
765   ,
766   {
767     nx, 1, "bvc", bra, "V==0", 0
768   }
769   ,
770   {
771     nx, 1, "bge", bra, "(N^V)==0", 0
772   }
773   ,
774   {
775     nx, 1, "bgt", bra, "(Z|(N^V))==0", 0
776   }
777   ,
778   {
779     nx, 1, "blt", bra, "(N^V)==1", 0
780   }
781   ,
782   {
783     nx, 1, "ble", bra, "(Z|(N^V))==1", 0
784   }
785   ,
786   {
787     nx, 1, "beq", bra, "Z==1", 0
788   }
789   ,
790   {
791     nx, 1, "bne", bra, "Z==0", 0
792   }
793   ,
794   {
795     nx, 1, "bsr", bsr, "", 0
796   }
797   ,
798   {
799     nx, 1, "jsr", jsr, 0, 0
800   }
801   ,
802   {
803     nx, 1, "jmp", jmp, 0, 0
804   }
805   ,
806   {
807     nx, 0, "rts", rts, 0, 0
808   }
809   ,
810   {
811     nx, 0, "rte", rte, 0, 0
812   }
813   ,
814   {
815     nx, 1, "andc", andc, 0, 0
816   }
817   ,
818   {
819     sf, 1, "shal", shal, 0, 0
820   }
821   ,
822   {
823     sf, 1, "shar", shar, 0, 0
824   }
825   ,
826   {
827     sf, 1, "shll", shll, 0, 0
828   }
829   ,
830   {
831     sf, 1, "shlr", shlr, 0, 0
832   }
833   ,
834   {
835     sf, 1, "rotxl", rotxl, 0, 0
836   }
837   ,
838   {
839     sf, 1, "rotxr", rotxr, 0, 0
840   }
841   ,
842   {
843     sf, 1, "rotl", rotl, 0, 0
844   }
845   ,
846   {
847     sf, 1, "rotr", rotr, 0, 0
848   }
849   ,
850   {
851     lf, 1, "xor", log, "^", 0
852   }
853   ,
854   {
855     lf, 1, "and", log, "&", 0
856   }
857   ,
858   {
859     lf, 1, "or", log, "|", 0
860   }
861   ,
862   {
863     lf, 1, "not", ulog, " ~", 0
864   }
865   ,
866   {
867     lf, 1, "neg", ulog, " - ", 0
868   }
869   ,
870   {
871     nx, 1, "adds", adds, "dst = srca + srcb", 0
872   }
873   ,
874   {
875     nx, 1, "subs", adds, "srca = -srca; dst = srcb + srca", 0
876   }
877   ,
878   {
879     af8, 1, "add.b", add, "dst = srca + srcb", 8
880   }
881   ,
882   {
883     af16, 1, "add.w", add, "dst = srca + srcb", 16
884   }
885   ,
886   {
887     af16, 1, "sub.w", add, "srca = -srca; dst = srcb + srca", 16
888   }
889   ,
890   {
891     af8, 1, "sub.b", add, "srca = -srca; dst = srcb + srca", 8
892   }
893   ,
894   {
895     af8, 1, "addx", addx, 0, 8
896   }
897   ,
898   {
899     af8, 1, "subx", subx, 0, 8
900   }
901   ,
902   {
903     af8, 0, "cmp.b", cmp, 0, 8
904   }
905   ,
906   {
907     af16, 0, "cmp.w", cmp, 0, 16
908   }
909   ,
910   {
911     nx, 1, "sleep", esleep, 0, 0
912   }
913   ,
914   {
915     nx, 0, "bpt", bpt, 0, 8
916   }
917   ,
918   {
919     nx, 0, "divxu", divxu, 0, 0
920   }
921   ,
922   {
923     nx, 0, "mulxu", mulxu, 0, 0
924   }
925   ,
926   {
927     mf8, 1, "mov.b", mov, 0, 8
928   }
929   ,
930   {
931     mf8, 1, "movtpe", mov, 0, 8
932   }
933   ,
934   {
935     mf8, 1, "movfpe", mov, 0, 8
936   }
937   ,
938   {
939     mf16, 1, "mov.w", mov, 0, 16
940   }
941   ,
942   {
943     0
944   }
945 };
946
947 static
948 void
949 edo (p)
950      struct h8_opcode *p;
951 {
952   int i;
953
954   printf ("%s %s %s\n", cs, p->name, ce);
955
956   for (i = 0; table[i].name; i++)
957     {
958       if (strcmp (table[i].name, p->name) == 0)
959         {
960           printf ("{\n");
961           if (table[i].decode)
962             decode (p, 1, table[i].size);
963           printf ("cycles += %d;\n", p->time);
964           printf ("npc = pc + %d;\n", p->length / 2);
965           table[i].func (p, table[i].arg, table[i].size);
966           if (table[i].decode)
967             decode (p, 0, table[i].size);
968           if (table[i].ftype)
969             printf (table[i].ftype);
970           else
971             printf ("goto next;\n");
972           printf ("}\n");
973           return;
974         }
975     }
976   printf ("%s not found %s\n", cs, ce);
977   printf ("saved_state.exception = SIGILL;\n");
978   printf ("break;\n");
979 }
980
981 static
982 int
983 owrite (i)
984      int i;
985 {
986   /* write if statements to select the right opcode */
987   struct h8_opcode **p;
988   int needand = 1;
989
990   p = h8_opcodes_sorted[i];
991   printf ("case 0x%03x:\n", i);
992
993   if (p[1] == 0)
994     {
995       /* See if the next few also match */
996       while (h8_opcodes_sorted[i + 1][0] == *p)
997         {
998           i++;
999           printf ("case 0x%03x:\n", i);
1000         }
1001
1002       /* Dont need any if's this is the only one */
1003       edo (*p);
1004     }
1005   else
1006     {
1007       while (*p)
1008         {
1009           /* start two nibbles in since we know we match in the first byte */
1010           int c;
1011           int nib = 2;
1012           int byte = 1;
1013           int mask1[5];
1014           int mask0[5];
1015           int nibshift = 4;
1016           int any = 0;
1017
1018           for (c = 0; c < 5; c++)
1019             {
1020               mask1[c] = 0;
1021               mask0[c] = 0;
1022             }
1023           printf ("%s %x%x", cs, (*p)->data.nib[0], (*p)->data.nib[1]);
1024           while ((c = (*p)->data.nib[nib]) != E)
1025             {
1026               if (c & B30)
1027                 {
1028                   /* bit 3 must be zero */
1029                   mask0[byte] |= 0x8 << nibshift;
1030                   printf ("0");
1031                   any = 1;
1032                 }
1033               else if (c & B31)
1034                 {
1035                   /* bit 3 must be one */
1036                   mask1[byte] |= 0x8 << nibshift;
1037                   printf ("8");
1038                   any = 1;
1039                 }
1040               else if (c <= HexF)
1041                 {
1042                   mask0[byte] |= ((~c) & 0xf) << nibshift;
1043                   mask1[byte] |= (c & 0xf) << nibshift;
1044                   printf ("%x", c);
1045                   any = 1;
1046                 }
1047               else
1048                 {
1049                   printf ("x");
1050                 }
1051               nib++;
1052               if (nibshift == 4)
1053                 {
1054                   nibshift = 0;
1055                 }
1056               else
1057                 {
1058                   byte++;
1059                   nibshift = 4;
1060                 }
1061             }
1062           printf ("*/\n");
1063           if (any)
1064             {
1065               printf ("if (");
1066               needand = 0;
1067               for (c = 1; c < byte; c++)
1068                 {
1069                   if (mask0[c] | mask1[c])
1070                     {
1071                       int sh;
1072
1073                       if (needand)
1074                         printf ("\n&&");
1075                       if (c & 1)
1076                         sh = 0;
1077                       else
1078                         sh = 8;
1079                       if (c / 2 == 0 && sh == 0)
1080                         printf ("((b1&0x%x)==0x%x)", mask0[c] | mask1[c],
1081                                 mask1[c]);
1082                       else
1083                         {
1084                           printf ("((pc[%d]&(0x%02x<<%d))==(0x%x<<%d))",
1085                                   c / 2, mask0[c] | mask1[c], sh,
1086                                   mask1[c], sh);
1087                         }
1088
1089                       needand = 1;
1090                     }
1091                 }
1092               printf (")\n");
1093             }
1094           edo (*p);
1095           p++;
1096
1097         }
1098     }
1099   return i;
1100 }
1101
1102 static
1103 void
1104 remove_dups ()
1105 {
1106   struct h8_opcode *s;
1107   struct h8_opcode *d;
1108
1109   for (d = s = h8_opcodes; s->name; s++)
1110     {
1111       int doit = 1;
1112
1113       if (strcmp (s->name, "push") == 0)
1114         doit = 0;
1115       if (strcmp (s->name, "bhs") == 0)
1116         doit = 0;
1117       if (strcmp (s->name, "blo") == 0)
1118         doit = 0;
1119       if (strcmp (s->name, "bt") == 0)
1120         doit = 0;
1121       if (strcmp (s->name, "bf") == 0)
1122         doit = 0;
1123       if (strcmp (s->name, "pop") == 0)
1124         doit = 0;
1125       if (doit)
1126         {
1127           *d++ = *s;
1128         }
1129     }
1130   *d++ = *s++;
1131 }
1132
1133 int
1134 main ()
1135 {
1136   int i;
1137
1138   remove_dups ();
1139   init ();
1140
1141   printf ("%s do the operation %s\n", cs, ce);
1142   printf ("switch (b0) \n{\n");
1143   for (i = 0; i < PTWO; i++)
1144     {
1145       i = owrite (i);
1146     }
1147   printf ("}\n");
1148
1149   return 0;
1150 }
This page took 0.087707 seconds and 4 git commands to generate.