1 /* Code generator for the Hitachi H8/300 architecture simulator.
3 Written by Steve Chamberlain of Cygnus Support.
6 This file is part of H8/300 sim
9 THIS SOFTWARE IS NOT COPYRIGHTED
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.
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.
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 */
29 #include"opcode/h8300.h"
34 static struct h8_opcode
*h8_opcodes_sorted
[PTWO
][MAXSAME
];
39 /* How to get at nibble n from the instruction */
52 /* how to get at the 3 bit immediate in the instruction */
62 /* How to get at a byte register from an index in the instruction at
70 "*(bhigh[pc[1]>>8])"};
72 /* How to get at a word register from an index in the instruction at
81 #define sorted_key noperands
83 /* sort the opcode table into h8_opcodes_sorted[0..255] */
90 for (p
= h8_opcodes
; p
->name
; p
++)
96 for (j
= 0; p
->data
.nib
[j
] != E
; j
++)
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
;
107 if ((int) p
->data
.nib
[0] < 16)
109 n1
= (int) p
->data
.nib
[0];
113 if ((int) p
->data
.nib
[1] < 16)
115 n2
= (int) p
->data
.nib
[1];
119 for (i
= 0; i
< MAXSAME
; i
++)
121 int j
= /* ((n3 >> 3) * 512) + ((n4 >> 3) * 256) + */ n1
* 16 + n2
;
123 if (h8_opcodes_sorted
[j
][i
] == (struct h8_opcode
*) NULL
)
125 h8_opcodes_sorted
[j
][i
] = p
;
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
++)
142 for (i
= 0; i
< PTWO
; i
++)
144 if (h8_opcodes_sorted
[i
][0])
145 p
= h8_opcodes_sorted
[i
][0];
147 h8_opcodes_sorted
[i
][0] = p
;
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 */
162 for (i
= 0; p
->data
.nib
[i
] != E
; i
++)
164 int x
= p
->data
.nib
[i
];
167 op
= (x
& DST
) ? 1 : 0;
186 printf ("ir%d = GET_LVAL_%d_REG(%d);\n", op
, size
, i
);
190 printf ("/* Imm has no lvalue */\n");
200 decode (p
, fetch
, size
)
217 printf ("saved_state.exception = SIGSTOP;\n");
226 printf ("dst = srca;\n");
233 printf ("SET_CCR(GET_CCR() & srca);\n");
240 printf ("dst = srca + srcb+ (c != 0);\n");
247 printf ("dst = srcb - srca - (c != 0);\n");
272 printf ("if (%s) npc += ((char )b1)>>1;\n", a
);
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");
293 printf ("srca = -srca;\n");
294 printf ("dst = srca + srcb;\n");
304 printf ("if (b1 == 0xc4) {\n");
305 printf ("printf(\"%%c\", reg[2]);\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");
321 printf ("npc = (lval>>1) + saved_state.mem;\n");
330 printf ("tmp = reg[7];\n");
331 printf ("reg[7]+=2;\n");
332 printf ("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n");
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");
354 printf ("tmp = GET_CCR();\n");
355 printf ("tmp %s= srca;\n", a
);
364 printf ("saved_state.exception = SIGTRAP;\n");
365 printf ("npc = pc;\n");
374 printf ("dst = srcb %s srca;\n", a
);
383 printf ("dst = %s srcb ;\n", a
);
394 printf ("c = srcb & 0x80;\n");
395 printf ("dst = srcb << 1;\n");
396 printf ("if (c) dst|=1;\n");
402 printf ("c = srcb & 1;\n");
403 printf ("dst = srcb >> 1;\n");
404 printf ("if (c) dst|=0x80;\n");
410 printf ("tmp = srcb & 0x80;\n");
411 printf ("dst = srcb << 1;\n");
412 printf ("if (c) dst|=1;\n");
413 printf ("c = tmp;\n");
419 printf ("tmp = srcb & 1;\n");
420 printf ("dst = srcb >> 1;\n");
421 printf ("if (c) dst|=0x80;\n");
422 printf ("c = tmp;\n");
428 printf ("c = srcb&0x80;\n");
429 printf ("dst = srcb << 1;\n");
436 printf ("c = srcb&0x1;\n");
437 printf ("if (srcb&0x80) dst = (srcb>>1) | 0x80;\n");
438 printf ("else dst = (srcb>>1) &~ 0x80;\n");
445 printf ("c = srcb&0x80;\n");
446 printf ("dst = srcb << 1;\n");
453 printf ("c = srcb&0x1;\n");
454 printf ("dst = (srcb>>1) &~ 0x80;\n");
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]);
473 printf ("srca = %s;\n", breg
[2]);
474 printf ("srcb = %s;\n", wreg
[3]);
476 printf ("dst = (srcb&0xff) * srca;\n");
477 printf ("%s = dst;\n", wreg
[3]);
484 printf ("dst = %s;\n", breg
[3]);
485 printf ("v = (dst==0x7f);\n");
487 printf ("%s= dst;\n", breg
[3]);
504 printf ("dst = %s;\n", breg
[3]);
505 printf ("v = (dst==0x80);\n");
507 printf ("%s = dst;\n", breg
[3]);
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;";
534 nx
, 1, "bld", bit
, "dst = srcb; c = (srcb>>srca)&1;", 8
538 nx
, 1, "bild", bit
, "dst = srcb; c = !((srcb>>srca)&1);", 8
542 nx
, 1, "band", bit
, "dst = srcb; c = C &&((srcb>>srca)&1);", 8
546 nx
, 1, "biand", bit
, "dst = srcb; c = C &&(!((srcb>>srca)&1));", 8
550 nx
, 1, "bior", bit
, "dst = srcb; c = C ||(!((srcb>>srca)&1));", 8
554 nx
, 1, "bor", bit
, "dst = srcb; c = C ||(((srcb>>srca)&1));", 8
558 nx
, 1, "bixor", bit
, "dst = srcb; c = C ^(!((srcb>>srca)&1));", 8
562 nx
, 1, "bxor", bit
, "dst = srcb; c = C ^(((srcb>>srca)&1));", 8
566 nx
, 1, "bnot", bit
, "dst = srcb ^ (1<<srca);", 8
570 nx
, 1, "bclr", bit
, "dst = srcb & ~(1<<srca);", 8
574 nx
, 1, "bset", bit
, "dst = srcb | (1<<srca);", 8
578 nx
, 1, "bst", bit
, "dst = (srcb & ~(1<<srca))| ((C)<<srca);", 8
582 nx
, 1, "bist", bit
, "dst = (srcb & ~(1<<srca))| ((!C)<<srca);", 8
586 nx
, 1, "btst", bit
, "dst = srcb; z = !((srcb>>srca)&1);", 8
590 icf
, 0, "dec", dec
, 0, 0
594 icf
, 0, "inc", inc
, 0, 0
598 saf
, 1, "orc", setf
, "|", 0
602 saf
, 1, "xorc", setf
, "^", 0
606 saf
, 1, "andc", setf
, "&", 0
610 nx
, 1, "nop", nop
, 0, 0
614 nx
, 1, "bra", bra
, "1", 0
618 nx
, 1, "brn", bra
, "0", 0
622 nx
, 1, "bhi", bra
, "(C||Z)==0", 0
626 nx
, 1, "bls", bra
, "(C||Z)==1", 0
630 nx
, 1, "bcs", bra
, "C==1", 0
634 nx
, 1, "bcc", bra
, "C==0", 0
638 nx
, 1, "bpl", bra
, "N==0", 0
642 nx
, 1, "bmi", bra
, "N==1", 0
646 nx
, 1, "bvs", bra
, "V==1", 0
650 nx
, 1, "bvc", bra
, "V==0", 0
654 nx
, 1, "bge", bra
, "(N^V)==0", 0
658 nx
, 1, "bgt", bra
, "(Z|(N^V))==0", 0
662 nx
, 1, "blt", bra
, "(N^V)==1", 0
666 nx
, 1, "ble", bra
, "(Z|(N^V))==1", 0
670 nx
, 1, "beq", bra
, "Z==1", 0
674 nx
, 1, "bne", bra
, "Z==0", 0
678 nx
, 1, "bsr", bsr
, "", 0
682 nx
, 1, "jsr", jsr
, 0, 0
686 nx
, 1, "jmp", jmp
, 0, 0
690 nx
, 0, "rts", rts
, 0, 0
694 nx
, 0, "rte", rte
, 0, 0
698 nx
, 1, "andc", andc
, 0, 0
702 sf
, 1, "shal", shal
, 0, 0
706 sf
, 1, "shar", shar
, 0, 0
710 sf
, 1, "shll", shll
, 0, 0
714 sf
, 1, "shlr", shlr
, 0, 0
718 sf
, 1, "rotxl", rotxl
, 0, 0
722 sf
, 1, "rotxr", rotxr
, 0, 0
726 sf
, 1, "rotl", rotl
, 0, 0
730 sf
, 1, "rotr", rotr
, 0, 0
734 lf
, 1, "xor", log
, "^", 0
738 lf
, 1, "and", log
, "&", 0
742 lf
, 1, "or", log
, "|", 0
746 lf
, 1, "not", ulog
, " ~", 0
750 lf
, 1, "neg", ulog
, " - ", 0
754 nx
, 1, "adds", adds
, "dst = srca + srcb", 0
758 nx
, 1, "subs", adds
, "srca = -srca; dst = srcb + srca", 0
762 af8
, 1, "add.b", add
, "dst = srca + srcb", 8
766 af16
, 1, "add.w", add
, "dst = srca + srcb", 16
770 af16
, 1, "sub.w", add
, "srca = -srca; dst = srcb + srca", 16
774 af8
, 1, "sub.b", add
, "srca = -srca; dst = srcb + srca", 8
778 af8
, 1, "addx", addx
, 0, 8
782 af8
, 1, "subx", subx
, 0, 8
786 af8
, 0, "cmp.b", cmp
, 0, 8
790 af16
, 0, "cmp.w", cmp
, 0, 16
794 nx
, 1, "sleep", esleep
, 0, 0
798 nx
, 0, "bpt", bpt
, 0, 8
802 nx
, 0, "divxu", divxu
, 0, 0
806 nx
, 0, "mulxu", mulxu
, 0, 0
810 mf8
, 1, "mov.b", mov
, 0, 8
814 mf8
, 1, "movtpe", mov
, 0, 8
818 mf8
, 1, "movfpe", mov
, 0, 8
822 mf16
, 1, "mov.w", mov
, 0, 16
837 printf ("%s %s %s\n", cs
, p
->name
, ce
);
839 for (i
= 0; table
[i
].name
; i
++)
841 if (strcmp (table
[i
].name
, p
->name
) == 0)
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
);
850 decode (p
, 0, table
[i
].size
);
852 printf (table
[i
].ftype
);
854 printf ("goto next;\n");
859 printf ("%s not found %s\n", cs
, ce
);
860 printf ("saved_state.exception = SIGILL;\n");
869 /* write if statements to select the right opcode */
870 struct h8_opcode
**p
;
873 p
= h8_opcodes_sorted
[i
];
874 printf ("case 0x%03x:\n", i
);
878 /* See if the next few also match */
879 while (h8_opcodes_sorted
[i
+ 1][0] == *p
)
882 printf ("case 0x%03x:\n", i
);
885 /* Don't need any if's this is the only one */
892 /* start two nibbles in since we know we match in the first byte */
901 for (c
= 0; c
< 5; c
++)
906 printf ("%s %x%x", cs
, (*p
)->data
.nib
[0], (*p
)->data
.nib
[1]);
907 while ((c
= (*p
)->data
.nib
[nib
]) != E
)
911 /* bit 3 must be zero */
912 mask0
[byte
] |= 0x8 << nibshift
;
918 /* bit 3 must be one */
919 mask1
[byte
] |= 0x8 << nibshift
;
925 mask0
[byte
] |= ((~c
) & 0xf) << nibshift
;
926 mask1
[byte
] |= (c
& 0xf) << nibshift
;
950 for (c
= 1; c
< byte
; c
++)
952 if (mask0
[c
] | mask1
[c
])
962 if (c
/ 2 == 0 && sh
== 0)
963 printf ("((b1&0x%x)==0x%x)", mask0
[c
] | mask1
[c
],
967 printf ("((pc[%d]&(0x%02x<<%d))==(0x%x<<%d))",
968 c
/ 2, mask0
[c
] | mask1
[c
], sh
,
992 for (d
= s
= h8_opcodes
; s
->name
; s
++)
996 if (strcmp (s
->name
, "push") == 0)
998 if (strcmp (s
->name
, "bhs") == 0)
1000 if (strcmp (s
->name
, "blo") == 0)
1002 if (strcmp (s
->name
, "bt") == 0)
1004 if (strcmp (s
->name
, "bf") == 0)
1006 if (strcmp (s
->name
, "pop") == 0)
1024 printf ("%s do the operation %s\n", cs
, ce
);
1025 printf ("switch (b0) \n{\n");
1026 for (i
= 0; i
< PTWO
; i
++)