H8/300 simulator
[deliverable/binutils-gdb.git] / sim / h8300 / writecode.c
1 /* BFD library support routines for the Hitachi H8/300 architecture.
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 #include"bfd.h"
22 #include"sysdep.h"
23
24 #define DEFINE_TABLE
25 #define INSIM
26 #include"opcode/h8300.h"
27
28 #define MAXSAME 14
29
30 #define PTWO 256
31 static struct h8_opcode *h8_opcodes_sorted[PTWO][MAXSAME];
32
33 /* Run through the opcodes and sort them into order to make them easy
34 to disassemble
35 */
36
37 char *cs = "/*";
38 char *ce = "*/";
39
40 char *nibs[] =
41 {
42 "foo",
43 "(b0&0xf)",
44 "((b1>>4)&0xf)",
45 "((b1)&0xf)",
46 "((pc[2]>>4)&0xf)",
47 "((pc[2])&0xf)",
48 "((pc[3]>>4)&0xf)",
49 "((pc[3])&0xf)",
50 0, 0};
51
52 char *abs8[] =
53 {
54 "foo",
55 "foo",
56 "b1",
57 "foo",
58 };
59
60 #define sorted_key noperands
61 char *abs16[] =
62 {
63 "foo",
64 "foo",
65 "foo",
66 "foo",
67
68 "(pc[2]<<8)|pc[3]",
69 "foo",
70 "foo",
71 "foo",
72 };
73 char *empty="fake";
74 init ()
75 {
76 unsigned int i;
77 struct h8_opcode *p;
78
79 for (p = h8_opcodes; p->name; p++)
80 {
81 int n1 = 0;
82 int n2 = 0;
83 int j;
84 for (j = 0; p->data.nib[j] != E; j++)
85 {
86 if ((int)p->data.nib[j] == ABS16ORREL8SRC)
87 p->data.nib[j] = ABS16SRC;
88 if ((int)p->data.nib[j ] == ABS16OR8SRC)
89 p->data.nib[j] = ABS16SRC;
90 if ((int)p->data.nib[j] == ABS16OR8DST)
91 p->data.nib[j] = ABS16DST;
92 }
93 /* Kill push and pop, they're duplicates */
94 if (strcmp(p->name,"push")==0) {
95 p->name = empty;
96 }
97 if (strcmp(p->name,"pop")==0) {
98 p->name = empty;
99 }
100
101
102 if ((int) p->data.nib[0] < 16)
103 {
104 n1 = (int) p->data.nib[0];
105 }
106 else
107 n1 = 0;
108 if ((int) p->data.nib[1] < 16)
109 {
110 n2 = (int) p->data.nib[1];
111 }
112 else
113 n2 = 0;
114 #if 0
115 if ((int) p->data.nib[2] < 16)
116 {
117 n3 = (int) p->data.nib[2];
118 }
119 else if ((int) p->data.nib[2] & B30)
120 {
121 n3 = 0;
122 }
123 else if ((int) p->data.nib[2] & B31)
124 {
125 n3 = 0x8;
126 }
127
128
129 if ((int) p->data.nib[3] < 16)
130 {
131 n4 = (int) p->data.nib[3];
132 }
133 else if ((int) p->data.nib[3] & B30)
134 {
135 n4 = 0;
136 }
137 else if ((int) p->data.nib[3] & B31)
138 {
139 n4 = 0x8;
140 }
141 #endif
142 for (i = 0; i < MAXSAME; i++)
143 {
144 int j = /* ((n3 >> 3) * 512) + ((n4 >> 3) * 256) + */ n1 * 16 + n2;
145
146 if (h8_opcodes_sorted[j][i] == (struct h8_opcode *) NULL)
147 {
148 h8_opcodes_sorted[j][i] = p;
149 p->sorted_key = j;
150 break;
151 }
152 }
153
154 if (i == MAXSAME)
155 abort ();
156
157 /* Just make sure there are an even number of nibbles in it, and
158 that the count is the same s the length */
159 for (i = 0; p->data.nib[i] != E; i++)
160 /*EMPTY*/ ;
161 if (i & 1)
162 abort ();
163 p->length = i / 2;
164 }
165 for (i = 0; i < PTWO; i++)
166 {
167 if (h8_opcodes_sorted[i][0])
168 p = h8_opcodes_sorted[i][0];
169 else
170 h8_opcodes_sorted[i][0] = p;
171 }
172 }
173
174 unsigned int
175 DEFUN (bfd_h8_disassemble, (addr, data, stream),
176 bfd_vma addr AND
177 CONST bfd_byte * data AND
178 FILE * stream)
179 {
180 /* Find the first entry in the table for this opcode */
181 CONST static char *regnames[] =
182 {
183 "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
184 "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"};
185
186 int rs = 0;
187 int rd = 0;
188 int rdisp = 0;
189 int abs = 0;
190 struct h8_opcode **p = h8_opcodes_sorted[(unsigned) (data[0])];
191 struct h8_opcode *q;
192
193 /* Find the exact opcode/arg combo */
194 while (*p)
195 {
196 op_type *nib;
197 unsigned int len = 0;
198
199 q = *p++;
200 nib = q->data.nib;
201 while (*nib != E)
202 {
203 op_type looking_for = *nib;
204 int thisnib = data[len >> 1];
205
206 thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
207 if ((int) looking_for & (int) B31)
208 {
209 if (((int) thisnib & 0x8) == 0)
210 goto fail;
211 looking_for = (op_type) ((int) looking_for & ~(int) B31);
212 }
213 if ((int) looking_for & (int) B30)
214 {
215 if (((int) thisnib & 0x8) != 0)
216 goto fail;
217 looking_for = (op_type) ((int) looking_for & ~(int) B30);
218 }
219 switch (looking_for)
220 {
221 case 0:
222 case 1:
223 case 2:
224 case 3:
225 case 4:
226 case 5:
227 case 6:
228 case 7:
229 case 8:
230 case 9:
231 case 10:
232 case 11:
233 case 12:
234 case 13:
235 case 14:
236 case 15:
237 if ((int) looking_for != thisnib)
238 goto fail;
239 break;
240 case ABS16SRC:
241 case ABS16OR8SRC:
242 case ABS16ORREL8SRC:
243 case ABS16DST:
244 case ABS16OR8DST:
245 case DISPSRC:
246 case DISPDST:
247 case IMM16:
248 abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
249 len += 3;
250 nib += 3;
251 break;
252 case DISPREG:
253 rdisp = thisnib;
254 break;
255 case KBIT:
256 abs = thisnib == 0x8 ? 2 : 1;
257 break;
258 case IMM8:
259 case ABS8SRC:
260 case ABS8DST:
261 case MEMIND:
262 case DISP8:
263 abs = data[len >> 1];
264 len++;
265 nib++;
266 break;
267 case IMM3:
268 abs = thisnib;
269 break;
270 case RS8:
271 case RS16:
272 case RSINC:
273 case RSIND:
274 rs = thisnib;
275 break;
276 case RD16:
277 case RDDEC:
278 case RD8:
279 case RDIND:
280 rd = thisnib;
281 break;
282 default:
283 fprintf (stream, "Dont understand \n");
284 goto found;
285 }
286 len++;
287 nib++;
288 }
289 goto found;
290 fail:
291 ;
292
293 }
294 fprintf (stream, "%02x %02x .word\tH'%x,H'%x",
295 data[0], data[1],
296 data[0], data[1]);
297 return 2;
298 found:;
299 {
300 int i;
301
302 for (i = 0; i < q->length; i++)
303 {
304 fprintf (stream, "%02x", data[i]);
305 }
306 for (; i < 6; i++)
307 {
308 fprintf (stream, " ");
309 }
310 }
311 fprintf (stream, "%s\t", q->name);
312 /* Now print out the args */
313 {
314 op_type *args = q->args.nib;
315 int hadone = 0;
316
317 while (*args != E)
318 {
319 if (hadone)
320 fprintf (stream, ",");
321 switch ((int) (*args) & ~((int) B30 | (int) B31))
322 {
323 case IMM16:
324 case IMM8:
325 case IMM3:
326 fprintf (stream, "#0x%x", (unsigned) abs);
327 break;
328 case RD8:
329 fprintf (stream, "%s", regnames[rd]);
330 break;
331 case RS8:
332 fprintf (stream, "%s", regnames[rs]);
333 break;
334 case RD16:
335 fprintf (stream, "r%d", rd & 0x7);
336 break;
337 case RS16:
338 fprintf (stream, "r%d", rs & 0x7);
339 break;
340 case RSINC:
341 fprintf (stream, "@r%d+", rs & 0x7);
342 break;
343 case RDDEC:
344 fprintf (stream, "@-r%d", rd & 0x7);
345 break;
346 case RDIND:
347 fprintf (stream, "@r%d", rd & 0x7);
348 break;
349 case RSIND:
350 fprintf (stream, "@r%d", rs & 0x7);
351 break;
352 case ABS8SRC:
353 case ABS16SRC:
354 case ABS16OR8SRC:
355 case ABS16ORREL8SRC:
356 case ABS16OR8DST:
357 case ABS16DST:
358 case ABS8DST:
359 fprintf (stream, "@0x%x", (unsigned) abs);
360 break;
361 case MEMIND:
362 fprintf (stream, "@@%d (%x)", abs, abs);
363 break;
364 case DISP8:
365 fprintf (stream, ".%s%d (%x)", (char) abs > 0 ? "+" : "", (char) abs,
366 addr + (char) abs + 2);
367 break;
368 case DISPSRC:
369 case DISPDST:
370 fprintf (stream, "@(0x%x,r%d)", abs, rdisp & 0x7);
371 break;
372 case CCR:
373 fprintf (stream, "ccr");
374 break;
375 case KBIT:
376 fprintf (stream, "#%d", abs);
377 break;
378 default:
379 abort ();
380 }
381 hadone = 1;
382 args++;
383 }
384 }
385 return q->length;
386 }
387
388 void
389 decode (p, fetch, size)
390 struct h8_opcode *p;
391 int fetch;
392 int size;
393 {
394 int i;
395 char *ss = size == 8 ? "BYTE" : "WORD";
396
397 for (i = 0; p->data.nib[i] != E; i++)
398 {
399 switch (p->data.nib[i])
400 {
401 case RS8:
402 if (fetch)
403 printf ("srca = BYTE_REG(%s);\n", nibs[i]);
404 break;
405 case RS16 | B30:
406 case RS16 | B31:
407 case RS16:
408 if (fetch)
409 printf ("srca = WORD_REG(%s);\n", nibs[i]);
410 break;
411 case RD8:
412 if (fetch)
413 printf ("srcb = BYTE_REG(%s);\n", nibs[i]);
414 else
415 printf ("SET_BYTE_REG(%s,dst);\n", nibs[i]);
416 break;
417 case RD16 | B30:
418 case RD16 | B31:
419 case RD16:
420 if (fetch)
421 printf ("srcb = WORD_REG(%s);\n", nibs[i]);
422 else
423 printf ("SET_WORD_REG(%s,dst);\n", nibs[i]);
424 break;
425 case IMM8:
426 if (fetch)
427 printf ("srca = b1;\n", i);
428 break;
429 case RSINC:
430 case RSINC | B30:
431 case RSINC | B31:
432
433 if (fetch)
434 {
435 printf("rn = %s &0x7;\n", nibs[i]);
436 printf ("srca = %s_MEM(reg[rn]);\n", size == 8 ? "BYTE"
437 : "WORD") ;
438 printf("reg[rn]+=%d;\n", size/8);
439 }
440 break;
441 case RSIND:
442 case RSIND | B30:
443 case RSIND | B31:
444 if (fetch) {
445 printf("lval = WORD_REG(%s);\n", nibs[i]);
446 printf("srca = %s_MEM(lval);\n", ss);
447 }
448 break;
449
450 case RDIND:
451 case RDIND | B30:
452 case RDIND | B31:
453 if (fetch) {
454 printf("rn = %s & 0x7;\n", nibs[i] );
455 printf("lval = reg[rn];\n");
456 printf("srcb = %s_MEM(lval);\n", size==8 ? "BYTE" : "WORD");
457 }
458 else
459 {
460 printf("SET_%s_MEM(lval,dst);\n",ss);
461 }
462 break;
463
464 case MEMIND:
465 if (fetch)
466 {
467 printf("lval = WORD_MEM(b1);\n");
468 }
469 break;
470 case RDDEC:
471 case RDDEC | B30:
472 case RDDEC | B31:
473 if (!fetch)
474 {
475 printf("rn = %s & 0x7;\n", nibs[i]);
476 printf("reg[rn]-=%d;\n", size/8);
477 printf ("SET_%s_MEM(reg[rn], dst);\n",
478 size == 8 ? "BYTE" : "WORD");
479 }
480 break;
481
482 case IMM16:
483 if (fetch)
484 printf ("srca =WORD_IMM;\n", i);
485 break;
486 case ABS8SRC:
487 if (fetch) {
488
489 printf("lval = (0xff00) + b1;\n");
490 printf ("srca = BYTE_MEM(lval);\n");
491 }
492
493 break;
494 case ABS8DST:
495 if (fetch)
496 {
497 printf("lval = (0xff00) + b1;\n");
498 printf ("srcb = BYTE_MEM(lval);\n");
499 }
500 else
501 {
502 printf ("SET_BYTE_MEM(lval,dst);\n");
503 }
504 break;
505 case KBIT:
506 if (fetch)
507 printf ("srca = KBIT(%d);\n", i);
508 break;
509 case ABS16ORREL8SRC:
510 case ABS16SRC:
511 if (fetch)
512 {
513 printf("lval = ((pc[2] << 8) + pc[3]);\n");
514 printf("srca = %s_MEM(lval);\n", size==8 ? "BYTE" : "WORD");
515 }
516 break;
517 case DISPREG|B30:
518 case DISPREG|B31:
519 case DISPREG:
520 printf("rn = %s & 0x7;\n", nibs[i]);
521 break;
522 case DISPSRC:
523 if (fetch)
524 {
525 printf("lval = 0xffff&((pc[2] << 8) + pc[3] +reg[rn]);\n");
526 printf("srca = %s_MEM(lval);\n", size == 8 ? "BYTE" : "WORD");
527 }
528 break;
529 case DISPDST:
530 if (fetch)
531 {
532 printf("lval = 0xffff&((pc[2] << 8) + pc[3] +reg[rn]);\n");
533 printf("srcb = %s_MEM(lval);\n", size == 8 ? "BYTE" : "WORD");
534 }
535 else {
536 printf("SET_%s_MEM(lval,dst);\n",ss);
537 }
538 break;
539 case ABS16DST:
540 if (fetch)
541 {
542 printf("lval = ((pc[2] << 8) + pc[3]);\n");
543 printf("srcb = WORD_MEM(lval);\n");
544 }
545 else
546 {
547 printf ("SET_WORD_MEM(lval,dst);\n", i);
548 }
549 break;
550 case IGNORE:
551 break;
552 case DISP8:
553 printf(" /* DISP8 handled in opcode */\n");
554 break;
555 default:
556 if (p->data.nib[i] > HexF)
557 {
558 printf("exception = SIGILL;\n");
559 }
560 }
561 }
562 }
563
564 #define NNIBS 8
565
566 typedef int keytype[NNIBS];
567 struct key
568 {
569 keytype k;
570 int nmatch;
571 /* struct h8_opcode *match[200];*/
572 };
573
574 struct key keys[50];
575 int nkeys;
576
577 calckey (p, key)
578 struct h8_opcode *p;
579 keytype key;
580 {
581 int j;
582
583 for (j = 0; (p->data.nib[j] != E); j++)
584 {
585 if (p->data.nib[j] == RS8 || p->data.nib[j] == RD8)
586 p->size = 8;
587 if (p->size != 8)
588 p->size=16;
589 if (p->data.nib[j] > HexF)
590 key[j] = p->data.nib[j];
591 else
592 key[j] = 0;
593 }
594 for (; j < NNIBS; j++)
595 key[j] = 0;
596 }
597
598 lookup (key)
599 keytype key;
600 {
601 int n;
602
603 /* See if this key matches one already found */
604 for (n = 0; n < nkeys; n++)
605 {
606 int i;
607
608 for (i = 0; i < NNIBS; i++)
609 {
610 if ((keys[n].k[i]&0x3f) != (key[i]&0x3f))
611 break;
612 }
613 if (i == NNIBS)
614 break;
615 }
616 return n;
617 }
618 BADNEWS(x) {
619 if (x == 0x7c) return 0;
620 if (x == 0x7d) return 0;
621 if (x == 0x7e) return 0;
622 if (x == 0x7f) return 0;
623 return 1;
624 }
625
626 sortmodes ()
627 {
628 /* look through all the addressing modes, work out which ones are unique*/
629 struct h8_opcode **p;
630 keytype key;
631
632 int n;
633 int i;
634
635 memset (keys, 0, sizeof (keys));
636
637
638 for (i = 0; i < PTWO; i++)
639 {
640 p = h8_opcodes_sorted[i];
641 while (*p)
642 {
643 int j;
644 int a = 0;
645
646 if (BADNEWS((*p)->sorted_key)) {
647 calckey (*p, key);
648 n = lookup (key);
649 if (n == nkeys)
650 {
651 /* No hit, insert */
652 memcpy (keys[n].k, key, sizeof (keys[n].k));
653 nkeys++;
654 }
655 /* Link in the p */
656 #if 0
657 /*e*/ keys[n].match[keys[n].nmatch++] = *p;
658 #endif
659 }
660 (*p)->idx = n;
661 p++;
662 }
663 }
664
665 #if 0
666 for (n = 0; n < nkeys; n++)
667 {
668 printf ("%d %d %d %d %d %d %d %d\n", keys[n][0], keys[n][1], keys[n][2], keys[n][3],
669 keys[n][4], keys[n][5], keys[n][6], keys[n][7]);
670
671 }
672 #endif
673
674 }
675 void
676 enop ()
677 {
678 printf ("%s Nop does nothing %s", cs, ce);
679 }
680
681 void
682 esleep ()
683 {
684 printf ("exception = SIGSTOP;\n");
685 }
686
687 rv (x)
688 {
689 printf ("v= 0;\n");
690 }
691
692 void
693 mov (p, s, sz)
694 struct h8_opcode *p;
695 char *s;
696 int sz;
697 {
698 printf("dst = srca;\n");
699 }
700
701 tn (s)
702 {
703 /* printf ("n = dst & 0x%x;\n", s == 8 ? 0x80 : 0x8000);*/
704 }
705
706 tz (s)
707 {
708 /* printf ("z = ~(dst & 0x%x);\n", s == 8 ? 0xff : 0xffff);*/
709 }
710
711 tv (s)
712 {
713 /* printf ("v = (( (~(srca ^ srcb) & (srca ^ dst))) & (1<<%d));\n", s);*/
714 }
715
716 tc (s)
717 {
718 printf ("c = dst & 0x%x;\n", 1 << s);
719 }
720
721 log_flags (s)
722 {
723 tn (s);
724 tz (s);
725 rv (s);
726 }
727
728 alu_flags (s)
729 {
730 tn (s);
731 tz (s);
732 tv (s);
733 tc (s);
734 }
735
736 void
737 bclr (p)
738 struct h8_opcode *p;
739 {
740 printf ("dst = srca & ~(1<<srcb);\n");
741 }
742
743 void
744 biand (p)
745 struct h8_opcode *p;
746 {
747 printf ("c =(C && ! ((srca >> srcb)&1));\n");
748 }
749
750 void
751 bild (p)
752 struct h8_opcode *p;
753 {
754 printf ("c = ( ! ((srca >> srcb)&1));\n");
755 }
756
757
758 void
759 andc (p)
760 struct h8_opcode *p;
761 {
762 printf ("SET_CCR(GET_CCR() & srca);\n");
763 }
764
765 void
766 addx (p)
767 struct h8_opcode *p;
768 {
769 printf ("dst = srca + srcb+ (c != 0);\n");
770 }
771
772 void
773 subx (p)
774 struct h8_opcode *p;
775 {
776 printf ("dst = srcb - srca - (c != 0);\n");
777 }
778
779 void
780 add (p, s, sz)
781 struct h8_opcode *p;
782 char *s;
783 int sz;
784 {
785 printf ("%s;\n", s);
786 alu_flags (sz);
787
788 }
789
790 void
791 adds (p, s)
792 struct h8_opcode *p;
793 char *s;
794 {
795 printf ("%s;\n", s);
796 }
797
798 void
799 bra (p, a)
800 struct h8_opcode *p;
801 char *a;
802 {
803 printf ("if (%s) npc += (((char *)pc)[1]);\n", a);
804 }
805
806
807 void
808 bsr (p, a)
809 struct h8_opcode *p;
810 char *a;
811 {
812 printf("reg[7]-=2;\n");
813 printf("tmp = reg[7];\n");
814 printf ("SET_WORD_MEM(tmp, npc-mem);\n");
815 printf ("npc += (((char *)pc)[1]);\n");
816 }
817
818 void
819 cmp (p, a, s)
820 struct h8_opcode *p;
821 char *a;
822 int s;
823 {
824 decode(p, 1, s);
825 printf("srca = -srca;\n");
826 printf ("dst = srca + srcb;\n");
827 }
828
829 void
830 jsr (p, a, s)
831 struct h8_opcode *p;
832 char *a;
833 int s;
834 {
835 printf("if (b1 == 0xc4) {\n");
836 printf("printf(\"%%c\", reg[2]);\n");
837 printf("}\n");
838 printf("else {\n");
839 printf("reg[7]-=2;\n");
840 printf("tmp = reg[7];\n");
841 printf ("SET_WORD_MEM(tmp, npc-mem);\n");
842 printf ("npc = lval + mem;\n");
843 printf("}");
844 }
845
846 void
847 jmp (p, a, s)
848 struct h8_opcode *p;
849 char *a;
850 int s;
851 {
852 printf ("npc = lval + mem;\n");
853 }
854
855 void
856 rts (p, a, s)
857 struct h8_opcode *p;
858 char *a;
859 int s;
860 {
861 printf ("tmp = reg[7];\n");
862 printf ("reg[7]+=2;\n");
863 printf ("npc = mem + WORD_MEM(tmp);\n");
864 }
865
866 void
867 setf (p, a, s)
868 struct h8_opcode *p;
869 char *a;
870 int s;
871 {
872 printf ("tmp = GET_CCR();\n");
873 printf ("tmp %s= srca;\n",a);
874 }
875
876 void
877 bpt (p, a, s)
878 struct h8_opcode *p;
879 char *a;
880 int s;
881 {
882 printf ("exception = SIGTRAP;\n");
883 printf( "npc = pc;\n");
884 }
885
886 void
887 log (p, a, s)
888 struct h8_opcode *p;
889 char *a;
890 int s;
891 {
892 printf ("dst = srcb %s srca;\n", a);
893 }
894
895 void
896 ulog (p, a, s)
897 struct h8_opcode *p;
898 char *a;
899 int s;
900 {
901 printf ("dst = %s srcb ;\n", a);
902 }
903
904 void
905 nop()
906 {
907 }
908
909 void
910 rotl()
911 {
912 printf("c = srcb & 0x80;\n");
913 printf("dst = srcb << 1;\n");
914 printf("if (c) dst|=1;\n");
915 }
916 void
917 rotr()
918 {
919 printf("c = srcb & 1;\n");
920 printf("dst = srcb >> 1;\n");
921 printf("if (c) dst|=0x80;\n");
922 }
923
924 void
925 rotxl()
926 {
927 printf("tmp = srcb & 0x80;\n");
928 printf("dst = srcb << 1;\n");
929 printf("if (c) dst|=1;\n");
930 printf("c = tmp;\n");
931 }
932 void
933 rotxr()
934 {
935 printf("tmp = srcb & 1;\n");
936 printf("dst = srcb >> 1;\n");
937 printf("if (c) dst|=0x80;\n");
938 printf("c = tmp;\n");
939 }
940
941
942 void
943 shal()
944 {
945 printf("c = srcb&0x80;\n");
946 printf("dst = srcb << 1;\n");
947 }
948
949 void
950 shar()
951 {
952 printf("c = srcb&0x1;\n");
953 printf("if (srcb&0x80) dst = (srcb>>1) | 0x80;\n");
954 printf("else dst = (srcb>>1) &~ 0x80;\n");
955 }
956
957 void
958 shll()
959 {
960 printf("c = srcb&0x80;\n");
961 printf("dst = srcb << 1;\n");
962 }
963
964 void
965 shlr()
966 {
967 printf("c = srcb&0x1;\n");
968 printf("dst = (srcb>>1) &~ 0x80;\n");
969 }
970
971 void
972 divxu()
973 {
974 printf("srca = BYTE_REG(pc[1]>>4);\n");
975 printf("srcb = WORD_REG(pc[1]&0x7);\n");
976 printf("n = srca & 0x80\;\n");
977 printf("z = !srca;\n");
978 printf("if (srca) dst = srcb / srca;tmp = srcb %% srca;\n");
979 printf("reg[pc[1]&0x7] = (dst & 0xff) | (tmp << 8);\n");
980 }
981 void
982 mulxu()
983 {
984 printf("srca = BYTE_REG(pc[1]>>4);\n");
985 printf("srcb = WORD_REG(pc[1]&0x7);\n");
986 printf("dst = (srcb&0xff) * srca;\n");
987 printf("reg[pc[1]&0x7] = dst;\n");
988 }
989 char saf[]="goto setflags;";
990 char sf[] = "goto shiftflags;";
991 char af8[] = "goto aluflags8;";
992 char af16[] = "goto aluflags16;";
993 char lf[] = "goto logflags;";
994 char mf8[]= "goto movflags8;";
995 char mf16[]= "goto movflags16;";
996 char nx[] = "goto next;";
997 struct
998 {
999 char *ftype;
1000 int decode;
1001 char *name;
1002 void (*func) ();
1003 char *arg;
1004 int size;
1005
1006 }
1007
1008
1009 table [] =
1010 {
1011 saf,1,"orc",setf,"|",0,
1012 saf,1,"xorc",setf,"^",0,
1013 saf,1,"andc",setf,"&",0,
1014 0,1, "bild", bild, 0, 0,
1015 0,1, "nop", nop, 0, 0,
1016 0,1, "bclr", bclr, 0, 0,
1017 0,1, "biand", biand, 0, 0,
1018 0,1, "bra", bra, "1", 0,
1019 0,1, "brn", bra, "0", 0,
1020 0,1, "bf", bra, "0", 0,
1021 0,1, "bhi", bra, "(C||Z)==0", 0,
1022 0,1, "bls", bra, "(C||Z)==1", 0,
1023 0,1, "bcs", bra, "C==1", 0,
1024 0,1, "bcc", bra, "C==0", 0,
1025 0,1, "bpl", bra, "N==0", 0,
1026 0,1, "bmi", bra, "N==1", 0,
1027 0,1, "bvs", bra, "V==1", 0,
1028 0,1, "bvc", bra, "V==0", 0,
1029 0,1, "bge", bra, "(N^V)==0", 0,
1030 0,1, "bgt", bra, "(Z|(N^V))==0", 0,
1031 0,1, "blt", bra, "(N^V)==1", 0,
1032 0,1, "ble", bra, "(Z|(N^V))==1", 0,
1033 0,1, "beq", bra, "Z==1", 0,
1034 0,1, "bne", bra, "Z==0", 0,
1035 0,1, "bsr", bsr, "", 0,
1036 0,1, "jsr", jsr, 0, 0,
1037 0,1, "jmp", jmp, 0, 0,
1038 0,0, "rts", rts, 0, 0,
1039 0,1, "andc", andc, 0, 0,
1040 0,1, "nop", enop, 0, 0,
1041 sf,1, "shal",shal,0,0,
1042 sf,1, "shar",shar,0,0,
1043 sf,1, "shll",shll,0,0,
1044 sf,1, "shlr",shlr,0,0,
1045 sf,1, "rotxl",rotxl,0,0,
1046 sf,1, "rotxr",rotxr,0,0,
1047 sf,1, "rotl",rotl,0,0,
1048 sf,1, "rotr",rotr,0,0,
1049 lf,1, "xor", log, "^",0,
1050 lf,1, "and", log, "&",0,
1051 lf,1, "or", log, "|",0,
1052 lf,1,"not", ulog," ~",0,
1053 lf,1,"neg", ulog," - ",0,
1054 0,1, "adds", adds, "dst = srca + srcb", 0,
1055 0,1, "subs", adds, "srca = -srca; dst = srcb + srca", 0,
1056 af8,1, "add.b", add, "dst = srca + srcb", 8,
1057 af16,1, "add.w", add, "dst = srca + srcb", 16,
1058 af16,1, "sub.w", add, "srca = -srca; dst = srcb + srca", 16,
1059 af8,1, "sub.b", add, "srca = -srca; dst = srcb + srca", 8,
1060 af8,1, "addx", addx, 0, 8,
1061 af8,1, "subx", subx, 0, 8,
1062 af8,0, "cmp.b", cmp, 0, 8,
1063 af16,0, "cmp.w", cmp, 0, 16,
1064 0,1, "sleep", esleep, 0, 0,
1065 0,0, "bpt", bpt, 0, 8,
1066 nx,0, "divxu", divxu,0,0,
1067 nx,0, "mulxu", mulxu,0,0,
1068 mf8,1, "mov.b", mov, 0, 8,
1069 mf16,1, "mov.w", mov, 0, 16,
1070 0, 0
1071 };
1072
1073 edo (p)
1074 struct h8_opcode *p;
1075 {
1076 int i;
1077
1078 printf ("%s %s %s\n", cs, p->name, ce);
1079
1080 for (i = 0; table[i].name; i++)
1081 {
1082 if (strcmp (table[i].name, p->name) == 0)
1083 {
1084 printf ("{\n");
1085 if (table[i].decode) decode(p, 1,p->size);
1086 printf ("cycles += %d;\n", p->time);
1087 printf ("npc = pc + %d;\n", p->length);
1088 table[i].func (p, table[i].arg, table[i].size);
1089 if (table[i].decode) decode(p, 0,p->size);
1090 if (table[i].ftype) printf(table[i].ftype);
1091 else printf ("goto next;\n");
1092 printf ("}\n");
1093 return;
1094 }
1095 }
1096 printf ("%s not found %s\n", cs, ce);
1097 printf("exception = SIGILL;\n");
1098 printf ("break;\n");
1099 }
1100
1101 int
1102 owrite (i)
1103 {
1104 /* write if statements to select the right opcode */
1105 struct h8_opcode **p;
1106 int needand = 1;
1107
1108 p = h8_opcodes_sorted[i];
1109 printf ("case 0x%03x:\n", i);
1110
1111 if (p[1] == 0)
1112 {
1113 /* See if the next few also match */
1114 while (h8_opcodes_sorted[i + 1][0] == *p)
1115 {
1116 i++;
1117 printf ("case 0x%03x:\n", i);
1118 }
1119
1120 /* Dont need any if's this is the only one */
1121 edo (*p);
1122 }
1123 else
1124 {
1125 while (*p)
1126 {
1127 /* start two nibbles in since we know we match in the first byte */
1128 int c;
1129 int nib = 2;
1130 int byte = 1;
1131 int mask1[5];
1132 int mask0[5];
1133 int nibshift = 4;
1134 int any = 0;
1135
1136 for (c = 0; c < 5; c++)
1137 {
1138 mask1[c] = 0;
1139 mask0[c] = 0;
1140 }
1141 printf ("%s %x%x", cs, (*p)->data.nib[0], (*p)->data.nib[1]);
1142 while ((c = (*p)->data.nib[nib]) != E)
1143 {
1144 if (c & B30)
1145 {
1146 /* bit 3 must be zero */
1147 mask0[byte] |= 0x8 << nibshift;
1148 printf ("0");
1149 any = 1;
1150 }
1151 else if (c & B31)
1152 {
1153 /* bit 3 must be one */
1154 mask1[byte] |= 0x8 << nibshift;
1155 printf ("8");
1156 any = 1;
1157 }
1158 else if (c <= HexF)
1159 {
1160 mask0[byte] |= ((~c) & 0xf) << nibshift;
1161 mask1[byte] |= (c & 0xf) << nibshift;
1162 printf ("%x", c);
1163 any = 1;
1164 }
1165 else
1166 {
1167 printf ("x");
1168 }
1169 nib++;
1170 if (nibshift == 4)
1171 {
1172 nibshift = 0;
1173 }
1174 else
1175 {
1176 byte++;
1177 nibshift = 4;
1178 }
1179 }
1180 printf ("*/\n");
1181 if (any)
1182 {
1183 printf ("if (");
1184 needand = 0;
1185 for (c = 1; c < byte; c++)
1186 {
1187 if (mask0[c] | mask1[c])
1188 {
1189 if (needand)
1190 printf ("\n&&");
1191 printf ("((pc[%d]&0x%02x)==0x%x)",
1192 c, mask0[c] | mask1[c], mask1[c]);
1193 needand = 1;
1194 }
1195 }
1196 printf (")\n");
1197 }
1198 edo (*p);
1199 p++;
1200
1201 }
1202 }
1203 return i;
1204 }
1205
1206 print_key (i)
1207 {
1208 int j;
1209
1210 printf ("%s", cs);
1211 for (j = 0; j < 8; j++)
1212 {
1213 /* printf ("(0x%02x ", keys[i].k[j]);*/
1214 if (keys[i].k[j] & B30)
1215 printf ("B30");
1216 else if (keys[i].k[j] & B31)
1217 printf ("B31");
1218 switch (keys[i].k[j])
1219 {
1220 case KBIT:
1221 printf ("KBIT");
1222 break;
1223 case IMM3:
1224 printf ("IMM3");
1225 break;
1226 case RD8:
1227 printf ("RD8");
1228 break;
1229 case RD16:
1230 printf ("RD16");
1231 break;
1232 case RS8:
1233 printf ("RS8");
1234 break;
1235 case RS16:
1236 printf ("RS16");
1237 break;
1238 case IMM8:
1239 printf ("IMM8");
1240 break;
1241 case IMM16:
1242 printf ("IMM16");
1243 break;
1244 case CCR:
1245 printf ("CCR");
1246 break;
1247 case ABS8SRC:
1248 printf ("ABS8SRC");
1249 break;
1250 case ABS8DST:
1251 printf ("ABS8SDST");
1252 break;
1253 case DISP8:
1254 printf ("DISP8");
1255 break;
1256 case ABS16SRC:
1257 printf ("ABS16SRC");
1258 break;
1259 case ABS16OR8SRC:
1260 printf ("ABS16OR8SRC");
1261 break;
1262 case ABS16DST:
1263 printf ("ABS16SDST");
1264 break;
1265 case ABS16OR8DST:
1266 printf ("ABS16OR8SDST");
1267 break;
1268 case DISPSRC:
1269 printf ("DISPSRC");
1270 break;
1271 case DISPDST:
1272 printf ("DISPSDST");
1273 break;
1274 case DISPREG:
1275 printf ("DISPREG");
1276 break;
1277 case RDDEC:
1278 printf ("RDDEC");
1279 break;
1280 case RSINC:
1281 printf ("RSINC");
1282 break;
1283 case RDIND:
1284 printf ("RDIND");
1285 break;
1286 case RSIND:
1287 printf ("RSIND");
1288 break;
1289 case MEMIND:
1290 printf ("MEMIND");
1291 break;
1292 case ABS16ORREL8SRC:
1293 printf ("ABS16ORREL8SRC");
1294 break;
1295 case IGNORE:
1296 printf ("IGNORE");
1297 break;
1298 default:
1299 printf ("**");
1300 break;
1301 }
1302 printf (" ");
1303 }
1304 printf ("%s\n", ce);
1305 }
1306
1307 dofetch (fetch)
1308 int fetch;
1309 {
1310 int i;
1311 int done_for[PTWO];
1312 memset (done_for, 0, sizeof (done_for));
1313 printf ("switch (b0) {\n");
1314 for (i = 0; i < nkeys; i++)
1315 {
1316 struct h8_opcode *p;
1317 struct h8_opcode *l = 0;
1318 int k;
1319
1320 printf ("%s KEY %d %s\n", cs,i,ce);
1321 print_key (i);
1322
1323 {
1324 int j;
1325
1326 l = 0;
1327 for (p = h8_opcodes; p->name; p++)
1328 {
1329 int key[8];
1330
1331 if (p->idx == i && p->name != empty)
1332 {
1333 l = p;
1334 if (done_for[p->sorted_key] != i + 1000)
1335 {
1336 done_for[p->sorted_key] = i + 1000;
1337 printf ("case 0x%03x:\n", p->sorted_key);
1338 printf("%s %s %s\n", cs, p->name, ce);
1339 }
1340 }
1341 }
1342 if (l)
1343 {
1344
1345 decode (l, fetch, l->size);
1346 printf ("break;\n");
1347 }
1348 else
1349 printf (" /* NO */\n");
1350 }
1351 }
1352 printf ("}\n");
1353 }
1354
1355 remove_dups()
1356 {
1357 struct h8_opcode *s;
1358 struct h8_opcode *d;
1359 for (d = s = h8_opcodes; s->name; s++)
1360 {
1361 int doit = 1;
1362 if (strcmp(s->name,"push") ==0) doit = 0;
1363 if (strcmp(s->name,"pop") ==0) doit = 0;
1364 if (doit)
1365 {
1366 *d++ = *s;
1367 }
1368 }
1369 *d++ = *s++;
1370 }
1371 main ()
1372 {
1373 int i;
1374 remove_dups();
1375 init ();
1376
1377 sortmodes ();
1378
1379 #if 0
1380 printf("%s fetch the args %s\n", cs,ce);
1381 dofetch (1);
1382 #endif
1383 printf("%s do the operation %s\n", cs,ce);
1384 printf ("switch (b0) \n{\n");
1385 for (i = 0; i < PTWO; i++)
1386 {
1387 i = owrite (i);
1388 }
1389 printf ("}\n");
1390 #if 0
1391 printf("%s store the result %s\n", cs,ce);
1392 dofetch (0);
1393 #endif
1394
1395 return 0;
1396 }
This page took 0.132244 seconds and 5 git commands to generate.