Commit | Line | Data |
---|---|---|
19139515 | 1 | /* code generator for the Hitachi H8/300 architecture simulator. |
a154eea7 SC |
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 | ||
19139515 SC |
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" | |
a154eea7 SC |
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 | ||
a154eea7 SC |
37 | char *cs = "/*"; |
38 | char *ce = "*/"; | |
39 | ||
19139515 | 40 | /* How to get at nibble n from the instruction */ |
a154eea7 SC |
41 | char *nibs[] = |
42 | { | |
43 | "foo", | |
44 | "(b0&0xf)", | |
45 | "((b1>>4)&0xf)", | |
46 | "((b1)&0xf)", | |
62b66d6d SC |
47 | "((pc[1]>>12)&0xf)", |
48 | "((pc[1]>>8)&0xf)", | |
49 | "((pc[1]>>4)&0xf)", | |
50 | "((pc[1])&0xf)", | |
a154eea7 SC |
51 | 0, 0}; |
52 | ||
19139515 SC |
53 | /* how to get at the 3 bit immediate in the instruction */ |
54 | char *imm3[] = | |
55 | {"foo", | |
56 | "foo", | |
62b66d6d | 57 | "((b1>>4)&0x7)", |
19139515 SC |
58 | "foo", |
59 | "foo", | |
60 | "foo", | |
62b66d6d | 61 | "(pc[1]>>4)&0x7"}; |
19139515 SC |
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])"}; | |
a154eea7 SC |
79 | |
80 | #define sorted_key noperands | |
a154eea7 | 81 | |
19139515 SC |
82 | /* sort the opcode table into h8_opcodes_sorted[0..255] */ |
83 | static void | |
a154eea7 SC |
84 | init () |
85 | { | |
86 | unsigned int i; | |
87 | struct h8_opcode *p; | |
88 | ||
89 | for (p = h8_opcodes; p->name; p++) | |
a154eea7 | 90 | { |
19139515 SC |
91 | int n1 = 0; |
92 | int n2 = 0; | |
93 | int j; | |
a154eea7 | 94 | |
19139515 SC |
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++) | |
a154eea7 | 117 | { |
19139515 | 118 | int j = /* ((n3 >> 3) * 512) + ((n4 >> 3) * 256) + */ n1 * 16 + n2; |
a154eea7 | 119 | |
19139515 | 120 | if (h8_opcodes_sorted[j][i] == (struct h8_opcode *) NULL) |
a154eea7 | 121 | { |
19139515 SC |
122 | h8_opcodes_sorted[j][i] = p; |
123 | p->sorted_key = j; | |
a154eea7 | 124 | break; |
a154eea7 | 125 | } |
a154eea7 | 126 | } |
a154eea7 | 127 | |
19139515 SC |
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; | |
a154eea7 | 145 | } |
a154eea7 SC |
146 | } |
147 | ||
19139515 SC |
148 | /* either fetch srca&srcb or store dst */ |
149 | ||
a154eea7 SC |
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++) | |
a154eea7 | 160 | { |
19139515 | 161 | switch (p->data.nib[i]) |
a154eea7 | 162 | { |
19139515 SC |
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: | |
a154eea7 | 194 | |
19139515 SC |
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; | |
a154eea7 | 210 | |
19139515 SC |
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; | |
a154eea7 | 224 | |
19139515 SC |
225 | case MEMIND: |
226 | if (fetch) | |
227 | { | |
62b66d6d | 228 | printf ("lval = pc[1];\n"); |
19139515 SC |
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: | |
a154eea7 | 243 | |
19139515 SC |
244 | if (fetch) |
245 | printf ("srca = %s;\n", imm3[i]); | |
246 | break; | |
247 | case IMM16: | |
248 | if (fetch) | |
62b66d6d | 249 | printf ("srca =( pc[1]);\n"); |
19139515 SC |
250 | break; |
251 | case ABS8SRC: | |
252 | if (fetch) | |
253 | { | |
a154eea7 | 254 | |
19139515 SC |
255 | printf ("lval = (0xff00) + b1;\n"); |
256 | printf ("srca = BYTE_MEM(lval);\n"); | |
257 | } | |
a154eea7 | 258 | |
19139515 SC |
259 | break; |
260 | case ABS8DST: | |
261 | if (fetch) | |
a154eea7 | 262 | { |
19139515 SC |
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 | { | |
62b66d6d | 279 | printf ("lval = pc[1];\n"); |
19139515 SC |
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 | { | |
62b66d6d | 291 | printf ("lval = 0xffff&(pc[1] +reg[rn]);\n"); |
19139515 SC |
292 | printf ("srca = %s_MEM(lval);\n", size == 8 ? "BYTE" : "WORD"); |
293 | } | |
294 | break; | |
295 | case DISPDST: | |
296 | if (fetch) | |
297 | { | |
62b66d6d | 298 | printf ("lval = 0xffff&(pc[1] +reg[rn]);\n"); |
19139515 SC |
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 | { | |
62b66d6d | 309 | printf ("lval = (pc[1]);\n"); |
19139515 SC |
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 | { | |
62b66d6d | 325 | printf ("saved_state.exception = SIGILL;\n"); |
a154eea7 | 326 | } |
a154eea7 SC |
327 | } |
328 | } | |
a154eea7 SC |
329 | } |
330 | ||
19139515 SC |
331 | |
332 | static void | |
a154eea7 SC |
333 | esleep () |
334 | { | |
62b66d6d | 335 | printf ("saved_state.exception = SIGSTOP;\n"); |
a154eea7 SC |
336 | } |
337 | ||
19139515 | 338 | static void |
a154eea7 SC |
339 | mov (p, s, sz) |
340 | struct h8_opcode *p; | |
341 | char *s; | |
342 | int sz; | |
343 | { | |
19139515 | 344 | printf ("dst = srca;\n"); |
a154eea7 SC |
345 | } |
346 | ||
19139515 | 347 | static void |
a154eea7 SC |
348 | andc (p) |
349 | struct h8_opcode *p; | |
350 | { | |
351 | printf ("SET_CCR(GET_CCR() & srca);\n"); | |
352 | } | |
353 | ||
19139515 | 354 | static void |
a154eea7 SC |
355 | addx (p) |
356 | struct h8_opcode *p; | |
357 | { | |
358 | printf ("dst = srca + srcb+ (c != 0);\n"); | |
359 | } | |
360 | ||
19139515 | 361 | static void |
a154eea7 SC |
362 | subx (p) |
363 | struct h8_opcode *p; | |
364 | { | |
365 | printf ("dst = srcb - srca - (c != 0);\n"); | |
366 | } | |
367 | ||
19139515 | 368 | static void |
a154eea7 SC |
369 | add (p, s, sz) |
370 | struct h8_opcode *p; | |
371 | char *s; | |
372 | int sz; | |
373 | { | |
374 | printf ("%s;\n", s); | |
a154eea7 SC |
375 | } |
376 | ||
19139515 | 377 | static void |
a154eea7 SC |
378 | adds (p, s) |
379 | struct h8_opcode *p; | |
380 | char *s; | |
381 | { | |
382 | printf ("%s;\n", s); | |
383 | } | |
384 | ||
19139515 | 385 | static void |
a154eea7 SC |
386 | bra (p, a) |
387 | struct h8_opcode *p; | |
388 | char *a; | |
389 | { | |
62b66d6d | 390 | printf ("if (%s) npc += ((char )b1)>>1;\n", a); |
a154eea7 SC |
391 | } |
392 | ||
19139515 | 393 | static void |
a154eea7 SC |
394 | bsr (p, a) |
395 | struct h8_opcode *p; | |
396 | char *a; | |
397 | { | |
19139515 SC |
398 | printf ("reg[7]-=2;\n"); |
399 | printf ("tmp = reg[7];\n"); | |
62b66d6d | 400 | printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n"); |
2c320e35 | 401 | printf ("npc += ((char)b1)>>1;\n"); |
a154eea7 SC |
402 | } |
403 | ||
19139515 | 404 | static void |
a154eea7 SC |
405 | cmp (p, a, s) |
406 | struct h8_opcode *p; | |
407 | char *a; | |
408 | int s; | |
409 | { | |
19139515 SC |
410 | decode (p, 1, s); |
411 | printf ("srca = -srca;\n"); | |
a154eea7 SC |
412 | printf ("dst = srca + srcb;\n"); |
413 | } | |
414 | ||
19139515 | 415 | static |
a154eea7 SC |
416 | void |
417 | jsr (p, a, s) | |
418 | struct h8_opcode *p; | |
419 | char *a; | |
420 | int s; | |
421 | { | |
19139515 SC |
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"); | |
62b66d6d SC |
428 | printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n"); |
429 | printf ("npc = (lval>>1) + saved_state.mem;\n"); | |
19139515 | 430 | printf ("}"); |
a154eea7 SC |
431 | } |
432 | ||
19139515 | 433 | static void |
a154eea7 SC |
434 | jmp (p, a, s) |
435 | struct h8_opcode *p; | |
436 | char *a; | |
437 | int s; | |
438 | { | |
62b66d6d | 439 | printf ("npc = (lval>>1) + saved_state.mem;\n"); |
a154eea7 SC |
440 | } |
441 | ||
19139515 | 442 | static void |
a154eea7 SC |
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"); | |
62b66d6d SC |
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"); | |
a154eea7 SC |
464 | } |
465 | ||
19139515 | 466 | static void |
a154eea7 SC |
467 | setf (p, a, s) |
468 | struct h8_opcode *p; | |
469 | char *a; | |
470 | int s; | |
471 | { | |
472 | printf ("tmp = GET_CCR();\n"); | |
19139515 | 473 | printf ("tmp %s= srca;\n", a); |
a154eea7 SC |
474 | } |
475 | ||
19139515 | 476 | static void |
a154eea7 SC |
477 | bpt (p, a, s) |
478 | struct h8_opcode *p; | |
479 | char *a; | |
480 | int s; | |
481 | { | |
62b66d6d | 482 | printf ("saved_state.exception = SIGTRAP;\n"); |
19139515 | 483 | printf ("npc = pc;\n"); |
a154eea7 SC |
484 | } |
485 | ||
19139515 | 486 | static void |
a154eea7 SC |
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 | ||
19139515 | 495 | static void |
a154eea7 SC |
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 | ||
19139515 SC |
504 | static void |
505 | nop () | |
a154eea7 SC |
506 | { |
507 | } | |
508 | ||
19139515 SC |
509 | static void |
510 | rotl () | |
a154eea7 | 511 | { |
19139515 SC |
512 | printf ("c = srcb & 0x80;\n"); |
513 | printf ("dst = srcb << 1;\n"); | |
514 | printf ("if (c) dst|=1;\n"); | |
a154eea7 | 515 | } |
19139515 SC |
516 | |
517 | static void | |
518 | rotr () | |
a154eea7 | 519 | { |
19139515 SC |
520 | printf ("c = srcb & 1;\n"); |
521 | printf ("dst = srcb >> 1;\n"); | |
522 | printf ("if (c) dst|=0x80;\n"); | |
a154eea7 SC |
523 | } |
524 | ||
19139515 SC |
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 () | |
a154eea7 | 545 | { |
19139515 SC |
546 | printf ("c = srcb&0x80;\n"); |
547 | printf ("dst = srcb << 1;\n"); | |
a154eea7 | 548 | } |
19139515 SC |
549 | |
550 | static | |
a154eea7 | 551 | void |
19139515 | 552 | shar () |
a154eea7 | 553 | { |
19139515 SC |
554 | printf ("c = srcb&0x1;\n"); |
555 | printf ("if (srcb&0x80) dst = (srcb>>1) | 0x80;\n"); | |
556 | printf ("else dst = (srcb>>1) &~ 0x80;\n"); | |
a154eea7 SC |
557 | } |
558 | ||
19139515 SC |
559 | static |
560 | void | |
561 | shll () | |
562 | { | |
563 | printf ("c = srcb&0x80;\n"); | |
564 | printf ("dst = srcb << 1;\n"); | |
565 | } | |
a154eea7 | 566 | |
19139515 | 567 | static |
a154eea7 | 568 | void |
19139515 | 569 | shlr () |
a154eea7 | 570 | { |
19139515 SC |
571 | printf ("c = srcb&0x1;\n"); |
572 | printf ("dst = (srcb>>1) &~ 0x80;\n"); | |
a154eea7 SC |
573 | } |
574 | ||
19139515 | 575 | static |
a154eea7 | 576 | void |
19139515 | 577 | divxu () |
a154eea7 | 578 | { |
19139515 SC |
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]); | |
a154eea7 SC |
585 | } |
586 | ||
19139515 | 587 | static |
a154eea7 | 588 | void |
19139515 | 589 | mulxu () |
a154eea7 | 590 | { |
19139515 SC |
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]); | |
a154eea7 SC |
596 | } |
597 | ||
19139515 | 598 | static |
a154eea7 | 599 | void |
19139515 | 600 | inc () |
a154eea7 | 601 | { |
19139515 SC |
602 | printf ("dst = %s;\n", breg[3]); |
603 | printf ("v = (dst==0x7f);\n"); | |
604 | printf ("dst++;\n"); | |
605 | printf ("%s= dst;\n", breg[3]); | |
a154eea7 SC |
606 | } |
607 | ||
19139515 | 608 | static |
a154eea7 | 609 | void |
19139515 SC |
610 | bit (p, a, s) |
611 | struct h8_opcode *p; | |
612 | char *a; | |
613 | int s; | |
a154eea7 | 614 | { |
19139515 | 615 | printf ("%s\n", a); |
a154eea7 | 616 | } |
19139515 SC |
617 | |
618 | static | |
a154eea7 | 619 | void |
19139515 | 620 | dec () |
a154eea7 | 621 | { |
19139515 SC |
622 | printf ("dst = %s;\n", breg[3]); |
623 | printf ("v = (dst==0x80);\n"); | |
624 | printf ("dst--;\n"); | |
625 | printf ("%s = dst;\n", breg[3]); | |
a154eea7 | 626 | } |
19139515 SC |
627 | |
628 | char saf[] = "goto setflags;"; | |
a154eea7 SC |
629 | char sf[] = "goto shiftflags;"; |
630 | char af8[] = "goto aluflags8;"; | |
631 | char af16[] = "goto aluflags16;"; | |
632 | char lf[] = "goto logflags;"; | |
19139515 SC |
633 | char icf[] = "goto incflags;"; |
634 | char mf8[] = "goto movflags8;"; | |
635 | char mf16[] = "goto movflags16;"; | |
a154eea7 | 636 | char nx[] = "goto next;"; |
19139515 | 637 | |
a154eea7 SC |
638 | struct |
639 | { | |
19139515 | 640 | char *ftype; |
a154eea7 SC |
641 | int decode; |
642 | char *name; | |
643 | void (*func) (); | |
644 | char *arg; | |
645 | int size; | |
646 | ||
647 | } | |
648 | ||
a154eea7 SC |
649 | table [] = |
650 | { | |
19139515 SC |
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 } , | |
62b66d6d | 691 | { nx, 0, "rte", rte, 0, 0 } , |
19139515 SC |
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 | }; | |
a154eea7 | 726 | |
19139515 SC |
727 | static |
728 | void | |
a154eea7 SC |
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++) | |
a154eea7 | 737 | { |
19139515 SC |
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); | |
62b66d6d | 744 | printf ("npc = pc + %d;\n", p->length/2); |
19139515 SC |
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 | } | |
a154eea7 | 755 | } |
a154eea7 | 756 | printf ("%s not found %s\n", cs, ce); |
62b66d6d | 757 | printf ("saved_state.exception = SIGILL;\n"); |
a154eea7 SC |
758 | printf ("break;\n"); |
759 | } | |
760 | ||
19139515 | 761 | static |
a154eea7 SC |
762 | int |
763 | owrite (i) | |
19139515 | 764 | int i; |
a154eea7 SC |
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 | { | |
62b66d6d | 851 | int sh; |
a154eea7 SC |
852 | if (needand) |
853 | printf ("\n&&"); | |
62b66d6d SC |
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 | ||
a154eea7 SC |
864 | needand = 1; |
865 | } | |
866 | } | |
867 | printf (")\n"); | |
868 | } | |
869 | edo (*p); | |
870 | p++; | |
871 | ||
872 | } | |
873 | } | |
874 | return i; | |
875 | } | |
876 | ||
19139515 SC |
877 | static |
878 | void | |
879 | remove_dups () | |
a154eea7 | 880 | { |
19139515 SC |
881 | struct h8_opcode *s; |
882 | struct h8_opcode *d; | |
a154eea7 | 883 | |
19139515 | 884 | for (d = s = h8_opcodes; s->name; s++) |
a154eea7 | 885 | { |
19139515 SC |
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) | |
a154eea7 | 901 | { |
19139515 | 902 | *d++ = *s; |
a154eea7 | 903 | } |
a154eea7 | 904 | } |
a154eea7 SC |
905 | *d++ = *s++; |
906 | } | |
19139515 SC |
907 | |
908 | int | |
a154eea7 SC |
909 | main () |
910 | { | |
911 | int i; | |
a154eea7 | 912 | |
19139515 SC |
913 | remove_dups (); |
914 | init (); | |
a154eea7 | 915 | |
19139515 | 916 | printf ("%s do the operation %s\n", cs, ce); |
a154eea7 SC |
917 | printf ("switch (b0) \n{\n"); |
918 | for (i = 0; i < PTWO; i++) | |
919 | { | |
920 | i = owrite (i); | |
921 | } | |
922 | printf ("}\n"); | |
a154eea7 SC |
923 | |
924 | return 0; | |
925 | } |