prim_ops.c 61.5 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 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
/****************************************************************************
*
*			Realmode X86 Emulator Library
*
*		Copyright (C) 1991-2004 SciTech Software, Inc.
*		     Copyright (C) David Mosberger-Tang
*		       Copyright (C) 1999 Egbert Eich
*
*  ========================================================================
*
*  Permission to use, copy, modify, distribute, and sell this software and
*  its documentation for any purpose is hereby granted without fee,
*  provided that the above copyright notice appear in all copies and that
*  both that copyright notice and this permission notice appear in
*  supporting documentation, and that the name of the authors not be used
*  in advertising or publicity pertaining to distribution of the software
*  without specific, written prior permission.	The authors makes no
*  representations about the suitability of this software for any purpose.
*  It is provided "as is" without express or implied warranty.
*
*  THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
*  INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
*  EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
*  CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
*  USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
*  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
*  PERFORMANCE OF THIS SOFTWARE.
*
*  ========================================================================
*
* Language:	ANSI C
* Environment:	Any
* Developer:	Kendall Bennett
*
* Description:	This file contains the code to implement the primitive
*		machine operations used by the emulation code in ops.c
*
* Carry Chain Calculation
*
* This represents a somewhat expensive calculation which is
* apparently required to emulate the setting of the OF343364 and AF flag.
* The latter is not so important, but the former is.  The overflow
* flag is the XOR of the top two bits of the carry chain for an
* addition (similar for subtraction).  Since we do not want to
* simulate the addition in a bitwise manner, we try to calculate the
* carry chain given the two operands and the result.
*
* So, given the following table, which represents the addition of two
* bits, we can derive a formula for the carry chain.
*
* a   b	  cin	r     cout
* 0   0	  0	0     0
* 0   0	  1	1     0
* 0   1	  0	1     0
* 0   1	  1	0     1
* 1   0	  0	1     0
* 1   0	  1	0     1
* 1   1	  0	0     1
* 1   1	  1	1     1
*
* Construction of table for cout:
*
* ab
* r  \	00   01	  11  10
* |------------------
* 0  |	 0    1	   1   1
* 1  |	 0    0	   1   0
*
* By inspection, one gets:  cc = ab +  r'(a + b)
*
* That represents alot of operations, but NO CHOICE....
*
* Borrow Chain Calculation.
*
* The following table represents the subtraction of two bits, from
* which we can derive a formula for the borrow chain.
*
* a   b	  bin	r     bout
* 0   0	  0	0     0
* 0   0	  1	1     1
* 0   1	  0	1     1
* 0   1	  1	0     1
* 1   0	  0	1     0
* 1   0	  1	0     0
* 1   1	  0	0     0
* 1   1	  1	1     1
*
* Construction of table for cout:
*
* ab
* r  \	00   01	  11  10
* |------------------
* 0  |	 0    1	   0   0
* 1  |	 1    1	   1   0
*
* By inspection, one gets:  bc = a'b +	r(a' + b)
*
****************************************************************************/

#include <common.h>

#define PRIM_OPS_NO_REDEFINE_ASM
#include "x86emu/x86emui.h"

/*------------------------- Global Variables ------------------------------*/

static u32 x86emu_parity_tab[8] =
{
    0x96696996,
    0x69969669,
    0x69969669,
    0x96696996,
    0x69969669,
    0x96696996,
    0x96696996,
    0x69969669,
};

#define PARITY(x)   (((x86emu_parity_tab[(x) / 32] >> ((x) % 32)) & 1) == 0)
#define XOR2(x)	    (((x) ^ ((x)>>1)) & 0x1)

/*----------------------------- Implementation ----------------------------*/


/*--------- Side effects helper functions -------*/

/****************************************************************************
REMARKS:
implements side efects for byte operations that don't overflow
****************************************************************************/

static void set_parity_flag(u32 res)
{
    CONDITIONAL_SET_FLAG(PARITY(res & 0xFF), F_PF);
}

static void set_szp_flags_8(u8 res)
{
    CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
    CONDITIONAL_SET_FLAG(res == 0, F_ZF);
    set_parity_flag(res);
}

static void set_szp_flags_16(u16 res)
{
    CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
    CONDITIONAL_SET_FLAG(res == 0, F_ZF);
    set_parity_flag(res);
}

static void set_szp_flags_32(u32 res)
{
    CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
    CONDITIONAL_SET_FLAG(res == 0, F_ZF);
    set_parity_flag(res);
}

static void no_carry_byte_side_eff(u8 res)
{
    CLEAR_FLAG(F_OF);
    CLEAR_FLAG(F_CF);
    CLEAR_FLAG(F_AF);
    set_szp_flags_8(res);
}

static void no_carry_word_side_eff(u16 res)
{
    CLEAR_FLAG(F_OF);
    CLEAR_FLAG(F_CF);
    CLEAR_FLAG(F_AF);
    set_szp_flags_16(res);
}

static void no_carry_long_side_eff(u32 res)
{
    CLEAR_FLAG(F_OF);
    CLEAR_FLAG(F_CF);
    CLEAR_FLAG(F_AF);
    set_szp_flags_32(res);
}

static void calc_carry_chain(int bits, u32 d, u32 s, u32 res, int set_carry)
{
    u32 cc;

    cc = (s & d) | ((~res) & (s | d));
    CONDITIONAL_SET_FLAG(XOR2(cc >> (bits - 2)), F_OF);
    CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
    if (set_carry) {
	CONDITIONAL_SET_FLAG(res & (1 << bits), F_CF);
    }
}

static void calc_borrow_chain(int bits, u32 d, u32 s, u32 res, int set_carry)
{
    u32 bc;

    bc = (res & (~d | s)) | (~d & s);
    CONDITIONAL_SET_FLAG(XOR2(bc >> (bits - 2)), F_OF);
    CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
    if (set_carry) {
	CONDITIONAL_SET_FLAG(bc & (1 << (bits - 1)), F_CF);
    }
}

/****************************************************************************
REMARKS:
Implements the AAA instruction and side effects.
****************************************************************************/
u16 aaa_word(u16 d)
{
    u16 res;
    if ((d & 0xf) > 0x9 || ACCESS_FLAG(F_AF)) {
	d += 0x6;
	d += 0x100;
	SET_FLAG(F_AF);
	SET_FLAG(F_CF);
    } else {
	CLEAR_FLAG(F_CF);
	CLEAR_FLAG(F_AF);
    }
    res = (u16)(d & 0xFF0F);
    set_szp_flags_16(res);
    return res;
}

/****************************************************************************
REMARKS:
Implements the AAA instruction and side effects.
****************************************************************************/
u16 aas_word(u16 d)
{
    u16 res;
    if ((d & 0xf) > 0x9 || ACCESS_FLAG(F_AF)) {
	d -= 0x6;
	d -= 0x100;
	SET_FLAG(F_AF);
	SET_FLAG(F_CF);
    } else {
	CLEAR_FLAG(F_CF);
	CLEAR_FLAG(F_AF);
    }
    res = (u16)(d & 0xFF0F);
    set_szp_flags_16(res);
    return res;
}

/****************************************************************************
REMARKS:
Implements the AAD instruction and side effects.
****************************************************************************/
u16 aad_word(u16 d)
{
    u16 l;
    u8 hb, lb;

    hb = (u8)((d >> 8) & 0xff);
    lb = (u8)((d & 0xff));
    l = (u16)((lb + 10 * hb) & 0xFF);

    no_carry_byte_side_eff(l & 0xFF);
    return l;
}

/****************************************************************************
REMARKS:
Implements the AAM instruction and side effects.
****************************************************************************/
u16 aam_word(u8 d)
{
    u16 h, l;

    h = (u16)(d / 10);
    l = (u16)(d % 10);
    l |= (u16)(h << 8);

    no_carry_byte_side_eff(l & 0xFF);
    return l;
}

/****************************************************************************
REMARKS:
Implements the ADC instruction and side effects.
****************************************************************************/
u8 adc_byte(u8 d, u8 s)
{
    u32 res;   /* all operands in native machine order */

    res = d + s;
    if (ACCESS_FLAG(F_CF)) res++;

    set_szp_flags_8(res);
    calc_carry_chain(8,s,d,res,1);

    return (u8)res;
}

/****************************************************************************
REMARKS:
Implements the ADC instruction and side effects.
****************************************************************************/
u16 adc_word(u16 d, u16 s)
{
    u32 res;   /* all operands in native machine order */

    res = d + s;
    if (ACCESS_FLAG(F_CF))
	res++;

    set_szp_flags_16((u16)res);
    calc_carry_chain(16,s,d,res,1);

    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the ADC instruction and side effects.
****************************************************************************/
u32 adc_long(u32 d, u32 s)
{
    u32 lo;    /* all operands in native machine order */
    u32 hi;
    u32 res;

    lo = (d & 0xFFFF) + (s & 0xFFFF);
    res = d + s;

    if (ACCESS_FLAG(F_CF)) {
	lo++;
	res++;
    }

    hi = (lo >> 16) + (d >> 16) + (s >> 16);

    set_szp_flags_32(res);
    calc_carry_chain(32,s,d,res,0);

    CONDITIONAL_SET_FLAG(hi & 0x10000, F_CF);

    return res;
}

/****************************************************************************
REMARKS:
Implements the ADD instruction and side effects.
****************************************************************************/
u8 add_byte(u8 d, u8 s)
{
    u32 res;   /* all operands in native machine order */

    res = d + s;
    set_szp_flags_8((u8)res);
    calc_carry_chain(8,s,d,res,1);

    return (u8)res;
}

/****************************************************************************
REMARKS:
Implements the ADD instruction and side effects.
****************************************************************************/
u16 add_word(u16 d, u16 s)
{
    u32 res;   /* all operands in native machine order */

    res = d + s;
    set_szp_flags_16((u16)res);
    calc_carry_chain(16,s,d,res,1);

    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the ADD instruction and side effects.
****************************************************************************/
u32 add_long(u32 d, u32 s)
{
    u32 res;

    res = d + s;
    set_szp_flags_32(res);
    calc_carry_chain(32,s,d,res,0);

    CONDITIONAL_SET_FLAG(res < d || res < s, F_CF);

    return res;
}

/****************************************************************************
REMARKS:
Implements the AND instruction and side effects.
****************************************************************************/
u8 and_byte(u8 d, u8 s)
{
    u8 res;    /* all operands in native machine order */

    res = d & s;

    no_carry_byte_side_eff(res);
    return res;
}

/****************************************************************************
REMARKS:
Implements the AND instruction and side effects.
****************************************************************************/
u16 and_word(u16 d, u16 s)
{
    u16 res;   /* all operands in native machine order */

    res = d & s;

    no_carry_word_side_eff(res);
    return res;
}

/****************************************************************************
REMARKS:
Implements the AND instruction and side effects.
****************************************************************************/
u32 and_long(u32 d, u32 s)
{
    u32 res;   /* all operands in native machine order */

    res = d & s;
    no_carry_long_side_eff(res);
    return res;
}

/****************************************************************************
REMARKS:
Implements the CMP instruction and side effects.
****************************************************************************/
u8 cmp_byte(u8 d, u8 s)
{
    u32 res;   /* all operands in native machine order */

    res = d - s;
    set_szp_flags_8((u8)res);
    calc_borrow_chain(8, d, s, res, 1);

    return d;
}

/****************************************************************************
REMARKS:
Implements the CMP instruction and side effects.
****************************************************************************/
u16 cmp_word(u16 d, u16 s)
{
    u32 res;   /* all operands in native machine order */

    res = d - s;
    set_szp_flags_16((u16)res);
    calc_borrow_chain(16, d, s, res, 1);

    return d;
}

/****************************************************************************
REMARKS:
Implements the CMP instruction and side effects.
****************************************************************************/
u32 cmp_long(u32 d, u32 s)
{
    u32 res;   /* all operands in native machine order */

    res = d - s;
    set_szp_flags_32(res);
    calc_borrow_chain(32, d, s, res, 1);

    return d;
}

/****************************************************************************
REMARKS:
Implements the DAA instruction and side effects.
****************************************************************************/
u8 daa_byte(u8 d)
{
    u32 res = d;
    if ((d & 0xf) > 9 || ACCESS_FLAG(F_AF)) {
	res += 6;
	SET_FLAG(F_AF);
    }
    if (res > 0x9F || ACCESS_FLAG(F_CF)) {
	res += 0x60;
	SET_FLAG(F_CF);
    }
    set_szp_flags_8((u8)res);
    return (u8)res;
}

/****************************************************************************
REMARKS:
Implements the DAS instruction and side effects.
****************************************************************************/
u8 das_byte(u8 d)
{
    if ((d & 0xf) > 9 || ACCESS_FLAG(F_AF)) {
	d -= 6;
	SET_FLAG(F_AF);
    }
    if (d > 0x9F || ACCESS_FLAG(F_CF)) {
	d -= 0x60;
	SET_FLAG(F_CF);
    }
    set_szp_flags_8(d);
    return d;
}

/****************************************************************************
REMARKS:
Implements the DEC instruction and side effects.
****************************************************************************/
u8 dec_byte(u8 d)
{
    u32 res;   /* all operands in native machine order */

    res = d - 1;
    set_szp_flags_8((u8)res);
    calc_borrow_chain(8, d, 1, res, 0);

    return (u8)res;
}

/****************************************************************************
REMARKS:
Implements the DEC instruction and side effects.
****************************************************************************/
u16 dec_word(u16 d)
{
    u32 res;   /* all operands in native machine order */

    res = d - 1;
    set_szp_flags_16((u16)res);
    calc_borrow_chain(16, d, 1, res, 0);

    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the DEC instruction and side effects.
****************************************************************************/
u32 dec_long(u32 d)
{
    u32 res;   /* all operands in native machine order */

    res = d - 1;

    set_szp_flags_32(res);
    calc_borrow_chain(32, d, 1, res, 0);

    return res;
}

/****************************************************************************
REMARKS:
Implements the INC instruction and side effects.
****************************************************************************/
u8 inc_byte(u8 d)
{
    u32 res;   /* all operands in native machine order */

    res = d + 1;
    set_szp_flags_8((u8)res);
    calc_carry_chain(8, d, 1, res, 0);

    return (u8)res;
}

/****************************************************************************
REMARKS:
Implements the INC instruction and side effects.
****************************************************************************/
u16 inc_word(u16 d)
{
    u32 res;   /* all operands in native machine order */

    res = d + 1;
    set_szp_flags_16((u16)res);
    calc_carry_chain(16, d, 1, res, 0);

    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the INC instruction and side effects.
****************************************************************************/
u32 inc_long(u32 d)
{
    u32 res;   /* all operands in native machine order */

    res = d + 1;
    set_szp_flags_32(res);
    calc_carry_chain(32, d, 1, res, 0);

    return res;
}

/****************************************************************************
REMARKS:
Implements the OR instruction and side effects.
****************************************************************************/
u8 or_byte(u8 d, u8 s)
{
    u8 res;    /* all operands in native machine order */

    res = d | s;
    no_carry_byte_side_eff(res);

    return res;
}

/****************************************************************************
REMARKS:
Implements the OR instruction and side effects.
****************************************************************************/
u16 or_word(u16 d, u16 s)
{
    u16 res;   /* all operands in native machine order */

    res = d | s;
    no_carry_word_side_eff(res);
    return res;
}

/****************************************************************************
REMARKS:
Implements the OR instruction and side effects.
****************************************************************************/
u32 or_long(u32 d, u32 s)
{
    u32 res;   /* all operands in native machine order */

    res = d | s;
    no_carry_long_side_eff(res);
    return res;
}

/****************************************************************************
REMARKS:
Implements the OR instruction and side effects.
****************************************************************************/
u8 neg_byte(u8 s)
{
    u8 res;

    CONDITIONAL_SET_FLAG(s != 0, F_CF);
    res = (u8)-s;
    set_szp_flags_8(res);
    calc_borrow_chain(8, 0, s, res, 0);

    return res;
}

/****************************************************************************
REMARKS:
Implements the OR instruction and side effects.
****************************************************************************/
u16 neg_word(u16 s)
{
    u16 res;

    CONDITIONAL_SET_FLAG(s != 0, F_CF);
    res = (u16)-s;
    set_szp_flags_16((u16)res);
    calc_borrow_chain(16, 0, s, res, 0);

    return res;
}

/****************************************************************************
REMARKS:
Implements the OR instruction and side effects.
****************************************************************************/
u32 neg_long(u32 s)
{
    u32 res;

    CONDITIONAL_SET_FLAG(s != 0, F_CF);
    res = (u32)-s;
    set_szp_flags_32(res);
    calc_borrow_chain(32, 0, s, res, 0);

    return res;
}

/****************************************************************************
REMARKS:
Implements the NOT instruction and side effects.
****************************************************************************/
u8 not_byte(u8 s)
{
    return ~s;
}

/****************************************************************************
REMARKS:
Implements the NOT instruction and side effects.
****************************************************************************/
u16 not_word(u16 s)
{
    return ~s;
}

/****************************************************************************
REMARKS:
Implements the NOT instruction and side effects.
****************************************************************************/
u32 not_long(u32 s)
{
    return ~s;
}

/****************************************************************************
REMARKS:
Implements the RCL instruction and side effects.
****************************************************************************/
u8 rcl_byte(u8 d, u8 s)
{
    unsigned int res, cnt, mask, cf;

    /* s is the rotate distance.  It varies from 0 - 8. */
    /* have

       CF  B_7 B_6 B_5 B_4 B_3 B_2 B_1 B_0

       want to rotate through the carry by "s" bits.  We could
       loop, but that's inefficient.  So the width is 9,
       and we split into three parts:

       The new carry flag   (was B_n)
       the stuff in B_n-1 .. B_0
       the stuff in B_7 .. B_n+1

       The new rotate is done mod 9, and given this,
       for a rotation of n bits (mod 9) the new carry flag is
       then located n bits from the MSB.  The low part is
       then shifted up cnt bits, and the high part is or'd
       in.  Using CAPS for new values, and lowercase for the
       original values, this can be expressed as:

       IF n > 0
       1) CF <-	 b_(8-n)
       2) B_(7) .. B_(n)  <-  b_(8-(n+1)) .. b_0
       3) B_(n-1) <- cf
       4) B_(n-2) .. B_0 <-  b_7 .. b_(8-(n-1))
     */
    res = d;
    if ((cnt = s % 9) != 0) {
	/* extract the new CARRY FLAG. */
	/* CF <-  b_(8-n)	      */
	cf = (d >> (8 - cnt)) & 0x1;

	/* get the low stuff which rotated
	   into the range B_7 .. B_cnt */
	/* B_(7) .. B_(n)  <-  b_(8-(n+1)) .. b_0  */
	/* note that the right hand side done by the mask */
	res = (d << cnt) & 0xff;

	/* now the high stuff which rotated around
	   into the positions B_cnt-2 .. B_0 */
	/* B_(n-2) .. B_0 <-  b_7 .. b_(8-(n-1)) */
	/* shift it downward, 7-(n-2) = 9-n positions.
	   and mask off the result before or'ing in.
	 */
	mask = (1 << (cnt - 1)) - 1;
	res |= (d >> (9 - cnt)) & mask;

	/* if the carry flag was set, or it in.	 */
	if (ACCESS_FLAG(F_CF)) {     /* carry flag is set */
	    /*	B_(n-1) <- cf */
	    res |= 1 << (cnt - 1);
	}
	/* set the new carry flag, based on the variable "cf" */
	CONDITIONAL_SET_FLAG(cf, F_CF);
	/* OVERFLOW is set *IFF* cnt==1, then it is the
	   xor of CF and the most significant bit.  Blecck. */
	/* parenthesized this expression since it appears to
	   be causing OF to be misset */
	CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 6) & 0x2)),
			     F_OF);

    }
    return (u8)res;
}

/****************************************************************************
REMARKS:
Implements the RCL instruction and side effects.
****************************************************************************/
u16 rcl_word(u16 d, u8 s)
{
    unsigned int res, cnt, mask, cf;

    res = d;
    if ((cnt = s % 17) != 0) {
	cf = (d >> (16 - cnt)) & 0x1;
	res = (d << cnt) & 0xffff;
	mask = (1 << (cnt - 1)) - 1;
	res |= (d >> (17 - cnt)) & mask;
	if (ACCESS_FLAG(F_CF)) {
	    res |= 1 << (cnt - 1);
	}
	CONDITIONAL_SET_FLAG(cf, F_CF);
	CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 14) & 0x2)),
			     F_OF);
    }
    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the RCL instruction and side effects.
****************************************************************************/
u32 rcl_long(u32 d, u8 s)
{
    u32 res, cnt, mask, cf;

    res = d;
    if ((cnt = s % 33) != 0) {
	cf = (d >> (32 - cnt)) & 0x1;
	res = (d << cnt) & 0xffffffff;
	mask = (1 << (cnt - 1)) - 1;
	res |= (d >> (33 - cnt)) & mask;
	if (ACCESS_FLAG(F_CF)) {     /* carry flag is set */
	    res |= 1 << (cnt - 1);
	}
	CONDITIONAL_SET_FLAG(cf, F_CF);
	CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 30) & 0x2)),
			     F_OF);
    }
    return res;
}

/****************************************************************************
REMARKS:
Implements the RCR instruction and side effects.
****************************************************************************/
u8 rcr_byte(u8 d, u8 s)
{
    u32 res, cnt;
    u32 mask, cf, ocf = 0;

    /* rotate right through carry */
    /*
       s is the rotate distance.  It varies from 0 - 8.
       d is the byte object rotated.

       have

       CF  B_7 B_6 B_5 B_4 B_3 B_2 B_1 B_0

       The new rotate is done mod 9, and given this,
       for a rotation of n bits (mod 9) the new carry flag is
       then located n bits from the LSB.  The low part is
       then shifted up cnt bits, and the high part is or'd
       in.  Using CAPS for new values, and lowercase for the
       original values, this can be expressed as:

       IF n > 0
       1) CF <-	 b_(n-1)
       2) B_(8-(n+1)) .. B_(0)	<-  b_(7) .. b_(n)
       3) B_(8-n) <- cf
       4) B_(7) .. B_(8-(n-1)) <-  b_(n-2) .. b_(0)
     */
    res = d;
    if ((cnt = s % 9) != 0) {
	/* extract the new CARRY FLAG. */
	/* CF <-  b_(n-1)	       */
	if (cnt == 1) {
	    cf = d & 0x1;
	    /* note hackery here.  Access_flag(..) evaluates to either
	       0 if flag not set
	       non-zero if flag is set.
	       doing access_flag(..) != 0 casts that into either
	       0..1 in any representation of the flags register
	       (i.e. packed bit array or unpacked.)
	     */
	    ocf = ACCESS_FLAG(F_CF) != 0;
	} else
	    cf = (d >> (cnt - 1)) & 0x1;

	/* B_(8-(n+1)) .. B_(0)	 <-  b_(7) .. b_n  */
	/* note that the right hand side done by the mask
	   This is effectively done by shifting the
	   object to the right.	 The result must be masked,
	   in case the object came in and was treated
	   as a negative number.  Needed??? */

	mask = (1 << (8 - cnt)) - 1;
	res = (d >> cnt) & mask;

	/* now the high stuff which rotated around
	   into the positions B_cnt-2 .. B_0 */
	/* B_(7) .. B_(8-(n-1)) <-  b_(n-2) .. b_(0) */
	/* shift it downward, 7-(n-2) = 9-n positions.
	   and mask off the result before or'ing in.
	 */
	res |= (d << (9 - cnt));

	/* if the carry flag was set, or it in.	 */
	if (ACCESS_FLAG(F_CF)) {     /* carry flag is set */
	    /*	B_(8-n) <- cf */
	    res |= 1 << (8 - cnt);
	}
	/* set the new carry flag, based on the variable "cf" */
	CONDITIONAL_SET_FLAG(cf, F_CF);
	/* OVERFLOW is set *IFF* cnt==1, then it is the
	   xor of CF and the most significant bit.  Blecck. */
	/* parenthesized... */
	if (cnt == 1) {
	    CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 6) & 0x2)),
				 F_OF);
	}
    }
    return (u8)res;
}

/****************************************************************************
REMARKS:
Implements the RCR instruction and side effects.
****************************************************************************/
u16 rcr_word(u16 d, u8 s)
{
    u32 res, cnt;
    u32 mask, cf, ocf = 0;

    /* rotate right through carry */
    res = d;
    if ((cnt = s % 17) != 0) {
	if (cnt == 1) {
	    cf = d & 0x1;
	    ocf = ACCESS_FLAG(F_CF) != 0;
	} else
	    cf = (d >> (cnt - 1)) & 0x1;
	mask = (1 << (16 - cnt)) - 1;
	res = (d >> cnt) & mask;
	res |= (d << (17 - cnt));
	if (ACCESS_FLAG(F_CF)) {
	    res |= 1 << (16 - cnt);
	}
	CONDITIONAL_SET_FLAG(cf, F_CF);
	if (cnt == 1) {
	    CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 14) & 0x2)),
				 F_OF);
	}
    }
    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the RCR instruction and side effects.
****************************************************************************/
u32 rcr_long(u32 d, u8 s)
{
    u32 res, cnt;
    u32 mask, cf, ocf = 0;

    /* rotate right through carry */
    res = d;
    if ((cnt = s % 33) != 0) {
	if (cnt == 1) {
	    cf = d & 0x1;
	    ocf = ACCESS_FLAG(F_CF) != 0;
	} else
	    cf = (d >> (cnt - 1)) & 0x1;
	mask = (1 << (32 - cnt)) - 1;
	res = (d >> cnt) & mask;
	if (cnt != 1)
	    res |= (d << (33 - cnt));
	if (ACCESS_FLAG(F_CF)) {     /* carry flag is set */
	    res |= 1 << (32 - cnt);
	}
	CONDITIONAL_SET_FLAG(cf, F_CF);
	if (cnt == 1) {
	    CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 30) & 0x2)),
				 F_OF);
	}
    }
    return res;
}

/****************************************************************************
REMARKS:
Implements the ROL instruction and side effects.
****************************************************************************/
u8 rol_byte(u8 d, u8 s)
{
    unsigned int res, cnt, mask;

    /* rotate left */
    /*
       s is the rotate distance.  It varies from 0 - 8.
       d is the byte object rotated.

       have

       CF  B_7 ... B_0

       The new rotate is done mod 8.
       Much simpler than the "rcl" or "rcr" operations.

       IF n > 0
       1) B_(7) .. B_(n)  <-  b_(8-(n+1)) .. b_(0)
       2) B_(n-1) .. B_(0) <-  b_(7) .. b_(8-n)
     */
    res = d;
    if ((cnt = s % 8) != 0) {
	/* B_(7) .. B_(n)  <-  b_(8-(n+1)) .. b_(0) */
	res = (d << cnt);

	/* B_(n-1) .. B_(0) <-	b_(7) .. b_(8-n) */
	mask = (1 << cnt) - 1;
	res |= (d >> (8 - cnt)) & mask;

	/* set the new carry flag, Note that it is the low order
	   bit of the result!!!				      */
	CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
	/* OVERFLOW is set *IFF* s==1, then it is the
	   xor of CF and the most significant bit.  Blecck. */
	CONDITIONAL_SET_FLAG(s == 1 &&
			     XOR2((res & 0x1) + ((res >> 6) & 0x2)),
			     F_OF);
    } if (s != 0) {
	/* set the new carry flag, Note that it is the low order
	   bit of the result!!!				      */
	CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
    }
    return (u8)res;
}

/****************************************************************************
REMARKS:
Implements the ROL instruction and side effects.
****************************************************************************/
u16 rol_word(u16 d, u8 s)
{
    unsigned int res, cnt, mask;

    res = d;
    if ((cnt = s % 16) != 0) {
	res = (d << cnt);
	mask = (1 << cnt) - 1;
	res |= (d >> (16 - cnt)) & mask;
	CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
	CONDITIONAL_SET_FLAG(s == 1 &&
			     XOR2((res & 0x1) + ((res >> 14) & 0x2)),
			     F_OF);
    } if (s != 0) {
	/* set the new carry flag, Note that it is the low order
	   bit of the result!!!				      */
	CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
    }
    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the ROL instruction and side effects.
****************************************************************************/
u32 rol_long(u32 d, u8 s)
{
    u32 res, cnt, mask;

    res = d;
    if ((cnt = s % 32) != 0) {
	res = (d << cnt);
	mask = (1 << cnt) - 1;
	res |= (d >> (32 - cnt)) & mask;
	CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
	CONDITIONAL_SET_FLAG(s == 1 &&
			     XOR2((res & 0x1) + ((res >> 30) & 0x2)),
			     F_OF);
    } if (s != 0) {
	/* set the new carry flag, Note that it is the low order
	   bit of the result!!!				      */
	CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
    }
    return res;
}

/****************************************************************************
REMARKS:
Implements the ROR instruction and side effects.
****************************************************************************/
u8 ror_byte(u8 d, u8 s)
{
    unsigned int res, cnt, mask;

    /* rotate right */
    /*
       s is the rotate distance.  It varies from 0 - 8.
       d is the byte object rotated.

       have

       B_7 ... B_0

       The rotate is done mod 8.

       IF n > 0
       1) B_(8-(n+1)) .. B_(0)	<-  b_(7) .. b_(n)
       2) B_(7) .. B_(8-n) <-  b_(n-1) .. b_(0)
     */
    res = d;
    if ((cnt = s % 8) != 0) {		/* not a typo, do nada if cnt==0 */
	/* B_(7) .. B_(8-n) <-	b_(n-1) .. b_(0) */
	res = (d << (8 - cnt));

	/* B_(8-(n+1)) .. B_(0)	 <-  b_(7) .. b_(n) */
	mask = (1 << (8 - cnt)) - 1;
	res |= (d >> (cnt)) & mask;

	/* set the new carry flag, Note that it is the low order
	   bit of the result!!!				      */
	CONDITIONAL_SET_FLAG(res & 0x80, F_CF);
	/* OVERFLOW is set *IFF* s==1, then it is the
	   xor of the two most significant bits.  Blecck. */
	CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 6), F_OF);
    } else if (s != 0) {
	/* set the new carry flag, Note that it is the low order
	   bit of the result!!!				      */
	CONDITIONAL_SET_FLAG(res & 0x80, F_CF);
    }
    return (u8)res;
}

/****************************************************************************
REMARKS:
Implements the ROR instruction and side effects.
****************************************************************************/
u16 ror_word(u16 d, u8 s)
{
    unsigned int res, cnt, mask;

    res = d;
    if ((cnt = s % 16) != 0) {
	res = (d << (16 - cnt));
	mask = (1 << (16 - cnt)) - 1;
	res |= (d >> (cnt)) & mask;
	CONDITIONAL_SET_FLAG(res & 0x8000, F_CF);
	CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 14), F_OF);
    } else if (s != 0) {
	/* set the new carry flag, Note that it is the low order
	   bit of the result!!!				      */
	CONDITIONAL_SET_FLAG(res & 0x8000, F_CF);
    }
    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the ROR instruction and side effects.
****************************************************************************/
u32 ror_long(u32 d, u8 s)
{
    u32 res, cnt, mask;

    res = d;
    if ((cnt = s % 32) != 0) {
	res = (d << (32 - cnt));
	mask = (1 << (32 - cnt)) - 1;
	res |= (d >> (cnt)) & mask;
	CONDITIONAL_SET_FLAG(res & 0x80000000, F_CF);
	CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 30), F_OF);
    } else if (s != 0) {
	/* set the new carry flag, Note that it is the low order
	   bit of the result!!!				      */
	CONDITIONAL_SET_FLAG(res & 0x80000000, F_CF);
    }
    return res;
}

/****************************************************************************
REMARKS:
Implements the SHL instruction and side effects.
****************************************************************************/
u8 shl_byte(u8 d, u8 s)
{
    unsigned int cnt, res, cf;

    if (s < 8) {
	cnt = s % 8;

	/* last bit shifted out goes into carry flag */
	if (cnt > 0) {
	    res = d << cnt;
	    cf = d & (1 << (8 - cnt));
	    CONDITIONAL_SET_FLAG(cf, F_CF);
	    set_szp_flags_8((u8)res);
	} else {
	    res = (u8) d;
	}

	if (cnt == 1) {
	    /* Needs simplification. */
	    CONDITIONAL_SET_FLAG(
				    (((res & 0x80) == 0x80) ^
				     (ACCESS_FLAG(F_CF) != 0)),
	    /* was (M.x86.R_FLG&F_CF)==F_CF)), */
				    F_OF);
	} else {
	    CLEAR_FLAG(F_OF);
	}
    } else {
	res = 0;
	CONDITIONAL_SET_FLAG((d << (s-1)) & 0x80, F_CF);
	CLEAR_FLAG(F_OF);
	CLEAR_FLAG(F_SF);
	SET_FLAG(F_PF);
	SET_FLAG(F_ZF);
    }
    return (u8)res;
}

/****************************************************************************
REMARKS:
Implements the SHL instruction and side effects.
****************************************************************************/
u16 shl_word(u16 d, u8 s)
{
    unsigned int cnt, res, cf;

    if (s < 16) {
	cnt = s % 16;
	if (cnt > 0) {
	    res = d << cnt;
	    cf = d & (1 << (16 - cnt));
	    CONDITIONAL_SET_FLAG(cf, F_CF);
	    set_szp_flags_16((u16)res);
	} else {
	    res = (u16) d;
	}

	if (cnt == 1) {
	    CONDITIONAL_SET_FLAG(
				    (((res & 0x8000) == 0x8000) ^
				     (ACCESS_FLAG(F_CF) != 0)),
				    F_OF);
	} else {
	    CLEAR_FLAG(F_OF);
	}
    } else {
	res = 0;
	CONDITIONAL_SET_FLAG((d << (s-1)) & 0x8000, F_CF);
	CLEAR_FLAG(F_OF);
	CLEAR_FLAG(F_SF);
	SET_FLAG(F_PF);
	SET_FLAG(F_ZF);
    }
    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the SHL instruction and side effects.
****************************************************************************/
u32 shl_long(u32 d, u8 s)
{
    unsigned int cnt, res, cf;

    if (s < 32) {
	cnt = s % 32;
	if (cnt > 0) {
	    res = d << cnt;
	    cf = d & (1 << (32 - cnt));
	    CONDITIONAL_SET_FLAG(cf, F_CF);
	    set_szp_flags_32((u32)res);
	} else {
	    res = d;
	}
	if (cnt == 1) {
	    CONDITIONAL_SET_FLAG((((res & 0x80000000) == 0x80000000) ^
				  (ACCESS_FLAG(F_CF) != 0)), F_OF);
	} else {
	    CLEAR_FLAG(F_OF);
	}
    } else {
	res = 0;
	CONDITIONAL_SET_FLAG((d << (s-1)) & 0x80000000, F_CF);
	CLEAR_FLAG(F_OF);
	CLEAR_FLAG(F_SF);
	SET_FLAG(F_PF);
	SET_FLAG(F_ZF);
    }
    return res;
}

/****************************************************************************
REMARKS:
Implements the SHR instruction and side effects.
****************************************************************************/
u8 shr_byte(u8 d, u8 s)
{
    unsigned int cnt, res, cf;

    if (s < 8) {
	cnt = s % 8;
	if (cnt > 0) {
	    cf = d & (1 << (cnt - 1));
	    res = d >> cnt;
	    CONDITIONAL_SET_FLAG(cf, F_CF);
	    set_szp_flags_8((u8)res);
	} else {
	    res = (u8) d;
	}

	if (cnt == 1) {
	    CONDITIONAL_SET_FLAG(XOR2(res >> 6), F_OF);
	} else {
	    CLEAR_FLAG(F_OF);
	}
    } else {
	res = 0;
	CONDITIONAL_SET_FLAG((d >> (s-1)) & 0x1, F_CF);
	CLEAR_FLAG(F_OF);
	CLEAR_FLAG(F_SF);
	SET_FLAG(F_PF);
	SET_FLAG(F_ZF);
    }
    return (u8)res;
}

/****************************************************************************
REMARKS:
Implements the SHR instruction and side effects.
****************************************************************************/
u16 shr_word(u16 d, u8 s)
{
    unsigned int cnt, res, cf;

    if (s < 16) {
	cnt = s % 16;
	if (cnt > 0) {
	    cf = d & (1 << (cnt - 1));
	    res = d >> cnt;
	    CONDITIONAL_SET_FLAG(cf, F_CF);
	    set_szp_flags_16((u16)res);
	} else {
	    res = d;
	}

	if (cnt == 1) {
	    CONDITIONAL_SET_FLAG(XOR2(res >> 14), F_OF);
	} else {
	    CLEAR_FLAG(F_OF);
	}
    } else {
	res = 0;
	CLEAR_FLAG(F_CF);
	CLEAR_FLAG(F_OF);
	SET_FLAG(F_ZF);
	CLEAR_FLAG(F_SF);
	CLEAR_FLAG(F_PF);
    }
    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the SHR instruction and side effects.
****************************************************************************/
u32 shr_long(u32 d, u8 s)
{
    unsigned int cnt, res, cf;

    if (s < 32) {
	cnt = s % 32;
	if (cnt > 0) {
	    cf = d & (1 << (cnt - 1));
	    res = d >> cnt;
	    CONDITIONAL_SET_FLAG(cf, F_CF);
	    set_szp_flags_32((u32)res);
	} else {
	    res = d;
	}
	if (cnt == 1) {
	    CONDITIONAL_SET_FLAG(XOR2(res >> 30), F_OF);
	} else {
	    CLEAR_FLAG(F_OF);
	}
    } else {
	res = 0;
	CLEAR_FLAG(F_CF);
	CLEAR_FLAG(F_OF);
	SET_FLAG(F_ZF);
	CLEAR_FLAG(F_SF);
	CLEAR_FLAG(F_PF);
    }
    return res;
}

/****************************************************************************
REMARKS:
Implements the SAR instruction and side effects.
****************************************************************************/
u8 sar_byte(u8 d, u8 s)
{
    unsigned int cnt, res, cf, mask, sf;

    res = d;
    sf = d & 0x80;
    cnt = s % 8;
    if (cnt > 0 && cnt < 8) {
	mask = (1 << (8 - cnt)) - 1;
	cf = d & (1 << (cnt - 1));
	res = (d >> cnt) & mask;
	CONDITIONAL_SET_FLAG(cf, F_CF);
	if (sf) {
	    res |= ~mask;
	}
	set_szp_flags_8((u8)res);
    } else if (cnt >= 8) {
	if (sf) {
	    res = 0xff;
	    SET_FLAG(F_CF);
	    CLEAR_FLAG(F_ZF);
	    SET_FLAG(F_SF);
	    SET_FLAG(F_PF);
	} else {
	    res = 0;
	    CLEAR_FLAG(F_CF);
	    SET_FLAG(F_ZF);
	    CLEAR_FLAG(F_SF);
	    CLEAR_FLAG(F_PF);
	}
    }
    return (u8)res;
}

/****************************************************************************
REMARKS:
Implements the SAR instruction and side effects.
****************************************************************************/
u16 sar_word(u16 d, u8 s)
{
    unsigned int cnt, res, cf, mask, sf;

    sf = d & 0x8000;
    cnt = s % 16;
    res = d;
    if (cnt > 0 && cnt < 16) {
	mask = (1 << (16 - cnt)) - 1;
	cf = d & (1 << (cnt - 1));
	res = (d >> cnt) & mask;
	CONDITIONAL_SET_FLAG(cf, F_CF);
	if (sf) {
	    res |= ~mask;
	}
	set_szp_flags_16((u16)res);
    } else if (cnt >= 16) {
	if (sf) {
	    res = 0xffff;
	    SET_FLAG(F_CF);
	    CLEAR_FLAG(F_ZF);
	    SET_FLAG(F_SF);
	    SET_FLAG(F_PF);
	} else {
	    res = 0;
	    CLEAR_FLAG(F_CF);
	    SET_FLAG(F_ZF);
	    CLEAR_FLAG(F_SF);
	    CLEAR_FLAG(F_PF);
	}
    }
    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the SAR instruction and side effects.
****************************************************************************/
u32 sar_long(u32 d, u8 s)
{
    u32 cnt, res, cf, mask, sf;

    sf = d & 0x80000000;
    cnt = s % 32;
    res = d;
    if (cnt > 0 && cnt < 32) {
	mask = (1 << (32 - cnt)) - 1;
	cf = d & (1 << (cnt - 1));
	res = (d >> cnt) & mask;
	CONDITIONAL_SET_FLAG(cf, F_CF);
	if (sf) {
	    res |= ~mask;
	}
	set_szp_flags_32(res);
    } else if (cnt >= 32) {
	if (sf) {
	    res = 0xffffffff;
	    SET_FLAG(F_CF);
	    CLEAR_FLAG(F_ZF);
	    SET_FLAG(F_SF);
	    SET_FLAG(F_PF);
	} else {
	    res = 0;
	    CLEAR_FLAG(F_CF);
	    SET_FLAG(F_ZF);
	    CLEAR_FLAG(F_SF);
	    CLEAR_FLAG(F_PF);
	}
    }
    return res;
}

/****************************************************************************
REMARKS:
Implements the SHLD instruction and side effects.
****************************************************************************/
u16 shld_word (u16 d, u16 fill, u8 s)
{
    unsigned int cnt, res, cf;

    if (s < 16) {
	cnt = s % 16;
	if (cnt > 0) {
	    res = (d << cnt) | (fill >> (16-cnt));
	    cf = d & (1 << (16 - cnt));
	    CONDITIONAL_SET_FLAG(cf, F_CF);
	    set_szp_flags_16((u16)res);
	} else {
	    res = d;
	}
	if (cnt == 1) {
	    CONDITIONAL_SET_FLAG((((res & 0x8000) == 0x8000) ^
				  (ACCESS_FLAG(F_CF) != 0)), F_OF);
	} else {
	    CLEAR_FLAG(F_OF);
	}
    } else {
	res = 0;
	CONDITIONAL_SET_FLAG((d << (s-1)) & 0x8000, F_CF);
	CLEAR_FLAG(F_OF);
	CLEAR_FLAG(F_SF);
	SET_FLAG(F_PF);
	SET_FLAG(F_ZF);
    }
    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the SHLD instruction and side effects.
****************************************************************************/
u32 shld_long (u32 d, u32 fill, u8 s)
{
    unsigned int cnt, res, cf;

    if (s < 32) {
	cnt = s % 32;
	if (cnt > 0) {
	    res = (d << cnt) | (fill >> (32-cnt));
	    cf = d & (1 << (32 - cnt));
	    CONDITIONAL_SET_FLAG(cf, F_CF);
	    set_szp_flags_32((u32)res);
	} else {
	    res = d;
	}
	if (cnt == 1) {
	    CONDITIONAL_SET_FLAG((((res & 0x80000000) == 0x80000000) ^
				  (ACCESS_FLAG(F_CF) != 0)), F_OF);
	} else {
	    CLEAR_FLAG(F_OF);
	}
    } else {
	res = 0;
	CONDITIONAL_SET_FLAG((d << (s-1)) & 0x80000000, F_CF);
	CLEAR_FLAG(F_OF);
	CLEAR_FLAG(F_SF);
	SET_FLAG(F_PF);
	SET_FLAG(F_ZF);
    }
    return res;
}

/****************************************************************************
REMARKS:
Implements the SHRD instruction and side effects.
****************************************************************************/
u16 shrd_word (u16 d, u16 fill, u8 s)
{
    unsigned int cnt, res, cf;

    if (s < 16) {
	cnt = s % 16;
	if (cnt > 0) {
	    cf = d & (1 << (cnt - 1));
	    res = (d >> cnt) | (fill << (16 - cnt));
	    CONDITIONAL_SET_FLAG(cf, F_CF);
	    set_szp_flags_16((u16)res);
	} else {
	    res = d;
	}

	if (cnt == 1) {
	    CONDITIONAL_SET_FLAG(XOR2(res >> 14), F_OF);
	} else {
	    CLEAR_FLAG(F_OF);
	}
    } else {
	res = 0;
	CLEAR_FLAG(F_CF);
	CLEAR_FLAG(F_OF);
	SET_FLAG(F_ZF);
	CLEAR_FLAG(F_SF);
	CLEAR_FLAG(F_PF);
    }
    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the SHRD instruction and side effects.
****************************************************************************/
u32 shrd_long (u32 d, u32 fill, u8 s)
{
    unsigned int cnt, res, cf;

    if (s < 32) {
	cnt = s % 32;
	if (cnt > 0) {
	    cf = d & (1 << (cnt - 1));
	    res = (d >> cnt) | (fill << (32 - cnt));
	    CONDITIONAL_SET_FLAG(cf, F_CF);
	    set_szp_flags_32((u32)res);
	} else {
	    res = d;
	}
	if (cnt == 1) {
	    CONDITIONAL_SET_FLAG(XOR2(res >> 30), F_OF);
	} else {
	    CLEAR_FLAG(F_OF);
	}
    } else {
	res = 0;
	CLEAR_FLAG(F_CF);
	CLEAR_FLAG(F_OF);
	SET_FLAG(F_ZF);
	CLEAR_FLAG(F_SF);
	CLEAR_FLAG(F_PF);
    }
    return res;
}

/****************************************************************************
REMARKS:
Implements the SBB instruction and side effects.
****************************************************************************/
u8 sbb_byte(u8 d, u8 s)
{
    u32 res;   /* all operands in native machine order */
    u32 bc;

    if (ACCESS_FLAG(F_CF))
	res = d - s - 1;
    else
	res = d - s;
    set_szp_flags_8((u8)res);

    /* calculate the borrow chain.  See note at top */
    bc = (res & (~d | s)) | (~d & s);
    CONDITIONAL_SET_FLAG(bc & 0x80, F_CF);
    CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
    CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
    return (u8)res;
}

/****************************************************************************
REMARKS:
Implements the SBB instruction and side effects.
****************************************************************************/
u16 sbb_word(u16 d, u16 s)
{
    u32 res;   /* all operands in native machine order */
    u32 bc;

    if (ACCESS_FLAG(F_CF))
	res = d - s - 1;
    else
	res = d - s;
    set_szp_flags_16((u16)res);

    /* calculate the borrow chain.  See note at top */
    bc = (res & (~d | s)) | (~d & s);
    CONDITIONAL_SET_FLAG(bc & 0x8000, F_CF);
    CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
    CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the SBB instruction and side effects.
****************************************************************************/
u32 sbb_long(u32 d, u32 s)
{
    u32 res;   /* all operands in native machine order */
    u32 bc;

    if (ACCESS_FLAG(F_CF))
	res = d - s - 1;
    else
	res = d - s;

    set_szp_flags_32(res);

    /* calculate the borrow chain.  See note at top */
    bc = (res & (~d | s)) | (~d & s);
    CONDITIONAL_SET_FLAG(bc & 0x80000000, F_CF);
    CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
    CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
    return res;
}

/****************************************************************************
REMARKS:
Implements the SUB instruction and side effects.
****************************************************************************/
u8 sub_byte(u8 d, u8 s)
{
    u32 res;   /* all operands in native machine order */
    u32 bc;

    res = d - s;
    set_szp_flags_8((u8)res);

    /* calculate the borrow chain.  See note at top */
    bc = (res & (~d | s)) | (~d & s);
    CONDITIONAL_SET_FLAG(bc & 0x80, F_CF);
    CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
    CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
    return (u8)res;
}

/****************************************************************************
REMARKS:
Implements the SUB instruction and side effects.
****************************************************************************/
u16 sub_word(u16 d, u16 s)
{
    u32 res;   /* all operands in native machine order */
    u32 bc;

    res = d - s;
    set_szp_flags_16((u16)res);

    /* calculate the borrow chain.  See note at top */
    bc = (res & (~d | s)) | (~d & s);
    CONDITIONAL_SET_FLAG(bc & 0x8000, F_CF);
    CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
    CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
    return (u16)res;
}

/****************************************************************************
REMARKS:
Implements the SUB instruction and side effects.
****************************************************************************/
u32 sub_long(u32 d, u32 s)
{
    u32 res;   /* all operands in native machine order */
    u32 bc;

    res = d - s;
    set_szp_flags_32(res);

    /* calculate the borrow chain.  See note at top */
    bc = (res & (~d | s)) | (~d & s);
    CONDITIONAL_SET_FLAG(bc & 0x80000000, F_CF);
    CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
    CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
    return res;
}

/****************************************************************************
REMARKS:
Implements the TEST instruction and side effects.
****************************************************************************/
void test_byte(u8 d, u8 s)
{
    u32 res;   /* all operands in native machine order */

    res = d & s;

    CLEAR_FLAG(F_OF);
    set_szp_flags_8((u8)res);
    /* AF == dont care */
    CLEAR_FLAG(F_CF);
}

/****************************************************************************
REMARKS:
Implements the TEST instruction and side effects.
****************************************************************************/
void test_word(u16 d, u16 s)
{
    u32 res;   /* all operands in native machine order */

    res = d & s;

    CLEAR_FLAG(F_OF);
    set_szp_flags_16((u16)res);
    /* AF == dont care */
    CLEAR_FLAG(F_CF);
}

/****************************************************************************
REMARKS:
Implements the TEST instruction and side effects.
****************************************************************************/
void test_long(u32 d, u32 s)
{
    u32 res;   /* all operands in native machine order */

    res = d & s;

    CLEAR_FLAG(F_OF);
    set_szp_flags_32(res);
    /* AF == dont care */
    CLEAR_FLAG(F_CF);
}

/****************************************************************************
REMARKS:
Implements the XOR instruction and side effects.
****************************************************************************/
u8 xor_byte(u8 d, u8 s)
{
    u8 res;    /* all operands in native machine order */

    res = d ^ s;
    no_carry_byte_side_eff(res);
    return res;
}

/****************************************************************************
REMARKS:
Implements the XOR instruction and side effects.
****************************************************************************/
u16 xor_word(u16 d, u16 s)
{
    u16 res;   /* all operands in native machine order */

    res = d ^ s;
    no_carry_word_side_eff(res);
    return res;
}

/****************************************************************************
REMARKS:
Implements the XOR instruction and side effects.
****************************************************************************/
u32 xor_long(u32 d, u32 s)
{
    u32 res;   /* all operands in native machine order */

    res = d ^ s;
    no_carry_long_side_eff(res);
    return res;
}

/****************************************************************************
REMARKS:
Implements the IMUL instruction and side effects.
****************************************************************************/
void imul_byte(u8 s)
{
    s16 res = (s16)((s8)M.x86.R_AL * (s8)s);

    M.x86.R_AX = res;
    if (((M.x86.R_AL & 0x80) == 0 && M.x86.R_AH == 0x00) ||
	((M.x86.R_AL & 0x80) != 0 && M.x86.R_AH == 0xFF)) {
	CLEAR_FLAG(F_CF);
	CLEAR_FLAG(F_OF);
    } else {
	SET_FLAG(F_CF);
	SET_FLAG(F_OF);
    }
}

/****************************************************************************
REMARKS:
Implements the IMUL instruction and side effects.
****************************************************************************/
void imul_word(u16 s)
{
    s32 res = (s16)M.x86.R_AX * (s16)s;

    M.x86.R_AX = (u16)res;
    M.x86.R_DX = (u16)(res >> 16);
    if (((M.x86.R_AX & 0x8000) == 0 && M.x86.R_DX == 0x0000) ||
	((M.x86.R_AX & 0x8000) != 0 && M.x86.R_DX == 0xFFFF)) {
	CLEAR_FLAG(F_CF);
	CLEAR_FLAG(F_OF);
    } else {
	SET_FLAG(F_CF);
	SET_FLAG(F_OF);
    }
}

/****************************************************************************
REMARKS:
Implements the IMUL instruction and side effects.
****************************************************************************/
void imul_long_direct(u32 *res_lo, u32* res_hi,u32 d, u32 s)
{
#ifdef	__HAS_LONG_LONG__
    s64 res = (s32)d * (s32)s;

    *res_lo = (u32)res;
    *res_hi = (u32)(res >> 32);
#else
    u32 d_lo,d_hi,d_sign;
    u32 s_lo,s_hi,s_sign;
    u32 rlo_lo,rlo_hi,rhi_lo;

    if ((d_sign = d & 0x80000000) != 0)
	d = -d;
    d_lo = d & 0xFFFF;
    d_hi = d >> 16;
    if ((s_sign = s & 0x80000000) != 0)
	s = -s;
    s_lo = s & 0xFFFF;
    s_hi = s >> 16;
    rlo_lo = d_lo * s_lo;
    rlo_hi = (d_hi * s_lo + d_lo * s_hi) + (rlo_lo >> 16);
    rhi_lo = d_hi * s_hi + (rlo_hi >> 16);
    *res_lo = (rlo_hi << 16) | (rlo_lo & 0xFFFF);
    *res_hi = rhi_lo;
    if (d_sign != s_sign) {
	d = ~*res_lo;
	s = (((d & 0xFFFF) + 1) >> 16) + (d >> 16);
	*res_lo = ~*res_lo+1;
	*res_hi = ~*res_hi+(s >> 16);
	}
#endif
}

/****************************************************************************
REMARKS:
Implements the IMUL instruction and side effects.
****************************************************************************/
void imul_long(u32 s)
{
    imul_long_direct(&M.x86.R_EAX,&M.x86.R_EDX,M.x86.R_EAX,s);
    if (((M.x86.R_EAX & 0x80000000) == 0 && M.x86.R_EDX == 0x00000000) ||
	((M.x86.R_EAX & 0x80000000) != 0 && M.x86.R_EDX == 0xFFFFFFFF)) {
	CLEAR_FLAG(F_CF);
	CLEAR_FLAG(F_OF);
    } else {
	SET_FLAG(F_CF);
	SET_FLAG(F_OF);
    }
}

/****************************************************************************
REMARKS:
Implements the MUL instruction and side effects.
****************************************************************************/
void mul_byte(u8 s)
{
    u16 res = (u16)(M.x86.R_AL * s);

    M.x86.R_AX = res;
    if (M.x86.R_AH == 0) {
	CLEAR_FLAG(F_CF);
	CLEAR_FLAG(F_OF);
    } else {
	SET_FLAG(F_CF);
	SET_FLAG(F_OF);
    }
}

/****************************************************************************
REMARKS:
Implements the MUL instruction and side effects.
****************************************************************************/
void mul_word(u16 s)
{
    u32 res = M.x86.R_AX * s;

    M.x86.R_AX = (u16)res;
    M.x86.R_DX = (u16)(res >> 16);
    if (M.x86.R_DX == 0) {
	CLEAR_FLAG(F_CF);
	CLEAR_FLAG(F_OF);
    } else {
	SET_FLAG(F_CF);
	SET_FLAG(F_OF);
    }
}

/****************************************************************************
REMARKS:
Implements the MUL instruction and side effects.
****************************************************************************/
void mul_long(u32 s)
{
#ifdef	__HAS_LONG_LONG__
    u64 res = (u32)M.x86.R_EAX * (u32)s;

    M.x86.R_EAX = (u32)res;
    M.x86.R_EDX = (u32)(res >> 32);
#else
    u32 a,a_lo,a_hi;
    u32 s_lo,s_hi;
    u32 rlo_lo,rlo_hi,rhi_lo;

    a = M.x86.R_EAX;
    a_lo = a & 0xFFFF;
    a_hi = a >> 16;
    s_lo = s & 0xFFFF;
    s_hi = s >> 16;
    rlo_lo = a_lo * s_lo;
    rlo_hi = (a_hi * s_lo + a_lo * s_hi) + (rlo_lo >> 16);
    rhi_lo = a_hi * s_hi + (rlo_hi >> 16);
    M.x86.R_EAX = (rlo_hi << 16) | (rlo_lo & 0xFFFF);
    M.x86.R_EDX = rhi_lo;
#endif
    if (M.x86.R_EDX == 0) {
	CLEAR_FLAG(F_CF);
	CLEAR_FLAG(F_OF);
    } else {
	SET_FLAG(F_CF);
	SET_FLAG(F_OF);
    }
}

/****************************************************************************
REMARKS:
Implements the IDIV instruction and side effects.
****************************************************************************/
void idiv_byte(u8 s)
{
    s32 dvd, div, mod;

    dvd = (s16)M.x86.R_AX;
    if (s == 0) {
	x86emu_intr_raise(0);
	return;
    }
    div = dvd / (s8)s;
    mod = dvd % (s8)s;
    if (abs(div) > 0x7f) {
	x86emu_intr_raise(0);
	return;
    }
    M.x86.R_AL = (s8) div;
    M.x86.R_AH = (s8) mod;
}

/****************************************************************************
REMARKS:
Implements the IDIV instruction and side effects.
****************************************************************************/
void idiv_word(u16 s)
{
    s32 dvd, div, mod;

    dvd = (((s32)M.x86.R_DX) << 16) | M.x86.R_AX;
    if (s == 0) {
	x86emu_intr_raise(0);
	return;
    }
    div = dvd / (s16)s;
    mod = dvd % (s16)s;
    if (abs(div) > 0x7fff) {
	x86emu_intr_raise(0);
	return;
    }
    CLEAR_FLAG(F_CF);
    CLEAR_FLAG(F_SF);
    CONDITIONAL_SET_FLAG(div == 0, F_ZF);
    set_parity_flag(mod);

    M.x86.R_AX = (u16)div;
    M.x86.R_DX = (u16)mod;
}

/****************************************************************************
REMARKS:
Implements the IDIV instruction and side effects.
****************************************************************************/
void idiv_long(u32 s)
{
#ifdef	__HAS_LONG_LONG__
    s64 dvd, div, mod;

    dvd = (((s64)M.x86.R_EDX) << 32) | M.x86.R_EAX;
    if (s == 0) {
	x86emu_intr_raise(0);
	return;
    }
    div = dvd / (s32)s;
    mod = dvd % (s32)s;
    if (abs(div) > 0x7fffffff) {
	x86emu_intr_raise(0);
	return;
    }
#else
    s32 div = 0, mod;
    s32 h_dvd = M.x86.R_EDX;
    u32 l_dvd = M.x86.R_EAX;
    u32 abs_s = s & 0x7FFFFFFF;
    u32 abs_h_dvd = h_dvd & 0x7FFFFFFF;
    u32 h_s = abs_s >> 1;
    u32 l_s = abs_s << 31;
    int counter = 31;
    int carry;

    if (s == 0) {
	x86emu_intr_raise(0);
	return;
    }
    do {
	div <<= 1;
	carry = (l_dvd >= l_s) ? 0 : 1;

	if (abs_h_dvd < (h_s + carry)) {
	    h_s >>= 1;
	    l_s = abs_s << (--counter);
	    continue;
	} else {
	    abs_h_dvd -= (h_s + carry);
	    l_dvd = carry ? ((0xFFFFFFFF - l_s) + l_dvd + 1)
		: (l_dvd - l_s);
	    h_s >>= 1;
	    l_s = abs_s << (--counter);
	    div |= 1;
	    continue;
	}

    } while (counter > -1);
    /* overflow */
    if (abs_h_dvd || (l_dvd > abs_s)) {
	x86emu_intr_raise(0);
	return;
    }
    /* sign */
    div |= ((h_dvd & 0x10000000) ^ (s & 0x10000000));
    mod = l_dvd;

#endif
    CLEAR_FLAG(F_CF);
    CLEAR_FLAG(F_AF);
    CLEAR_FLAG(F_SF);
    SET_FLAG(F_ZF);
    set_parity_flag(mod);

    M.x86.R_EAX = (u32)div;
    M.x86.R_EDX = (u32)mod;
}

/****************************************************************************
REMARKS:
Implements the DIV instruction and side effects.
****************************************************************************/
void div_byte(u8 s)
{
    u32 dvd, div, mod;

    dvd = M.x86.R_AX;
    if (s == 0) {
	x86emu_intr_raise(0);
	return;
    }
    div = dvd / (u8)s;
    mod = dvd % (u8)s;
    if (abs(div) > 0xff) {
	x86emu_intr_raise(0);
	return;
    }
    M.x86.R_AL = (u8)div;
    M.x86.R_AH = (u8)mod;
}

/****************************************************************************
REMARKS:
Implements the DIV instruction and side effects.
****************************************************************************/
void div_word(u16 s)
{
    u32 dvd, div, mod;

    dvd = (((u32)M.x86.R_DX) << 16) | M.x86.R_AX;
    if (s == 0) {
	x86emu_intr_raise(0);
	return;
    }
    div = dvd / (u16)s;
    mod = dvd % (u16)s;
    if (abs(div) > 0xffff) {
	x86emu_intr_raise(0);
	return;
    }
    CLEAR_FLAG(F_CF);
    CLEAR_FLAG(F_SF);
    CONDITIONAL_SET_FLAG(div == 0, F_ZF);
    set_parity_flag(mod);

    M.x86.R_AX = (u16)div;
    M.x86.R_DX = (u16)mod;
}

/****************************************************************************
REMARKS:
Implements the DIV instruction and side effects.
****************************************************************************/
void div_long(u32 s)
{
#ifdef	__HAS_LONG_LONG__
    u64 dvd, div, mod;

    dvd = (((u64)M.x86.R_EDX) << 32) | M.x86.R_EAX;
    if (s == 0) {
	x86emu_intr_raise(0);
	return;
    }
    div = dvd / (u32)s;
    mod = dvd % (u32)s;
    if (abs(div) > 0xffffffff) {
	x86emu_intr_raise(0);
	return;
    }
#else
    s32 div = 0, mod;
    s32 h_dvd = M.x86.R_EDX;
    u32 l_dvd = M.x86.R_EAX;

    u32 h_s = s;
    u32 l_s = 0;
    int counter = 32;
    int carry;

    if (s == 0) {
	x86emu_intr_raise(0);
	return;
    }
    do {
	div <<= 1;
	carry = (l_dvd >= l_s) ? 0 : 1;

	if (h_dvd < (h_s + carry)) {
	    h_s >>= 1;
	    l_s = s << (--counter);
	    continue;
	} else {
	    h_dvd -= (h_s + carry);
	    l_dvd = carry ? ((0xFFFFFFFF - l_s) + l_dvd + 1)
		: (l_dvd - l_s);
	    h_s >>= 1;
	    l_s = s << (--counter);
	    div |= 1;
	    continue;
	}

    } while (counter > -1);
    /* overflow */
    if (h_dvd || (l_dvd > s)) {
	x86emu_intr_raise(0);
	return;
    }
    mod = l_dvd;
#endif
    CLEAR_FLAG(F_CF);
    CLEAR_FLAG(F_AF);
    CLEAR_FLAG(F_SF);
    SET_FLAG(F_ZF);
    set_parity_flag(mod);

    M.x86.R_EAX = (u32)div;
    M.x86.R_EDX = (u32)mod;
}

/****************************************************************************
REMARKS:
Implements the IN string instruction and side effects.
****************************************************************************/

static void single_in(int size)
{
    if(size == 1)
	store_data_byte_abs(M.x86.R_ES, M.x86.R_DI,(*sys_inb)(M.x86.R_DX));
    else if (size == 2)
	store_data_word_abs(M.x86.R_ES, M.x86.R_DI,(*sys_inw)(M.x86.R_DX));
    else
	store_data_long_abs(M.x86.R_ES, M.x86.R_DI,(*sys_inl)(M.x86.R_DX));
}

void ins(int size)
{
    int inc = size;

    if (ACCESS_FLAG(F_DF)) {
	inc = -size;
    }
    if (M.x86.mode & (SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE)) {
	/* dont care whether REPE or REPNE */
	/* in until CX is ZERO. */
	u32 count = ((M.x86.mode & SYSMODE_PREFIX_DATA) ?
		     M.x86.R_ECX : M.x86.R_CX);

	while (count--) {
	  single_in(size);
	  M.x86.R_DI += inc;
	  }
	M.x86.R_CX = 0;
	if (M.x86.mode & SYSMODE_PREFIX_DATA) {
	    M.x86.R_ECX = 0;
	}
	M.x86.mode &= ~(SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE);
    } else {
	single_in(size);
	M.x86.R_DI += inc;
    }
}

/****************************************************************************
REMARKS:
Implements the OUT string instruction and side effects.
****************************************************************************/

static void single_out(int size)
{
     if(size == 1)
       (*sys_outb)(M.x86.R_DX,fetch_data_byte_abs(M.x86.R_ES, M.x86.R_SI));
     else if (size == 2)
       (*sys_outw)(M.x86.R_DX,fetch_data_word_abs(M.x86.R_ES, M.x86.R_SI));
     else
       (*sys_outl)(M.x86.R_DX,fetch_data_long_abs(M.x86.R_ES, M.x86.R_SI));
}

void outs(int size)
{
    int inc = size;

    if (ACCESS_FLAG(F_DF)) {
	inc = -size;
    }
    if (M.x86.mode & (SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE)) {
	/* dont care whether REPE or REPNE */
	/* out until CX is ZERO. */
	u32 count = ((M.x86.mode & SYSMODE_PREFIX_DATA) ?
		     M.x86.R_ECX : M.x86.R_CX);
	while (count--) {
	  single_out(size);
	  M.x86.R_SI += inc;
	  }
	M.x86.R_CX = 0;
	if (M.x86.mode & SYSMODE_PREFIX_DATA) {
	    M.x86.R_ECX = 0;
	}
	M.x86.mode &= ~(SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE);
    } else {
	single_out(size);
	M.x86.R_SI += inc;
    }
}

/****************************************************************************
PARAMETERS:
addr	- Address to fetch word from

REMARKS:
Fetches a word from emulator memory using an absolute address.
****************************************************************************/
u16 mem_access_word(int addr)
{
DB( if (CHECK_MEM_ACCESS())
      x86emu_check_mem_access(addr);)
    return (*sys_rdw)(addr);
}

/****************************************************************************
REMARKS:
Pushes a word onto the stack.

NOTE: Do not inline this, as (*sys_wrX) is already inline!
****************************************************************************/
void push_word(u16 w)
{
DB( if (CHECK_SP_ACCESS())
      x86emu_check_sp_access();)
    M.x86.R_SP -= 2;
    (*sys_wrw)(((u32)M.x86.R_SS << 4)  + M.x86.R_SP, w);
}

/****************************************************************************
REMARKS:
Pushes a long onto the stack.

NOTE: Do not inline this, as (*sys_wrX) is already inline!
****************************************************************************/
void push_long(u32 w)
{
DB( if (CHECK_SP_ACCESS())
      x86emu_check_sp_access();)
    M.x86.R_SP -= 4;
    (*sys_wrl)(((u32)M.x86.R_SS << 4)  + M.x86.R_SP, w);
}

/****************************************************************************
REMARKS:
Pops a word from the stack.

NOTE: Do not inline this, as (*sys_rdX) is already inline!
****************************************************************************/
u16 pop_word(void)
{
    u16 res;

DB( if (CHECK_SP_ACCESS())
      x86emu_check_sp_access();)
    res = (*sys_rdw)(((u32)M.x86.R_SS << 4)  + M.x86.R_SP);
    M.x86.R_SP += 2;
    return res;
}

/****************************************************************************
REMARKS:
Pops a long from the stack.

NOTE: Do not inline this, as (*sys_rdX) is already inline!
****************************************************************************/
u32 pop_long(void)
{
    u32 res;

DB( if (CHECK_SP_ACCESS())
      x86emu_check_sp_access();)
    res = (*sys_rdl)(((u32)M.x86.R_SS << 4)  + M.x86.R_SP);
    M.x86.R_SP += 4;
    return res;
}