Commit | Line | Data |
---|---|---|
c906108c SS |
1 | |
2 | /* generate instructions for Z8KSIM | |
3 | Copyright (C) 1992, 1993 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of Z8KSIM | |
6 | ||
7 | Z8KSIM 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, or (at your option) | |
10 | any later version. | |
11 | ||
12 | Z8KSIM 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 Z8KZIM; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | /* This program generates the code which emulates each of the z8k | |
22 | instructions | |
23 | ||
24 | code goes into three files, tc-gen1.h, tc-gen2.h and tc-gen3.h. | |
25 | which file being made depends upon the options | |
26 | ||
27 | -1 tc-gen1.h contains the fully expanded code for some selected | |
28 | opcodes, (those in the quick.c list) | |
29 | ||
30 | -2 tc-gen2.h contains a list of pointers to functions, one for each | |
31 | opcode. It points to functions in tc-gen3.h and tc-gen1.h | |
32 | depending upon quick.c | |
33 | ||
34 | -3 tc-gen3.h contains all the opcodes in unexpanded form. | |
35 | ||
36 | -b3 tc-genb3.h same as -3 but for long pointers | |
37 | ||
38 | -m regenerates list.c, which is an inverted list of opcodes to | |
39 | pointers into the z8k dissassemble opcode table, it's just there | |
40 | to makes things faster. | |
41 | */ | |
42 | ||
43 | /* steve chamberlain | |
44 | sac@cygnus.com */ | |
45 | ||
46 | #include "config.h" | |
47 | ||
48 | #include <ansidecl.h> | |
49 | #include <stdio.h> | |
50 | #ifdef HAVE_STDLIB_H | |
51 | #include <stdlib.h> | |
52 | #endif | |
53 | #ifdef HAVE_STRING_H | |
54 | #include <string.h> | |
55 | #else | |
56 | #ifdef HAVE_STRINGS_H | |
57 | #include <strings.h> | |
58 | #endif | |
59 | #endif | |
60 | ||
61 | #define NICENAMES | |
62 | ||
63 | #define DEFINE_TABLE | |
64 | #include "../opcodes/z8k-opc.h" | |
65 | ||
66 | #define NOPS 500 | |
67 | ||
68 | #define DIRTY_HACK 0 /* Enable if your gcc can't cope with huge tables */ | |
69 | extern short z8k_inv_list[]; | |
70 | struct opcode_value | |
71 | { | |
72 | int n; | |
73 | struct opcode_value *next; | |
74 | }; | |
75 | ||
76 | #define NICENAMES | |
77 | int BIG; | |
78 | ||
79 | static char *reg_names[] = | |
80 | {"bad", "src", "dst", "aux_a", "aux_b", "aux_r", "aux_x"}; | |
81 | ||
82 | #define IS_DST(x) ((x & 0xf) == 2) | |
83 | #define IS_SRC(x) ((x & 0xf)==1) | |
84 | #define SIZE_ADDRESS (BIG ? 8 : 4) /* number of nibbles in a ptr*/ | |
85 | ||
86 | static int file; | |
87 | static int makelist; | |
88 | ||
89 | static int nibs = 0; | |
90 | ||
91 | static char *current_size; | |
92 | static char *current_name; | |
93 | static char current_word0[40]; | |
94 | static char current_byte0[40]; | |
95 | static char current_byte1[40]; | |
96 | static int indent; | |
97 | static char *p; | |
98 | static char *d; | |
99 | ||
100 | struct opcode_value *list[NOPS]; | |
101 | ||
102 | static opcode_entry_type * | |
103 | lookup_inst (what) | |
104 | int what; | |
105 | { | |
106 | if (makelist) | |
107 | { | |
108 | ||
109 | int nibl_index; | |
110 | int nibl_matched; | |
111 | unsigned short instr_nibl; | |
112 | unsigned short tabl_datum, datum_class, datum_value; | |
113 | char instr_nibbles[8]; | |
114 | ||
115 | opcode_entry_type *ptr = z8k_table; | |
116 | ||
117 | nibl_matched = 0; | |
118 | ||
119 | instr_nibbles[3] = (what >> 0) & 0xf; | |
120 | instr_nibbles[2] = (what >> 4) & 0xf; | |
121 | instr_nibbles[1] = (what >> 8) & 0xf; | |
122 | instr_nibbles[0] = (what >> 12) & 0xf; | |
123 | ||
124 | while (ptr->name) | |
125 | { | |
126 | nibl_matched = 1; | |
127 | for (nibl_index = 0; nibl_index < 4 && nibl_matched; nibl_index++) | |
128 | { | |
129 | instr_nibl = instr_nibbles[nibl_index]; | |
130 | ||
131 | tabl_datum = ptr->byte_info[nibl_index]; | |
132 | datum_class = tabl_datum & CLASS_MASK; | |
133 | datum_value = ~CLASS_MASK & tabl_datum; | |
134 | ||
135 | switch (datum_class) | |
136 | { | |
137 | case CLASS_BIT_1OR2: | |
138 | if (datum_value != (instr_nibl & ~0x2)) | |
139 | nibl_matched = 0; | |
140 | break; | |
141 | ||
142 | case CLASS_BIT: | |
143 | if (datum_value != instr_nibl) | |
144 | nibl_matched = 0; | |
145 | break; | |
146 | case CLASS_00II: | |
147 | if (!((~instr_nibl) & 0x4)) | |
148 | nibl_matched = 0; | |
149 | break; | |
150 | case CLASS_01II: | |
151 | if (!(instr_nibl & 0x4)) | |
152 | nibl_matched = 0; | |
153 | break; | |
154 | case CLASS_0CCC: | |
155 | if (!((~instr_nibl) & 0x8)) | |
156 | nibl_matched = 0; | |
157 | break; | |
158 | case CLASS_1CCC: | |
159 | if (!(instr_nibl & 0x8)) | |
160 | nibl_matched = 0; | |
161 | break; | |
162 | case CLASS_0DISP7: | |
163 | if (!((~instr_nibl) & 0x8)) | |
164 | nibl_matched = 0; | |
165 | nibl_index += 1; | |
166 | break; | |
167 | case CLASS_1DISP7: | |
168 | if (!(instr_nibl & 0x8)) | |
169 | nibl_matched = 0; | |
170 | nibl_index += 1; | |
171 | break; | |
172 | case CLASS_REGN0: | |
173 | if (instr_nibl == 0) | |
174 | nibl_matched = 0; | |
175 | break; | |
176 | default: | |
177 | break; | |
178 | } | |
179 | } | |
180 | if (nibl_matched) | |
181 | { | |
182 | return ptr; | |
183 | } | |
184 | ptr++; | |
185 | } | |
186 | return 0; | |
187 | } | |
188 | else | |
189 | { | |
190 | ||
191 | if (z8k_inv_list[what] < 0) | |
192 | return 0; | |
193 | return z8k_table + z8k_inv_list[what]; | |
194 | } | |
195 | } | |
196 | ||
197 | static char * | |
198 | insn_4 (n) | |
199 | int n; | |
200 | { | |
201 | switch (n) | |
202 | { | |
203 | case 1: | |
204 | return "((iwords_0>>8) & 0xf)"; | |
205 | case 2: | |
206 | return "((ibytes_1 >> 4) & 0xf)"; | |
207 | case 3: | |
208 | return "((ibytes_1) & 0xf)"; | |
209 | case 4: | |
210 | return "((ibytes_2>>4) & 0xf)"; | |
211 | case 5: | |
212 | return "((ibytes_2) & 0xf)"; | |
213 | case 6: | |
214 | return "((ibytes_3 >> 4) & 0xf)"; | |
215 | case 7: | |
216 | return "((ibytes_3) & 0xf)"; | |
217 | default: | |
218 | return "****"; | |
219 | } | |
220 | } | |
221 | ||
222 | char * | |
223 | ptr_mode () | |
224 | { | |
225 | if (BIG) | |
226 | { | |
227 | return "ptr_long"; | |
228 | } | |
229 | return "word"; | |
230 | } | |
231 | ||
232 | static | |
233 | char * | |
234 | ptr_size () | |
235 | { | |
236 | if (BIG) | |
237 | return "4"; | |
238 | return "2"; | |
239 | } | |
240 | ||
241 | static char * | |
242 | reg_n (x) | |
243 | unsigned int x; | |
244 | { | |
245 | return reg_names[x & 0xf]; | |
246 | } | |
247 | ||
248 | char * | |
249 | stack_ptr () | |
250 | { | |
251 | return BIG ? "14" : "15"; | |
252 | } | |
253 | ||
254 | char * | |
255 | mem () | |
256 | { | |
257 | #if 0 | |
258 | return BIG ? "segmem" : "unsegmem"; | |
259 | #else | |
260 | return "mem"; | |
261 | #endif | |
262 | } | |
263 | ||
264 | int | |
265 | match (a) | |
266 | char *a; | |
267 | { | |
268 | if (strncmp (p, a, strlen (a)) == 0) | |
269 | { | |
270 | p += strlen (a); | |
271 | return 1; | |
272 | } | |
273 | return 0; | |
274 | } | |
275 | ||
276 | static | |
277 | void | |
278 | sub (y) | |
279 | char *y; | |
280 | { | |
281 | sprintf (d, "%s", y); | |
282 | d += strlen (d); | |
283 | } | |
284 | ||
285 | static char * | |
286 | insn_16 (n) | |
287 | int n; | |
288 | { | |
289 | switch (n) | |
290 | { | |
291 | case 0: | |
292 | return "(iwords_0)"; | |
293 | case 4: | |
294 | return "(iwords_1)"; | |
295 | case 8: | |
296 | return "(iwords_2)"; | |
297 | case 12: | |
298 | return "(iwords_3)"; | |
299 | default: | |
300 | return "****"; | |
301 | } | |
302 | } | |
303 | ||
304 | static | |
305 | char * | |
306 | insn_32 (n) | |
307 | int n; | |
308 | { | |
309 | switch (n) | |
310 | { | |
311 | case 0: | |
312 | return "((iwords_0<<16) | (iwords_1))"; | |
313 | case 4: | |
314 | return "((iwords_1<<16) | (iwords_2))"; | |
315 | case 8: | |
316 | return "((iwords_2<<16) | (iwords_3))"; | |
317 | default: | |
318 | return "****"; | |
319 | } | |
320 | } | |
321 | ||
322 | static char * | |
323 | size_name (x) | |
324 | int x; | |
325 | { | |
326 | switch (x) | |
327 | { | |
328 | case 8: | |
329 | return "byte"; | |
330 | case 16: | |
331 | return "word"; | |
332 | case 32: | |
333 | return "long"; | |
334 | case 64: | |
335 | return "quad"; | |
336 | } | |
337 | return "!!"; | |
338 | } | |
339 | ||
340 | /*VARARGS*/ | |
341 | void | |
342 | emit (string, a1, a2, a3, a4, a5) | |
343 | char *string; | |
344 | char* a1; | |
345 | char* a2; | |
346 | char* a3; | |
347 | char* a4; | |
348 | char* a5; | |
349 | { | |
350 | int indent_inc = 0; | |
351 | int indent_dec = 0; | |
352 | int i; | |
353 | char buffer[1000]; | |
354 | ||
355 | d = buffer; | |
356 | p = string; | |
357 | ||
358 | while (*p) | |
359 | { | |
360 | if (match ("<fop>")) | |
361 | { | |
362 | if (BIG) | |
363 | { | |
364 | sub ("bfop"); | |
365 | } | |
366 | else | |
367 | { | |
368 | sub ("sfop"); | |
369 | } | |
370 | } | |
371 | else if (match ("<iptr>")) | |
372 | { | |
373 | if (BIG) | |
374 | { | |
375 | switch (nibs) | |
376 | { | |
377 | case 4: | |
378 | sub ("(((iwords_1 << 8) | (iwords_2)) & 0x7fffff)"); | |
379 | break; | |
380 | default: | |
381 | sub ("fail(context,124)"); | |
382 | break; | |
383 | } | |
384 | } | |
385 | else | |
386 | { | |
387 | switch (nibs) | |
388 | { | |
389 | case 4: | |
390 | sub ("iwords_1"); | |
391 | break; | |
392 | default: | |
393 | sub ("fail(context,123)"); | |
394 | break; | |
395 | } | |
396 | } | |
397 | } | |
398 | else if (match ("<name>")) | |
399 | { | |
400 | sub (current_name); | |
401 | } | |
402 | else if (match ("<size>")) | |
403 | { | |
404 | sub (current_size); | |
405 | } | |
406 | else if (match ("<insn_4>")) | |
407 | { | |
408 | sub (insn_4 (nibs)); | |
409 | } | |
410 | else if (match ("<insn_16>")) | |
411 | { | |
412 | sub (insn_16 (nibs)); | |
413 | } | |
414 | else if (match ("<insn_32>")) | |
415 | { | |
416 | sub (insn_32 (nibs)); | |
417 | } | |
418 | else if (match ("iwords_0")) | |
419 | { | |
420 | sub (current_word0); | |
421 | } | |
422 | else if (match ("ibytes_0")) | |
423 | { | |
424 | sub (current_byte0); | |
425 | } | |
426 | else if (match ("<ibytes_1>")) | |
427 | { | |
428 | sub (current_byte1); | |
429 | } | |
430 | else if (match ("<next_size>")) | |
431 | { | |
432 | if (strcmp (current_size, "word") == 0) | |
433 | sub ("long"); | |
434 | if (strcmp (current_size, "byte") == 0) | |
435 | sub ("word"); | |
436 | else if (strcmp (current_size, "long") == 0) | |
437 | sub ("quad"); | |
438 | else | |
439 | abort (); | |
440 | } | |
441 | else if (match ("<addr_type>")) | |
442 | { | |
443 | if (BIG) | |
444 | sub ("unsigned long"); | |
445 | else | |
446 | sub ("unsigned short"); | |
447 | } | |
448 | ||
449 | else if (match ("<c_size>")) | |
450 | { | |
451 | if (strcmp (current_size, "word") == 0) | |
452 | sub ("short"); | |
453 | else if (strcmp (current_size, "byte") == 0) | |
454 | sub ("char"); | |
455 | else if (strcmp (current_size, "long") == 0) | |
456 | sub ("long"); | |
457 | else | |
458 | abort (); | |
459 | } | |
460 | ||
461 | else if (match ("<pc>")) | |
462 | { | |
463 | sub ("pc"); | |
464 | } | |
465 | else if (match ("<mem>")) | |
466 | { | |
467 | sub (mem ()); | |
468 | } | |
469 | else if (match ("<sp>")) | |
470 | { | |
471 | sub (stack_ptr ()); | |
472 | } | |
473 | else if (match ("<ptr_size>")) | |
474 | { | |
475 | sub (ptr_size ()); | |
476 | } | |
477 | else if (match ("<ptr_mode>")) | |
478 | { | |
479 | sub (ptr_mode ()); | |
480 | } | |
481 | else if (match ("<insn_8>")) | |
482 | { | |
483 | switch (nibs) | |
484 | { | |
485 | case 2: | |
486 | sub ("(iwords_0&0xff)"); | |
487 | break; | |
488 | case 4: | |
489 | sub ("(iwords_1>>8)"); | |
490 | break; | |
491 | case 6: | |
492 | sub ("(iwords_1&0xff)"); | |
493 | break; | |
494 | case 8: | |
495 | sub ("(iwords_2>>8)"); | |
496 | break; | |
497 | case 12: | |
498 | sub ("(/* WHO */iwords_3&0xff)"); | |
499 | break; | |
500 | default: | |
501 | abort (); | |
502 | } | |
503 | } | |
504 | else | |
505 | { | |
506 | if (*p == '{') | |
507 | indent_inc++; | |
508 | if (*p == '}') | |
509 | indent_dec++; | |
510 | *d++ = *p; | |
511 | p++; | |
512 | } | |
513 | } | |
514 | *d++ = 0; | |
515 | indent -= indent_dec; | |
516 | for (i = 0; i < indent; i++) | |
517 | printf ("\t"); | |
518 | indent += indent_inc; | |
519 | printf (buffer, a1, a2, a3, a4, a5); | |
520 | ||
521 | } | |
522 | ||
523 | /* fetch the lvalues of the operands */ | |
524 | void | |
525 | info_args (p) | |
526 | opcode_entry_type *p; | |
527 | { | |
528 | unsigned int *s; | |
529 | ||
530 | int done_one_imm8 = 0; | |
531 | ||
532 | /* int done_read = 4;*/ | |
533 | s = p->byte_info; | |
534 | nibs = 0; | |
535 | while (*s) | |
536 | { | |
537 | switch (*s & CLASS_MASK) | |
538 | { | |
539 | case CLASS_BIT_1OR2: | |
540 | emit ("register unsigned int imm_src=(<insn_4>& 2)?2:1;\n"); | |
541 | break; | |
542 | case CLASS_BIT: | |
543 | /* Just ignore these, we've already decoded this bit */ | |
544 | nibs++; | |
545 | break; | |
546 | case CLASS_REGN0: | |
547 | case CLASS_REG: | |
548 | /* this nibble tells us which register to use as an arg, | |
549 | if we've already gobbled the nibble we know what to use */ | |
550 | { | |
551 | int regname = *s & 0xf; | |
552 | ||
553 | emit ("register unsigned int reg_%s=<insn_4>;\n", | |
554 | reg_names[regname]); | |
555 | ||
556 | nibs++; | |
557 | } | |
558 | break; | |
559 | case CLASS_ADDRESS: | |
560 | emit ("register unsigned base_%s=<iptr>;\n", reg_n (*s)); | |
561 | ||
562 | nibs += SIZE_ADDRESS; | |
563 | ||
564 | break; | |
565 | case CLASS_01II: | |
566 | case CLASS_00II: | |
567 | emit ("register unsigned int imm_src=<insn_4>&0x2;\n"); | |
568 | nibs++; | |
569 | break; | |
570 | case CLASS_FLAGS: | |
571 | emit ("register unsigned int imm_src=<insn_4>;\n"); | |
572 | nibs++; | |
573 | break; | |
574 | case CLASS_IMM: | |
575 | /* Work out the size of the think to fetch */ | |
576 | ||
577 | { | |
578 | switch (*s & ~CLASS_MASK) | |
579 | { | |
580 | case ARG_IMM16: | |
581 | emit ("register unsigned imm_src=<insn_16>;\n"); | |
582 | nibs += 4; | |
583 | break; | |
584 | case ARG_IMM32: | |
585 | emit ("register unsigned int imm_src= %s;\n", insn_32 (nibs)); | |
586 | nibs += 8; | |
587 | break; | |
588 | case ARG_IMM4: | |
589 | emit ("register unsigned int imm_src=<insn_4>;\n"); | |
590 | nibs++; | |
591 | break; | |
592 | case ARG_IMM2: | |
593 | emit ("register unsigned int imm_src=<insn_4> & 0x2;\n"); | |
594 | nibs++; | |
595 | break; | |
596 | ||
597 | case ARG_IMM4M1: | |
598 | emit ("register unsigned int imm_src=(<insn_4> + 1);\n"); | |
599 | nibs++; | |
600 | break; | |
601 | case ARG_IMM_1: | |
602 | emit ("register unsigned int imm_src=1;\n"); | |
603 | break; | |
604 | case ARG_IMM_2: | |
605 | emit ("register unsigned int imm_src=2;\n"); | |
606 | break; | |
607 | case ARG_NIM8: | |
608 | emit ("register unsigned int imm_src=-<insn_8>;\n"); | |
609 | nibs += 2; | |
610 | break; | |
611 | case ARG_IMM8: | |
612 | if (!done_one_imm8) | |
613 | { | |
614 | emit ("register unsigned int imm_src=<insn_8>;\n"); | |
615 | nibs += 2; | |
616 | done_one_imm8 = 1; | |
617 | } | |
618 | break; | |
619 | default: | |
620 | emit ("register int fail%d=fail(context,1);\n", nibs); | |
621 | break; | |
622 | } | |
623 | break; | |
624 | ||
625 | case CLASS_DISP8: | |
626 | /* We can't use `(char)' since char might be unsigned. | |
627 | We can't use `(signed char)' because the compiler might be K&R. | |
628 | This seems safe, since it only assumes that bytes are 8 | |
629 | bits. */ | |
630 | emit ("register unsigned int oplval_dst=((ibytes_1 << (sizeof (int) * 8 - 8)) >> (sizeof (int) * 8 - 9)) + pc;\n"); | |
631 | #if 0 | |
632 | /* Original code: fails if characters are unsigned. */ | |
633 | emit ("register unsigned int oplval_dst=(((char)ibytes_1)<<1) + pc;\n"); | |
634 | #endif | |
635 | nibs += 2; | |
636 | break; | |
637 | case CLASS_CC: | |
638 | emit ("register unsigned int op_cc=<insn_4>;\n"); | |
639 | nibs++; | |
640 | break; | |
641 | default: | |
642 | emit ("register int FAIL%d=fail(context,2);\n", nibs); | |
643 | break; | |
644 | } | |
645 | ; | |
646 | /* work out how to fetch the immediate value */ | |
647 | } | |
648 | ||
649 | s++; | |
650 | } | |
651 | } | |
652 | ||
653 | void | |
654 | info_special (p, getdst, nostore, before, nosrc) | |
655 | opcode_entry_type *p; | |
656 | int *getdst; | |
657 | int *nostore; | |
658 | int *before; | |
659 | int *nosrc; | |
660 | { | |
661 | switch (p->opcode) | |
662 | { | |
663 | case OPC_exts: | |
664 | case OPC_extsb: | |
665 | case OPC_extsl: | |
666 | *nostore = 1; | |
667 | *nosrc = 1; | |
668 | break; | |
669 | case OPC_ldm: | |
670 | *nostore = 1; | |
671 | *nosrc = 1; | |
672 | break; | |
673 | case OPC_negb: | |
674 | case OPC_neg: | |
675 | case OPC_sla: | |
676 | case OPC_slab: | |
677 | case OPC_slal: | |
678 | case OPC_sda: | |
679 | case OPC_sdab: | |
680 | case OPC_sdal: | |
681 | case OPC_com: | |
682 | case OPC_comb: | |
683 | case OPC_adc: | |
684 | case OPC_sbc: | |
685 | case OPC_nop: | |
686 | case OPC_adcb: | |
687 | case OPC_add: | |
688 | case OPC_addb: | |
689 | case OPC_addl: | |
690 | case OPC_inc: | |
691 | case OPC_sub: | |
692 | case OPC_subb: | |
693 | case OPC_subl: | |
694 | case OPC_and: | |
695 | case OPC_andb: | |
696 | case OPC_xorb: | |
697 | case OPC_xor: | |
698 | break; | |
699 | ||
700 | case OPC_mult: | |
701 | case OPC_multl: | |
702 | case OPC_div: | |
703 | case OPC_divl: | |
704 | ||
705 | *nostore = 1; | |
706 | break; | |
707 | ||
708 | case OPC_testb: | |
709 | case OPC_test: | |
710 | case OPC_testl: | |
711 | case OPC_cp: | |
712 | case OPC_cpb: | |
713 | case OPC_cpl: | |
714 | case OPC_bit: | |
715 | *nostore = 1; | |
716 | *before = 0; | |
717 | break; | |
718 | ||
719 | case OPC_bpt: | |
720 | case OPC_jr: | |
721 | case OPC_jp: | |
722 | case OPC_ret: | |
723 | case OPC_call: | |
724 | case OPC_tcc: | |
725 | *nosrc = 1; | |
726 | *nostore = 1; | |
727 | *before = 1; | |
728 | break; | |
729 | case OPC_sc: | |
730 | *nostore = 1; | |
731 | *before = 0; | |
732 | break; | |
733 | case OPC_clrb: | |
734 | case OPC_clr: | |
735 | *before = 1; | |
736 | *nosrc = 1; | |
737 | break; | |
738 | case OPC_ldi: | |
739 | case OPC_ldib: | |
740 | case OPC_lddb: | |
741 | case OPC_ldd: | |
742 | ||
743 | *before = 1; | |
744 | *nostore = 1; | |
745 | *nosrc = 1; | |
746 | break; | |
747 | case OPC_ldk: | |
748 | case OPC_ld: | |
749 | case OPC_ldb: | |
750 | case OPC_ldl: | |
751 | *before = 1; | |
752 | *getdst = 0; | |
753 | break; | |
754 | case OPC_push: | |
755 | case OPC_pushl: | |
756 | case OPC_pop: | |
757 | case OPC_popl: | |
758 | *before = 1; | |
759 | *getdst = 0; | |
760 | break; | |
761 | case OPC_lda: | |
762 | *nosrc = 1; | |
763 | break; | |
764 | } | |
765 | } | |
766 | ||
767 | /* calculate the lvalues required for the opcode */ | |
768 | void | |
769 | info_lvals (p) | |
770 | opcode_entry_type *p; | |
771 | { | |
772 | /* emit code to work out lvalues, if any */ | |
773 | unsigned int *i = p->arg_info; | |
774 | ||
775 | while (*i) | |
776 | { | |
777 | current_name = reg_n (*i); | |
778 | current_size = size_name (p->type); | |
779 | switch (*i & CLASS_MASK) | |
780 | { | |
781 | case CLASS_X: | |
782 | /* address(reg) */ | |
783 | emit ("register <addr_type> oplval_<name>= ((base_<name> + (short)get_word_reg(context,reg_<name>)) & 0xffff) + (base_<name> & ~0xffff);\n"); | |
784 | break; | |
785 | case CLASS_IR: | |
786 | /* Indirect register */ | |
787 | emit ("register int oplval_<name> = get_<ptr_mode>_reg(context,reg_<name>);\n"); | |
788 | break; | |
789 | case CLASS_DA: | |
790 | emit ("register int oplval_<name>=base_<name>;\n"); | |
791 | break; | |
792 | case CLASS_IMM: | |
793 | case CLASS_REG_WORD: | |
794 | case CLASS_REG_LONG: | |
795 | case CLASS_REG_BYTE: | |
796 | case CLASS_PR: | |
797 | break; | |
798 | case CLASS_BA: | |
799 | emit ("register int oplval_<name> = get_<ptr_mode>_reg(context,reg_<name>) + (short)(imm_src);\n"); | |
800 | break; | |
801 | case CLASS_BX: | |
802 | emit ("register int oplval_<name> = get_<ptr_mode>_reg(context,reg_<name>)\n"); | |
803 | emit (" + get_word_reg(context,reg_aux_x);\n"); | |
804 | break; | |
805 | } | |
806 | i++; | |
807 | } | |
808 | } | |
809 | ||
810 | /* emit code to fetch the args from calculated lvalues */ | |
811 | int allregs; | |
812 | void | |
813 | info_fetch (p, getdst) | |
814 | opcode_entry_type *p; | |
815 | int getdst; | |
816 | { | |
817 | unsigned int *i = p->arg_info; | |
818 | int had_src = 0; | |
819 | ||
820 | allregs = 1; | |
821 | while (*i) | |
822 | { | |
823 | ||
824 | current_name = reg_n (*i); | |
825 | current_size = size_name (p->type); | |
826 | switch (*i & CLASS_MASK) | |
827 | { | |
828 | case CLASS_X: | |
829 | case CLASS_IR: | |
830 | case CLASS_BA: | |
831 | case CLASS_BX: | |
832 | case CLASS_DA: | |
833 | if (!getdst && IS_DST (*i)) | |
834 | break; | |
835 | emit ("register int op_<name>= get_<size>_<mem>_da(context,oplval_<name>);\n"); | |
836 | allregs = 0; | |
837 | break; | |
838 | case CLASS_IMM: | |
839 | if (!had_src) | |
840 | { | |
841 | if (p->opcode == OPC_out || | |
842 | p->opcode == OPC_outb || | |
843 | p->opcode == OPC_sout || | |
844 | p->opcode == OPC_soutb) | |
845 | { | |
846 | /* The imm is a dest here */ | |
847 | emit ("register int op_dst = imm_src;\n"); | |
848 | } | |
849 | else | |
850 | { | |
851 | emit ("register int op_src = imm_src;\n"); | |
852 | } | |
853 | } | |
854 | break; | |
855 | case CLASS_REG_QUAD: | |
856 | if (!getdst && IS_DST (*i)) | |
857 | break; | |
858 | had_src |= IS_SRC (*i); | |
859 | emit ("UDItype op_<name> ;\n"); | |
860 | ||
861 | break; | |
862 | case CLASS_REG_WORD: | |
863 | if (!getdst && IS_DST (*i)) | |
864 | break; | |
865 | had_src |= IS_SRC (*i); | |
866 | emit ("register int op_<name> = get_word_reg(context,reg_<name>);\n"); | |
867 | break; | |
868 | ||
869 | case CLASS_REG_LONG: | |
870 | if (!getdst && IS_DST (*i)) | |
871 | break; | |
872 | had_src |= IS_SRC (*i); | |
873 | emit ("register int op_<name> = get_long_reg(context,reg_<name>);\n"); | |
874 | break; | |
875 | case CLASS_REG_BYTE: | |
876 | if (!getdst && IS_DST (*i)) | |
877 | break; | |
878 | had_src |= IS_SRC (*i); | |
879 | emit ("register int op_<name> = get_byte_reg(context,reg_<name>);\n"); | |
880 | break; | |
881 | } | |
882 | i++; | |
883 | } | |
884 | } | |
885 | ||
886 | static void | |
887 | normal_flags (p, s, neg) | |
888 | opcode_entry_type *p; | |
889 | char *s; | |
890 | { | |
891 | emit (" %s;\n", s); | |
892 | emit ("NORMAL_FLAGS(context,%d, tmp, op_dst, op_src,%d); \n", p->type,neg); | |
893 | } | |
894 | ||
895 | static void | |
896 | test_normal_flags (p, s, opt) | |
897 | opcode_entry_type *p; | |
898 | char *s; | |
899 | int opt; | |
900 | { | |
901 | emit (" %s;\n", s); | |
902 | if (0 && opt) | |
903 | { | |
904 | emit ("context->broken_flags = TST_FLAGS;\n"); | |
905 | emit ("context->size = %d;\n", p->type); | |
906 | } | |
907 | else | |
908 | { | |
909 | emit ("TEST_NORMAL_FLAGS(context,%d, tmp); \n", p->type); | |
910 | } | |
911 | ||
912 | } | |
913 | ||
914 | static void | |
915 | optimize_normal_flags (p, s,neg) | |
916 | opcode_entry_type *p; | |
917 | char *s; | |
918 | { | |
919 | emit (" %s;\n", s); | |
920 | #if 0 | |
921 | emit ("context->broken_flags = CMP_FLAGS;\n"); | |
922 | #else | |
923 | emit ("NORMAL_FLAGS(context,%d, tmp, op_dst, op_src,%d); \n", p->type, neg); | |
924 | #endif | |
925 | } | |
926 | ||
927 | static | |
928 | void | |
929 | jp (p) | |
930 | opcode_entry_type *p; | |
931 | { | |
932 | ||
933 | emit ("if(op_cc == 8 || COND(context,op_cc)) pc = oplval_dst;\n"); | |
934 | } | |
935 | ||
936 | static void | |
937 | jr (p) | |
938 | opcode_entry_type *p; | |
939 | { | |
940 | emit ("if(op_cc == 8 || COND(context,op_cc)) pc = oplval_dst;\n"); | |
941 | } | |
942 | ||
943 | static void | |
944 | ret (p) | |
945 | opcode_entry_type *p; | |
946 | { | |
947 | emit ("if(op_cc == 8 || COND(context,op_cc))\n{\n"); | |
948 | emit ("pc = get_<ptr_mode>_<mem>_ir(context,<sp>);\n"); | |
949 | emit ("put_<ptr_mode>_reg(context,<sp>, get_<ptr_mode>_reg(context,<sp>) + <ptr_size>);\n"); | |
950 | emit ("};\n"); | |
951 | } | |
952 | ||
953 | static void | |
954 | call (p) | |
955 | opcode_entry_type *p; | |
956 | { | |
957 | emit ("put_<ptr_mode>_reg(context,<sp>,tmp = get_<ptr_mode>_reg(context,<sp>) - <ptr_size>);\n"); | |
958 | emit ("put_<ptr_mode>_<mem>_da(context,tmp, pc);\n"); | |
959 | emit ("pc = oplval_dst;\n"); | |
960 | } | |
961 | ||
962 | static void | |
963 | push (p) | |
964 | opcode_entry_type *p; | |
965 | { | |
966 | emit ("tmp = op_src;\n"); | |
967 | emit ("oplval_dst -= %d;\n", p->type / 8); | |
968 | emit ("put_<ptr_mode>_reg(context,reg_dst, oplval_dst);\n"); | |
969 | } | |
970 | ||
971 | static void | |
972 | pop (p) | |
973 | opcode_entry_type *p; | |
974 | { | |
975 | emit ("tmp = op_src;\n"); | |
976 | emit ("put_<ptr_mode>_reg(context,reg_src, oplval_src + %d);\n", p->type / 8); | |
977 | } | |
978 | ||
979 | static void | |
980 | ld (p) | |
981 | opcode_entry_type *p; | |
982 | { | |
983 | emit ("tmp = op_src;\n"); | |
984 | } | |
985 | ||
986 | static void | |
987 | sc () | |
988 | { | |
989 | emit ("support_call(context,imm_src);\n"); | |
990 | } | |
991 | ||
992 | static void | |
993 | bpt () | |
994 | { | |
995 | emit ("pc -=2; \n"); | |
996 | emit ("context->exception = SIM_BREAKPOINT;\n"); | |
997 | } | |
998 | ||
999 | static void | |
1000 | ldi (p, size, inc) | |
1001 | opcode_entry_type *p; | |
1002 | int size; | |
1003 | int inc; | |
1004 | { | |
1005 | int dinc = (size / 8) * inc; | |
1006 | ||
1007 | current_size = size_name (size); | |
1008 | emit ("{ \n"); | |
1009 | emit ("int type = %s;\n", insn_4 (7)); | |
1010 | emit ("int rs = get_<ptr_mode>_reg(context,reg_src);\n"); | |
1011 | emit ("int rd = get_<ptr_mode>_reg(context,reg_dst);\n"); | |
1012 | emit ("int rr = get_word_reg(context,reg_aux_r);\n"); | |
1013 | emit ("do {\n"); | |
1014 | emit ("put_<size>_<mem>_da(context,rd, get_<size>_<mem>_da(context,rs));\n"); | |
1015 | emit ("rd += %d;\n", dinc); | |
1016 | emit ("rs += %d;\n", dinc); | |
1017 | emit ("rr --;\n"); | |
1018 | emit ("context->cycles += 9;\n"); | |
1019 | emit ("} while (!type && rr != 0 && context->exception <= 1);\n"); | |
1020 | emit ("if (context->exception>1) pc -=4;\n"); | |
1021 | emit ("put_<ptr_mode>_reg(context,reg_src, rs);\n"); | |
1022 | emit ("put_<ptr_mode>_reg(context,reg_dst, rd);\n"); | |
1023 | emit ("put_word_reg(context,reg_aux_r, rr);\n"); | |
1024 | emit ("}\n"); | |
1025 | ||
1026 | } | |
1027 | ||
1028 | static void | |
1029 | shift (p, arith) | |
1030 | opcode_entry_type *p; | |
1031 | int arith; | |
1032 | { | |
1033 | ||
1034 | /* We can't use `(char)' since char might be unsigned. | |
1035 | We can't use `(signed char)' because the compiler might be K&R. | |
1036 | This seems safe, since it only assumes that bytes are 8 bits. */ | |
1037 | emit ("op_src = (op_src << (sizeof (int) * 8 - 8)) >> (sizeof (int) * 8 - 8);\n"); | |
1038 | #if 0 | |
1039 | /* Original code: fails if characters are unsigned. */ | |
1040 | emit ("op_src = (char)op_src;\n"); | |
1041 | #endif | |
1042 | emit ("if (op_src < 0) \n"); | |
1043 | emit ("{\n"); | |
1044 | emit ("op_src = -op_src;\n"); | |
1045 | emit ("op_dst = (%s <c_size>)op_dst;\n", arith ? "" : "unsigned"); | |
1046 | emit ("tmp = (%s op_dst) >> op_src;\n", arith ? "" : "(unsigned)"); | |
1047 | emit ("context->carry = op_dst >> (op_src-1);\n", p->type); | |
1048 | emit ("}\n"); | |
1049 | emit ("else\n"); | |
1050 | emit ("{\n"); | |
1051 | emit ("tmp = op_dst << op_src;\n"); | |
1052 | emit ("context->carry = op_dst >> (%d - op_src);\n", p->type); | |
1053 | emit ("}\n"); | |
1054 | emit ("context->zero = (<c_size>)tmp == 0;\n"); | |
1055 | emit ("context->sign = (int)((<c_size>)tmp) < 0;\n"); | |
1056 | emit ("context->overflow = ((int)tmp < 0) != ((int)op_dst < 0);\n"); | |
1057 | emit ("context->cycles += 3*op_src;\n"); | |
1058 | emit ("context->broken_flags = 0;\n"); | |
1059 | ||
1060 | } | |
1061 | ||
1062 | static void | |
1063 | rotate (p, through_carry, size, left) | |
1064 | opcode_entry_type *p; | |
1065 | int through_carry; | |
1066 | int size; | |
1067 | int left; | |
1068 | { | |
1069 | ||
1070 | if (!left) | |
1071 | { | |
1072 | emit ("while (op_src--) {\n"); | |
1073 | emit ("int rotbit;\n"); | |
1074 | emit ("rotbit = op_dst & 1;\n"); | |
1075 | emit ("op_dst = ((unsigned)op_dst) >> 1;\n"); | |
1076 | ||
1077 | if (through_carry) | |
1078 | { | |
1079 | emit ("op_dst |= context->carry << %d;\n", size - 1); | |
1080 | } | |
1081 | else | |
1082 | { | |
1083 | emit ("op_dst |= rotbit << %d;\n", size - 1); | |
1084 | } | |
1085 | emit ("context->carry = rotbit;\n"); | |
1086 | emit ("}\n"); | |
1087 | } | |
1088 | else | |
1089 | { | |
1090 | emit ("while (op_src--) {\n"); | |
1091 | emit ("int rotbit;\n"); | |
1092 | ||
1093 | emit ("rotbit = (op_dst >> (%d))&1;\n", size - 1); | |
1094 | emit ("op_dst <<=1;\n"); | |
1095 | if (through_carry) | |
1096 | { | |
1097 | emit ("if (context->carry) op_dst |=1;\n"); | |
1098 | } | |
1099 | else | |
1100 | { | |
1101 | emit ("if (rotbit) op_dst |= 1;\n"); | |
1102 | } | |
1103 | emit ("context->carry = rotbit;\n"); | |
1104 | emit ("}\n"); | |
1105 | } | |
1106 | emit ("tmp = (<c_size>)op_dst;\n"); | |
1107 | emit ("context->zero = tmp == 0;\n"); | |
1108 | emit ("context->sign = (int)tmp < 0;\n"); | |
1109 | emit ("context->overflow = ((int)tmp < 0) != ((int)op_dst < 0);\n"); | |
1110 | emit ("context->cycles += 3*op_src;\n"); | |
1111 | emit ("context->broken_flags = 0;\n"); | |
1112 | ||
1113 | } | |
1114 | ||
1115 | static void | |
1116 | adiv (p) | |
1117 | opcode_entry_type *p; | |
1118 | { | |
1119 | emit ("if (op_src==0)\n"); | |
1120 | emit ("{\n"); | |
1121 | emit ("context->exception = SIM_DIV_ZERO;\n"); | |
1122 | emit ("}\n"); | |
1123 | emit ("else\n"); | |
1124 | emit ("{\n"); | |
1125 | ||
1126 | if (p->type == 32) | |
1127 | { | |
1128 | emit ("op_dst.low = (int)get_long_reg(context,reg_dst+2);\n"); | |
1129 | emit ("op_dst.high = (int)get_long_reg(context,reg_dst+0);\n"); | |
1130 | #ifdef __GNUC__ | |
1131 | emit ("tmp = (((long long)op_dst.high << 32) + (op_dst.low)) / (int)op_src;\n"); | |
1132 | #else | |
1133 | emit ("tmp = (long)op_dst.low / (int)op_src;\n"); | |
1134 | #endif | |
1135 | emit ("put_long_reg(context,reg_dst+2, tmp);\n"); | |
1136 | #ifdef __GNUC__ | |
1137 | emit ("put_long_reg(context,reg_dst, (((long long)op_dst.high << 32) + (op_dst.low)) %% (int)op_src);\n"); | |
1138 | #else | |
1139 | emit ("put_long_reg(context,reg_dst, (int)op_dst.low %% (int)op_src);\n"); | |
1140 | #endif | |
1141 | ||
1142 | emit ("context->zero = op_src == 0 || (op_dst.low==0 && op_dst.high==0);\n"); | |
1143 | } | |
1144 | else | |
1145 | { | |
1146 | emit ("tmp = (long)op_dst / (short)op_src;\n"); | |
1147 | emit ("put_word_reg(context,reg_dst+1, tmp);\n"); | |
1148 | emit ("put_word_reg(context,reg_dst, (long) op_dst %% (short)op_src);\n"); | |
1149 | emit ("context->zero = op_src == 0 || op_dst==0;\n"); | |
1150 | } | |
1151 | ||
1152 | emit ("context->sign = (int)tmp < 0;\n"); | |
1153 | emit ("context->overflow =(tmp & 0x%x) != 0;\n", | |
1154 | ~((1 << (p->type)) - 1)); | |
1155 | emit ("context->carry = (tmp & 0x%x) != 0;\n", | |
1156 | ~(1 << (p->type))); | |
1157 | ||
1158 | emit ("}\n"); | |
1159 | } | |
1160 | ||
1161 | static void | |
1162 | dobit (p) | |
1163 | opcode_entry_type *p; | |
1164 | { | |
1165 | emit("context->zero = (op_dst & (1<<op_src))==0;\n"); | |
1166 | emit("context->broken_flags = 0;\n"); | |
1167 | } | |
1168 | static void | |
1169 | doset (p, v) | |
1170 | opcode_entry_type*p; | |
1171 | int v; | |
1172 | { | |
1173 | if (v) | |
1174 | emit (" tmp = op_dst | (1<< op_src);\n"); | |
1175 | else | |
1176 | emit (" tmp = op_dst & ~(1<< op_src);\n"); | |
1177 | } | |
1178 | ||
1179 | static void | |
1180 | mult (p) | |
1181 | opcode_entry_type *p; | |
1182 | { | |
1183 | ||
1184 | if (p->type == 32) | |
1185 | { | |
1186 | emit ("op_dst.low = get_long_reg(context,reg_dst+2);\n"); | |
1187 | emit ("tmp = op_dst.low * op_src;\n"); | |
1188 | emit ("put_long_reg(context,reg_dst+2, tmp);\n"); | |
1189 | emit ("put_long_reg(context,reg_dst, 0);\n"); | |
1190 | } | |
1191 | else | |
1192 | { | |
1193 | emit ("op_dst = get_word_reg(context,reg_dst+1);\n"); | |
1194 | emit ("tmp = op_dst * op_src;\n"); | |
1195 | emit ("put_long_reg(context,reg_dst, tmp);\n"); | |
1196 | } | |
1197 | ||
1198 | emit ("context->sign = (int)tmp < 0;\n"); | |
1199 | emit ("context->overflow =0;\n"); | |
1200 | emit ("context->carry = (tmp & 0x%x) != 0;\n", ~((1 << (p->type)) - 1)); | |
1201 | emit ("context->zero = tmp == 0;\n"); | |
1202 | ||
1203 | } | |
1204 | ||
1205 | static void | |
1206 | exts (p) | |
1207 | opcode_entry_type *p; | |
1208 | { | |
1209 | /* Fetch the ls part of the src */ | |
1210 | current_size = size_name (p->type * 2); | |
1211 | ||
1212 | if (p->type == 32) | |
1213 | { | |
1214 | emit ("tmp= get_long_reg(context,reg_dst+2);\n"); | |
1215 | emit ("if (tmp & (1<<%d)) {\n", p->type - 1); | |
1216 | emit ("put_long_reg(context,reg_dst, 0xffffffff);\n"); | |
1217 | emit ("}\n"); | |
1218 | emit ("else\n"); | |
1219 | emit ("{\n"); | |
1220 | emit ("put_long_reg(context,reg_dst, 0);\n"); | |
1221 | emit ("}\n"); | |
1222 | } | |
1223 | else | |
1224 | { | |
1225 | emit ("tmp= get_<size>_reg(context,reg_dst);\n"); | |
1226 | emit ("if (tmp & (1<<%d)) {\n", p->type - 1); | |
1227 | emit ("tmp |= 0x%x;\n", ~((1 << p->type) - 1)); | |
1228 | emit ("}\n"); | |
1229 | emit ("else\n"); | |
1230 | emit ("{\n"); | |
1231 | ||
1232 | emit ("tmp &= 0x%x;\n", ((1 << p->type) - 1)); | |
1233 | emit ("}\n"); | |
1234 | emit ("put_<size>_reg(context,reg_dst, tmp);\n"); | |
1235 | } | |
1236 | } | |
1237 | doflag(on) | |
1238 | int on; | |
1239 | { | |
1240 | /* Load up the flags */ | |
1241 | emit(" COND (context, 0x0b);\n"); | |
1242 | ||
1243 | if (on) | |
1244 | emit ("{ int on =1;\n "); | |
1245 | else | |
1246 | emit ("{ int on =0;\n "); | |
1247 | ||
1248 | emit ("if (imm_src & 1)\n"); | |
1249 | emit ("PSW_OVERFLOW = on;\n"); | |
1250 | ||
1251 | emit ("if (imm_src & 2)\n"); | |
1252 | emit ("PSW_SIGN = on;\n"); | |
1253 | ||
1254 | emit ("if (imm_src & 4)\n"); | |
1255 | emit ("PSW_ZERO = on;\n"); | |
1256 | ||
1257 | emit ("if (imm_src & 8)\n"); | |
1258 | emit ("PSW_CARRY = on;\n"); | |
1259 | emit("}\n"); | |
1260 | ||
1261 | ||
1262 | } | |
1263 | /* emit code to perform operation */ | |
1264 | void | |
1265 | info_docode (p) | |
1266 | opcode_entry_type *p; | |
1267 | { | |
1268 | switch (p->opcode) | |
1269 | { | |
1270 | case OPC_clr: | |
1271 | case OPC_clrb: | |
1272 | emit ("tmp = 0;\n"); | |
1273 | break; | |
1274 | case OPC_ex: | |
1275 | case OPC_exb: | |
1276 | ||
1277 | emit ("tmp = op_src; \n"); | |
1278 | if (allregs) | |
1279 | { | |
1280 | emit ("put_<size>_reg(context,reg_src, op_dst);\n"); | |
1281 | } | |
1282 | else | |
1283 | { | |
1284 | emit ("put_<size>_mem_da(context, oplval_src, op_dst);\n"); | |
1285 | } | |
1286 | break; | |
1287 | case OPC_adc: | |
1288 | case OPC_adcb: | |
1289 | normal_flags (p, "op_src += COND(context,7);tmp = op_dst + op_src ;",0); | |
1290 | break; | |
1291 | case OPC_sbc: | |
1292 | normal_flags (p, "op_src += COND(context,7);tmp = op_dst - op_src ;",1); | |
1293 | break; | |
1294 | case OPC_nop: | |
1295 | break; | |
1296 | case OPC_com: | |
1297 | case OPC_comb: | |
1298 | test_normal_flags (p, "tmp = ~ op_dst", 1); | |
1299 | break; | |
1300 | case OPC_and: | |
1301 | case OPC_andb: | |
1302 | test_normal_flags (p, "tmp = op_dst & op_src", 1); | |
1303 | break; | |
1304 | case OPC_xor: | |
1305 | case OPC_xorb: | |
1306 | test_normal_flags (p, "tmp = op_dst ^ op_src", 1); | |
1307 | break; | |
1308 | case OPC_or: | |
1309 | case OPC_orb: | |
1310 | test_normal_flags (p, "tmp = op_dst | op_src", 1); | |
1311 | break; | |
1312 | case OPC_sla: | |
1313 | case OPC_slab: | |
1314 | case OPC_slal: | |
1315 | case OPC_sda: | |
1316 | case OPC_sdab: | |
1317 | case OPC_sdal: | |
1318 | shift (p, 1); | |
1319 | break; | |
1320 | ||
1321 | case OPC_sll: | |
1322 | case OPC_sllb: | |
1323 | case OPC_slll: | |
1324 | case OPC_sdl: | |
1325 | case OPC_sdlb: | |
1326 | case OPC_sdll: | |
1327 | shift (p, 0); | |
1328 | break; | |
1329 | case OPC_rl: | |
1330 | rotate (p, 0, 16, 1); | |
1331 | break; | |
1332 | case OPC_rlb: | |
1333 | rotate (p, 0, 8, 1); | |
1334 | break; | |
1335 | case OPC_rr: | |
1336 | rotate (p, 0, 16, 0); | |
1337 | break; | |
1338 | case OPC_rrb: | |
1339 | rotate (p, 0, 8, 0); | |
1340 | break; | |
1341 | case OPC_rrc: | |
1342 | rotate (p, 1, 16, 0); | |
1343 | break; | |
1344 | case OPC_rrcb: | |
1345 | rotate (p, 1, 8, 0); | |
1346 | break; | |
1347 | case OPC_rlc: | |
1348 | rotate (p, 1, 16, 1); | |
1349 | break; | |
1350 | case OPC_rlcb: | |
1351 | rotate (p, 1, 8, 1); | |
1352 | break; | |
1353 | ||
1354 | case OPC_extsb: | |
1355 | case OPC_exts: | |
1356 | case OPC_extsl: | |
1357 | exts (p); | |
1358 | break; | |
1359 | case OPC_add: | |
1360 | case OPC_addb: | |
1361 | case OPC_addl: | |
1362 | case OPC_inc: | |
1363 | case OPC_incb: | |
1364 | optimize_normal_flags (p, "tmp = op_dst + op_src",0); | |
1365 | break; | |
1366 | case OPC_testb: | |
1367 | case OPC_test: | |
1368 | case OPC_testl: | |
1369 | test_normal_flags (p, "tmp = op_dst", 0); | |
1370 | break; | |
1371 | case OPC_cp: | |
1372 | case OPC_cpb: | |
1373 | case OPC_cpl: | |
1374 | normal_flags (p, "tmp = op_dst - op_src",1); | |
1375 | break; | |
1376 | case OPC_negb: | |
1377 | case OPC_neg: | |
1378 | emit ("{\n"); | |
1379 | emit ("int op_src = -op_dst;\n"); | |
1380 | emit ("op_dst = 0;\n"); | |
1381 | optimize_normal_flags (p, "tmp = op_dst + op_src;\n",1); | |
1382 | emit ("}"); | |
1383 | break; | |
1384 | ||
1385 | case OPC_sub: | |
1386 | case OPC_subb: | |
1387 | case OPC_subl: | |
1388 | case OPC_dec: | |
1389 | case OPC_decb: | |
1390 | optimize_normal_flags (p, "tmp = op_dst - op_src",1); | |
1391 | break; | |
1392 | case OPC_bpt: | |
1393 | bpt (); | |
1394 | break; | |
1395 | case OPC_jr: | |
1396 | jr (p); | |
1397 | break; | |
1398 | case OPC_sc: | |
1399 | sc (); | |
1400 | break; | |
1401 | case OPC_jp: | |
1402 | jp (p); | |
1403 | break; | |
1404 | case OPC_ret: | |
1405 | ret (p); | |
1406 | break; | |
1407 | case OPC_call: | |
1408 | call (p); | |
1409 | break; | |
1410 | case OPC_tcc: | |
1411 | case OPC_tccb: | |
1412 | emit ("if(op_cc == 8 || COND(context,op_cc)) put_word_reg(context,reg_dst, 1);\n"); | |
1413 | break; | |
1414 | case OPC_lda: | |
1415 | emit ("tmp = oplval_src; \n"); | |
1416 | /*(((oplval_src) & 0xff0000) << 8) | (oplval_src & 0xffff); \n");*/ | |
1417 | break; | |
1418 | case OPC_ldk: | |
1419 | case OPC_ld: | |
1420 | ||
1421 | case OPC_ldb: | |
1422 | case OPC_ldl: | |
1423 | ld (p); | |
1424 | break; | |
1425 | case OPC_ldib: | |
1426 | ldi (p, 8, 1); | |
1427 | break; | |
1428 | case OPC_ldi: | |
1429 | ldi (p, 16, 1); | |
1430 | break; | |
1431 | ||
1432 | case OPC_lddb: | |
1433 | ldi (p, 8, -1); | |
1434 | break; | |
1435 | case OPC_ldd: | |
1436 | ldi (p, 16, -1); | |
1437 | break; | |
1438 | ||
1439 | case OPC_push: | |
1440 | case OPC_pushl: | |
1441 | push (p); | |
1442 | break; | |
1443 | ||
1444 | case OPC_div: | |
1445 | case OPC_divl: | |
1446 | adiv (p); | |
1447 | break; | |
1448 | case OPC_mult: | |
1449 | case OPC_multl: | |
1450 | mult (p); | |
1451 | break; | |
1452 | case OPC_pop: | |
1453 | case OPC_popl: | |
1454 | pop (p); | |
1455 | break; | |
1456 | case OPC_set: | |
1457 | doset (p,1); | |
1458 | break; | |
1459 | case OPC_res: | |
1460 | doset (p,0); | |
1461 | break; | |
1462 | case OPC_bit: | |
1463 | dobit(p); | |
1464 | break; | |
1465 | case OPC_resflg: | |
1466 | doflag(0); | |
1467 | break; | |
1468 | case OPC_setflg: | |
1469 | doflag(1); | |
1470 | break; | |
1471 | default: | |
1472 | ||
1473 | emit ("tmp = fail(context,%d);\n", p->opcode); | |
1474 | break; | |
1475 | } | |
1476 | } | |
1477 | ||
1478 | /* emit code to store result in calculated lvalue */ | |
1479 | ||
1480 | void | |
1481 | info_store (p) | |
1482 | opcode_entry_type *p; | |
1483 | { | |
1484 | unsigned int *i = p->arg_info; | |
1485 | ||
1486 | while (*i) | |
1487 | { | |
1488 | current_name = reg_n (*i); | |
1489 | current_size = size_name (p->type); | |
1490 | ||
1491 | if (IS_DST (*i)) | |
1492 | { | |
1493 | switch (*i & CLASS_MASK) | |
1494 | { | |
1495 | case CLASS_PR: | |
1496 | emit ("put_<ptr_mode>_reg(context,reg_<name>, tmp);\n"); | |
1497 | break; | |
1498 | case CLASS_REG_LONG: | |
1499 | case CLASS_REG_WORD: | |
1500 | case CLASS_REG_BYTE: | |
1501 | ||
1502 | emit ("put_<size>_reg(context,reg_<name>,tmp);\n"); | |
1503 | break; | |
1504 | case CLASS_X: | |
1505 | case CLASS_IR: | |
1506 | case CLASS_DA: | |
1507 | case CLASS_BX: | |
1508 | case CLASS_BA: | |
1509 | ||
1510 | emit ("put_<size>_<mem>_da(context,oplval_<name>, tmp);\n"); | |
1511 | break; | |
1512 | case CLASS_IMM: | |
1513 | break; | |
1514 | default: | |
1515 | emit ("abort(); "); | |
1516 | break; | |
1517 | } | |
1518 | ||
1519 | } | |
1520 | i++; | |
1521 | } | |
1522 | } | |
1523 | ||
1524 | static | |
1525 | void | |
1526 | mangle (p, shortcut, value) | |
1527 | opcode_entry_type *p; | |
1528 | int shortcut; | |
1529 | int value; | |
1530 | { | |
1531 | int nostore = 0; | |
1532 | int extra; | |
1533 | int getdst = 1; | |
1534 | int before = 0; | |
1535 | int nosrc = 0; | |
1536 | ||
1537 | emit ("/\052 %s \052/\n", p->nicename); | |
1538 | if (shortcut) | |
1539 | { | |
1540 | emit ("int <fop>_%04x(context,pc)\n", value); | |
1541 | } | |
1542 | else | |
1543 | { | |
1544 | emit ("int <fop>_%d(context,pc,iwords0)\n", p->idx); | |
1545 | emit ("int iwords0;\n"); | |
1546 | } | |
1547 | emit ("sim_state_type *context;\n"); | |
1548 | emit ("int pc;\n"); | |
1549 | emit ("{\n"); | |
1550 | emit ("register unsigned int tmp;\n"); | |
1551 | if (shortcut) | |
1552 | { | |
1553 | emit ("register unsigned int iwords0 = 0x%x;\n", value); | |
1554 | } | |
1555 | ||
1556 | /* work out how much bigger this opcode could be because it's large | |
1557 | model */ | |
1558 | if (BIG) | |
1559 | { | |
1560 | int i; | |
1561 | ||
1562 | extra = 0; | |
1563 | for (i = 0; i < 4; i++) | |
1564 | { | |
1565 | if ((p->arg_info[i] & CLASS_MASK) == CLASS_DA | |
1566 | || (p->arg_info[i] & CLASS_MASK) == CLASS_X) | |
1567 | extra += 2; | |
1568 | } | |
1569 | } | |
1570 | else | |
1571 | { | |
1572 | extra = 0; | |
1573 | } | |
1574 | printf (" /* Length %d */ \n", p->length + extra); | |
1575 | switch (p->length + extra) | |
1576 | { | |
1577 | case 2: | |
1578 | emit ("pc += 2\n;"); | |
1579 | break; | |
1580 | case 4: | |
1581 | emit ("register unsigned int iwords1 = get_word_mem_da(context,pc+2);\n"); | |
1582 | emit ("pc += 4;\n"); | |
1583 | break; | |
1584 | case 6: | |
1585 | ||
1586 | emit ("register unsigned int iwords1 = get_word_mem_da(context,pc+2);\n"); | |
1587 | emit ("register unsigned int iwords2 = get_word_mem_da(context,pc+4);\n"); | |
1588 | emit ("pc += 6;\n"); | |
1589 | break; | |
1590 | case 8: | |
1591 | emit ("register unsigned int iwords1 = get_word_mem_da(context,pc+2);\n"); | |
1592 | emit ("register unsigned int iwords2 = get_word_mem_da(context,pc+4);\n"); | |
1593 | emit ("register unsigned int iwords3 = get_word_mem_da(context,pc+6);\n"); | |
1594 | emit ("pc += 8;\n"); | |
1595 | break; | |
1596 | default: | |
1597 | break; | |
1598 | ||
1599 | } | |
1600 | emit ("context->cycles += %d;\n", p->cycles); | |
1601 | ||
1602 | emit ("{\n"); | |
1603 | info_args (p); | |
1604 | info_special (p, &getdst, &nostore, &before, &nosrc); | |
1605 | ||
1606 | info_lvals (p); | |
1607 | if (!nosrc) | |
1608 | { | |
1609 | info_fetch (p, getdst); | |
1610 | } | |
1611 | ||
1612 | if (before) | |
1613 | { | |
1614 | info_docode (p); | |
1615 | } | |
1616 | else | |
1617 | { | |
1618 | info_docode (p); | |
1619 | } | |
1620 | if (!nostore) | |
1621 | info_store (p); | |
1622 | emit ("}\n"); | |
1623 | emit ("return pc;\n"); | |
1624 | emit ("}\n"); | |
1625 | } | |
1626 | ||
1627 | void | |
1628 | static | |
1629 | one_instruction (i) | |
1630 | int i; | |
1631 | { | |
1632 | /* find the table entry */ | |
1633 | opcode_entry_type *p = z8k_table + i; | |
1634 | ||
1635 | if (!p) | |
1636 | return; | |
1637 | mangle (p, 0, 0); | |
1638 | } | |
1639 | ||
1640 | void | |
1641 | add_to_list (ptr, value) | |
1642 | struct opcode_value **ptr; | |
1643 | int value; | |
1644 | { | |
1645 | struct opcode_value *prev; | |
1646 | ||
1647 | prev = *ptr; | |
1648 | *ptr = (struct opcode_value *) malloc (sizeof (struct opcode_value)); | |
1649 | ||
1650 | (*ptr)->n = value; | |
1651 | (*ptr)->next = prev; | |
1652 | } | |
1653 | ||
1654 | void | |
1655 | build_list (i) | |
1656 | int i; | |
1657 | { | |
1658 | opcode_entry_type *p = lookup_inst (i); | |
1659 | ||
1660 | if (!p) | |
1661 | return; | |
1662 | add_to_list (&list[p->idx], i); | |
1663 | } | |
1664 | ||
1665 | int | |
1666 | main (ac, av) | |
1667 | int ac; | |
1668 | char **av; | |
1669 | { | |
1670 | int i; | |
1671 | int needcomma = 0; | |
1672 | ||
1673 | makelist = 0; | |
1674 | ||
1675 | for (i = 1; i < ac; i++) | |
1676 | { | |
1677 | if (strcmp (av[i], "-m") == 0) | |
1678 | makelist = 1; | |
1679 | if (strcmp (av[i], "-1") == 0) | |
1680 | file = 1; | |
1681 | if (strcmp (av[i], "-2") == 0) | |
1682 | file = 2; | |
1683 | if (strcmp (av[i], "-3") == 0) | |
1684 | file = 3; | |
1685 | if (strcmp (av[i], "-b3") == 0) | |
1686 | { | |
1687 | file = 3; | |
1688 | BIG = 1; | |
1689 | } | |
1690 | ||
1691 | } | |
1692 | if (makelist) | |
1693 | { | |
1694 | ||
1695 | int i; | |
1696 | needcomma = 0; | |
1697 | printf ("short int z8k_inv_list[] = {\n"); | |
1698 | ||
1699 | for (i = 0; i < 1 << 16; i++) | |
1700 | { | |
1701 | opcode_entry_type *p = lookup_inst (i); | |
1702 | ||
1703 | if(needcomma) | |
1704 | printf(","); | |
1705 | if ((i & 0xf) == 0) | |
1706 | printf ("\n"); | |
1707 | ||
1708 | #if 0 | |
1709 | printf ("\n /*%04x %s */", i, p ? p->nicename : ""); | |
1710 | #endif | |
1711 | ||
1712 | if (!p) | |
1713 | { | |
1714 | printf ("-1"); | |
1715 | } | |
1716 | else | |
1717 | { | |
1718 | printf ("%d", p->idx); | |
1719 | } | |
1720 | ||
1721 | if ((i & 0x3f) == 0 && DIRTY_HACK) | |
1722 | { | |
1723 | printf ("\n#ifdef __GNUC__\n"); | |
1724 | printf ("};\n"); | |
1725 | printf("short int int_list%d[] = {\n", i); | |
1726 | printf ("#else\n"); | |
1727 | printf (",\n"); | |
1728 | printf ("#endif\n"); | |
1729 | needcomma = 0; | |
1730 | } | |
1731 | else | |
1732 | needcomma = 1; | |
1733 | ||
1734 | } | |
1735 | printf ("};\n"); | |
1736 | return 1; | |
1737 | } | |
1738 | ||
1739 | /* First work out which opcodes use which bit patterns, | |
1740 | build a list of all matching bit pattens */ | |
1741 | for (i = 0; i < 1 << 16; i++) | |
1742 | { | |
1743 | build_list (i); | |
1744 | } | |
1745 | #if DUMP_LIST | |
1746 | for (i = 0; i < NOPS; i++) | |
1747 | { | |
1748 | struct opcode_value *p; | |
1749 | ||
1750 | printf ("%d,", i); | |
1751 | p = list[i]; | |
1752 | while (p) | |
1753 | { | |
1754 | printf (" %04x,", p->n); | |
1755 | p = p->next; | |
1756 | } | |
1757 | printf ("-1\n"); | |
1758 | } | |
1759 | ||
1760 | #endif | |
1761 | ||
1762 | if (file == 1) | |
1763 | { | |
1764 | extern int quick[]; | |
1765 | ||
1766 | /* Do the shortcuts */ | |
1767 | printf (" /* SHORTCUTS */\n"); | |
1768 | for (i = 0; quick[i]; i++) | |
1769 | { | |
1770 | int t = quick[i]; | |
1771 | ||
1772 | mangle (z8k_table + z8k_inv_list[t], | |
1773 | 1, | |
1774 | t); | |
1775 | } | |
1776 | } | |
1777 | if (file == 3) | |
1778 | { | |
1779 | printf (" /* NOT -SHORTCUTS */\n"); | |
1780 | for (i = 0; i < NOPS; i++) | |
1781 | { | |
1782 | if (list[i]) | |
1783 | { | |
1784 | one_instruction (i); | |
1785 | } | |
1786 | else | |
1787 | { | |
1788 | emit ("int <fop>_%d(context,pc)\n", i); | |
1789 | printf ("sim_state_type *context;\n"); | |
1790 | printf ("int pc;\n"); | |
1791 | emit ("{ <fop>_bad1();return pc; }\n"); | |
1792 | } | |
1793 | } | |
1794 | emit ("int <fop>_bad() ;\n"); | |
1795 | ||
1796 | /* Write the jump table */ | |
1797 | emit ("int (*(<fop>_table[]))() = {"); | |
1798 | needcomma = 0; | |
1799 | for (i = 0; i < NOPS; i++) | |
1800 | { | |
1801 | if (needcomma) | |
1802 | printf (","); | |
1803 | emit ("<fop>_%d\n", i); | |
1804 | needcomma = 1; | |
1805 | if ((i & 0x3f) == 0 && DIRTY_HACK) | |
1806 | { | |
1807 | printf ("#ifdef __GNUC__\n"); | |
1808 | printf ("};\n"); | |
1809 | emit ("int (*(<fop>_table%d[]))() = {\n", i); | |
1810 | printf ("#else\n"); | |
1811 | printf (",\n"); | |
1812 | printf ("#endif\n"); | |
1813 | needcomma = 0; | |
1814 | } | |
1815 | } | |
1816 | emit ("};\n"); | |
1817 | } | |
1818 | ||
1819 | if (file == 2) | |
1820 | { | |
1821 | extern int quick[]; | |
1822 | /* Static - since it's too be to be automatic on the apollo */ | |
1823 | static int big[64 * 1024]; | |
1824 | ||
1825 | for (i = 0; i < 64 * 1024; i++) | |
1826 | big[i] = 0; | |
1827 | ||
1828 | for (i = 0; quick[i]; i++) | |
1829 | { | |
1830 | #if 0 | |
1831 | ||
1832 | printf ("extern int <fop>_%04x();\n", quick[i]); | |
1833 | #endif | |
1834 | ||
1835 | big[quick[i]] = 1; | |
1836 | } | |
1837 | ||
1838 | for (i = 0; i < NOPS; i++) | |
1839 | { | |
1840 | #if 0 | |
1841 | printf ("extern int fop_%d();\n", i); | |
1842 | #endif | |
1843 | } | |
1844 | #if 0 | |
1845 | printf ("extern int fop_bad();\n"); | |
1846 | #endif | |
1847 | printf ("struct op_info op_info_table[] = {\n"); | |
1848 | for (i = 0; i < 1 << 16; i++) | |
1849 | { | |
1850 | int inv = z8k_inv_list[i]; | |
1851 | opcode_entry_type *p = z8k_table + inv; | |
1852 | ||
1853 | if (needcomma) | |
1854 | printf (","); | |
1855 | #if 0 | |
1856 | if (big[i]) | |
1857 | { | |
1858 | printf ("<fop>_%04x", i); | |
1859 | } | |
1860 | else | |
1861 | #endif | |
1862 | if (inv >= 0) | |
1863 | { | |
1864 | printf ("%d", inv); | |
1865 | } | |
1866 | else | |
1867 | printf ("400"); | |
1868 | if (inv >= 0) | |
1869 | { | |
1870 | printf (" /* %04x %s */\n", i, p->nicename); | |
1871 | } | |
1872 | else | |
1873 | { | |
1874 | printf ("\n"); | |
1875 | } | |
1876 | needcomma = 1; | |
1877 | if ((i & 0x3f) == 0 && DIRTY_HACK) | |
1878 | { | |
1879 | printf ("#ifdef __GNUC__\n"); | |
1880 | printf ("}; \n"); | |
1881 | printf ("struct op_info op_info_table%d[] = {\n", i); | |
1882 | printf ("#else\n"); | |
1883 | printf (",\n"); | |
1884 | ||
1885 | printf ("#endif\n"); | |
1886 | needcomma = 0; | |
1887 | } | |
1888 | } | |
1889 | printf ("};\n"); | |
1890 | ||
1891 | } | |
1892 | return 0; | |
1893 | } | |
1894 | ||
1895 | char * | |
1896 | insn_ptr (n) | |
1897 | int n; | |
1898 | { | |
1899 | if (BIG) | |
1900 | { | |
1901 | abort (); | |
1902 | } | |
1903 | ||
1904 | switch (n) | |
1905 | { | |
1906 | case 4: | |
1907 | return "iwords_1"; | |
1908 | default: | |
1909 | return "fail(context,123)"; | |
1910 | } | |
1911 | } | |
1912 | ||
1913 | /* work out if the opcode only wants lvalues */ | |
1914 | int | |
1915 | lvalue (p) | |
1916 | opcode_entry_type *p; | |
1917 | { | |
1918 | switch (p->opcode) | |
1919 | { | |
1920 | case OPC_lda: | |
1921 | return 1; | |
1922 | case OPC_call: | |
1923 | case OPC_jp: | |
1924 | return 1; | |
1925 | default: | |
1926 | return 0; | |
1927 | } | |
1928 | } | |
1929 | ||
1930 | int | |
1931 | info_len_in_words (o) | |
1932 | opcode_entry_type *o; | |
1933 | { | |
1934 | unsigned int *p = o->byte_info; | |
1935 | int nibs = 0; | |
1936 | ||
1937 | while (*p) | |
1938 | { | |
1939 | switch (*p & CLASS_MASK) | |
1940 | { | |
1941 | case CLASS_BIT: | |
1942 | case CLASS_REGN0: | |
1943 | case CLASS_REG: | |
1944 | case CLASS_01II: | |
1945 | case CLASS_00II: | |
1946 | nibs++; | |
1947 | break; | |
1948 | case CLASS_ADDRESS: | |
1949 | nibs += SIZE_ADDRESS; | |
1950 | break; | |
1951 | case CLASS_IMM: | |
1952 | switch (*p & ~CLASS_MASK) | |
1953 | { | |
1954 | case ARG_IMM16: | |
1955 | nibs += 4; | |
1956 | break; | |
1957 | case ARG_IMM32: | |
1958 | nibs += 8; | |
1959 | break; | |
1960 | case ARG_IMM2: | |
1961 | case ARG_IMM4: | |
1962 | case ARG_IMM4M1: | |
1963 | case ARG_IMM_1: | |
1964 | case ARG_IMM_2: | |
1965 | case ARG_IMMNMINUS1: | |
1966 | nibs++; | |
1967 | break; | |
1968 | case ARG_NIM8: | |
1969 | ||
1970 | case ARG_IMM8: | |
1971 | nibs += 2; | |
1972 | break; | |
1973 | default: | |
1974 | abort (); | |
1975 | } | |
1976 | break; | |
1977 | case CLASS_DISP: | |
1978 | switch (*p & ~CLASS_MASK) | |
1979 | { | |
1980 | case ARG_DISP16: | |
1981 | nibs += 4; | |
1982 | break; | |
1983 | case ARG_DISP12: | |
1984 | nibs += 3; | |
1985 | break; | |
1986 | case ARG_DISP8: | |
1987 | nibs += 2; | |
1988 | break; | |
1989 | default: | |
1990 | abort (); | |
1991 | } | |
1992 | break; | |
1993 | case CLASS_0DISP7: | |
1994 | case CLASS_1DISP7: | |
1995 | case CLASS_DISP8: | |
1996 | nibs += 2; | |
1997 | break; | |
1998 | case CLASS_BIT_1OR2: | |
1999 | case CLASS_0CCC: | |
2000 | case CLASS_1CCC: | |
2001 | case CLASS_CC: | |
2002 | nibs++; | |
2003 | break; | |
2004 | default: | |
2005 | emit ("don't know %x\n", *p); | |
2006 | } | |
2007 | p++; | |
2008 | } | |
2009 | ||
2010 | return nibs / 4; /* return umber of words */ | |
2011 | } |