Commit | Line | Data |
---|---|---|
19139515 | 1 | /* code generator for the Hitachi H8/300 architecture simulator. |
b0c9f026 | 2 | Copyright (C) 1993 Free Software Foundation, Inc. |
a154eea7 SC |
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 | 331 | static void |
a154eea7 SC |
332 | esleep () |
333 | { | |
62b66d6d | 334 | printf ("saved_state.exception = SIGSTOP;\n"); |
a154eea7 SC |
335 | } |
336 | ||
19139515 | 337 | static void |
a154eea7 SC |
338 | mov (p, s, sz) |
339 | struct h8_opcode *p; | |
340 | char *s; | |
341 | int sz; | |
342 | { | |
19139515 | 343 | printf ("dst = srca;\n"); |
a154eea7 SC |
344 | } |
345 | ||
19139515 | 346 | static void |
a154eea7 SC |
347 | andc (p) |
348 | struct h8_opcode *p; | |
349 | { | |
350 | printf ("SET_CCR(GET_CCR() & srca);\n"); | |
351 | } | |
352 | ||
19139515 | 353 | static void |
a154eea7 SC |
354 | addx (p) |
355 | struct h8_opcode *p; | |
356 | { | |
357 | printf ("dst = srca + srcb+ (c != 0);\n"); | |
358 | } | |
359 | ||
19139515 | 360 | static void |
a154eea7 SC |
361 | subx (p) |
362 | struct h8_opcode *p; | |
363 | { | |
364 | printf ("dst = srcb - srca - (c != 0);\n"); | |
365 | } | |
366 | ||
19139515 | 367 | static void |
a154eea7 SC |
368 | add (p, s, sz) |
369 | struct h8_opcode *p; | |
370 | char *s; | |
371 | int sz; | |
372 | { | |
373 | printf ("%s;\n", s); | |
a154eea7 SC |
374 | } |
375 | ||
19139515 | 376 | static void |
a154eea7 SC |
377 | adds (p, s) |
378 | struct h8_opcode *p; | |
379 | char *s; | |
380 | { | |
381 | printf ("%s;\n", s); | |
382 | } | |
383 | ||
19139515 | 384 | static void |
a154eea7 SC |
385 | bra (p, a) |
386 | struct h8_opcode *p; | |
387 | char *a; | |
388 | { | |
62b66d6d | 389 | printf ("if (%s) npc += ((char )b1)>>1;\n", a); |
a154eea7 SC |
390 | } |
391 | ||
19139515 | 392 | static void |
a154eea7 SC |
393 | bsr (p, a) |
394 | struct h8_opcode *p; | |
395 | char *a; | |
396 | { | |
19139515 SC |
397 | printf ("reg[7]-=2;\n"); |
398 | printf ("tmp = reg[7];\n"); | |
62b66d6d | 399 | printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n"); |
2c320e35 | 400 | printf ("npc += ((char)b1)>>1;\n"); |
a154eea7 SC |
401 | } |
402 | ||
19139515 | 403 | static void |
a154eea7 SC |
404 | cmp (p, a, s) |
405 | struct h8_opcode *p; | |
406 | char *a; | |
407 | int s; | |
408 | { | |
19139515 SC |
409 | decode (p, 1, s); |
410 | printf ("srca = -srca;\n"); | |
a154eea7 SC |
411 | printf ("dst = srca + srcb;\n"); |
412 | } | |
413 | ||
19139515 | 414 | static |
a154eea7 SC |
415 | void |
416 | jsr (p, a, s) | |
417 | struct h8_opcode *p; | |
418 | char *a; | |
419 | int s; | |
420 | { | |
19139515 SC |
421 | printf ("if (b1 == 0xc4) {\n"); |
422 | printf ("printf(\"%%c\", reg[2]);\n"); | |
423 | printf ("}\n"); | |
424 | printf ("else {\n"); | |
425 | printf ("reg[7]-=2;\n"); | |
426 | printf ("tmp = reg[7];\n"); | |
62b66d6d SC |
427 | printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n"); |
428 | printf ("npc = (lval>>1) + saved_state.mem;\n"); | |
19139515 | 429 | printf ("}"); |
a154eea7 SC |
430 | } |
431 | ||
19139515 | 432 | static void |
a154eea7 SC |
433 | jmp (p, a, s) |
434 | struct h8_opcode *p; | |
435 | char *a; | |
436 | int s; | |
437 | { | |
62b66d6d | 438 | printf ("npc = (lval>>1) + saved_state.mem;\n"); |
a154eea7 SC |
439 | } |
440 | ||
19139515 | 441 | static void |
a154eea7 SC |
442 | rts (p, a, s) |
443 | struct h8_opcode *p; | |
444 | char *a; | |
445 | int s; | |
446 | { | |
447 | printf ("tmp = reg[7];\n"); | |
448 | printf ("reg[7]+=2;\n"); | |
62b66d6d SC |
449 | printf ("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n"); |
450 | } | |
451 | ||
452 | static void | |
453 | rte (p, a, s) | |
454 | struct h8_opcode *p; | |
455 | char *a; | |
456 | int s; | |
457 | { | |
458 | printf ("reg[7]+=2;\n"); | |
459 | printf ("tmp = reg[7];\n"); | |
460 | printf ("reg[7]+=2;\n"); | |
461 | printf ("SET_CCR(tmp);\n"); | |
b0c9f026 | 462 | printf ("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n"); |
a154eea7 SC |
463 | } |
464 | ||
19139515 | 465 | static void |
a154eea7 SC |
466 | setf (p, a, s) |
467 | struct h8_opcode *p; | |
468 | char *a; | |
469 | int s; | |
470 | { | |
471 | printf ("tmp = GET_CCR();\n"); | |
19139515 | 472 | printf ("tmp %s= srca;\n", a); |
a154eea7 SC |
473 | } |
474 | ||
19139515 | 475 | static void |
a154eea7 SC |
476 | bpt (p, a, s) |
477 | struct h8_opcode *p; | |
478 | char *a; | |
479 | int s; | |
480 | { | |
62b66d6d | 481 | printf ("saved_state.exception = SIGTRAP;\n"); |
19139515 | 482 | printf ("npc = pc;\n"); |
a154eea7 SC |
483 | } |
484 | ||
19139515 | 485 | static void |
a154eea7 SC |
486 | log (p, a, s) |
487 | struct h8_opcode *p; | |
488 | char *a; | |
489 | int s; | |
490 | { | |
491 | printf ("dst = srcb %s srca;\n", a); | |
492 | } | |
493 | ||
19139515 | 494 | static void |
a154eea7 SC |
495 | ulog (p, a, s) |
496 | struct h8_opcode *p; | |
497 | char *a; | |
498 | int s; | |
499 | { | |
500 | printf ("dst = %s srcb ;\n", a); | |
501 | } | |
502 | ||
19139515 SC |
503 | static void |
504 | nop () | |
a154eea7 SC |
505 | { |
506 | } | |
507 | ||
19139515 SC |
508 | static void |
509 | rotl () | |
a154eea7 | 510 | { |
19139515 SC |
511 | printf ("c = srcb & 0x80;\n"); |
512 | printf ("dst = srcb << 1;\n"); | |
513 | printf ("if (c) dst|=1;\n"); | |
a154eea7 | 514 | } |
19139515 SC |
515 | |
516 | static void | |
517 | rotr () | |
a154eea7 | 518 | { |
19139515 SC |
519 | printf ("c = srcb & 1;\n"); |
520 | printf ("dst = srcb >> 1;\n"); | |
521 | printf ("if (c) dst|=0x80;\n"); | |
a154eea7 SC |
522 | } |
523 | ||
19139515 SC |
524 | static void |
525 | rotxl () | |
526 | { | |
527 | printf ("tmp = srcb & 0x80;\n"); | |
528 | printf ("dst = srcb << 1;\n"); | |
529 | printf ("if (c) dst|=1;\n"); | |
530 | printf ("c = tmp;\n"); | |
531 | } | |
532 | ||
533 | static void | |
534 | rotxr () | |
535 | { | |
536 | printf ("tmp = srcb & 1;\n"); | |
537 | printf ("dst = srcb >> 1;\n"); | |
538 | printf ("if (c) dst|=0x80;\n"); | |
539 | printf ("c = tmp;\n"); | |
540 | } | |
541 | ||
542 | static void | |
543 | shal () | |
a154eea7 | 544 | { |
19139515 SC |
545 | printf ("c = srcb&0x80;\n"); |
546 | printf ("dst = srcb << 1;\n"); | |
a154eea7 | 547 | } |
19139515 SC |
548 | |
549 | static | |
a154eea7 | 550 | void |
19139515 | 551 | shar () |
a154eea7 | 552 | { |
19139515 SC |
553 | printf ("c = srcb&0x1;\n"); |
554 | printf ("if (srcb&0x80) dst = (srcb>>1) | 0x80;\n"); | |
555 | printf ("else dst = (srcb>>1) &~ 0x80;\n"); | |
a154eea7 SC |
556 | } |
557 | ||
19139515 SC |
558 | static |
559 | void | |
560 | shll () | |
561 | { | |
562 | printf ("c = srcb&0x80;\n"); | |
563 | printf ("dst = srcb << 1;\n"); | |
564 | } | |
a154eea7 | 565 | |
19139515 | 566 | static |
a154eea7 | 567 | void |
19139515 | 568 | shlr () |
a154eea7 | 569 | { |
19139515 SC |
570 | printf ("c = srcb&0x1;\n"); |
571 | printf ("dst = (srcb>>1) &~ 0x80;\n"); | |
a154eea7 SC |
572 | } |
573 | ||
19139515 | 574 | static |
a154eea7 | 575 | void |
19139515 | 576 | divxu () |
a154eea7 | 577 | { |
19139515 SC |
578 | printf ("srca = %s;\n", breg[2]); |
579 | printf ("srcb = %s;\n", wreg[3]); | |
580 | printf ("n = srca & 0x80;\n"); | |
581 | printf ("z = !srca;\n"); | |
582 | printf ("if (srca) dst = srcb / srca;tmp = srcb %% srca;\n"); | |
583 | printf ("%s = (dst & 0xff) | (tmp << 8);\n", wreg[3]); | |
a154eea7 SC |
584 | } |
585 | ||
19139515 | 586 | static |
a154eea7 | 587 | void |
19139515 | 588 | mulxu () |
a154eea7 | 589 | { |
19139515 SC |
590 | printf ("srca = %s;\n", breg[2]); |
591 | printf ("srcb = %s;\n", wreg[3]); | |
592 | ||
593 | printf ("dst = (srcb&0xff) * srca;\n"); | |
594 | printf ("%s = dst;\n", wreg[3]); | |
a154eea7 SC |
595 | } |
596 | ||
19139515 | 597 | static |
a154eea7 | 598 | void |
19139515 | 599 | inc () |
a154eea7 | 600 | { |
19139515 SC |
601 | printf ("dst = %s;\n", breg[3]); |
602 | printf ("v = (dst==0x7f);\n"); | |
603 | printf ("dst++;\n"); | |
604 | printf ("%s= dst;\n", breg[3]); | |
a154eea7 SC |
605 | } |
606 | ||
19139515 | 607 | static |
a154eea7 | 608 | void |
19139515 SC |
609 | bit (p, a, s) |
610 | struct h8_opcode *p; | |
611 | char *a; | |
612 | int s; | |
a154eea7 | 613 | { |
19139515 | 614 | printf ("%s\n", a); |
a154eea7 | 615 | } |
19139515 SC |
616 | |
617 | static | |
a154eea7 | 618 | void |
19139515 | 619 | dec () |
a154eea7 | 620 | { |
19139515 SC |
621 | printf ("dst = %s;\n", breg[3]); |
622 | printf ("v = (dst==0x80);\n"); | |
623 | printf ("dst--;\n"); | |
624 | printf ("%s = dst;\n", breg[3]); | |
a154eea7 | 625 | } |
19139515 SC |
626 | |
627 | char saf[] = "goto setflags;"; | |
a154eea7 SC |
628 | char sf[] = "goto shiftflags;"; |
629 | char af8[] = "goto aluflags8;"; | |
630 | char af16[] = "goto aluflags16;"; | |
631 | char lf[] = "goto logflags;"; | |
19139515 SC |
632 | char icf[] = "goto incflags;"; |
633 | char mf8[] = "goto movflags8;"; | |
634 | char mf16[] = "goto movflags16;"; | |
a154eea7 | 635 | char nx[] = "goto next;"; |
19139515 | 636 | |
a154eea7 SC |
637 | struct |
638 | { | |
19139515 | 639 | char *ftype; |
a154eea7 SC |
640 | int decode; |
641 | char *name; | |
642 | void (*func) (); | |
643 | char *arg; | |
644 | int size; | |
645 | ||
646 | } | |
647 | ||
a154eea7 SC |
648 | table [] = |
649 | { | |
b0c9f026 SC |
650 | { |
651 | nx, 1, "bld", bit, "dst = srcb; c = (srcb>>srca)&1;", 8 | |
652 | } | |
653 | , | |
654 | { | |
655 | nx, 1, "bild", bit, "dst = srcb; c = !((srcb>>srca)&1);", 8 | |
656 | } | |
657 | , | |
658 | { | |
659 | nx, 1, "band", bit, "dst = srcb; c = C &&((srcb>>srca)&1);", 8 | |
660 | } | |
661 | , | |
662 | { | |
663 | nx, 1, "biand", bit, "dst = srcb; c = C &&(!((srcb>>srca)&1));", 8 | |
664 | } | |
665 | , | |
666 | { | |
667 | nx, 1, "bior", bit, "dst = srcb; c = C ||(!((srcb>>srca)&1));", 8 | |
668 | } | |
669 | , | |
670 | { | |
671 | nx, 1, "bor", bit, "dst = srcb; c = C ||(((srcb>>srca)&1));", 8 | |
672 | } | |
673 | , | |
674 | { | |
675 | nx, 1, "bixor", bit, "dst = srcb; c = C ^(!((srcb>>srca)&1));", 8 | |
676 | } | |
677 | , | |
678 | { | |
679 | nx, 1, "bxor", bit, "dst = srcb; c = C ^(((srcb>>srca)&1));", 8 | |
680 | } | |
681 | , | |
682 | { | |
683 | nx, 1, "bnot", bit, "dst = srcb ^ (1<<srca);", 8 | |
684 | } | |
685 | , | |
686 | { | |
687 | nx, 1, "bclr", bit, "dst = srcb & ~(1<<srca);", 8 | |
688 | } | |
689 | , | |
690 | { | |
691 | nx, 1, "bset", bit, "dst = srcb | (1<<srca);", 8 | |
692 | } | |
693 | , | |
694 | { | |
695 | nx, 1, "bst", bit, "dst = (srcb & ~(1<<srca))| ((C)<<srca);", 8 | |
696 | } | |
697 | , | |
698 | { | |
699 | nx, 1, "bist", bit, "dst = (srcb & ~(1<<srca))| ((!C)<<srca);", 8 | |
700 | } | |
701 | , | |
702 | { | |
703 | nx, 1, "btst", bit, "dst = srcb; z = !((srcb>>srca)&1);", 8 | |
704 | } | |
705 | , | |
706 | { | |
707 | icf, 0, "dec", dec, 0, 0 | |
708 | } | |
709 | , | |
710 | { | |
711 | icf, 0, "inc", inc, 0, 0 | |
712 | } | |
713 | , | |
714 | { | |
715 | saf, 1, "orc", setf, "|", 0 | |
716 | } | |
717 | , | |
718 | { | |
719 | saf, 1, "xorc", setf, "^", 0 | |
720 | } | |
721 | , | |
722 | { | |
723 | saf, 1, "andc", setf, "&", 0 | |
724 | } | |
725 | , | |
726 | { | |
727 | nx, 1, "nop", nop, 0, 0 | |
728 | } | |
729 | , | |
730 | { | |
731 | nx, 1, "bra", bra, "1", 0 | |
732 | } | |
733 | , | |
734 | { | |
735 | nx, 1, "brn", bra, "0", 0 | |
736 | } | |
737 | , | |
738 | { | |
739 | nx, 1, "bhi", bra, "(C||Z)==0", 0 | |
740 | } | |
741 | , | |
742 | { | |
743 | nx, 1, "bls", bra, "(C||Z)==1", 0 | |
744 | } | |
745 | , | |
746 | { | |
747 | nx, 1, "bcs", bra, "C==1", 0 | |
748 | } | |
749 | , | |
750 | { | |
751 | nx, 1, "bcc", bra, "C==0", 0 | |
752 | } | |
753 | , | |
754 | { | |
755 | nx, 1, "bpl", bra, "N==0", 0 | |
756 | } | |
757 | , | |
758 | { | |
759 | nx, 1, "bmi", bra, "N==1", 0 | |
760 | } | |
761 | , | |
762 | { | |
763 | nx, 1, "bvs", bra, "V==1", 0 | |
764 | } | |
765 | , | |
766 | { | |
767 | nx, 1, "bvc", bra, "V==0", 0 | |
768 | } | |
769 | , | |
770 | { | |
771 | nx, 1, "bge", bra, "(N^V)==0", 0 | |
772 | } | |
773 | , | |
774 | { | |
775 | nx, 1, "bgt", bra, "(Z|(N^V))==0", 0 | |
776 | } | |
777 | , | |
778 | { | |
779 | nx, 1, "blt", bra, "(N^V)==1", 0 | |
780 | } | |
781 | , | |
782 | { | |
783 | nx, 1, "ble", bra, "(Z|(N^V))==1", 0 | |
784 | } | |
785 | , | |
786 | { | |
787 | nx, 1, "beq", bra, "Z==1", 0 | |
788 | } | |
789 | , | |
790 | { | |
791 | nx, 1, "bne", bra, "Z==0", 0 | |
792 | } | |
793 | , | |
794 | { | |
795 | nx, 1, "bsr", bsr, "", 0 | |
796 | } | |
797 | , | |
798 | { | |
799 | nx, 1, "jsr", jsr, 0, 0 | |
800 | } | |
801 | , | |
802 | { | |
803 | nx, 1, "jmp", jmp, 0, 0 | |
804 | } | |
805 | , | |
806 | { | |
807 | nx, 0, "rts", rts, 0, 0 | |
808 | } | |
809 | , | |
810 | { | |
811 | nx, 0, "rte", rte, 0, 0 | |
812 | } | |
813 | , | |
814 | { | |
815 | nx, 1, "andc", andc, 0, 0 | |
816 | } | |
817 | , | |
818 | { | |
819 | sf, 1, "shal", shal, 0, 0 | |
820 | } | |
821 | , | |
822 | { | |
823 | sf, 1, "shar", shar, 0, 0 | |
824 | } | |
825 | , | |
826 | { | |
827 | sf, 1, "shll", shll, 0, 0 | |
828 | } | |
829 | , | |
830 | { | |
831 | sf, 1, "shlr", shlr, 0, 0 | |
832 | } | |
833 | , | |
834 | { | |
835 | sf, 1, "rotxl", rotxl, 0, 0 | |
836 | } | |
837 | , | |
838 | { | |
839 | sf, 1, "rotxr", rotxr, 0, 0 | |
840 | } | |
841 | , | |
842 | { | |
843 | sf, 1, "rotl", rotl, 0, 0 | |
844 | } | |
845 | , | |
846 | { | |
847 | sf, 1, "rotr", rotr, 0, 0 | |
848 | } | |
849 | , | |
850 | { | |
851 | lf, 1, "xor", log, "^", 0 | |
852 | } | |
853 | , | |
854 | { | |
855 | lf, 1, "and", log, "&", 0 | |
856 | } | |
857 | , | |
858 | { | |
859 | lf, 1, "or", log, "|", 0 | |
860 | } | |
861 | , | |
862 | { | |
863 | lf, 1, "not", ulog, " ~", 0 | |
864 | } | |
865 | , | |
866 | { | |
867 | lf, 1, "neg", ulog, " - ", 0 | |
868 | } | |
869 | , | |
870 | { | |
871 | nx, 1, "adds", adds, "dst = srca + srcb", 0 | |
872 | } | |
873 | , | |
874 | { | |
875 | nx, 1, "subs", adds, "srca = -srca; dst = srcb + srca", 0 | |
876 | } | |
877 | , | |
878 | { | |
879 | af8, 1, "add.b", add, "dst = srca + srcb", 8 | |
880 | } | |
881 | , | |
882 | { | |
883 | af16, 1, "add.w", add, "dst = srca + srcb", 16 | |
884 | } | |
885 | , | |
886 | { | |
887 | af16, 1, "sub.w", add, "srca = -srca; dst = srcb + srca", 16 | |
888 | } | |
889 | , | |
890 | { | |
891 | af8, 1, "sub.b", add, "srca = -srca; dst = srcb + srca", 8 | |
892 | } | |
893 | , | |
894 | { | |
895 | af8, 1, "addx", addx, 0, 8 | |
896 | } | |
897 | , | |
898 | { | |
899 | af8, 1, "subx", subx, 0, 8 | |
900 | } | |
901 | , | |
902 | { | |
903 | af8, 0, "cmp.b", cmp, 0, 8 | |
904 | } | |
905 | , | |
906 | { | |
907 | af16, 0, "cmp.w", cmp, 0, 16 | |
908 | } | |
909 | , | |
910 | { | |
911 | nx, 1, "sleep", esleep, 0, 0 | |
912 | } | |
913 | , | |
914 | { | |
915 | nx, 0, "bpt", bpt, 0, 8 | |
916 | } | |
917 | , | |
918 | { | |
919 | nx, 0, "divxu", divxu, 0, 0 | |
920 | } | |
921 | , | |
922 | { | |
923 | nx, 0, "mulxu", mulxu, 0, 0 | |
924 | } | |
925 | , | |
926 | { | |
927 | mf8, 1, "mov.b", mov, 0, 8 | |
928 | } | |
929 | , | |
930 | { | |
931 | mf8, 1, "movtpe", mov, 0, 8 | |
932 | } | |
933 | , | |
934 | { | |
935 | mf8, 1, "movfpe", mov, 0, 8 | |
936 | } | |
937 | , | |
938 | { | |
939 | mf16, 1, "mov.w", mov, 0, 16 | |
940 | } | |
941 | , | |
942 | { | |
943 | 0 | |
944 | } | |
19139515 | 945 | }; |
a154eea7 | 946 | |
19139515 SC |
947 | static |
948 | void | |
a154eea7 SC |
949 | edo (p) |
950 | struct h8_opcode *p; | |
951 | { | |
952 | int i; | |
953 | ||
954 | printf ("%s %s %s\n", cs, p->name, ce); | |
955 | ||
956 | for (i = 0; table[i].name; i++) | |
a154eea7 | 957 | { |
19139515 SC |
958 | if (strcmp (table[i].name, p->name) == 0) |
959 | { | |
960 | printf ("{\n"); | |
961 | if (table[i].decode) | |
962 | decode (p, 1, table[i].size); | |
963 | printf ("cycles += %d;\n", p->time); | |
b0c9f026 | 964 | printf ("npc = pc + %d;\n", p->length / 2); |
19139515 SC |
965 | table[i].func (p, table[i].arg, table[i].size); |
966 | if (table[i].decode) | |
967 | decode (p, 0, table[i].size); | |
968 | if (table[i].ftype) | |
969 | printf (table[i].ftype); | |
970 | else | |
971 | printf ("goto next;\n"); | |
972 | printf ("}\n"); | |
973 | return; | |
974 | } | |
a154eea7 | 975 | } |
a154eea7 | 976 | printf ("%s not found %s\n", cs, ce); |
62b66d6d | 977 | printf ("saved_state.exception = SIGILL;\n"); |
a154eea7 SC |
978 | printf ("break;\n"); |
979 | } | |
980 | ||
19139515 | 981 | static |
a154eea7 SC |
982 | int |
983 | owrite (i) | |
19139515 | 984 | int i; |
a154eea7 SC |
985 | { |
986 | /* write if statements to select the right opcode */ | |
987 | struct h8_opcode **p; | |
988 | int needand = 1; | |
989 | ||
990 | p = h8_opcodes_sorted[i]; | |
991 | printf ("case 0x%03x:\n", i); | |
992 | ||
993 | if (p[1] == 0) | |
994 | { | |
995 | /* See if the next few also match */ | |
996 | while (h8_opcodes_sorted[i + 1][0] == *p) | |
997 | { | |
998 | i++; | |
999 | printf ("case 0x%03x:\n", i); | |
1000 | } | |
1001 | ||
1002 | /* Dont need any if's this is the only one */ | |
1003 | edo (*p); | |
1004 | } | |
1005 | else | |
1006 | { | |
1007 | while (*p) | |
1008 | { | |
1009 | /* start two nibbles in since we know we match in the first byte */ | |
1010 | int c; | |
1011 | int nib = 2; | |
1012 | int byte = 1; | |
1013 | int mask1[5]; | |
1014 | int mask0[5]; | |
1015 | int nibshift = 4; | |
1016 | int any = 0; | |
1017 | ||
1018 | for (c = 0; c < 5; c++) | |
1019 | { | |
1020 | mask1[c] = 0; | |
1021 | mask0[c] = 0; | |
1022 | } | |
1023 | printf ("%s %x%x", cs, (*p)->data.nib[0], (*p)->data.nib[1]); | |
1024 | while ((c = (*p)->data.nib[nib]) != E) | |
1025 | { | |
1026 | if (c & B30) | |
1027 | { | |
1028 | /* bit 3 must be zero */ | |
1029 | mask0[byte] |= 0x8 << nibshift; | |
1030 | printf ("0"); | |
1031 | any = 1; | |
1032 | } | |
1033 | else if (c & B31) | |
1034 | { | |
1035 | /* bit 3 must be one */ | |
1036 | mask1[byte] |= 0x8 << nibshift; | |
1037 | printf ("8"); | |
1038 | any = 1; | |
1039 | } | |
1040 | else if (c <= HexF) | |
1041 | { | |
1042 | mask0[byte] |= ((~c) & 0xf) << nibshift; | |
1043 | mask1[byte] |= (c & 0xf) << nibshift; | |
1044 | printf ("%x", c); | |
1045 | any = 1; | |
1046 | } | |
1047 | else | |
1048 | { | |
1049 | printf ("x"); | |
1050 | } | |
1051 | nib++; | |
1052 | if (nibshift == 4) | |
1053 | { | |
1054 | nibshift = 0; | |
1055 | } | |
1056 | else | |
1057 | { | |
1058 | byte++; | |
1059 | nibshift = 4; | |
1060 | } | |
1061 | } | |
1062 | printf ("*/\n"); | |
1063 | if (any) | |
1064 | { | |
1065 | printf ("if ("); | |
1066 | needand = 0; | |
1067 | for (c = 1; c < byte; c++) | |
1068 | { | |
1069 | if (mask0[c] | mask1[c]) | |
1070 | { | |
62b66d6d | 1071 | int sh; |
b0c9f026 | 1072 | |
a154eea7 SC |
1073 | if (needand) |
1074 | printf ("\n&&"); | |
b0c9f026 SC |
1075 | if (c & 1) |
1076 | sh = 0; | |
1077 | else | |
1078 | sh = 8; | |
1079 | if (c / 2 == 0 && sh == 0) | |
1080 | printf ("((b1&0x%x)==0x%x)", mask0[c] | mask1[c], | |
1081 | mask1[c]); | |
1082 | else | |
1083 | { | |
1084 | printf ("((pc[%d]&(0x%02x<<%d))==(0x%x<<%d))", | |
1085 | c / 2, mask0[c] | mask1[c], sh, | |
1086 | mask1[c], sh); | |
1087 | } | |
62b66d6d | 1088 | |
a154eea7 SC |
1089 | needand = 1; |
1090 | } | |
1091 | } | |
1092 | printf (")\n"); | |
1093 | } | |
1094 | edo (*p); | |
1095 | p++; | |
1096 | ||
1097 | } | |
1098 | } | |
1099 | return i; | |
1100 | } | |
1101 | ||
19139515 SC |
1102 | static |
1103 | void | |
1104 | remove_dups () | |
a154eea7 | 1105 | { |
19139515 SC |
1106 | struct h8_opcode *s; |
1107 | struct h8_opcode *d; | |
a154eea7 | 1108 | |
19139515 | 1109 | for (d = s = h8_opcodes; s->name; s++) |
a154eea7 | 1110 | { |
19139515 SC |
1111 | int doit = 1; |
1112 | ||
1113 | if (strcmp (s->name, "push") == 0) | |
1114 | doit = 0; | |
1115 | if (strcmp (s->name, "bhs") == 0) | |
1116 | doit = 0; | |
1117 | if (strcmp (s->name, "blo") == 0) | |
1118 | doit = 0; | |
1119 | if (strcmp (s->name, "bt") == 0) | |
1120 | doit = 0; | |
1121 | if (strcmp (s->name, "bf") == 0) | |
1122 | doit = 0; | |
1123 | if (strcmp (s->name, "pop") == 0) | |
1124 | doit = 0; | |
1125 | if (doit) | |
a154eea7 | 1126 | { |
19139515 | 1127 | *d++ = *s; |
a154eea7 | 1128 | } |
a154eea7 | 1129 | } |
a154eea7 SC |
1130 | *d++ = *s++; |
1131 | } | |
19139515 SC |
1132 | |
1133 | int | |
a154eea7 SC |
1134 | main () |
1135 | { | |
1136 | int i; | |
a154eea7 | 1137 | |
19139515 SC |
1138 | remove_dups (); |
1139 | init (); | |
a154eea7 | 1140 | |
19139515 | 1141 | printf ("%s do the operation %s\n", cs, ce); |
a154eea7 SC |
1142 | printf ("switch (b0) \n{\n"); |
1143 | for (i = 0; i < PTWO; i++) | |
1144 | { | |
1145 | i = owrite (i); | |
1146 | } | |
1147 | printf ("}\n"); | |
a154eea7 SC |
1148 | |
1149 | return 0; | |
1150 | } |