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