* p1.c, p3.c, run.c, writecode.c: all used h8/300 opcodes in and
[deliverable/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[2]>>4)&0xf)",
48 "((pc[2])&0xf)",
49 "((pc[3]>>4)&0xf)",
50 "((pc[3])&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 "((pc[1]>>4)&0x7)",
58 "foo",
59 "foo",
60 "foo",
61 "((pc[3]>>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[2]<<8)|pc[3]);\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[2] << 8) | pc[3];\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[2] << 8) + pc[3]);\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[2] << 8) + pc[3] +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[2] << 8) + pc[3] +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[2] << 8) + pc[3]);\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 ("exception = SIGILL;\n");
326 }
327 }
328 }
329 }
330
331
332 static void
333 esleep ()
334 {
335 printf ("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 *)pc)[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-mem);\n");
401 printf ("npc += (((char *)pc)[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-mem);\n");
429 printf ("npc = lval + 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 + 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 = mem + WORD_MEM(tmp);\n");
451 }
452
453 static void
454 setf (p, a, s)
455 struct h8_opcode *p;
456 char *a;
457 int s;
458 {
459 printf ("tmp = GET_CCR();\n");
460 printf ("tmp %s= srca;\n", a);
461 }
462
463 static void
464 bpt (p, a, s)
465 struct h8_opcode *p;
466 char *a;
467 int s;
468 {
469 printf ("exception = SIGTRAP;\n");
470 printf ("npc = pc;\n");
471 }
472
473 static void
474 log (p, a, s)
475 struct h8_opcode *p;
476 char *a;
477 int s;
478 {
479 printf ("dst = srcb %s srca;\n", a);
480 }
481
482 static void
483 ulog (p, a, s)
484 struct h8_opcode *p;
485 char *a;
486 int s;
487 {
488 printf ("dst = %s srcb ;\n", a);
489 }
490
491 static void
492 nop ()
493 {
494 }
495
496 static void
497 rotl ()
498 {
499 printf ("c = srcb & 0x80;\n");
500 printf ("dst = srcb << 1;\n");
501 printf ("if (c) dst|=1;\n");
502 }
503
504 static void
505 rotr ()
506 {
507 printf ("c = srcb & 1;\n");
508 printf ("dst = srcb >> 1;\n");
509 printf ("if (c) dst|=0x80;\n");
510 }
511
512 static void
513 rotxl ()
514 {
515 printf ("tmp = srcb & 0x80;\n");
516 printf ("dst = srcb << 1;\n");
517 printf ("if (c) dst|=1;\n");
518 printf ("c = tmp;\n");
519 }
520
521 static void
522 rotxr ()
523 {
524 printf ("tmp = srcb & 1;\n");
525 printf ("dst = srcb >> 1;\n");
526 printf ("if (c) dst|=0x80;\n");
527 printf ("c = tmp;\n");
528 }
529
530 static void
531 shal ()
532 {
533 printf ("c = srcb&0x80;\n");
534 printf ("dst = srcb << 1;\n");
535 }
536
537 static
538 void
539 shar ()
540 {
541 printf ("c = srcb&0x1;\n");
542 printf ("if (srcb&0x80) dst = (srcb>>1) | 0x80;\n");
543 printf ("else dst = (srcb>>1) &~ 0x80;\n");
544 }
545
546 static
547 void
548 shll ()
549 {
550 printf ("c = srcb&0x80;\n");
551 printf ("dst = srcb << 1;\n");
552 }
553
554 static
555 void
556 shlr ()
557 {
558 printf ("c = srcb&0x1;\n");
559 printf ("dst = (srcb>>1) &~ 0x80;\n");
560 }
561
562 static
563 void
564 divxu ()
565 {
566 printf ("srca = %s;\n", breg[2]);
567 printf ("srcb = %s;\n", wreg[3]);
568 printf ("n = srca & 0x80;\n");
569 printf ("z = !srca;\n");
570 printf ("if (srca) dst = srcb / srca;tmp = srcb %% srca;\n");
571 printf ("%s = (dst & 0xff) | (tmp << 8);\n", wreg[3]);
572 }
573
574 static
575 void
576 mulxu ()
577 {
578 printf ("srca = %s;\n", breg[2]);
579 printf ("srcb = %s;\n", wreg[3]);
580
581 printf ("dst = (srcb&0xff) * srca;\n");
582 printf ("%s = dst;\n", wreg[3]);
583 }
584
585 static
586 void
587 inc ()
588 {
589 printf ("dst = %s;\n", breg[3]);
590 printf ("v = (dst==0x7f);\n");
591 printf ("dst++;\n");
592 printf ("%s= dst;\n", breg[3]);
593 }
594
595 static
596 void
597 bit (p, a, s)
598 struct h8_opcode *p;
599 char *a;
600 int s;
601 {
602 printf ("%s\n", a);
603 }
604
605 static
606 void
607 dec ()
608 {
609 printf ("dst = %s;\n", breg[3]);
610 printf ("v = (dst==0x80);\n");
611 printf ("dst--;\n");
612 printf ("%s = dst;\n", breg[3]);
613 }
614
615 char saf[] = "goto setflags;";
616 char sf[] = "goto shiftflags;";
617 char af8[] = "goto aluflags8;";
618 char af16[] = "goto aluflags16;";
619 char lf[] = "goto logflags;";
620 char icf[] = "goto incflags;";
621 char mf8[] = "goto movflags8;";
622 char mf16[] = "goto movflags16;";
623 char nx[] = "goto next;";
624
625 struct
626 {
627 char *ftype;
628 int decode;
629 char *name;
630 void (*func) ();
631 char *arg;
632 int size;
633
634 }
635
636 table [] =
637 {
638 { nx, 1, "bld", bit, "dst = srcb; c = (srcb>>srca)&1;", 8 } ,
639 { nx, 1, "bild", bit, "dst = srcb; c = !((srcb>>srca)&1);", 8 } ,
640 { nx, 1, "band", bit, "dst = srcb; c = C &&((srcb>>srca)&1);", 8 } ,
641 { nx, 1, "biand", bit, "dst = srcb; c = C &&(!((srcb>>srca)&1));", 8 } ,
642 { nx, 1, "bior", bit, "dst = srcb; c = C ||(!((srcb>>srca)&1));", 8 } ,
643 { nx, 1, "bor", bit, "dst = srcb; c = C ||(((srcb>>srca)&1));", 8 } ,
644 { nx, 1, "bixor", bit, "dst = srcb; c = C ^(!((srcb>>srca)&1));", 8 } ,
645 { nx, 1, "bxor", bit, "dst = srcb; c = C ^(((srcb>>srca)&1));", 8 } ,
646 { nx, 1, "bnot", bit, "dst = srcb ^ (1<<srca);", 8 } ,
647 { nx, 1, "bclr", bit, "dst = srcb & ~(1<<srca);", 8 } ,
648 { nx, 1, "bset", bit, "dst = srcb | (1<<srca);", 8 } ,
649 { nx, 1, "bst", bit, "dst = (srcb & ~(1<<srca))| ((C)<<srca);", 8 } ,
650 { nx, 1, "bist", bit, "dst = (srcb & ~(1<<srca))| ((!C)<<srca);", 8 } ,
651 { nx, 1, "btst", bit, "dst = srcb; z = !((srcb>>srca)&1);", 8 } ,
652 { icf, 0, "dec", dec, 0, 0 } ,
653 { icf, 0, "inc", inc, 0, 0 } ,
654 { saf, 1, "orc", setf, "|", 0 } ,
655 { saf, 1, "xorc", setf, "^", 0 } ,
656 { saf, 1, "andc", setf, "&", 0 } ,
657 { nx, 1, "nop", nop, 0, 0 } ,
658 { nx, 1, "bra", bra, "1", 0 } ,
659 { nx, 1, "brn", bra, "0", 0 } ,
660 { nx, 1, "bhi", bra, "(C||Z)==0", 0 } ,
661 { nx, 1, "bls", bra, "(C||Z)==1", 0 } ,
662 { nx, 1, "bcs", bra, "C==1", 0 } ,
663 { nx, 1, "bcc", bra, "C==0", 0 } ,
664 { nx, 1, "bpl", bra, "N==0", 0 } ,
665 { nx, 1, "bmi", bra, "N==1", 0 } ,
666 { nx, 1, "bvs", bra, "V==1", 0 } ,
667 { nx, 1, "bvc", bra, "V==0", 0 } ,
668 { nx, 1, "bge", bra, "(N^V)==0", 0 } ,
669 { nx, 1, "bgt", bra, "(Z|(N^V))==0", 0 } ,
670 { nx, 1, "blt", bra, "(N^V)==1", 0 } ,
671 { nx, 1, "ble", bra, "(Z|(N^V))==1", 0 } ,
672 { nx, 1, "beq", bra, "Z==1", 0 } ,
673 { nx, 1, "bne", bra, "Z==0", 0 } ,
674 { nx, 1, "bsr", bsr, "", 0 } ,
675 { nx, 1, "jsr", jsr, 0, 0 } ,
676 { nx, 1, "jmp", jmp, 0, 0 } ,
677 { nx, 0, "rts", rts, 0, 0 } ,
678 { nx, 1, "andc", andc, 0, 0 } ,
679 { sf, 1, "shal", shal, 0, 0 } ,
680 { sf, 1, "shar", shar, 0, 0 } ,
681 { sf, 1, "shll", shll, 0, 0 } ,
682 { sf, 1, "shlr", shlr, 0, 0 } ,
683 { sf, 1, "rotxl", rotxl, 0, 0 } ,
684 { sf, 1, "rotxr", rotxr, 0, 0 } ,
685 { sf, 1, "rotl", rotl, 0, 0 } ,
686 { sf, 1, "rotr", rotr, 0, 0 } ,
687 { lf, 1, "xor", log, "^", 0 } ,
688 { lf, 1, "and", log, "&", 0 } ,
689 { lf, 1, "or", log, "|", 0 } ,
690 { lf, 1, "not", ulog, " ~", 0 } ,
691 { lf, 1, "neg", ulog, " - ", 0 } ,
692 { nx, 1, "adds", adds, "dst = srca + srcb", 0 } ,
693 { nx, 1, "subs", adds, "srca = -srca; dst = srcb + srca", 0 } ,
694 { af8, 1, "add.b", add, "dst = srca + srcb", 8 } ,
695 { af16, 1, "add.w", add, "dst = srca + srcb", 16 } ,
696 { af16, 1, "sub.w", add, "srca = -srca; dst = srcb + srca", 16 } ,
697 { af8, 1, "sub.b", add, "srca = -srca; dst = srcb + srca", 8 } ,
698 { af8, 1, "addx", addx, 0, 8 } ,
699 { af8, 1, "subx", subx, 0, 8 } ,
700 { af8, 0, "cmp.b", cmp, 0, 8 } ,
701 { af16, 0, "cmp.w", cmp, 0, 16 } ,
702 { nx, 1, "sleep", esleep, 0, 0 } ,
703 { nx, 0, "bpt", bpt, 0, 8 } ,
704 { nx, 0, "divxu", divxu, 0, 0 } ,
705 { nx, 0, "mulxu", mulxu, 0, 0 } ,
706 { mf8, 1, "mov.b", mov, 0, 8 } ,
707 { mf8, 1, "movtpe", mov, 0, 8 } ,
708 { mf8, 1, "movfpe", mov, 0, 8 } ,
709 { mf16, 1, "mov.w", mov, 0, 16 } ,
710 { 0 }
711 };
712
713 static
714 void
715 edo (p)
716 struct h8_opcode *p;
717 {
718 int i;
719
720 printf ("%s %s %s\n", cs, p->name, ce);
721
722 for (i = 0; table[i].name; i++)
723 {
724 if (strcmp (table[i].name, p->name) == 0)
725 {
726 printf ("{\n");
727 if (table[i].decode)
728 decode (p, 1, table[i].size);
729 printf ("cycles += %d;\n", p->time);
730 printf ("npc = pc + %d;\n", p->length);
731 table[i].func (p, table[i].arg, table[i].size);
732 if (table[i].decode)
733 decode (p, 0, table[i].size);
734 if (table[i].ftype)
735 printf (table[i].ftype);
736 else
737 printf ("goto next;\n");
738 printf ("}\n");
739 return;
740 }
741 }
742 printf ("%s not found %s\n", cs, ce);
743 printf ("exception = SIGILL;\n");
744 printf ("break;\n");
745 }
746
747 static
748 int
749 owrite (i)
750 int i;
751 {
752 /* write if statements to select the right opcode */
753 struct h8_opcode **p;
754 int needand = 1;
755
756 p = h8_opcodes_sorted[i];
757 printf ("case 0x%03x:\n", i);
758
759 if (p[1] == 0)
760 {
761 /* See if the next few also match */
762 while (h8_opcodes_sorted[i + 1][0] == *p)
763 {
764 i++;
765 printf ("case 0x%03x:\n", i);
766 }
767
768 /* Dont need any if's this is the only one */
769 edo (*p);
770 }
771 else
772 {
773 while (*p)
774 {
775 /* start two nibbles in since we know we match in the first byte */
776 int c;
777 int nib = 2;
778 int byte = 1;
779 int mask1[5];
780 int mask0[5];
781 int nibshift = 4;
782 int any = 0;
783
784 for (c = 0; c < 5; c++)
785 {
786 mask1[c] = 0;
787 mask0[c] = 0;
788 }
789 printf ("%s %x%x", cs, (*p)->data.nib[0], (*p)->data.nib[1]);
790 while ((c = (*p)->data.nib[nib]) != E)
791 {
792 if (c & B30)
793 {
794 /* bit 3 must be zero */
795 mask0[byte] |= 0x8 << nibshift;
796 printf ("0");
797 any = 1;
798 }
799 else if (c & B31)
800 {
801 /* bit 3 must be one */
802 mask1[byte] |= 0x8 << nibshift;
803 printf ("8");
804 any = 1;
805 }
806 else if (c <= HexF)
807 {
808 mask0[byte] |= ((~c) & 0xf) << nibshift;
809 mask1[byte] |= (c & 0xf) << nibshift;
810 printf ("%x", c);
811 any = 1;
812 }
813 else
814 {
815 printf ("x");
816 }
817 nib++;
818 if (nibshift == 4)
819 {
820 nibshift = 0;
821 }
822 else
823 {
824 byte++;
825 nibshift = 4;
826 }
827 }
828 printf ("*/\n");
829 if (any)
830 {
831 printf ("if (");
832 needand = 0;
833 for (c = 1; c < byte; c++)
834 {
835 if (mask0[c] | mask1[c])
836 {
837 if (needand)
838 printf ("\n&&");
839 printf ("((pc[%d]&0x%02x)==0x%x)",
840 c, mask0[c] | mask1[c], mask1[c]);
841 needand = 1;
842 }
843 }
844 printf (")\n");
845 }
846 edo (*p);
847 p++;
848
849 }
850 }
851 return i;
852 }
853
854 static
855 void
856 remove_dups ()
857 {
858 struct h8_opcode *s;
859 struct h8_opcode *d;
860
861 for (d = s = h8_opcodes; s->name; s++)
862 {
863 int doit = 1;
864
865 if (strcmp (s->name, "push") == 0)
866 doit = 0;
867 if (strcmp (s->name, "bhs") == 0)
868 doit = 0;
869 if (strcmp (s->name, "blo") == 0)
870 doit = 0;
871 if (strcmp (s->name, "bt") == 0)
872 doit = 0;
873 if (strcmp (s->name, "bf") == 0)
874 doit = 0;
875 if (strcmp (s->name, "pop") == 0)
876 doit = 0;
877 if (doit)
878 {
879 *d++ = *s;
880 }
881 }
882 *d++ = *s++;
883 }
884
885 int
886 main ()
887 {
888 int i;
889
890 remove_dups ();
891 init ();
892
893 printf ("%s do the operation %s\n", cs, ce);
894 printf ("switch (b0) \n{\n");
895 for (i = 0; i < PTWO; i++)
896 {
897 i = owrite (i);
898 }
899 printf ("}\n");
900
901 return 0;
902 }
This page took 0.063876 seconds and 5 git commands to generate.