]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/h8300/writecode.c
fix endian problem
[thirdparty/binutils-gdb.git] / sim / h8300 / writecode.c
1 /* code generator for the Hitachi H8/300 architecture simulator.
2 Copyright (C) 1990-1991 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
332 static void
333 esleep ()
334 {
335 printf ("saved_state.exception = SIGSTOP;\n");
336 }
337
338 static void
339 mov (p, s, sz)
340 struct h8_opcode *p;
341 char *s;
342 int sz;
343 {
344 printf ("dst = srca;\n");
345 }
346
347 static void
348 andc (p)
349 struct h8_opcode *p;
350 {
351 printf ("SET_CCR(GET_CCR() & srca);\n");
352 }
353
354 static void
355 addx (p)
356 struct h8_opcode *p;
357 {
358 printf ("dst = srca + srcb+ (c != 0);\n");
359 }
360
361 static void
362 subx (p)
363 struct h8_opcode *p;
364 {
365 printf ("dst = srcb - srca - (c != 0);\n");
366 }
367
368 static void
369 add (p, s, sz)
370 struct h8_opcode *p;
371 char *s;
372 int sz;
373 {
374 printf ("%s;\n", s);
375 }
376
377 static void
378 adds (p, s)
379 struct h8_opcode *p;
380 char *s;
381 {
382 printf ("%s;\n", s);
383 }
384
385 static void
386 bra (p, a)
387 struct h8_opcode *p;
388 char *a;
389 {
390 printf ("if (%s) npc += ((char )b1)>>1;\n", a);
391 }
392
393 static void
394 bsr (p, a)
395 struct h8_opcode *p;
396 char *a;
397 {
398 printf ("reg[7]-=2;\n");
399 printf ("tmp = reg[7];\n");
400 printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n");
401 printf ("npc += ((char)b1)>>1;\n");
402 }
403
404 static void
405 cmp (p, a, s)
406 struct h8_opcode *p;
407 char *a;
408 int s;
409 {
410 decode (p, 1, s);
411 printf ("srca = -srca;\n");
412 printf ("dst = srca + srcb;\n");
413 }
414
415 static
416 void
417 jsr (p, a, s)
418 struct h8_opcode *p;
419 char *a;
420 int s;
421 {
422 printf ("if (b1 == 0xc4) {\n");
423 printf ("printf(\"%%c\", reg[2]);\n");
424 printf ("}\n");
425 printf ("else {\n");
426 printf ("reg[7]-=2;\n");
427 printf ("tmp = reg[7];\n");
428 printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n");
429 printf ("npc = (lval>>1) + saved_state.mem;\n");
430 printf ("}");
431 }
432
433 static void
434 jmp (p, a, s)
435 struct h8_opcode *p;
436 char *a;
437 int s;
438 {
439 printf ("npc = (lval>>1) + saved_state.mem;\n");
440 }
441
442 static void
443 rts (p, a, s)
444 struct h8_opcode *p;
445 char *a;
446 int s;
447 {
448 printf ("tmp = reg[7];\n");
449 printf ("reg[7]+=2;\n");
450 printf ("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n");
451 }
452
453 static void
454 rte (p, a, s)
455 struct h8_opcode *p;
456 char *a;
457 int s;
458 {
459 printf ("reg[7]+=2;\n");
460 printf ("tmp = reg[7];\n");
461 printf ("reg[7]+=2;\n");
462 printf ("SET_CCR(tmp);\n");
463 printf("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n");
464 }
465
466 static void
467 setf (p, a, s)
468 struct h8_opcode *p;
469 char *a;
470 int s;
471 {
472 printf ("tmp = GET_CCR();\n");
473 printf ("tmp %s= srca;\n", a);
474 }
475
476 static void
477 bpt (p, a, s)
478 struct h8_opcode *p;
479 char *a;
480 int s;
481 {
482 printf ("saved_state.exception = SIGTRAP;\n");
483 printf ("npc = pc;\n");
484 }
485
486 static void
487 log (p, a, s)
488 struct h8_opcode *p;
489 char *a;
490 int s;
491 {
492 printf ("dst = srcb %s srca;\n", a);
493 }
494
495 static void
496 ulog (p, a, s)
497 struct h8_opcode *p;
498 char *a;
499 int s;
500 {
501 printf ("dst = %s srcb ;\n", a);
502 }
503
504 static void
505 nop ()
506 {
507 }
508
509 static void
510 rotl ()
511 {
512 printf ("c = srcb & 0x80;\n");
513 printf ("dst = srcb << 1;\n");
514 printf ("if (c) dst|=1;\n");
515 }
516
517 static void
518 rotr ()
519 {
520 printf ("c = srcb & 1;\n");
521 printf ("dst = srcb >> 1;\n");
522 printf ("if (c) dst|=0x80;\n");
523 }
524
525 static void
526 rotxl ()
527 {
528 printf ("tmp = srcb & 0x80;\n");
529 printf ("dst = srcb << 1;\n");
530 printf ("if (c) dst|=1;\n");
531 printf ("c = tmp;\n");
532 }
533
534 static void
535 rotxr ()
536 {
537 printf ("tmp = srcb & 1;\n");
538 printf ("dst = srcb >> 1;\n");
539 printf ("if (c) dst|=0x80;\n");
540 printf ("c = tmp;\n");
541 }
542
543 static void
544 shal ()
545 {
546 printf ("c = srcb&0x80;\n");
547 printf ("dst = srcb << 1;\n");
548 }
549
550 static
551 void
552 shar ()
553 {
554 printf ("c = srcb&0x1;\n");
555 printf ("if (srcb&0x80) dst = (srcb>>1) | 0x80;\n");
556 printf ("else dst = (srcb>>1) &~ 0x80;\n");
557 }
558
559 static
560 void
561 shll ()
562 {
563 printf ("c = srcb&0x80;\n");
564 printf ("dst = srcb << 1;\n");
565 }
566
567 static
568 void
569 shlr ()
570 {
571 printf ("c = srcb&0x1;\n");
572 printf ("dst = (srcb>>1) &~ 0x80;\n");
573 }
574
575 static
576 void
577 divxu ()
578 {
579 printf ("srca = %s;\n", breg[2]);
580 printf ("srcb = %s;\n", wreg[3]);
581 printf ("n = srca & 0x80;\n");
582 printf ("z = !srca;\n");
583 printf ("if (srca) dst = srcb / srca;tmp = srcb %% srca;\n");
584 printf ("%s = (dst & 0xff) | (tmp << 8);\n", wreg[3]);
585 }
586
587 static
588 void
589 mulxu ()
590 {
591 printf ("srca = %s;\n", breg[2]);
592 printf ("srcb = %s;\n", wreg[3]);
593
594 printf ("dst = (srcb&0xff) * srca;\n");
595 printf ("%s = dst;\n", wreg[3]);
596 }
597
598 static
599 void
600 inc ()
601 {
602 printf ("dst = %s;\n", breg[3]);
603 printf ("v = (dst==0x7f);\n");
604 printf ("dst++;\n");
605 printf ("%s= dst;\n", breg[3]);
606 }
607
608 static
609 void
610 bit (p, a, s)
611 struct h8_opcode *p;
612 char *a;
613 int s;
614 {
615 printf ("%s\n", a);
616 }
617
618 static
619 void
620 dec ()
621 {
622 printf ("dst = %s;\n", breg[3]);
623 printf ("v = (dst==0x80);\n");
624 printf ("dst--;\n");
625 printf ("%s = dst;\n", breg[3]);
626 }
627
628 char saf[] = "goto setflags;";
629 char sf[] = "goto shiftflags;";
630 char af8[] = "goto aluflags8;";
631 char af16[] = "goto aluflags16;";
632 char lf[] = "goto logflags;";
633 char icf[] = "goto incflags;";
634 char mf8[] = "goto movflags8;";
635 char mf16[] = "goto movflags16;";
636 char nx[] = "goto next;";
637
638 struct
639 {
640 char *ftype;
641 int decode;
642 char *name;
643 void (*func) ();
644 char *arg;
645 int size;
646
647 }
648
649 table [] =
650 {
651 { nx, 1, "bld", bit, "dst = srcb; c = (srcb>>srca)&1;", 8 } ,
652 { nx, 1, "bild", bit, "dst = srcb; c = !((srcb>>srca)&1);", 8 } ,
653 { nx, 1, "band", bit, "dst = srcb; c = C &&((srcb>>srca)&1);", 8 } ,
654 { nx, 1, "biand", bit, "dst = srcb; c = C &&(!((srcb>>srca)&1));", 8 } ,
655 { nx, 1, "bior", bit, "dst = srcb; c = C ||(!((srcb>>srca)&1));", 8 } ,
656 { nx, 1, "bor", bit, "dst = srcb; c = C ||(((srcb>>srca)&1));", 8 } ,
657 { nx, 1, "bixor", bit, "dst = srcb; c = C ^(!((srcb>>srca)&1));", 8 } ,
658 { nx, 1, "bxor", bit, "dst = srcb; c = C ^(((srcb>>srca)&1));", 8 } ,
659 { nx, 1, "bnot", bit, "dst = srcb ^ (1<<srca);", 8 } ,
660 { nx, 1, "bclr", bit, "dst = srcb & ~(1<<srca);", 8 } ,
661 { nx, 1, "bset", bit, "dst = srcb | (1<<srca);", 8 } ,
662 { nx, 1, "bst", bit, "dst = (srcb & ~(1<<srca))| ((C)<<srca);", 8 } ,
663 { nx, 1, "bist", bit, "dst = (srcb & ~(1<<srca))| ((!C)<<srca);", 8 } ,
664 { nx, 1, "btst", bit, "dst = srcb; z = !((srcb>>srca)&1);", 8 } ,
665 { icf, 0, "dec", dec, 0, 0 } ,
666 { icf, 0, "inc", inc, 0, 0 } ,
667 { saf, 1, "orc", setf, "|", 0 } ,
668 { saf, 1, "xorc", setf, "^", 0 } ,
669 { saf, 1, "andc", setf, "&", 0 } ,
670 { nx, 1, "nop", nop, 0, 0 } ,
671 { nx, 1, "bra", bra, "1", 0 } ,
672 { nx, 1, "brn", bra, "0", 0 } ,
673 { nx, 1, "bhi", bra, "(C||Z)==0", 0 } ,
674 { nx, 1, "bls", bra, "(C||Z)==1", 0 } ,
675 { nx, 1, "bcs", bra, "C==1", 0 } ,
676 { nx, 1, "bcc", bra, "C==0", 0 } ,
677 { nx, 1, "bpl", bra, "N==0", 0 } ,
678 { nx, 1, "bmi", bra, "N==1", 0 } ,
679 { nx, 1, "bvs", bra, "V==1", 0 } ,
680 { nx, 1, "bvc", bra, "V==0", 0 } ,
681 { nx, 1, "bge", bra, "(N^V)==0", 0 } ,
682 { nx, 1, "bgt", bra, "(Z|(N^V))==0", 0 } ,
683 { nx, 1, "blt", bra, "(N^V)==1", 0 } ,
684 { nx, 1, "ble", bra, "(Z|(N^V))==1", 0 } ,
685 { nx, 1, "beq", bra, "Z==1", 0 } ,
686 { nx, 1, "bne", bra, "Z==0", 0 } ,
687 { nx, 1, "bsr", bsr, "", 0 } ,
688 { nx, 1, "jsr", jsr, 0, 0 } ,
689 { nx, 1, "jmp", jmp, 0, 0 } ,
690 { nx, 0, "rts", rts, 0, 0 } ,
691 { nx, 0, "rte", rte, 0, 0 } ,
692 { nx, 1, "andc", andc, 0, 0 } ,
693 { sf, 1, "shal", shal, 0, 0 } ,
694 { sf, 1, "shar", shar, 0, 0 } ,
695 { sf, 1, "shll", shll, 0, 0 } ,
696 { sf, 1, "shlr", shlr, 0, 0 } ,
697 { sf, 1, "rotxl", rotxl, 0, 0 } ,
698 { sf, 1, "rotxr", rotxr, 0, 0 } ,
699 { sf, 1, "rotl", rotl, 0, 0 } ,
700 { sf, 1, "rotr", rotr, 0, 0 } ,
701 { lf, 1, "xor", log, "^", 0 } ,
702 { lf, 1, "and", log, "&", 0 } ,
703 { lf, 1, "or", log, "|", 0 } ,
704 { lf, 1, "not", ulog, " ~", 0 } ,
705 { lf, 1, "neg", ulog, " - ", 0 } ,
706 { nx, 1, "adds", adds, "dst = srca + srcb", 0 } ,
707 { nx, 1, "subs", adds, "srca = -srca; dst = srcb + srca", 0 } ,
708 { af8, 1, "add.b", add, "dst = srca + srcb", 8 } ,
709 { af16, 1, "add.w", add, "dst = srca + srcb", 16 } ,
710 { af16, 1, "sub.w", add, "srca = -srca; dst = srcb + srca", 16 } ,
711 { af8, 1, "sub.b", add, "srca = -srca; dst = srcb + srca", 8 } ,
712 { af8, 1, "addx", addx, 0, 8 } ,
713 { af8, 1, "subx", subx, 0, 8 } ,
714 { af8, 0, "cmp.b", cmp, 0, 8 } ,
715 { af16, 0, "cmp.w", cmp, 0, 16 } ,
716 { nx, 1, "sleep", esleep, 0, 0 } ,
717 { nx, 0, "bpt", bpt, 0, 8 } ,
718 { nx, 0, "divxu", divxu, 0, 0 } ,
719 { nx, 0, "mulxu", mulxu, 0, 0 } ,
720 { mf8, 1, "mov.b", mov, 0, 8 } ,
721 { mf8, 1, "movtpe", mov, 0, 8 } ,
722 { mf8, 1, "movfpe", mov, 0, 8 } ,
723 { mf16, 1, "mov.w", mov, 0, 16 } ,
724 { 0 }
725 };
726
727 static
728 void
729 edo (p)
730 struct h8_opcode *p;
731 {
732 int i;
733
734 printf ("%s %s %s\n", cs, p->name, ce);
735
736 for (i = 0; table[i].name; i++)
737 {
738 if (strcmp (table[i].name, p->name) == 0)
739 {
740 printf ("{\n");
741 if (table[i].decode)
742 decode (p, 1, table[i].size);
743 printf ("cycles += %d;\n", p->time);
744 printf ("npc = pc + %d;\n", p->length/2);
745 table[i].func (p, table[i].arg, table[i].size);
746 if (table[i].decode)
747 decode (p, 0, table[i].size);
748 if (table[i].ftype)
749 printf (table[i].ftype);
750 else
751 printf ("goto next;\n");
752 printf ("}\n");
753 return;
754 }
755 }
756 printf ("%s not found %s\n", cs, ce);
757 printf ("saved_state.exception = SIGILL;\n");
758 printf ("break;\n");
759 }
760
761 static
762 int
763 owrite (i)
764 int i;
765 {
766 /* write if statements to select the right opcode */
767 struct h8_opcode **p;
768 int needand = 1;
769
770 p = h8_opcodes_sorted[i];
771 printf ("case 0x%03x:\n", i);
772
773 if (p[1] == 0)
774 {
775 /* See if the next few also match */
776 while (h8_opcodes_sorted[i + 1][0] == *p)
777 {
778 i++;
779 printf ("case 0x%03x:\n", i);
780 }
781
782 /* Dont need any if's this is the only one */
783 edo (*p);
784 }
785 else
786 {
787 while (*p)
788 {
789 /* start two nibbles in since we know we match in the first byte */
790 int c;
791 int nib = 2;
792 int byte = 1;
793 int mask1[5];
794 int mask0[5];
795 int nibshift = 4;
796 int any = 0;
797
798 for (c = 0; c < 5; c++)
799 {
800 mask1[c] = 0;
801 mask0[c] = 0;
802 }
803 printf ("%s %x%x", cs, (*p)->data.nib[0], (*p)->data.nib[1]);
804 while ((c = (*p)->data.nib[nib]) != E)
805 {
806 if (c & B30)
807 {
808 /* bit 3 must be zero */
809 mask0[byte] |= 0x8 << nibshift;
810 printf ("0");
811 any = 1;
812 }
813 else if (c & B31)
814 {
815 /* bit 3 must be one */
816 mask1[byte] |= 0x8 << nibshift;
817 printf ("8");
818 any = 1;
819 }
820 else if (c <= HexF)
821 {
822 mask0[byte] |= ((~c) & 0xf) << nibshift;
823 mask1[byte] |= (c & 0xf) << nibshift;
824 printf ("%x", c);
825 any = 1;
826 }
827 else
828 {
829 printf ("x");
830 }
831 nib++;
832 if (nibshift == 4)
833 {
834 nibshift = 0;
835 }
836 else
837 {
838 byte++;
839 nibshift = 4;
840 }
841 }
842 printf ("*/\n");
843 if (any)
844 {
845 printf ("if (");
846 needand = 0;
847 for (c = 1; c < byte; c++)
848 {
849 if (mask0[c] | mask1[c])
850 {
851 int sh;
852 if (needand)
853 printf ("\n&&");
854 if (c & 1) sh = 0;else sh = 8;
855 if (c/2 == 0 && sh == 0)
856 printf("((b1&0x%x)==0x%x)", mask0[c]| mask1[c],
857 mask1[c]);
858 else {
859 printf ("((pc[%d]&(0x%02x<<%d))==(0x%x<<%d))",
860 c/2, mask0[c] | mask1[c],sh,
861 mask1[c],sh);
862 }
863
864 needand = 1;
865 }
866 }
867 printf (")\n");
868 }
869 edo (*p);
870 p++;
871
872 }
873 }
874 return i;
875 }
876
877 static
878 void
879 remove_dups ()
880 {
881 struct h8_opcode *s;
882 struct h8_opcode *d;
883
884 for (d = s = h8_opcodes; s->name; s++)
885 {
886 int doit = 1;
887
888 if (strcmp (s->name, "push") == 0)
889 doit = 0;
890 if (strcmp (s->name, "bhs") == 0)
891 doit = 0;
892 if (strcmp (s->name, "blo") == 0)
893 doit = 0;
894 if (strcmp (s->name, "bt") == 0)
895 doit = 0;
896 if (strcmp (s->name, "bf") == 0)
897 doit = 0;
898 if (strcmp (s->name, "pop") == 0)
899 doit = 0;
900 if (doit)
901 {
902 *d++ = *s;
903 }
904 }
905 *d++ = *s++;
906 }
907
908 int
909 main ()
910 {
911 int i;
912
913 remove_dups ();
914 init ();
915
916 printf ("%s do the operation %s\n", cs, ce);
917 printf ("switch (b0) \n{\n");
918 for (i = 0; i < PTWO; i++)
919 {
920 i = owrite (i);
921 }
922 printf ("}\n");
923
924 return 0;
925 }