writecode.c 37.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
/* generate instructions for Z8KSIM

   Copyright 1992, 1993, 2002 Free Software Foundation, Inc.

This file is part of Z8KSIM

Z8KSIM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Z8KSIM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Z8KZIM; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

/* This program generates the code which emulates each of the z8k
   instructions

   code goes into three files, tc-gen1.h, tc-gen2.h and tc-gen3.h.
   which file being made depends upon the options

   -1  tc-gen1.h contains the fully expanded code for some selected
       opcodes, (those in the quick.c list)

   -2   tc-gen2.h contains a list of pointers to functions, one for each
   opcode.  It points to functions in tc-gen3.h and tc-gen1.h
   depending upon quick.c

   -3   tc-gen3.h contains all the opcodes in unexpanded form.

   -b3   tc-genb3.h same as -3 but for long pointers

   */

/* steve chamberlain
   sac@cygnus.com */

#include "config.h"

#include <ansidecl.h>
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#else
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#endif

#define NICENAMES

#define DEFINE_TABLE
#include "../opcodes/z8k-opc.h"

#define NOPS 500

struct opcode_value
{
  int n;
  struct opcode_value *next;
};

#define NICENAMES
int BIG;

static char *reg_names[] =
{"bad", "src", "dst", "aux_a", "aux_b", "aux_r", "aux_x"};

#define IS_DST(x) ((x & 0xf) == 2)
#define IS_SRC(x) ((x & 0xf)==1)
#define SIZE_ADDRESS (BIG ? 8 : 4)	/* number of nibbles in a ptr*/

static int file;

static int nibs = 0;

static char *current_size;
static char *current_name;
static char current_word0[40];
static char current_byte0[40];
static char current_byte1[40];
static int indent;
static char *p;
static char *d;

struct opcode_value *list[NOPS];

static opcode_entry_type *
lookup_inst (what)
     int what;
{
  static short *z8k_inv_list = NULL;
  const nr_z8k_inv_list_elements = 1 << 16;
  if (z8k_inv_list == NULL)
    {
      /* Initialize the list to 0xff == -1 */
      z8k_inv_list = calloc (nr_z8k_inv_list_elements, sizeof (short));
      memset (z8k_inv_list, 0xff, nr_z8k_inv_list_elements * sizeof (short));
    }
  /* Entry empty? Fill it in.  */
  if (z8k_inv_list[what] == -1)
    {

      int nibl_index;
      int nibl_matched;
      unsigned short instr_nibl;
      unsigned short tabl_datum, datum_class, datum_value;
      char instr_nibbles[8];

      opcode_entry_type *ptr = z8k_table;

      nibl_matched = 0;

      instr_nibbles[3] = (what >> 0) & 0xf;
      instr_nibbles[2] = (what >> 4) & 0xf;
      instr_nibbles[1] = (what >> 8) & 0xf;
      instr_nibbles[0] = (what >> 12) & 0xf;

      /* Assume it won't be found.  */
      z8k_inv_list[what] = -2;

      while (ptr->name)
	{
	  nibl_matched = 1;
	  for (nibl_index = 0; nibl_index < 4 && nibl_matched; nibl_index++)
	    {
	      instr_nibl = instr_nibbles[nibl_index];

	      tabl_datum = ptr->byte_info[nibl_index];
	      datum_class = tabl_datum & CLASS_MASK;
	      datum_value = ~CLASS_MASK & tabl_datum;

	      switch (datum_class)
		{
		case CLASS_BIT_1OR2:
		  if (datum_value != (instr_nibl & ~0x2))
		    nibl_matched = 0;
		  break;

		case CLASS_IGNORE:
		  break;
		case CLASS_BIT:
		  if (datum_value != instr_nibl)
		    nibl_matched = 0;
		  break;
		case CLASS_00II:
		  if (!((~instr_nibl) & 0x4))
		    nibl_matched = 0;
		  break;
		case CLASS_01II:
		  if (!(instr_nibl & 0x4))
		    nibl_matched = 0;
		  break;
		case CLASS_0CCC:
		  if (!((~instr_nibl) & 0x8))
		    nibl_matched = 0;
		  break;
		case CLASS_1CCC:
		  if (!(instr_nibl & 0x8))
		    nibl_matched = 0;
		  break;
		case CLASS_0DISP7:
		  if (!((~instr_nibl) & 0x8))
		    nibl_matched = 0;
		  nibl_index += 1;
		  break;
		case CLASS_1DISP7:
		  if (!(instr_nibl & 0x8))
		    nibl_matched = 0;
		  nibl_index += 1;
		  break;
		case CLASS_REGN0:
		  if (instr_nibl == 0)
		    nibl_matched = 0;
		  break;
		default:
		  break;
		}
	    }
	  if (nibl_matched)
	    {
	      z8k_inv_list[what] = ptr->idx;
	      break; /* while */
	    }
	  ptr++;
	}
    }
  if (z8k_inv_list[what] >= 0)
    return z8k_table + z8k_inv_list[what];
  return 0;
}

static char *
insn_4 (n)
     int n;
{
  switch (n)
    {
    case 1:
      return "((iwords_0>>8) & 0xf)";
    case 2:
      return "((ibytes_1 >> 4) & 0xf)";
    case 3:
      return "((ibytes_1) & 0xf)";
    case 4:
      return "((ibytes_2>>4) & 0xf)";
    case 5:
      return "((ibytes_2) & 0xf)";
    case 6:
      return "((ibytes_3 >> 4) & 0xf)";
    case 7:
      return "((ibytes_3) & 0xf)";
    default:
      return "****";
    }
}

char *
ptr_mode ()
{
  if (BIG)
    {
      return "ptr_long";
    }
  return "word";
}

static
char *
ptr_size ()
{
  if (BIG)
    return "4";
  return "2";
}

static char *
reg_n (x)
     unsigned int x;
{
  return reg_names[x & 0xf];
}

char *
stack_ptr ()
{
  return BIG ? "14" : "15";
}

char *
mem ()
{
#if 0
  return BIG ? "segmem" : "unsegmem";
#else
  return "mem";
#endif
}

int
match (a)
     char *a;
{
  if (strncmp (p, a, strlen (a)) == 0)
    {
      p += strlen (a);
      return 1;
    }
  return 0;
}

static
void
sub (y)
     char *y;
{
  sprintf (d, "%s", y);
  d += strlen (d);
}

static char *
insn_16 (n)
     int n;
{
  switch (n)
    {
    case 0:
      return "(iwords_0)";
    case 4:
      return "(iwords_1)";
    case 8:
      return "(iwords_2)";
    case 12:
      return "(iwords_3)";
    default:
      return "****";
    }
}

static
char *
insn_32 (n)
     int n;
{
  switch (n)
    {
    case 0:
      return "((iwords_0<<16) | (iwords_1))";
    case 4:
      return "((iwords_1<<16) | (iwords_2))";
    case 8:
      return "((iwords_2<<16) | (iwords_3))";
    default:
      return "****";
    }
}

static char *
size_name (x)
     int x;
{
  switch (x)
    {
    case 8:
      return "byte";
    case 16:
      return "word";
    case 32:
      return "long";
    case 64:
      return "quad";
    }
  return "!!";
}

/*VARARGS*/
void
emit (string, a1, a2, a3, a4, a5)
     char *string;
     char* a1;
     char* a2;
     char* a3;
     char* a4;
     char* a5;
{
  int indent_inc = 0;
  int indent_dec = 0;
  int i;
  char buffer[1000];

  d = buffer;
  p = string;

  while (*p)
    {
      if (match ("<fop>"))
	{
	  if (BIG)
	    {
	      sub ("bfop");
	    }
	  else
	    {
	      sub ("sfop");
	    }
	}
      else if (match ("<iptr>"))
	{
	  if (BIG)
	    {
	      switch (nibs)
		{
		case 4:
		  sub ("(((iwords_1 << 8) | (iwords_2)) & 0x7fffff)");
		  break;
		default:
		  sub ("fail(context,124)");
		  break;
		}
	    }
	  else
	    {
	      switch (nibs)
		{
		case 4:
		  sub ("iwords_1");
		  break;
		default:
		  sub ("fail(context,123)");
		  break;
		}
	    }
	}
      else if (match ("<name>"))
	{
	  sub (current_name);
	}
      else if (match ("<size>"))
	{
	  sub (current_size);
	}
      else if (match ("<insn_4>"))
	{
	  sub (insn_4 (nibs));
	}
      else if (match ("<insn_16>"))
	{
	  sub (insn_16 (nibs));
	}
      else if (match ("<insn_32>"))
	{
	  sub (insn_32 (nibs));
	}
      else if (match ("iwords_0"))
	{
	  sub (current_word0);
	}
      else if (match ("ibytes_0"))
	{
	  sub (current_byte0);
	}
      else if (match ("<ibytes_1>"))
	{
	  sub (current_byte1);
	}
      else if (match ("<next_size>"))
	{
	  if (strcmp (current_size, "word") == 0)
	    sub ("long");
	  if (strcmp (current_size, "byte") == 0)
	    sub ("word");
	  else if (strcmp (current_size, "long") == 0)
	    sub ("quad");
	  else
	    abort ();
	}
      else if (match ("<addr_type>"))
	{
	  if (BIG)
	    sub ("unsigned long");
	  else
	    sub ("unsigned short");
	}

      else if (match ("<c_size>"))
	{
	  if (strcmp (current_size, "word") == 0)
	    sub ("short");
	  else if (strcmp (current_size, "byte") == 0)
	    sub ("char");
	  else if (strcmp (current_size, "long") == 0)
	    sub ("long");
	  else
	    abort ();
	}

      else if (match ("<pc>"))
	{
	  sub ("pc");
	}
      else if (match ("<mem>"))
	{
	  sub (mem ());
	}
      else if (match ("<sp>"))
	{
	  sub (stack_ptr ());
	}
      else if (match ("<ptr_size>"))
	{
	  sub (ptr_size ());
	}
      else if (match ("<ptr_mode>"))
	{
	  sub (ptr_mode ());
	}
      else if (match ("<insn_8>"))
	{
	  switch (nibs)
	    {
	    case 2:
	      sub ("(iwords_0&0xff)");
	      break;
	    case 4:
	      sub ("(iwords_1>>8)");
	      break;
	    case 6:
	      sub ("(iwords_1&0xff)");
	      break;
	    case 8:
	      sub ("(iwords_2>>8)");
	      break;
	    case 12:
	      sub ("(/* WHO */iwords_3&0xff)");
	      break;
	    default:
	      abort ();
	    }
	}
      else
	{
	  if (*p == '{')
	    indent_inc++;
	  if (*p == '}')
	    indent_dec++;
	  *d++ = *p;
	  p++;
	}
    }
  *d++ = 0;
  indent -= indent_dec;
  for (i = 0; i < indent; i++)
    printf ("\t");
  indent += indent_inc;
  printf (buffer, a1, a2, a3, a4, a5);

}

/* fetch the lvalues of the operands */
void
info_args (p)
     opcode_entry_type *p;
{
  unsigned int *s;

  int done_one_imm8 = 0;

  /* int done_read = 4;*/
  s = p->byte_info;
  nibs = 0;
  while (*s)
    {
      switch (*s & CLASS_MASK)
	{
	case CLASS_BIT_1OR2:
	  emit ("register unsigned int imm_src=(<insn_4>& 2)?2:1;\n");
	  break;
	case CLASS_IGNORE:
	case CLASS_BIT:
	  /* Just ignore these, we've already decoded this bit */
	  nibs++;
	  break;
	case CLASS_REGN0:
	case CLASS_REG:
	  /* this nibble tells us which register to use as an arg,
	     if we've already gobbled the nibble we know what to use */
	  {
	    int regname = *s & 0xf;

	    emit ("register unsigned int reg_%s=<insn_4>;\n",
		  reg_names[regname]);

	    nibs++;
	  }
	  break;
	case CLASS_ADDRESS:
	  emit ("register unsigned base_%s=<iptr>;\n", reg_n (*s));

	  nibs += SIZE_ADDRESS;

	  break;
	case CLASS_01II:
	case CLASS_00II:
	  emit ("register unsigned int imm_src=<insn_4>&0x2;\n");
	  nibs++;
	  break;
	case CLASS_FLAGS:
		emit ("register unsigned int imm_src=<insn_4>;\n");
		nibs++;
break;	  
	case CLASS_IMM:
	  /* Work out the size of the think to fetch */

	  {
	    switch (*s & ~CLASS_MASK)
	      {
	      case ARG_IMM16:
		emit ("register unsigned imm_src=<insn_16>;\n");
		nibs += 4;
		break;
	      case ARG_IMM32:
		emit ("register unsigned int imm_src= %s;\n", insn_32 (nibs));
		nibs += 8;
		break;
	      case ARG_IMM4:
		emit ("register unsigned int imm_src=<insn_4>;\n");
		nibs++;
		break;
	      case ARG_NIM4:
		emit ("register unsigned int imm_src = - <insn_4>;\n");
		nibs++;
		break;
	      case ARG_IMM2:
		emit ("register unsigned int imm_src=<insn_4> & 0x2;\n");
		nibs++;
		break;

	      case ARG_IMM4M1:
		emit ("register unsigned int imm_src=(<insn_4> + 1);\n");
		nibs++;
		break;
	      case ARG_IMM_1:
		emit ("register unsigned int imm_src=1;\n");
		break;
	      case ARG_IMM_2:
		emit ("register unsigned int imm_src=2;\n");
		break;
	      case ARG_NIM8:
		emit ("register unsigned int imm_src=-<insn_8>;\n");
		nibs += 2;
		break;
	      case ARG_IMM8:
		if (!done_one_imm8)
		  {
		    emit ("register unsigned int imm_src=<insn_8>;\n");
		    nibs += 2;
		    done_one_imm8 = 1;
		  }
		break;
	      default:
		emit ("register int fail%d=fail(context,1);\n", nibs);
		break;
	      }
	    break;

	case CLASS_DISP8:
	    /* We can't use `(char)' since char might be unsigned.
	       We can't use `(signed char)' because the compiler might be K&R.
	       This seems safe, since it only assumes that bytes are 8
	       bits.  */
	    emit ("register unsigned int oplval_dst=((ibytes_1 << (sizeof (int) * 8 - 8)) >> (sizeof (int) * 8 - 9)) + pc;\n");
#if 0
	    /* Original code: fails if characters are unsigned.  */
	    emit ("register unsigned int oplval_dst=(((char)ibytes_1)<<1) + pc;\n");
#endif
	    nibs += 2;
	    break;
	case CLASS_CC:
	    emit ("register unsigned int op_cc=<insn_4>;\n");
	    nibs++;
	    break;
	default:
	    emit ("register int FAIL%d=fail(context,2);\n", nibs);
	    break;
	  }
	  ;
	  /* work out how to fetch the immediate value */
	}

      s++;
    }
}

void
info_special (p, getdst, nostore, before, nosrc)
     opcode_entry_type *p;
     int *getdst;
     int *nostore;
     int *before;
     int *nosrc;
{
  switch (p->opcode)
    {
    case OPC_exts:
    case OPC_extsb:
    case OPC_extsl:
      *nostore = 1;
      *nosrc = 1;
      break;
    case OPC_ldm:
      *nostore = 1;
      *nosrc = 1;
      break;
    case OPC_negb:
    case OPC_neg:
    case OPC_sla:
    case OPC_slab:
    case OPC_slal:
    case OPC_sda:
    case OPC_sdab:
    case OPC_sdal:
    case OPC_com:
    case OPC_comb:
    case OPC_adc:
    case OPC_sbc:
    case OPC_nop:
    case OPC_adcb:
    case OPC_add:
    case OPC_addb:
    case OPC_addl:
    case OPC_inc:
    case OPC_sub:
    case OPC_subb:
    case OPC_subl:
    case OPC_and:
    case OPC_andb:
    case OPC_xorb:
    case OPC_xor:
      break;

    case OPC_mult:
    case OPC_multl:
    case OPC_div:
    case OPC_divl:

      *nostore = 1;
      break;

    case OPC_testb:
    case OPC_test:
    case OPC_testl:
    case OPC_cp:
    case OPC_cpb:
    case OPC_cpl:
    case OPC_bit:
      *nostore = 1;
      *before = 0;
      break;

    case OPC_bpt:
    case OPC_jr:
    case OPC_jp:
    case OPC_ret:
    case OPC_call:
    case OPC_tcc:
      *nosrc = 1;
      *nostore = 1;
      *before = 1;
      break;
    case OPC_sc:
      *nostore = 1;
      *before = 0;
      break;
    case OPC_clrb:
    case OPC_clr:
      *before = 1;
      *nosrc = 1;
      break;
    case OPC_ldi:
    case OPC_ldib:
    case OPC_lddb:
    case OPC_ldd:

      *before = 1;
      *nostore = 1;
      *nosrc = 1;
      break;
    case OPC_ldk:
    case OPC_ld:
    case OPC_ldb:
    case OPC_ldl:
      *before = 1;
      *getdst = 0;
      break;
    case OPC_push:
    case OPC_pushl:
    case OPC_pop:
    case OPC_popl:
      *before = 1;
      *getdst = 0;
      break;
    case OPC_lda:
      *nosrc = 1;
      break;
    }
}

/* calculate the lvalues required for the opcode */
void
info_lvals (p)
     opcode_entry_type *p;
{
  /* emit code to work out lvalues, if any */
  unsigned int *i = p->arg_info;

  while (*i)
    {
      current_name = reg_n (*i);
      current_size = size_name (p->type);
      switch (*i & CLASS_MASK)
	{
	case CLASS_X:
	  /* address(reg) */
	  emit ("register  <addr_type> oplval_<name>= ((base_<name> + (short)get_word_reg(context,reg_<name>)) & 0xffff) + (base_<name> & ~0xffff);\n");
	  break;
	case CLASS_IR:
	  /* Indirect register */
	  emit ("register  int oplval_<name> = get_<ptr_mode>_reg(context,reg_<name>);\n");
	  break;
	case CLASS_DA:
	  emit ("register  int oplval_<name>=base_<name>;\n");
	  break;
	case CLASS_IMM:
	case CLASS_REG_WORD:
	case CLASS_REG_LONG:
	case CLASS_REG_BYTE:
	case CLASS_PR:
	  break;
	case CLASS_BA:
	  emit ("register  int oplval_<name> = get_<ptr_mode>_reg(context,reg_<name>) + (short)(imm_src);\n");
	  break;
	case CLASS_BX:
	  emit ("register  int oplval_<name> = get_<ptr_mode>_reg(context,reg_<name>)\n");
	  emit ("  + get_word_reg(context,reg_aux_x);\n");
	  break;
	}
      i++;
    }
}

/* emit code to fetch the args from calculated lvalues */
int allregs;
void
info_fetch (p, getdst)
     opcode_entry_type *p;
     int getdst;
{
  unsigned int *i = p->arg_info;
  int had_src = 0;

  allregs = 1;
  while (*i)
    {

      current_name = reg_n (*i);
      current_size = size_name (p->type);
      switch (*i & CLASS_MASK)
	{
	case CLASS_X:
	case CLASS_IR:
	case CLASS_BA:
	case CLASS_BX:
	case CLASS_DA:
	  if (!getdst && IS_DST (*i))
	    break;
	  emit ("register int op_<name>= get_<size>_<mem>_da(context,oplval_<name>);\n");
	  allregs = 0;
	  break;
	case CLASS_IMM:
	  if (!had_src)
	    {
	      if (p->opcode == OPC_out ||
		  p->opcode == OPC_outb ||
		  p->opcode == OPC_sout ||
		  p->opcode == OPC_soutb)
		{
		  /* The imm is a dest here */
		  emit ("register int op_dst = imm_src;\n");
		}
	      else
		{
		  emit ("register int op_src = imm_src;\n");
		}
	    }
	  break;
	case CLASS_REG_QUAD:
	  if (!getdst && IS_DST (*i))
	    break;
	  had_src |= IS_SRC (*i);
	  emit ("UDItype op_<name> ;\n");

	  break;
	case CLASS_REG_WORD:
	  if (!getdst && IS_DST (*i))
	    break;
	  had_src |= IS_SRC (*i);
	  emit ("register int op_<name> = get_word_reg(context,reg_<name>);\n");
	  break;

	case CLASS_REG_LONG:
	  if (!getdst && IS_DST (*i))
	    break;
	  had_src |= IS_SRC (*i);
	  emit ("register int op_<name> = get_long_reg(context,reg_<name>);\n");
	  break;
	case CLASS_REG_BYTE:
	  if (!getdst && IS_DST (*i))
	    break;
	  had_src |= IS_SRC (*i);
	  emit ("register int op_<name> = get_byte_reg(context,reg_<name>);\n");
	  break;
	}
      i++;
    }
}

static void
normal_flags (p, s, neg)
     opcode_entry_type *p;
     char *s;
{
  emit (" %s;\n", s);
  emit ("NORMAL_FLAGS(context,%d, tmp,  op_dst, op_src,%d); \n", p->type,neg);
}

static void
test_normal_flags (p, s, opt)
     opcode_entry_type *p;
     char *s;
     int opt;
{
  emit (" %s;\n", s);
  if (0 && opt)
    {
      emit ("context->broken_flags = TST_FLAGS;\n");
      emit ("context->size = %d;\n", p->type);
    }
  else
    {
      emit ("TEST_NORMAL_FLAGS(context,%d, tmp); \n", p->type);
    }

}

static void
optimize_normal_flags (p, s,neg)
     opcode_entry_type *p;
     char *s;
{
  emit (" %s;\n", s);
#if 0
  emit ("context->broken_flags = CMP_FLAGS;\n");
#else
  emit ("NORMAL_FLAGS(context,%d, tmp,  op_dst, op_src,%d); \n", p->type, neg);
#endif
}

static
void
jp (p)
     opcode_entry_type *p;
{

  emit ("if(op_cc == 8 || COND(context,op_cc)) pc = oplval_dst;\n");
}

static void
jr (p)
     opcode_entry_type *p;
{
  emit ("if(op_cc == 8 || COND(context,op_cc)) pc = oplval_dst;\n");
}

static void
ret (p)
     opcode_entry_type *p;
{
  emit ("if(op_cc == 8 || COND(context,op_cc))\n{\n");
  emit ("pc = get_<ptr_mode>_<mem>_ir(context,<sp>);\n");
  emit ("put_<ptr_mode>_reg(context,<sp>, get_<ptr_mode>_reg(context,<sp>) + <ptr_size>);\n");
  emit ("};\n");
}

static void
call (p)
     opcode_entry_type *p;
{
  emit ("put_<ptr_mode>_reg(context,<sp>,tmp =  get_<ptr_mode>_reg(context,<sp>) - <ptr_size>);\n");
  emit ("put_<ptr_mode>_<mem>_da(context,tmp, pc);\n");
  emit ("pc = oplval_dst;\n");
}

static void
push (p)
     opcode_entry_type *p;
{
  emit ("tmp = op_src;\n");
  emit ("oplval_dst -= %d;\n", p->type / 8);
  emit ("put_<ptr_mode>_reg(context,reg_dst, oplval_dst);\n");
}

static void
pop (p)
     opcode_entry_type *p;
{
  emit ("tmp = op_src;\n");
  emit ("put_<ptr_mode>_reg(context,reg_src, oplval_src + %d);\n", p->type / 8);
}

static void
ld (p)
     opcode_entry_type *p;
{
  emit ("tmp = op_src;\n");
}

static void
sc ()
{
  emit ("support_call(context,imm_src);\n");
}

static void
bpt ()
{
  emit ("pc -=2; \n");
  emit ("context->exception = SIM_BREAKPOINT;\n");
}

static void
ldi (p, size, inc)
     opcode_entry_type *p;
     int size;
     int inc;
{
  int dinc = (size / 8) * inc;

  current_size = size_name (size);
  emit ("{ \n");
  emit ("int type = %s;\n", insn_4 (7));
  emit ("int rs = get_<ptr_mode>_reg(context,reg_src);\n");
  emit ("int rd = get_<ptr_mode>_reg(context,reg_dst);\n");
  emit ("int rr = get_word_reg(context,reg_aux_r);\n");
  emit ("do {\n");
  emit ("put_<size>_<mem>_da(context,rd, get_<size>_<mem>_da(context,rs));\n");
  emit ("rd += %d;\n", dinc);
  emit ("rs += %d;\n", dinc);
  emit ("rr --;\n");
  emit ("context->cycles += 9;\n");
  emit ("} while (!type && rr != 0 && context->exception <= 1);\n");
  emit ("if (context->exception>1) pc -=4;\n");
  emit ("put_<ptr_mode>_reg(context,reg_src, rs);\n");
  emit ("put_<ptr_mode>_reg(context,reg_dst, rd);\n");
  emit ("put_word_reg(context,reg_aux_r, rr);\n");
  emit ("}\n");

}

static void
shift (p, arith)
     opcode_entry_type *p;
     int arith;
{

  /* We can't use `(char)' since char might be unsigned.
     We can't use `(signed char)' because the compiler might be K&R.
     This seems safe, since it only assumes that bytes are 8 bits.  */
  emit ("op_src = (op_src << (sizeof (int) * 8 - 8)) >> (sizeof (int) * 8 - 8);\n");
#if 0
  /* Original code: fails if characters are unsigned.  */
  emit ("op_src = (char)op_src;\n");
#endif
  emit ("if (op_src < 0) \n");
  emit ("{\n");
  emit ("op_src = -op_src;\n");
  emit ("op_dst = (%s <c_size>)op_dst;\n", arith ? "" : "unsigned");
  emit ("tmp = (%s op_dst) >> op_src;\n", arith ? "" : "(unsigned)");
  emit ("context->carry = op_dst >> (op_src-1);\n", p->type);
  emit ("}\n");
  emit ("else\n");
  emit ("{\n");
  emit ("tmp = op_dst << op_src;\n");
  emit ("context->carry = op_dst >> (%d - op_src);\n", p->type);
  emit ("}\n");
  emit ("context->zero = (<c_size>)tmp == 0;\n");
  emit ("context->sign = (int)((<c_size>)tmp) < 0;\n");
  emit ("context->overflow = ((int)tmp < 0) != ((int)op_dst < 0);\n");
  emit ("context->cycles += 3*op_src;\n");
  emit ("context->broken_flags = 0;\n");

}

static void
rotate (p, through_carry, size, left)
     opcode_entry_type *p;
     int through_carry;
     int size;
     int left;
{

  if (!left)
    {
      emit ("while (op_src--) {\n");
      emit ("int rotbit;\n");
      emit ("rotbit = op_dst & 1;\n");
      emit ("op_dst = ((unsigned)op_dst) >> 1;\n");

      if (through_carry)
	{
	  emit ("op_dst |= context->carry << %d;\n", size - 1);
	}
      else
	{
	  emit ("op_dst |= rotbit << %d;\n", size - 1);
	}
      emit ("context->carry = rotbit;\n");
      emit ("}\n");
    }
  else
    {
      emit ("while (op_src--) {\n");
      emit ("int rotbit;\n");

      emit ("rotbit = (op_dst >> (%d))&1;\n", size - 1);
      emit ("op_dst <<=1;\n");
      if (through_carry)
	{
	  emit ("if (context->carry) op_dst |=1;\n");
	}
      else
	{
	  emit ("if (rotbit) op_dst |= 1;\n");
	}
      emit ("context->carry = rotbit;\n");
      emit ("}\n");
    }
  emit ("tmp = (<c_size>)op_dst;\n");
  emit ("context->zero = tmp == 0;\n");
  emit ("context->sign = (int)tmp < 0;\n");
  emit ("context->overflow = ((int)tmp < 0) != ((int)op_dst < 0);\n");
  emit ("context->cycles += 3*op_src;\n");
  emit ("context->broken_flags = 0;\n");

}

static void
adiv (p)
     opcode_entry_type *p;
{
  emit ("if (op_src==0)\n");
  emit ("{\n");
  emit ("context->exception = SIM_DIV_ZERO;\n");
  emit ("}\n");
  emit ("else\n");
  emit ("{\n");

  if (p->type == 32)
    {
      emit ("op_dst.low = (int)get_long_reg(context,reg_dst+2);\n");
      emit ("op_dst.high = (int)get_long_reg(context,reg_dst+0);\n");
#ifdef __GNUC__
      emit ("tmp = (((long long)op_dst.high << 32) + (op_dst.low)) / (int)op_src;\n");
#else
      emit ("tmp = (long)op_dst.low / (int)op_src;\n");
#endif
      emit ("put_long_reg(context,reg_dst+2, tmp);\n");
#ifdef __GNUC__
      emit ("put_long_reg(context,reg_dst, (((long long)op_dst.high << 32) + (op_dst.low)) %% (int)op_src);\n");
#else
      emit ("put_long_reg(context,reg_dst, (int)op_dst.low %% (int)op_src);\n");
#endif

      emit ("context->zero = op_src == 0 || (op_dst.low==0 && op_dst.high==0);\n");
    }
  else
    {
      emit ("tmp = (long)op_dst / (short)op_src;\n");
      emit ("put_word_reg(context,reg_dst+1, tmp);\n");
      emit ("put_word_reg(context,reg_dst, (long) op_dst %% (short)op_src);\n");
      emit ("context->zero = op_src == 0 || op_dst==0;\n");
    }

  emit ("context->sign = (int)tmp < 0;\n");
  emit ("context->overflow =(tmp & 0x%x) != 0;\n",
	~((1 << (p->type)) - 1));
  emit ("context->carry = (tmp & 0x%x) != 0;\n",
	~(1 << (p->type)));

  emit ("}\n");
}

static void
dobit (p)
opcode_entry_type *p;
{
  emit("context->zero = (op_dst & (1<<op_src))==0;\n");
  emit("context->broken_flags = 0;\n");
}
static void
doset (p, v)
opcode_entry_type*p;
int v;
{
  if (v) 
    emit (" tmp = op_dst | (1<< op_src);\n");
  else
    emit (" tmp = op_dst & ~(1<< op_src);\n");
}

static void
mult (p)
     opcode_entry_type *p;
{

  if (p->type == 32)
    {
      emit ("op_dst.low =  get_long_reg(context,reg_dst+2);\n");
      emit ("tmp = op_dst.low * op_src;\n");
      emit ("put_long_reg(context,reg_dst+2, tmp);\n");
      emit ("put_long_reg(context,reg_dst, 0);\n");
    }
  else
    {
      emit ("op_dst =  get_word_reg(context,reg_dst+1);\n");
      emit ("tmp = op_dst * op_src;\n");
      emit ("put_long_reg(context,reg_dst, tmp);\n");
    }

  emit ("context->sign = (int)tmp < 0;\n");
  emit ("context->overflow =0;\n");
  emit ("context->carry = (tmp & 0x%x) != 0;\n", ~((1 << (p->type)) - 1));
  emit ("context->zero = tmp == 0;\n");

}

static void
exts (p)
     opcode_entry_type *p;
{
  /* Fetch the ls part of the src */
  current_size = size_name (p->type * 2);

  if (p->type == 32)
    {
      emit ("tmp= get_long_reg(context,reg_dst+2);\n");
      emit ("if (tmp & (1<<%d)) {\n", p->type - 1);
      emit ("put_long_reg(context,reg_dst, 0xffffffff);\n");
      emit ("}\n");
      emit ("else\n");
      emit ("{\n");
      emit ("put_long_reg(context,reg_dst, 0);\n");
      emit ("}\n");
    }
  else
    {
      emit ("tmp= get_<size>_reg(context,reg_dst);\n");
      emit ("if (tmp & (1<<%d)) {\n", p->type - 1);
      emit ("tmp |= 0x%x;\n", ~((1 << p->type) - 1));
      emit ("}\n");
      emit ("else\n");
      emit ("{\n");

      emit ("tmp &= 0x%x;\n", ((1 << p->type) - 1));
      emit ("}\n");
      emit ("put_<size>_reg(context,reg_dst, tmp);\n");
    }
}
doflag(on)
int on;
{
  /* Load up the flags */
  emit(" COND (context, 0x0b);\n");

  if (on)
    emit ("{ int on =1;\n ");
  else
    emit ("{ int on =0;\n ");

  emit ("if (imm_src & 1)\n");
  emit ("PSW_OVERFLOW = on;\n");

  emit ("if (imm_src & 2)\n");
  emit ("PSW_SIGN = on;\n");

  emit ("if (imm_src & 4)\n");
  emit ("PSW_ZERO = on;\n");

  emit ("if (imm_src & 8)\n");
  emit ("PSW_CARRY = on;\n");
  emit("}\n");


}
/* emit code to perform operation */
void
info_docode (p)
     opcode_entry_type *p;
{
  switch (p->opcode)
    {
    case OPC_clr:
    case OPC_clrb:
      emit ("tmp = 0;\n");
      break;
    case OPC_ex:
    case OPC_exb:

      emit ("tmp = op_src; \n");
      if (allregs)
	{
	  emit ("put_<size>_reg(context,reg_src, op_dst);\n");
	}
      else
	{
	  emit ("put_<size>_mem_da(context, oplval_src, op_dst);\n");
	}
      break;
    case OPC_adc:
    case OPC_adcb:
      normal_flags (p, "op_src += COND(context,7);tmp = op_dst + op_src ;",0);
      break;
    case OPC_sbc:
      normal_flags (p, "op_src +=  COND(context,7);tmp = op_dst - op_src ;",1);
      break;
    case OPC_nop:
      break;
    case OPC_com:
    case OPC_comb:
      test_normal_flags (p, "tmp = ~ op_dst", 1);
      break;
    case OPC_and:
    case OPC_andb:
      test_normal_flags (p, "tmp = op_dst & op_src", 1);
      break;
    case OPC_xor:
    case OPC_xorb:
      test_normal_flags (p, "tmp = op_dst ^ op_src", 1);
      break;
    case OPC_or:
    case OPC_orb:
      test_normal_flags (p, "tmp = op_dst | op_src", 1);
      break;
    case OPC_sla:
    case OPC_slab:
    case OPC_slal:
    case OPC_sda:
    case OPC_sdab:
    case OPC_sdal:
      shift (p, 1);
      break;

    case OPC_sll:
    case OPC_sllb:
    case OPC_slll:
    case OPC_sdl:
    case OPC_sdlb:
    case OPC_sdll:
      shift (p, 0);
      break;
    case OPC_rl:
      rotate (p, 0, 16, 1);
      break;
    case OPC_rlb:
      rotate (p, 0, 8, 1);
      break;
    case OPC_rr:
      rotate (p, 0, 16, 0);
      break;
    case OPC_rrb:
      rotate (p, 0, 8, 0);
      break;
    case OPC_rrc:
      rotate (p, 1, 16, 0);
      break;
    case OPC_rrcb:
      rotate (p, 1, 8, 0);
      break;
    case OPC_rlc:
      rotate (p, 1, 16, 1);
      break;
    case OPC_rlcb:
      rotate (p, 1, 8, 1);
      break;

    case OPC_extsb:
    case OPC_exts:
    case OPC_extsl:
      exts (p);
      break;
    case OPC_add:
    case OPC_addb:
    case OPC_addl:
    case OPC_inc:
    case OPC_incb:
      optimize_normal_flags (p, "tmp = op_dst + op_src",0);
      break;
    case OPC_testb:
    case OPC_test:
    case OPC_testl:
      test_normal_flags (p, "tmp = op_dst", 0);
      break;
    case OPC_cp:
    case OPC_cpb:
    case OPC_cpl:
      normal_flags (p, "tmp = op_dst - op_src",1);
      break;
    case OPC_negb:
    case OPC_neg:
      emit ("{\n");
      emit ("int op_src = -op_dst;\n");
      emit ("op_dst = 0;\n");
      optimize_normal_flags (p, "tmp = op_dst + op_src;\n",1);
      emit ("}");
      break;

    case OPC_sub:
    case OPC_subb:
    case OPC_subl:
    case OPC_dec:
    case OPC_decb:
      optimize_normal_flags (p, "tmp = op_dst - op_src",1);
      break;
    case OPC_bpt:
      bpt ();
      break;
    case OPC_jr:
      jr (p);
      break;
    case OPC_sc:
      sc ();
      break;
    case OPC_jp:
      jp (p);
      break;
    case OPC_ret:
      ret (p);
      break;
    case OPC_call:
      call (p);
      break;
    case OPC_tcc:
    case OPC_tccb:
      emit ("if(op_cc == 8 || COND(context,op_cc)) put_word_reg(context,reg_dst, 1);\n");
      break;
    case OPC_lda:
      emit ("tmp = oplval_src; \n");
      /*(((oplval_src) & 0xff0000) << 8) | (oplval_src & 0xffff); \n");*/
      break;
    case OPC_ldk:
    case OPC_ld:

    case OPC_ldb:
    case OPC_ldl:
      ld (p);
      break;
    case OPC_ldib:
      ldi (p, 8, 1);
      break;
    case OPC_ldi:
      ldi (p, 16, 1);
      break;

    case OPC_lddb:
      ldi (p, 8, -1);
      break;
    case OPC_ldd:
      ldi (p, 16, -1);
      break;

    case OPC_push:
    case OPC_pushl:
      push (p);
      break;

    case OPC_div:
    case OPC_divl:
      adiv (p);
      break;
    case OPC_mult:
    case OPC_multl:
      mult (p);
      break;
    case OPC_pop:
    case OPC_popl:
      pop (p);
      break;
    case OPC_set:
      doset (p,1);
      break;
    case OPC_res:
      doset (p,0);
      break;
    case OPC_bit:
      dobit(p);
      break;
    case OPC_resflg:
      doflag(0);
      break;
    case OPC_setflg:
      doflag(1);
      break;
    default:

      emit ("tmp = fail(context,%d);\n", p->opcode);
      break;
    }
}

/* emit code to store result in calculated lvalue */

void
info_store (p)
     opcode_entry_type *p;
{
  unsigned int *i = p->arg_info;

  while (*i)
    {
      current_name = reg_n (*i);
      current_size = size_name (p->type);

      if (IS_DST (*i))
	{
	  switch (*i & CLASS_MASK)
	    {
	    case CLASS_PR:
	      emit ("put_<ptr_mode>_reg(context,reg_<name>, tmp);\n");
	      break;
	    case CLASS_REG_LONG:
	    case CLASS_REG_WORD:
	    case CLASS_REG_BYTE:

	      emit ("put_<size>_reg(context,reg_<name>,tmp);\n");
	      break;
	    case CLASS_X:
	    case CLASS_IR:
	    case CLASS_DA:
	    case CLASS_BX:
	    case CLASS_BA:

	      emit ("put_<size>_<mem>_da(context,oplval_<name>, tmp);\n");
	      break;
	    case CLASS_IMM:
	      break;
	    default:
	      emit ("abort(); ");
	      break;
	    }

	}
      i++;
    }
}

static
void
mangle (p, shortcut, value)
     opcode_entry_type *p;
     int shortcut;
     int value;
{
  int nostore = 0;
  int extra;
  int getdst = 1;
  int before = 0;
  int nosrc = 0;

  emit ("/\052 %s \052/\n", p->nicename);
  if (shortcut)
    {
      emit ("int <fop>_%04x(context,pc)\n", value);
    }
  else
    {
      emit ("int <fop>_%d(context,pc,iwords0)\n", p->idx);
      emit ("int iwords0;\n");
    }
  emit ("sim_state_type *context;\n");
  emit ("int pc;\n");
  emit ("{\n");
  emit ("register unsigned int tmp;\n");
  if (shortcut)
    {
      emit ("register unsigned int iwords0 = 0x%x;\n", value);
    }

  /* work out how much bigger this opcode could be because it's large
     model */
  if (BIG)
    {
      int i;

      extra = 0;
      for (i = 0; i < 4; i++)
	{
	  if ((p->arg_info[i] & CLASS_MASK) == CLASS_DA
	      || (p->arg_info[i] & CLASS_MASK) == CLASS_X)
	    extra += 2;
	}
    }
  else
    {
      extra = 0;
    }
  printf ("			/* Length %d */ \n", p->length + extra);
  switch (p->length + extra)
    {
    case 2:
      emit ("pc += 2\n;");
      break;
    case 4:
      emit ("register unsigned int iwords1 = get_word_mem_da(context,pc+2);\n");
      emit ("pc += 4;\n");
      break;
    case 6:

      emit ("register unsigned int iwords1 = get_word_mem_da(context,pc+2);\n");
      emit ("register unsigned int iwords2 = get_word_mem_da(context,pc+4);\n");
      emit ("pc += 6;\n");
      break;
    case 8:
      emit ("register unsigned int iwords1 = get_word_mem_da(context,pc+2);\n");
      emit ("register unsigned int iwords2 = get_word_mem_da(context,pc+4);\n");
      emit ("register unsigned int iwords3 = get_word_mem_da(context,pc+6);\n");
      emit ("pc += 8;\n");
      break;
    default:
      break;

    }
  emit ("context->cycles += %d;\n", p->cycles);

  emit ("{\n");
  info_args (p);
  info_special (p, &getdst, &nostore, &before, &nosrc);

  info_lvals (p);
  if (!nosrc)
    {
      info_fetch (p, getdst);
    }

  if (before)
    {
      info_docode (p);
    }
  else
    {
      info_docode (p);
    }
  if (!nostore)
    info_store (p);
  emit ("}\n");
  emit ("return pc;\n");
  emit ("}\n");
}

void
static
one_instruction (i)
     int i;
{
  /* find the table entry */
  opcode_entry_type *p = z8k_table + i;

  if (!p)
    return;
  mangle (p, 0, 0);
}

void
add_to_list (ptr, value)
     struct opcode_value **ptr;
     int value;
{
  struct opcode_value *prev;

  prev = *ptr;
  *ptr = (struct opcode_value *) malloc (sizeof (struct opcode_value));

  (*ptr)->n = value;
  (*ptr)->next = prev;
}

void
build_list (i)
     int i;
{
  opcode_entry_type *p = lookup_inst (i);

  if (!p)
    return;
  add_to_list (&list[p->idx], i);
}

int
main (ac, av)
     int ac;
     char **av;
{
  int i;
  int needcomma = 0;

  for (i = 1; i < ac; i++)
    {
      if (strcmp (av[i], "-1") == 0)
	file = 1;
      if (strcmp (av[i], "-2") == 0)
	file = 2;
      if (strcmp (av[i], "-3") == 0)
	file = 3;
      if (strcmp (av[i], "-b3") == 0)
	{
	  file = 3;
	  BIG = 1;
	}

    }

  /* First work out which opcodes use which bit patterns,
     build a list of all matching bit pattens */
  for (i = 0; i < 1 << 16; i++)
    {
      build_list (i);
    }
#if DUMP_LIST
  for (i = 0; i < NOPS; i++)
    {
      struct opcode_value *p;

      printf ("%d,", i);
      p = list[i];
      while (p)
	{
	  printf (" %04x,", p->n);
	  p = p->next;
	}
      printf ("-1\n");
    }

#endif

  if (file == 1)
    {
      extern int quick[];

      /* Do the shortcuts */
      printf ("			/* SHORTCUTS */\n");
      for (i = 0; quick[i]; i++)
	{
	  int t = quick[i];

	  mangle (lookup_inst (t), 1, t);
	}
    }
  if (file == 3)
    {
      printf ("			/* NOT -SHORTCUTS */\n");
      for (i = 0; i < NOPS; i++)
	{
	  if (list[i])
	    {
	      one_instruction (i);
	    }
	  else
	    {
	      emit ("int <fop>_%d(context,pc)\n", i);
	      printf ("sim_state_type *context;\n");
	      printf ("int pc;\n");
	      emit ("{ <fop>_bad1();return pc; }\n");
	    }
	}
      emit ("int <fop>_bad() ;\n");

      /* Write the jump table */
      emit ("int (*(<fop>_table[]))() = {");
      needcomma = 0;
      for (i = 0; i < NOPS; i++)
	{
	  if (needcomma)
	    printf (",");
	  emit ("<fop>_%d\n", i);
	  needcomma = 1;
	}
      emit ("};\n");
    }

  if (file == 2)
    {
      extern int quick[];
      /* Static - since it's too be to be automatic on the apollo */
      static int big[64 * 1024];

      for (i = 0; i < 64 * 1024; i++)
	big[i] = 0;

      for (i = 0; quick[i]; i++)
	{
#if 0

	  printf ("extern int <fop>_%04x();\n", quick[i]);
#endif

	  big[quick[i]] = 1;
	}

      for (i = 0; i < NOPS; i++)
	{
#if 0
	  printf ("extern int fop_%d();\n", i);
#endif
	}
#if 0
      printf ("extern int fop_bad();\n");
#endif
      printf ("struct op_info op_info_table[] = {\n");
      for (i = 0; i < 1 << 16; i++)
	{
	  opcode_entry_type *p = lookup_inst (i);

	  if (needcomma)
	    printf (",");
#if 0
	  if (big[i])
	    {
	      printf ("<fop>_%04x", i);
	    }
	  else
#endif
	  if (p != NULL)
	    {
	      printf ("%d", p->idx);
	    }
	  else
	    printf ("400");
	  if (p != NULL)
	    {
	      printf ("		/* %04x %s */\n", i, p->nicename);
	    }
	  else
	    {
	      printf ("\n");
	    }
	  needcomma = 1;
	}
      printf ("};\n");

    }
  return 0;
}

char *
insn_ptr (n)
     int n;
{
  if (BIG)
    {
      abort ();
    }

  switch (n)
    {
    case 4:
      return "iwords_1";
    default:
      return "fail(context,123)";
    }
}

/* work out if the opcode only wants lvalues */
int
lvalue (p)
     opcode_entry_type *p;
{
  switch (p->opcode)
    {
    case OPC_lda:
      return 1;
    case OPC_call:
    case OPC_jp:
      return 1;
    default:
      return 0;
    }
}

int
info_len_in_words (o)
     opcode_entry_type *o;
{
  unsigned  int *p = o->byte_info;
  int nibs = 0;

  while (*p)
    {
      switch (*p & CLASS_MASK)
	{
	case CLASS_IGNORE:
	case CLASS_BIT:
	case CLASS_REGN0:
	case CLASS_REG:
	case CLASS_01II:
	case CLASS_00II:
	  nibs++;
	  break;
	case CLASS_ADDRESS:
	  nibs += SIZE_ADDRESS;
	  break;
	case CLASS_IMM:
	  switch (*p & ~CLASS_MASK)
	    {
	    case ARG_IMM16:
	      nibs += 4;
	      break;
	    case ARG_IMM32:
	      nibs += 8;
	      break;
	    case ARG_IMM2:
	    case ARG_IMM4:
	    case ARG_NIM4:
	    case ARG_IMM4M1:
	    case ARG_IMM_1:
	    case ARG_IMM_2:
	    case ARG_IMMNMINUS1:
	      nibs++;
	      break;
	    case ARG_NIM8:

	    case ARG_IMM8:
	      nibs += 2;
	      break;
	    default:
	      abort ();
	    }
	  break;
	case CLASS_DISP:
	  switch (*p & ~CLASS_MASK)
	    {
	    case ARG_DISP16:
	      nibs += 4;
	      break;
	    case ARG_DISP12:
	      nibs += 3;
	      break;
	    case ARG_DISP8:
	      nibs += 2;
	      break;
	    default:
	      abort ();
	    }
	  break;
	case CLASS_0DISP7:
	case CLASS_1DISP7:
	case CLASS_DISP8:
	  nibs += 2;
	  break;
	case CLASS_BIT_1OR2:
	case CLASS_0CCC:
	case CLASS_1CCC:
	case CLASS_CC:
	  nibs++;
	  break;
	default:
	  emit ("don't know %x\n", *p);
	}
      p++;
    }

  return nibs / 4;		/* return umber of words */
}