ivm_core.c 64.7 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 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149
// SPDX-License-Identifier: GPL-2.0+
/*
 * Porting to u-boot:
 *
 * (C) Copyright 2010
 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
 *
 * Lattice ispVME Embedded code to load Lattice's FPGA:
 *
 * Copyright 2009 Lattice Semiconductor Corp.
 *
 * ispVME Embedded allows programming of Lattice's suite of FPGA
 * devices on embedded systems through the JTAG port.  The software
 * is distributed in source code form and is open to re - distribution
 * and modification where applicable.
 *
 * Revision History of ivm_core.c module:
 * 4/25/06 ht   Change some variables from unsigned short or int
 *              to long int to make the code compiler independent.
 * 5/24/06 ht   Support using RESET (TRST) pin as a special purpose
 *              control pin such as triggering the loading of known
 *              state exit.
 * 3/6/07 ht added functions to support output to terminals
 *
 * 09/11/07 NN Type cast mismatch variables
 *		   Moved the sclock() function to hardware.c
 * 08/28/08 NN Added Calculate checksum support.
 * 4/1/09 Nguyen replaced the recursive function call codes on
 *        the ispVMLCOUNT function
 */

#include <common.h>
#include <linux/string.h>
#include <malloc.h>
#include <lattice.h>

#define vme_out_char(c)	printf("%c", c)
#define vme_out_hex(c)	printf("%x", c)
#define vme_out_string(s) printf("%s", s)

/*
 *
 * Global variables used to specify the flow control and data type.
 *
 *	g_usFlowControl:	flow control register. Each bit in the
 *                               register can potentially change the
 *                               personality of the embedded engine.
 *	g_usDataType:		holds the data type of the current row.
 *
 */

static unsigned short g_usFlowControl;
unsigned short g_usDataType;

/*
 *
 * Global variables used to specify the ENDDR and ENDIR.
 *
 *	g_ucEndDR:		the state that the device goes to after SDR.
 *	g_ucEndIR:		the state that the device goes to after SIR.
 *
 */

unsigned char g_ucEndDR = DRPAUSE;
unsigned char g_ucEndIR = IRPAUSE;

/*
 *
 * Global variables used to support header/trailer.
 *
 *	g_usHeadDR:		the number of lead devices in bypass.
 *	g_usHeadIR:		the sum of IR length of lead devices.
 *	g_usTailDR:		the number of tail devices in bypass.
 *	g_usTailIR:		the sum of IR length of tail devices.
 *
 */

static unsigned short g_usHeadDR;
static unsigned short g_usHeadIR;
static unsigned short g_usTailDR;
static unsigned short g_usTailIR;

/*
 *
 * Global variable to store the number of bits of data or instruction
 * to be shifted into or out from the device.
 *
 */

static unsigned short g_usiDataSize;

/*
 *
 * Stores the frequency. Default to 1 MHz.
 *
 */

static int g_iFrequency = 1000;

/*
 *
 * Stores the maximum amount of ram needed to hold a row of data.
 *
 */

static unsigned short g_usMaxSize;

/*
 *
 * Stores the LSH or RSH value.
 *
 */

static unsigned short g_usShiftValue;

/*
 *
 * Stores the current repeat loop value.
 *
 */

static unsigned short g_usRepeatLoops;

/*
 *
 * Stores the current vendor.
 *
 */

static signed char g_cVendor = LATTICE;

/*
 *
 * Stores the VME file CRC.
 *
 */

unsigned short g_usCalculatedCRC;

/*
 *
 * Stores the Device Checksum.
 *
 */
/* 08/28/08 NN Added Calculate checksum support. */
unsigned long g_usChecksum;
static unsigned int g_uiChecksumIndex;

/*
 *
 * Stores the current state of the JTAG state machine.
 *
 */

static signed char g_cCurrentJTAGState;

/*
 *
 * Global variables used to support looping.
 *
 *	g_pucHeapMemory:	holds the entire repeat loop.
 *	g_iHeapCounter:		points to the current byte in the repeat loop.
 *	g_iHEAPSize:		the current size of the repeat in bytes.
 *
 */

unsigned char *g_pucHeapMemory;
unsigned short g_iHeapCounter;
unsigned short g_iHEAPSize;
static unsigned short previous_size;

/*
 *
 * Global variables used to support intelligent programming.
 *
 *	g_usIntelDataIndex:     points to the current byte of the
 *                               intelligent buffer.
 *	g_usIntelBufferSize:	holds the size of the intelligent
 *                               buffer.
 *
 */

unsigned short g_usIntelDataIndex;
unsigned short g_usIntelBufferSize;

/*
 *
 * Supported VME versions.
 *
 */

const char *const g_szSupportedVersions[] = {
	"__VME2.0", "__VME3.0", "____12.0", "____12.1", 0};

/*
 *
 * Holds the maximum size of each respective buffer. These variables are used
 * to write the HEX files when converting VME to HEX.
 *
*/

static unsigned short g_usTDOSize;
static unsigned short g_usMASKSize;
static unsigned short g_usTDISize;
static unsigned short g_usDMASKSize;
static unsigned short g_usLCOUNTSize;
static unsigned short g_usHDRSize;
static unsigned short g_usTDRSize;
static unsigned short g_usHIRSize;
static unsigned short g_usTIRSize;
static unsigned short g_usHeapSize;

/*
 *
 * Global variables used to store data.
 *
 *	g_pucOutMaskData:	local RAM to hold one row of MASK data.
 *	g_pucInData:		local RAM to hold one row of TDI data.
 *	g_pucOutData:		local RAM to hold one row of TDO data.
 *	g_pucHIRData:		local RAM to hold the current SIR header.
 *	g_pucTIRData:		local RAM to hold the current SIR trailer.
 *	g_pucHDRData:		local RAM to hold the current SDR header.
 *	g_pucTDRData:		local RAM to hold the current SDR trailer.
 *	g_pucIntelBuffer:	local RAM to hold the current intelligent buffer
 *	g_pucOutDMaskData:	local RAM to hold one row of DMASK data.
 *
 */

unsigned char	*g_pucOutMaskData	= NULL,
		*g_pucInData		= NULL,
		*g_pucOutData		= NULL,
		*g_pucHIRData		= NULL,
		*g_pucTIRData		= NULL,
		*g_pucHDRData		= NULL,
		*g_pucTDRData		= NULL,
		*g_pucIntelBuffer	= NULL,
		*g_pucOutDMaskData	= NULL;

/*
 *
 * JTAG state machine transition table.
 *
 */

struct {
	 unsigned char  CurState;  /* From this state */
	 unsigned char  NextState; /* Step to this state */
	 unsigned char  Pattern;   /* The tragetory of TMS */
	 unsigned char  Pulses;    /* The number of steps */
} g_JTAGTransistions[25] = {
{ RESET,	RESET,		0xFC, 6 },	/* Transitions from RESET */
{ RESET,	IDLE,		0x00, 1 },
{ RESET,	DRPAUSE,	0x50, 5 },
{ RESET,	IRPAUSE,	0x68, 6 },
{ IDLE,		RESET,		0xE0, 3 },	/* Transitions from IDLE */
{ IDLE,		DRPAUSE,	0xA0, 4 },
{ IDLE,		IRPAUSE,	0xD0, 5 },
{ DRPAUSE,	RESET,		0xF8, 5 },	/* Transitions from DRPAUSE */
{ DRPAUSE,	IDLE,		0xC0, 3 },
{ DRPAUSE,	IRPAUSE,	0xF4, 7 },
{ DRPAUSE,	DRPAUSE,	0xE8, 6 },/* 06/14/06 Support POLL STATUS LOOP*/
{ IRPAUSE,	RESET,		0xF8, 5 },	/* Transitions from IRPAUSE */
{ IRPAUSE,	IDLE,		0xC0, 3 },
{ IRPAUSE,	DRPAUSE,	0xE8, 6 },
{ DRPAUSE,	SHIFTDR,	0x80, 2 }, /* Extra transitions using SHIFTDR */
{ IRPAUSE,	SHIFTDR,	0xE0, 5 },
{ SHIFTDR,	DRPAUSE,	0x80, 2 },
{ SHIFTDR,	IDLE,		0xC0, 3 },
{ IRPAUSE,	SHIFTIR,	0x80, 2 },/* Extra transitions using SHIFTIR */
{ SHIFTIR,	IRPAUSE,	0x80, 2 },
{ SHIFTIR,	IDLE,		0xC0, 3 },
{ DRPAUSE,	DRCAPTURE,	0xE0, 4 }, /* 11/15/05 Support DRCAPTURE*/
{ DRCAPTURE, DRPAUSE,	0x80, 2 },
{ IDLE,     DRCAPTURE,	0x80, 2 },
{ IRPAUSE,  DRCAPTURE,  0xE0, 4 }
};

/*
 *
 * List to hold all LVDS pairs.
 *
 */

LVDSPair *g_pLVDSList;
unsigned short g_usLVDSPairCount;

/*
 *
 * Function prototypes.
 *
 */

static signed char ispVMDataCode(void);
static long int ispVMDataSize(void);
static void ispVMData(unsigned char *Data);
static signed char ispVMShift(signed char Code);
static signed char ispVMAmble(signed char Code);
static signed char ispVMLoop(unsigned short a_usLoopCount);
static signed char ispVMBitShift(signed char mode, unsigned short bits);
static void ispVMComment(unsigned short a_usCommentSize);
static void ispVMHeader(unsigned short a_usHeaderSize);
static signed char ispVMLCOUNT(unsigned short a_usCountSize);
static void ispVMClocks(unsigned short Clocks);
static void ispVMBypass(signed char ScanType, unsigned short Bits);
static void ispVMStateMachine(signed char NextState);
static signed char ispVMSend(unsigned short int);
static signed char ispVMRead(unsigned short int);
static signed char ispVMReadandSave(unsigned short int);
static signed char ispVMProcessLVDS(unsigned short a_usLVDSCount);
static void ispVMMemManager(signed char types, unsigned short size);

/*
 *
 * External variables and functions in hardware.c module
 *
 */
static signed char g_cCurrentJTAGState;

#ifdef DEBUG

/*
 *
 * GetState
 *
 * Returns the state as a string based on the opcode. Only used
 * for debugging purposes.
 *
 */

const char *GetState(unsigned char a_ucState)
{
	switch (a_ucState) {
	case RESET:
		return "RESET";
	case IDLE:
		return "IDLE";
	case IRPAUSE:
		return "IRPAUSE";
	case DRPAUSE:
		return "DRPAUSE";
	case SHIFTIR:
		return "SHIFTIR";
	case SHIFTDR:
		return "SHIFTDR";
	case DRCAPTURE:/* 11/15/05 support DRCAPTURE*/
		return "DRCAPTURE";
	default:
		break;
	}

	return 0;
}

/*
 *
 * PrintData
 *
 * Prints the data. Only used for debugging purposes.
 *
 */

void PrintData(unsigned short a_iDataSize, unsigned char *a_pucData)
{
	/* 09/11/07 NN added local variables initialization */
	unsigned short usByteSize  = 0;
	unsigned short usBitIndex  = 0;
	signed short usByteIndex   = 0;
	unsigned char ucByte       = 0;
	unsigned char ucFlipByte   = 0;

	if (a_iDataSize % 8) {
		/* 09/11/07 NN Type cast mismatch variables */
		usByteSize = (unsigned short)(a_iDataSize / 8 + 1);
	} else {
		/* 09/11/07 NN Type cast mismatch variables */
		usByteSize = (unsigned short)(a_iDataSize / 8);
	}
	puts("(");
	/* 09/11/07 NN Type cast mismatch variables */
	for (usByteIndex = (signed short)(usByteSize - 1);
		usByteIndex >= 0; usByteIndex--) {
		ucByte = a_pucData[usByteIndex];
		ucFlipByte = 0x00;

		/*
		*
		* Flip each byte.
		*
		*/

		for (usBitIndex = 0; usBitIndex < 8; usBitIndex++) {
			ucFlipByte <<= 1;
			if (ucByte & 0x1) {
				ucFlipByte |= 0x1;
			}

			ucByte >>= 1;
		}

		/*
		*
		* Print the flipped byte.
		*
		*/

		printf("%.02X", ucFlipByte);
		if ((usByteSize - usByteIndex) % 40 == 39) {
			puts("\n\t\t");
		}
		if (usByteIndex < 0)
			break;
	}
	puts(")");
}
#endif /* DEBUG */

void ispVMMemManager(signed char cTarget, unsigned short usSize)
{
	switch (cTarget) {
	case XTDI:
	case TDI:
		if (g_pucInData != NULL) {
			if (previous_size == usSize) {/*memory exist*/
				break;
			} else {
				free(g_pucInData);
				g_pucInData = NULL;
			}
		}
		g_pucInData = (unsigned char *) malloc(usSize / 8 + 2);
		previous_size = usSize;
	case XTDO:
	case TDO:
		if (g_pucOutData != NULL) {
			if (previous_size == usSize) { /*already exist*/
				break;
			} else {
				free(g_pucOutData);
				g_pucOutData = NULL;
			}
		}
		g_pucOutData = (unsigned char *) malloc(usSize / 8 + 2);
		previous_size = usSize;
		break;
	case MASK:
		if (g_pucOutMaskData != NULL) {
			if (previous_size == usSize) {/*already allocated*/
				break;
			} else {
				free(g_pucOutMaskData);
				g_pucOutMaskData = NULL;
			}
		}
		g_pucOutMaskData = (unsigned char *) malloc(usSize / 8 + 2);
		previous_size = usSize;
		break;
	case HIR:
		if (g_pucHIRData != NULL) {
			free(g_pucHIRData);
			g_pucHIRData = NULL;
		}
		g_pucHIRData = (unsigned char *) malloc(usSize / 8 + 2);
		break;
	case TIR:
		if (g_pucTIRData != NULL) {
			free(g_pucTIRData);
			g_pucTIRData = NULL;
		}
		g_pucTIRData = (unsigned char *) malloc(usSize / 8 + 2);
		break;
	case HDR:
		if (g_pucHDRData != NULL) {
			free(g_pucHDRData);
			g_pucHDRData = NULL;
		}
		g_pucHDRData = (unsigned char *) malloc(usSize / 8 + 2);
		break;
	case TDR:
		if (g_pucTDRData != NULL) {
			free(g_pucTDRData);
			g_pucTDRData = NULL;
		}
		g_pucTDRData = (unsigned char *) malloc(usSize / 8 + 2);
		break;
	case HEAP:
		if (g_pucHeapMemory != NULL) {
			free(g_pucHeapMemory);
			g_pucHeapMemory = NULL;
		}
		g_pucHeapMemory = (unsigned char *) malloc(usSize + 2);
		break;
	case DMASK:
		if (g_pucOutDMaskData != NULL) {
			if (previous_size == usSize) { /*already allocated*/
				break;
			} else {
				free(g_pucOutDMaskData);
				g_pucOutDMaskData = NULL;
			}
		}
		g_pucOutDMaskData = (unsigned char *) malloc(usSize / 8 + 2);
		previous_size = usSize;
		break;
	case LHEAP:
		if (g_pucIntelBuffer != NULL) {
			free(g_pucIntelBuffer);
			g_pucIntelBuffer = NULL;
		}
		g_pucIntelBuffer = (unsigned char *) malloc(usSize + 2);
		break;
	case LVDS:
		if (g_pLVDSList != NULL) {
			free(g_pLVDSList);
			g_pLVDSList = NULL;
		}
		g_pLVDSList = (LVDSPair *) malloc(usSize * sizeof(LVDSPair));
		if (g_pLVDSList)
			memset(g_pLVDSList, 0, usSize * sizeof(LVDSPair));
		break;
	default:
		return;
    }
}

void ispVMFreeMem(void)
{
	if (g_pucHeapMemory != NULL) {
		free(g_pucHeapMemory);
		g_pucHeapMemory = NULL;
	}

	if (g_pucOutMaskData != NULL) {
		free(g_pucOutMaskData);
		g_pucOutMaskData = NULL;
	}

	if (g_pucInData != NULL) {
		free(g_pucInData);
		g_pucInData = NULL;
	}

	if (g_pucOutData != NULL) {
		free(g_pucOutData);
		g_pucOutData = NULL;
	}

	if (g_pucHIRData != NULL) {
		free(g_pucHIRData);
		g_pucHIRData = NULL;
	}

	if (g_pucTIRData != NULL) {
		free(g_pucTIRData);
		g_pucTIRData = NULL;
	}

	if (g_pucHDRData != NULL) {
		free(g_pucHDRData);
		g_pucHDRData = NULL;
	}

	if (g_pucTDRData != NULL) {
		free(g_pucTDRData);
		g_pucTDRData = NULL;
	}

	if (g_pucOutDMaskData != NULL) {
		free(g_pucOutDMaskData);
		g_pucOutDMaskData = NULL;
	}

	if (g_pucIntelBuffer != NULL) {
		free(g_pucIntelBuffer);
		g_pucIntelBuffer = NULL;
	}

	if (g_pLVDSList != NULL) {
		free(g_pLVDSList);
		g_pLVDSList = NULL;
	}
}


/*
 *
 * ispVMDataSize
 *
 * Returns a VME-encoded number, usually used to indicate the
 * bit length of an SIR/SDR command.
 *
 */

long int ispVMDataSize()
{
	/* 09/11/07 NN added local variables initialization */
	long int iSize           = 0;
	signed char cCurrentByte = 0;
	signed char cIndex       = 0;
	cIndex = 0;
	while ((cCurrentByte = GetByte()) & 0x80) {
		iSize |= ((long int) (cCurrentByte & 0x7F)) << cIndex;
		cIndex += 7;
	}
	iSize |= ((long int) (cCurrentByte & 0x7F)) << cIndex;
	return iSize;
}

/*
 *
 * ispVMCode
 *
 * This is the heart of the embedded engine. All the high-level opcodes
 * are extracted here. Once they have been identified, then it
 * will call other functions to handle the processing.
 *
 */

signed char ispVMCode()
{
	/* 09/11/07 NN added local variables initialization */
	unsigned short iRepeatSize = 0;
	signed char cOpcode	   = 0;
	signed char cRetCode       = 0;
	unsigned char ucState      = 0;
	unsigned short usDelay     = 0;
	unsigned short usToggle    = 0;
	unsigned char usByte       = 0;

	/*
	*
	* Check the compression flag only if this is the first time
	* this function is entered. Do not check the compression flag if
	* it is being called recursively from other functions within
	* the embedded engine.
	*
	*/

	if (!(g_usDataType & LHEAP_IN) && !(g_usDataType & HEAP_IN)) {
		usByte = GetByte();
		if (usByte == 0xf1) {
			g_usDataType |= COMPRESS;
		} else if (usByte == 0xf2) {
			g_usDataType &= ~COMPRESS;
		} else {
			return VME_INVALID_FILE;
		}
	}

	/*
	*
	* Begin looping through all the VME opcodes.
	*
	*/

	while ((cOpcode = GetByte()) >= 0) {

		switch (cOpcode) {
		case STATE:

			/*
			 * Step the JTAG state machine.
			 */

			ucState = GetByte();

			/*
			 * Step the JTAG state machine to DRCAPTURE
			 * to support Looping.
			 */

			if ((g_usDataType & LHEAP_IN) &&
				 (ucState == DRPAUSE) &&
				 (g_cCurrentJTAGState == ucState)) {
				ispVMStateMachine(DRCAPTURE);
			}

			ispVMStateMachine(ucState);

#ifdef DEBUG
			if (g_usDataType & LHEAP_IN) {
				debug("LDELAY %s ", GetState(ucState));
			} else {
				debug("STATE %s;\n", GetState(ucState));
			}
#endif /* DEBUG */
			break;
		case SIR:
		case SDR:
		case XSDR:

#ifdef DEBUG
			switch (cOpcode) {
			case SIR:
				puts("SIR ");
				break;
			case SDR:
			case XSDR:
				if (g_usDataType & LHEAP_IN) {
					puts("LSDR ");
				} else {
					puts("SDR ");
				}
				break;
			}
#endif /* DEBUG */
			/*
			*
			* Shift in data into the device.
			*
			*/

			cRetCode = ispVMShift(cOpcode);
			if (cRetCode != 0) {
				return cRetCode;
			}
			break;
		case WAIT:

			/*
			*
			* Observe delay.
			*
			*/

			/* 09/11/07 NN Type cast mismatch variables */
			usDelay = (unsigned short) ispVMDataSize();
			ispVMDelay(usDelay);

#ifdef DEBUG
			if (usDelay & 0x8000) {

				/*
				 * Since MSB is set, the delay time must be
				 * decoded to millisecond. The SVF2VME encodes
				 * the MSB to represent millisecond.
				 */

				usDelay &= ~0x8000;
				if (g_usDataType & LHEAP_IN) {
					printf("%.2E SEC;\n",
						(float) usDelay / 1000);
				} else {
					printf("RUNTEST %.2E SEC;\n",
						(float) usDelay / 1000);
				}
			} else {
				/*
				 * Since MSB is not set, the delay time
				 * is given as microseconds.
				 */

				if (g_usDataType & LHEAP_IN) {
					printf("%.2E SEC;\n",
						(float) usDelay / 1000000);
				} else {
					printf("RUNTEST %.2E SEC;\n",
						(float) usDelay / 1000000);
				}
			}
#endif /* DEBUG */
			break;
		case TCK:

			/*
			 * Issue clock toggles.
			*/

			/* 09/11/07 NN Type cast mismatch variables */
			usToggle = (unsigned short) ispVMDataSize();
			ispVMClocks(usToggle);

#ifdef DEBUG
			printf("RUNTEST %d TCK;\n", usToggle);
#endif /* DEBUG */
			break;
		case ENDDR:

			/*
			*
			* Set the ENDDR.
			*
			*/

			g_ucEndDR = GetByte();

#ifdef DEBUG
			printf("ENDDR %s;\n", GetState(g_ucEndDR));
#endif /* DEBUG */
			break;
		case ENDIR:

			/*
			*
			* Set the ENDIR.
			*
			*/

			g_ucEndIR = GetByte();

#ifdef DEBUG
			printf("ENDIR %s;\n", GetState(g_ucEndIR));
#endif /* DEBUG */
			break;
		case HIR:
		case TIR:
		case HDR:
		case TDR:

#ifdef DEBUG
			switch (cOpcode) {
			case HIR:
				puts("HIR ");
				break;
			case TIR:
				puts("TIR ");
				break;
			case HDR:
				puts("HDR ");
				break;
			case TDR:
				puts("TDR ");
				break;
			}
#endif /* DEBUG */
			/*
			 * Set the header/trailer of the device in order
			 * to bypass
			 * successfully.
			 */

			cRetCode = ispVMAmble(cOpcode);
			if (cRetCode != 0) {
				return cRetCode;
			}

#ifdef DEBUG
			puts(";\n");
#endif /* DEBUG */
			break;
		case MEM:

			/*
			 * The maximum RAM required to support
			 * processing one row of the VME file.
			 */

			/* 09/11/07 NN Type cast mismatch variables */
			g_usMaxSize = (unsigned short) ispVMDataSize();

#ifdef DEBUG
			printf("// MEMSIZE %d\n", g_usMaxSize);
#endif /* DEBUG */
			break;
		case VENDOR:

			/*
			*
			* Set the VENDOR type.
			*
			*/

			cOpcode = GetByte();
			switch (cOpcode) {
			case LATTICE:
#ifdef DEBUG
				puts("// VENDOR LATTICE\n");
#endif /* DEBUG */
				g_cVendor = LATTICE;
				break;
			case ALTERA:
#ifdef DEBUG
				puts("// VENDOR ALTERA\n");
#endif /* DEBUG */
				g_cVendor = ALTERA;
				break;
			case XILINX:
#ifdef DEBUG
				puts("// VENDOR XILINX\n");
#endif /* DEBUG */
				g_cVendor = XILINX;
				break;
			default:
				break;
			}
			break;
		case SETFLOW:

			/*
			 * Set the flow control. Flow control determines
			 * the personality of the embedded engine.
			 */

			/* 09/11/07 NN Type cast mismatch variables */
			g_usFlowControl |= (unsigned short) ispVMDataSize();
			break;
		case RESETFLOW:

			/*
			*
			* Unset the flow control.
			*
			*/

			/* 09/11/07 NN Type cast mismatch variables */
			g_usFlowControl &= (unsigned short) ~(ispVMDataSize());
			break;
		case HEAP:

			/*
			*
			* Allocate heap size to store loops.
			*
			*/

			cRetCode = GetByte();
			if (cRetCode != SECUREHEAP) {
				return VME_INVALID_FILE;
			}
			/* 09/11/07 NN Type cast mismatch variables */
			g_iHEAPSize = (unsigned short) ispVMDataSize();

			/*
			 * Store the maximum size of the HEAP buffer.
			 * Used to convert VME to HEX.
			 */

			if (g_iHEAPSize > g_usHeapSize) {
				g_usHeapSize = g_iHEAPSize;
			}

			ispVMMemManager(HEAP, (unsigned short) g_iHEAPSize);
			break;
		case REPEAT:

			/*
			*
			* Execute loops.
			*
			*/

			g_usRepeatLoops = 0;

			/* 09/11/07 NN Type cast mismatch variables */
			iRepeatSize = (unsigned short) ispVMDataSize();

			cRetCode = ispVMLoop((unsigned short) iRepeatSize);
			if (cRetCode != 0) {
				return cRetCode;
			}
			break;
		case ENDLOOP:

			/*
			*
			* Exit point from processing loops.
			*
			*/

			return cRetCode;
		case ENDVME:

			/*
			 * The only valid exit point that indicates
			 * end of programming.
			 */

			return cRetCode;
		case SHR:

			/*
			*
			* Right-shift address.
			*
			*/

			g_usFlowControl |= SHIFTRIGHT;

			/* 09/11/07 NN Type cast mismatch variables */
			g_usShiftValue = (unsigned short) (g_usRepeatLoops *
				(unsigned short)GetByte());
			break;
		case SHL:

			/*
			 * Left-shift address.
			 */

			g_usFlowControl |= SHIFTLEFT;

			/* 09/11/07 NN Type cast mismatch variables */
			g_usShiftValue = (unsigned short) (g_usRepeatLoops *
				(unsigned short)GetByte());
			break;
		case FREQUENCY:

			/*
			*
			* Set the frequency.
			*
			*/

			/* 09/11/07 NN Type cast mismatch variables */
			g_iFrequency = (int) (ispVMDataSize() / 1000);
			if (g_iFrequency == 1)
				g_iFrequency = 1000;

#ifdef DEBUG
			printf("FREQUENCY %.2E HZ;\n",
				(float) g_iFrequency * 1000);
#endif /* DEBUG */
			break;
		case LCOUNT:

			/*
			*
			* Process LCOUNT command.
			*
			*/

			cRetCode = ispVMLCOUNT((unsigned short)ispVMDataSize());
			if (cRetCode != 0) {
				return cRetCode;
			}
			break;
		case VUES:

			/*
			*
			* Set the flow control to verify USERCODE.
			*
			*/

			g_usFlowControl |= VERIFYUES;
			break;
		case COMMENT:

			/*
			*
			* Display comment.
			*
			*/

			ispVMComment((unsigned short) ispVMDataSize());
			break;
		case LVDS:

			/*
			*
			* Process LVDS command.
			*
			*/

			ispVMProcessLVDS((unsigned short) ispVMDataSize());
			break;
		case HEADER:

			/*
			*
			* Discard header.
			*
			*/

			ispVMHeader((unsigned short) ispVMDataSize());
			break;
		/* 03/14/06 Support Toggle ispENABLE signal*/
		case ispEN:
			ucState = GetByte();
			if ((ucState == ON) || (ucState == 0x01))
				writePort(g_ucPinENABLE, 0x01);
			else
				writePort(g_ucPinENABLE, 0x00);
			ispVMDelay(1);
			break;
		/* 05/24/06 support Toggle TRST pin*/
		case TRST:
			ucState = GetByte();
			if (ucState == 0x01)
				writePort(g_ucPinTRST, 0x01);
			else
				writePort(g_ucPinTRST, 0x00);
			ispVMDelay(1);
			break;
		default:

			/*
			*
			* Invalid opcode encountered.
			*
			*/

#ifdef DEBUG
			printf("\nINVALID OPCODE: 0x%.2X\n", cOpcode);
#endif /* DEBUG */

			return VME_INVALID_FILE;
		}
	}

	/*
	*
	* Invalid exit point. Processing the token 'ENDVME' is the only
	* valid way to exit the embedded engine.
	*
	*/

	return VME_INVALID_FILE;
}

/*
 *
 * ispVMDataCode
 *
 * Processes the TDI/TDO/MASK/DMASK etc of an SIR/SDR command.
 *
 */

signed char ispVMDataCode()
{
	/* 09/11/07 NN added local variables initialization */
	signed char cDataByte    = 0;
	signed char siDataSource = 0;  /*source of data from file by default*/

	if (g_usDataType & HEAP_IN) {
		siDataSource = 1;  /*the source of data from memory*/
	}

	/*
	*
	* Clear the data type register.
	*
	**/

	g_usDataType &= ~(MASK_DATA + TDI_DATA +
		TDO_DATA + DMASK_DATA + CMASK_DATA);

	/*
	 * Iterate through SIR/SDR command and look for TDI,
	 * TDO, MASK, etc.
	 */

	while ((cDataByte = GetByte()) >= 0) {
			ispVMMemManager(cDataByte, g_usMaxSize);
			switch (cDataByte) {
			case TDI:

				/*
				 * Store the maximum size of the TDI buffer.
				 * Used to convert VME to HEX.
				 */

				if (g_usiDataSize > g_usTDISize) {
					g_usTDISize = g_usiDataSize;
				}
				/*
				 * Updated data type register to indicate that
				 * TDI data is currently being used. Process the
				 * data in the VME file into the TDI buffer.
				 */

				g_usDataType |= TDI_DATA;
				ispVMData(g_pucInData);
				break;
			case XTDO:

				/*
				 * Store the maximum size of the TDO buffer.
				 * Used to convert VME to HEX.
				 */

				if (g_usiDataSize > g_usTDOSize) {
					g_usTDOSize = g_usiDataSize;
				}

				/*
				 * Updated data type register to indicate that
				 * TDO data is currently being used.
				 */

				g_usDataType |= TDO_DATA;
				break;
			case TDO:

				/*
				 * Store the maximum size of the TDO buffer.
				 * Used to convert VME to HEX.
				 */

				if (g_usiDataSize > g_usTDOSize) {
					g_usTDOSize = g_usiDataSize;
				}

				/*
				 * Updated data type register to indicate
				 * that TDO data is currently being used.
				 * Process the data in the VME file into the
				 * TDO buffer.
				 */

				g_usDataType |= TDO_DATA;
				ispVMData(g_pucOutData);
				break;
			case MASK:

				/*
				 * Store the maximum size of the MASK buffer.
				 * Used to convert VME to HEX.
				 */

				if (g_usiDataSize > g_usMASKSize) {
					g_usMASKSize = g_usiDataSize;
				}

				/*
				 * Updated data type register to indicate that
				 * MASK data is currently being used. Process
				 * the data in the VME file into the MASK buffer
				 */

				g_usDataType |= MASK_DATA;
				ispVMData(g_pucOutMaskData);
				break;
			case DMASK:

				/*
				 * Store the maximum size of the DMASK buffer.
				 * Used to convert VME to HEX.
				 */

				if (g_usiDataSize > g_usDMASKSize) {
					g_usDMASKSize = g_usiDataSize;
				}

				/*
				 * Updated data type register to indicate that
				 * DMASK data is currently being used. Process
				 * the data in the VME file into the DMASK
				 * buffer.
				 */

				g_usDataType |= DMASK_DATA;
				ispVMData(g_pucOutDMaskData);
				break;
			case CMASK:

				/*
				 * Updated data type register to indicate that
				 * MASK data is currently being used. Process
				 * the data in the VME file into the MASK buffer
				 */

				g_usDataType |= CMASK_DATA;
				ispVMData(g_pucOutMaskData);
				break;
			case CONTINUE:
				return 0;
			default:
				/*
				 * Encountered invalid opcode.
				 */
				return VME_INVALID_FILE;
			}

			switch (cDataByte) {
			case TDI:

				/*
				 * Left bit shift. Used when performing
				 * algorithm looping.
				 */

				if (g_usFlowControl & SHIFTLEFT) {
					ispVMBitShift(SHL, g_usShiftValue);
					g_usFlowControl &= ~SHIFTLEFT;
				}

				/*
				 * Right bit shift. Used when performing
				 * algorithm looping.
				 */

				if (g_usFlowControl & SHIFTRIGHT) {
					ispVMBitShift(SHR, g_usShiftValue);
					g_usFlowControl &= ~SHIFTRIGHT;
				}
			default:
				break;
			}

			if (siDataSource) {
				g_usDataType |= HEAP_IN; /*restore from memory*/
			}
	}

	if (siDataSource) {  /*fetch data from heap memory upon return*/
		g_usDataType |= HEAP_IN;
	}

	if (cDataByte < 0) {

		/*
		 * Encountered invalid opcode.
		 */

		return VME_INVALID_FILE;
	} else {
		return 0;
	}
}

/*
 *
 * ispVMData
 * Extract one row of data operand from the current data type opcode. Perform
 * the decompression if necessary. Extra RAM is not required for the
 * decompression process. The decompression scheme employed in this module
 * is on row by row basis. The format of the data stream:
 * [compression code][compressed data stream]
 * 0x00    --No compression
 * 0x01    --Compress by 0x00.
 *           Example:
 *           Original stream:   0x000000000000000000000001
 *           Compressed stream: 0x01000901
 *           Detail:            0x01 is the code, 0x00 is the key,
 *                              0x09 is the count of 0x00 bytes,
 *                              0x01 is the uncompressed byte.
 * 0x02    --Compress by 0xFF.
 *           Example:
 *           Original stream:   0xFFFFFFFFFFFFFFFFFFFFFF01
 *           Compressed stream: 0x02FF0901
 *           Detail:            0x02 is the code, 0xFF is the key,
 *                              0x09 is the count of 0xFF bytes,
 *                              0x01 is the uncompressed byte.
 * 0x03
 * : :
 * 0xFE   -- Compress by nibble blocks.
 *           Example:
 *           Original stream:   0x84210842108421084210
 *           Compressed stream: 0x0584210
 *           Detail:            0x05 is the code, means 5 nibbles block.
 *                              0x84210 is the 5 nibble blocks.
 *                              The whole row is 80 bits given by g_usiDataSize.
 *                              The number of times the block repeat itself
 *                              is found by g_usiDataSize/(4*0x05) which is 4.
 * 0xFF   -- Compress by the most frequently happen byte.
 *           Example:
 *           Original stream:   0x04020401030904040404
 *           Compressed stream: 0xFF04(0,1,0x02,0,1,0x01,1,0x03,1,0x09,0,0,0)
 *                          or: 0xFF044090181C240
 *           Detail:            0xFF is the code, 0x04 is the key.
 *                              a bit of 0 represent the key shall be put into
 *                              the current bit position and a bit of 1
 *                              represent copying the next of 8 bits of data
 *                              in.
 *
 */

void ispVMData(unsigned char *ByteData)
{
	/* 09/11/07 NN added local variables initialization */
	unsigned short size               = 0;
	unsigned short i, j, m, getData   = 0;
	unsigned char cDataByte           = 0;
	unsigned char compress            = 0;
	unsigned short FFcount            = 0;
	unsigned char compr_char          = 0xFF;
	unsigned short index              = 0;
	signed char compression           = 0;

	/*convert number in bits to bytes*/
	if (g_usiDataSize % 8 > 0) {
		/* 09/11/07 NN Type cast mismatch variables */
		size = (unsigned short)(g_usiDataSize / 8 + 1);
	} else {
		/* 09/11/07 NN Type cast mismatch variables */
		size = (unsigned short)(g_usiDataSize / 8);
	}

	/*
	 * If there is compression, then check if compress by key
	 * of 0x00 or 0xFF or by other keys or by nibble blocks
	 */

	if (g_usDataType & COMPRESS) {
		compression = 1;
		compress = GetByte();
		if ((compress  == VAR) && (g_usDataType & HEAP_IN)) {
			getData = 1;
			g_usDataType &= ~(HEAP_IN);
			compress = GetByte();
		}

		switch (compress) {
		case 0x00:
			/* No compression */
			compression = 0;
			break;
		case 0x01:
			/* Compress by byte 0x00 */
			compr_char = 0x00;
			break;
		case 0x02:
			/* Compress by byte 0xFF */
			compr_char = 0xFF;
			break;
		case 0xFF:
			/* Huffman encoding */
			compr_char = GetByte();
			i = 8;
			for (index = 0; index < size; index++) {
				ByteData[index] = 0x00;
				if (i > 7) {
					cDataByte = GetByte();
					i = 0;
				}
				if ((cDataByte << i++) & 0x80)
					m = 8;
				else {
					ByteData[index] = compr_char;
					m = 0;
				}

				for (j = 0; j < m; j++) {
					if (i > 7) {
						cDataByte = GetByte();
						i = 0;
					}
					ByteData[index] |=
					((cDataByte << i++) & 0x80) >> j;
				}
			}
			size = 0;
			break;
		default:
			for (index = 0; index < size; index++)
				ByteData[index] = 0x00;
			for (index = 0; index < compress; index++) {
				if (index % 2 == 0)
					cDataByte = GetByte();
				for (i = 0; i < size * 2 / compress; i++) {
					j = (unsigned short)(index +
						(i * (unsigned short)compress));
					/*clear the nibble to zero first*/
					if (j%2) {
						if (index % 2)
							ByteData[j/2] |=
								cDataByte & 0xF;
						else
							ByteData[j/2] |=
								cDataByte >> 4;
					} else {
						if (index % 2)
							ByteData[j/2] |=
								cDataByte << 4;
						else
							ByteData[j/2] |=
							cDataByte & 0xF0;
					}
				}
			}
			size = 0;
			break;
		}
	}

	FFcount = 0;

	/* Decompress by byte 0x00 or 0xFF */
	for (index = 0; index < size; index++) {
		if (FFcount <= 0) {
			cDataByte = GetByte();
			if ((cDataByte == VAR) && (g_usDataType&HEAP_IN) &&
				!getData && !(g_usDataType&COMPRESS)) {
				getData = 1;
				g_usDataType &= ~(HEAP_IN);
				cDataByte = GetByte();
			}
			ByteData[index] = cDataByte;
			if ((compression) && (cDataByte == compr_char))
				/* 09/11/07 NN Type cast mismatch variables */
				FFcount = (unsigned short) ispVMDataSize();
				/*The number of 0xFF or 0x00 bytes*/
		} else {
			FFcount--; /*Use up the 0xFF chain first*/
			ByteData[index] = compr_char;
		}
	}

	if (getData) {
		g_usDataType |= HEAP_IN;
		getData = 0;
	}
}

/*
 *
 * ispVMShift
 *
 * Processes the SDR/XSDR/SIR commands.
 *
 */

signed char ispVMShift(signed char a_cCode)
{
	/* 09/11/07 NN added local variables initialization */
	unsigned short iDataIndex  = 0;
	unsigned short iReadLoop   = 0;
	signed char cRetCode       = 0;

	cRetCode = 0;
	/* 09/11/07 NN Type cast mismatch variables */
	g_usiDataSize = (unsigned short) ispVMDataSize();

	/*clear the flags first*/
	g_usDataType &= ~(SIR_DATA + EXPRESS + SDR_DATA);
	switch (a_cCode) {
	case SIR:
		g_usDataType |= SIR_DATA;
		/*
		 * 1/15/04 If performing cascading, then go directly to SHIFTIR.
		 *  Else, go to IRPAUSE before going to SHIFTIR
		 */
		if (g_usFlowControl & CASCADE) {
			ispVMStateMachine(SHIFTIR);
		} else {
			ispVMStateMachine(IRPAUSE);
			ispVMStateMachine(SHIFTIR);
			if (g_usHeadIR > 0) {
				ispVMBypass(HIR, g_usHeadIR);
				sclock();
			}
		}
		break;
	case XSDR:
		g_usDataType |= EXPRESS; /*mark simultaneous in and out*/
	case SDR:
		g_usDataType |= SDR_DATA;
		/*
		 * 1/15/04 If already in SHIFTDR, then do not move state or
		 * shift in header.  This would imply that the previously
		 * shifted frame was a cascaded frame.
		 */
		if (g_cCurrentJTAGState != SHIFTDR) {
			/*
			 * 1/15/04 If performing cascading, then go directly
			 * to SHIFTDR.  Else, go to DRPAUSE before going
			 * to SHIFTDR
			 */
			if (g_usFlowControl & CASCADE) {
				if (g_cCurrentJTAGState == DRPAUSE) {
					ispVMStateMachine(SHIFTDR);
					/*
					 * 1/15/04 If cascade flag has been seat
					 * and the current state is DRPAUSE,
					 * this implies that the first cascaded
					 * frame is about to be shifted in.  The
					 * header must be shifted prior to
					 * shifting the first cascaded frame.
					 */
					if (g_usHeadDR > 0) {
						ispVMBypass(HDR, g_usHeadDR);
						sclock();
					}
				} else {
					ispVMStateMachine(SHIFTDR);
				}
			} else {
				ispVMStateMachine(DRPAUSE);
				ispVMStateMachine(SHIFTDR);
				if (g_usHeadDR > 0) {
					ispVMBypass(HDR, g_usHeadDR);
					sclock();
				}
			}
		}
		break;
	default:
		return VME_INVALID_FILE;
	}

	cRetCode = ispVMDataCode();

	if (cRetCode != 0) {
		return VME_INVALID_FILE;
	}

#ifdef DEBUG
	printf("%d ", g_usiDataSize);

	if (g_usDataType & TDI_DATA) {
		puts("TDI ");
		PrintData(g_usiDataSize, g_pucInData);
	}

	if (g_usDataType & TDO_DATA) {
		puts("\n\t\tTDO ");
		PrintData(g_usiDataSize, g_pucOutData);
	}

	if (g_usDataType & MASK_DATA) {
		puts("\n\t\tMASK ");
		PrintData(g_usiDataSize, g_pucOutMaskData);
	}

	if (g_usDataType & DMASK_DATA) {
		puts("\n\t\tDMASK ");
		PrintData(g_usiDataSize, g_pucOutDMaskData);
	}

	puts(";\n");
#endif /* DEBUG */

	if (g_usDataType & TDO_DATA || g_usDataType & DMASK_DATA) {
		if (g_usDataType & DMASK_DATA) {
			cRetCode = ispVMReadandSave(g_usiDataSize);
			if (!cRetCode) {
				if (g_usTailDR > 0) {
					sclock();
					ispVMBypass(TDR, g_usTailDR);
				}
				ispVMStateMachine(DRPAUSE);
				ispVMStateMachine(SHIFTDR);
				if (g_usHeadDR > 0) {
					ispVMBypass(HDR, g_usHeadDR);
					sclock();
				}
				for (iDataIndex = 0;
					iDataIndex < g_usiDataSize / 8 + 1;
					iDataIndex++)
					g_pucInData[iDataIndex] =
						g_pucOutData[iDataIndex];
				g_usDataType &= ~(TDO_DATA + DMASK_DATA);
				cRetCode = ispVMSend(g_usiDataSize);
			}
		} else {
			cRetCode = ispVMRead(g_usiDataSize);
			if (cRetCode == -1 && g_cVendor == XILINX) {
				for (iReadLoop = 0; iReadLoop < 30;
					iReadLoop++) {
					cRetCode = ispVMRead(g_usiDataSize);
					if (!cRetCode) {
						break;
					} else {
						/* Always DRPAUSE */
						ispVMStateMachine(DRPAUSE);
						/*
						 * Bypass other devices
						 * when appropriate
						 */
						ispVMBypass(TDR, g_usTailDR);
						ispVMStateMachine(g_ucEndDR);
						ispVMStateMachine(IDLE);
						ispVMDelay(1000);
					}
				}
			}
		}
	} else { /*TDI only*/
		cRetCode = ispVMSend(g_usiDataSize);
	}

	/*transfer the input data to the output buffer for the next verify*/
	if ((g_usDataType & EXPRESS) || (a_cCode == SDR)) {
		if (g_pucOutData) {
			for (iDataIndex = 0; iDataIndex < g_usiDataSize / 8 + 1;
				iDataIndex++)
				g_pucOutData[iDataIndex] =
					g_pucInData[iDataIndex];
		}
	}

	switch (a_cCode) {
	case SIR:
		/* 1/15/04 If not performing cascading, then shift ENDIR */
		if (!(g_usFlowControl & CASCADE)) {
			if (g_usTailIR > 0) {
				sclock();
				ispVMBypass(TIR, g_usTailIR);
			}
			ispVMStateMachine(g_ucEndIR);
		}
		break;
	case XSDR:
	case SDR:
		/* 1/15/04 If not performing cascading, then shift ENDDR */
		if (!(g_usFlowControl & CASCADE)) {
			if (g_usTailDR > 0) {
				sclock();
				ispVMBypass(TDR, g_usTailDR);
			}
			ispVMStateMachine(g_ucEndDR);
		}
		break;
	default:
		break;
	}

	return cRetCode;
}

/*
 *
 * ispVMAmble
 *
 * This routine is to extract Header and Trailer parameter for SIR and
 * SDR operations.
 *
 * The Header and Trailer parameter are the pre-amble and post-amble bit
 * stream need to be shifted into TDI or out of TDO of the devices. Mostly
 * is for the purpose of bypassing the leading or trailing devices. ispVM
 * supports only shifting data into TDI to bypass the devices.
 *
 * For a single device, the header and trailer parameters are all set to 0
 * as default by ispVM. If it is for multiple devices, the header and trailer
 * value will change as specified by the VME file.
 *
 */

signed char ispVMAmble(signed char Code)
{
	signed char compress = 0;
	/* 09/11/07 NN Type cast mismatch variables */
	g_usiDataSize = (unsigned short)ispVMDataSize();

#ifdef DEBUG
	printf("%d", g_usiDataSize);
#endif /* DEBUG */

	if (g_usiDataSize) {

		/*
		 * Discard the TDI byte and set the compression bit in the data
		 * type register to false if compression is set because TDI data
		 * after HIR/HDR/TIR/TDR is not compressed.
		 */

		GetByte();
		if (g_usDataType & COMPRESS) {
			g_usDataType &= ~(COMPRESS);
			compress = 1;
		}
	}

	switch (Code) {
	case HIR:

		/*
		 * Store the maximum size of the HIR buffer.
		 * Used to convert VME to HEX.
		 */

		if (g_usiDataSize > g_usHIRSize) {
			g_usHIRSize = g_usiDataSize;
		}

		/*
		 * Assign the HIR value and allocate memory.
		 */

		g_usHeadIR = g_usiDataSize;
		if (g_usHeadIR) {
			ispVMMemManager(HIR, g_usHeadIR);
			ispVMData(g_pucHIRData);

#ifdef DEBUG
			puts(" TDI ");
			PrintData(g_usHeadIR, g_pucHIRData);
#endif /* DEBUG */
		}
		break;
	case TIR:

		/*
		 * Store the maximum size of the TIR buffer.
		 * Used to convert VME to HEX.
		 */

		if (g_usiDataSize > g_usTIRSize) {
			g_usTIRSize = g_usiDataSize;
		}

		/*
		 * Assign the TIR value and allocate memory.
		 */

		g_usTailIR = g_usiDataSize;
		if (g_usTailIR) {
			ispVMMemManager(TIR, g_usTailIR);
			ispVMData(g_pucTIRData);

#ifdef DEBUG
			puts(" TDI ");
			PrintData(g_usTailIR, g_pucTIRData);
#endif /* DEBUG */
		}
		break;
	case HDR:

		/*
		 * Store the maximum size of the HDR buffer.
		 * Used to convert VME to HEX.
		 */

		if (g_usiDataSize > g_usHDRSize) {
			g_usHDRSize = g_usiDataSize;
		}

		/*
		 * Assign the HDR value and allocate memory.
		 *
		 */

		g_usHeadDR = g_usiDataSize;
		if (g_usHeadDR) {
			ispVMMemManager(HDR, g_usHeadDR);
			ispVMData(g_pucHDRData);

#ifdef DEBUG
			puts(" TDI ");
			PrintData(g_usHeadDR, g_pucHDRData);
#endif /* DEBUG */
		}
		break;
	case TDR:

		/*
		 * Store the maximum size of the TDR buffer.
		 * Used to convert VME to HEX.
		 */

		if (g_usiDataSize > g_usTDRSize) {
			g_usTDRSize = g_usiDataSize;
		}

		/*
		 * Assign the TDR value and allocate memory.
		 *
		 */

		g_usTailDR = g_usiDataSize;
		if (g_usTailDR) {
			ispVMMemManager(TDR, g_usTailDR);
			ispVMData(g_pucTDRData);

#ifdef DEBUG
			puts(" TDI ");
			PrintData(g_usTailDR, g_pucTDRData);
#endif /* DEBUG */
		}
		break;
	default:
		break;
	}

	/*
	*
	* Re-enable compression if it was previously set.
	*
	**/

	if (compress) {
		g_usDataType |= COMPRESS;
	}

	if (g_usiDataSize) {
		Code = GetByte();
		if (Code == CONTINUE) {
			return 0;
		} else {

			/*
			 * Encountered invalid opcode.
			 */

			return VME_INVALID_FILE;
		}
	}

	return 0;
}

/*
 *
 * ispVMLoop
 *
 * Perform the function call upon by the REPEAT opcode.
 * Memory is to be allocated to store the entire loop from REPEAT to ENDLOOP.
 * After the loop is stored then execution begin. The REPEATLOOP flag is set
 * on the g_usFlowControl register to indicate the repeat loop is in session
 * and therefore fetch opcode from the memory instead of from the file.
 *
 */

signed char ispVMLoop(unsigned short a_usLoopCount)
{
	/* 09/11/07 NN added local variables initialization */
	signed char cRetCode      = 0;
	unsigned short iHeapIndex = 0;
	unsigned short iLoopIndex = 0;

	g_usShiftValue = 0;
	for (iHeapIndex = 0; iHeapIndex < g_iHEAPSize; iHeapIndex++) {
		g_pucHeapMemory[iHeapIndex] = GetByte();
	}

	if (g_pucHeapMemory[iHeapIndex - 1] != ENDLOOP) {
		return VME_INVALID_FILE;
	}

	g_usFlowControl |= REPEATLOOP;
	g_usDataType |= HEAP_IN;

	for (iLoopIndex = 0; iLoopIndex < a_usLoopCount; iLoopIndex++) {
		g_iHeapCounter = 0;
		cRetCode = ispVMCode();
		g_usRepeatLoops++;
		if (cRetCode < 0) {
			break;
		}
	}

	g_usDataType &= ~(HEAP_IN);
	g_usFlowControl &= ~(REPEATLOOP);
	return cRetCode;
}

/*
 *
 * ispVMBitShift
 *
 * Shift the TDI stream left or right by the number of bits. The data in
 * *g_pucInData is of the VME format, so the actual shifting is the reverse of
 * IEEE 1532 or SVF format.
 *
 */

signed char ispVMBitShift(signed char mode, unsigned short bits)
{
	/* 09/11/07 NN added local variables initialization */
	unsigned short i       = 0;
	unsigned short size    = 0;
	unsigned short tmpbits = 0;

	if (g_usiDataSize % 8 > 0) {
		/* 09/11/07 NN Type cast mismatch variables */
		size = (unsigned short)(g_usiDataSize / 8 + 1);
	} else {
		/* 09/11/07 NN Type cast mismatch variables */
		size = (unsigned short)(g_usiDataSize / 8);
	}

	switch (mode) {
	case SHR:
		for (i = 0; i < size; i++) {
			if (g_pucInData[i] != 0) {
				tmpbits = bits;
				while (tmpbits > 0) {
					g_pucInData[i] <<= 1;
					if (g_pucInData[i] == 0) {
						i--;
						g_pucInData[i] = 1;
					}
					tmpbits--;
				}
			}
		}
		break;
	case SHL:
		for (i = 0; i < size; i++) {
			if (g_pucInData[i] != 0) {
				tmpbits = bits;
				while (tmpbits > 0) {
					g_pucInData[i] >>= 1;
					if (g_pucInData[i] == 0) {
						i--;
						g_pucInData[i] = 8;
					}
					tmpbits--;
				}
			}
		}
		break;
	default:
		return VME_INVALID_FILE;
	}

	return 0;
}

/*
 *
 * ispVMComment
 *
 * Displays the SVF comments.
 *
 */

void ispVMComment(unsigned short a_usCommentSize)
{
	char cCurByte = 0;
	for (; a_usCommentSize > 0; a_usCommentSize--) {
		/*
		*
		* Print character to the terminal.
		*
		**/
		cCurByte = GetByte();
		vme_out_char(cCurByte);
	}
	cCurByte = '\n';
	vme_out_char(cCurByte);
}

/*
 *
 * ispVMHeader
 *
 * Iterate the length of the header and discard it.
 *
 */

void ispVMHeader(unsigned short a_usHeaderSize)
{
	for (; a_usHeaderSize > 0; a_usHeaderSize--) {
		GetByte();
	}
}

/*
 *
 * ispVMCalculateCRC32
 *
 * Calculate the 32-bit CRC.
 *
 */

void ispVMCalculateCRC32(unsigned char a_ucData)
{
	/* 09/11/07 NN added local variables initialization */
	unsigned char ucIndex          = 0;
	unsigned char ucFlipData       = 0;
	unsigned short usCRCTableEntry = 0;
	unsigned int crc_table[16] = {
		0x0000, 0xCC01, 0xD801,
		0x1400, 0xF001, 0x3C00,
		0x2800, 0xE401, 0xA001,
		0x6C00, 0x7800, 0xB401,
		0x5000, 0x9C01, 0x8801,
		0x4400
	};

	for (ucIndex = 0; ucIndex < 8; ucIndex++) {
		ucFlipData <<= 1;
		if (a_ucData & 0x01) {
			ucFlipData |= 0x01;
		}
		a_ucData >>= 1;
	}

	/* 09/11/07 NN Type cast mismatch variables */
	usCRCTableEntry = (unsigned short)(crc_table[g_usCalculatedCRC & 0xF]);
	g_usCalculatedCRC = (unsigned short)((g_usCalculatedCRC >> 4) & 0x0FFF);
	g_usCalculatedCRC = (unsigned short)(g_usCalculatedCRC ^
			usCRCTableEntry ^ crc_table[ucFlipData & 0xF]);
	usCRCTableEntry = (unsigned short)(crc_table[g_usCalculatedCRC & 0xF]);
	g_usCalculatedCRC = (unsigned short)((g_usCalculatedCRC >> 4) & 0x0FFF);
	g_usCalculatedCRC = (unsigned short)(g_usCalculatedCRC ^
		usCRCTableEntry ^ crc_table[(ucFlipData >> 4) & 0xF]);
}

/*
 *
 * ispVMLCOUNT
 *
 * Process the intelligent programming loops.
 *
 */

signed char ispVMLCOUNT(unsigned short a_usCountSize)
{
	unsigned short usContinue	  = 1;
	unsigned short usIntelBufferIndex = 0;
	unsigned short usCountIndex       = 0;
	signed char cRetCode              = 0;
	signed char cRepeatHeap           = 0;
	signed char cOpcode               = 0;
	unsigned char ucState             = 0;
	unsigned short usDelay            = 0;
	unsigned short usToggle           = 0;

	g_usIntelBufferSize = (unsigned short)ispVMDataSize();

	/*
	 * Allocate memory for intel buffer.
	 *
	 */

	ispVMMemManager(LHEAP, g_usIntelBufferSize);

	/*
	 * Store the maximum size of the intelligent buffer.
	 * Used to convert VME to HEX.
	 */

	if (g_usIntelBufferSize > g_usLCOUNTSize) {
		g_usLCOUNTSize = g_usIntelBufferSize;
	}

	/*
	 * Copy intel data to the buffer.
	 */

	for (usIntelBufferIndex = 0; usIntelBufferIndex < g_usIntelBufferSize;
		usIntelBufferIndex++) {
		g_pucIntelBuffer[usIntelBufferIndex] = GetByte();
	}

	/*
	 * Set the data type register to get data from the intelligent
	 * data buffer.
	 */

	g_usDataType |= LHEAP_IN;

	/*
	*
	* If the HEAP_IN flag is set, temporarily unset the flag so data will be
	* retrieved from the status buffer.
	*
	**/

	if (g_usDataType & HEAP_IN) {
		g_usDataType &= ~HEAP_IN;
		cRepeatHeap = 1;
	}

#ifdef DEBUG
	printf("LCOUNT %d;\n", a_usCountSize);
#endif /* DEBUG */

	/*
	 * Iterate through the intelligent programming command.
	*/

	for (usCountIndex = 0; usCountIndex < a_usCountSize; usCountIndex++) {

		/*
		*
		* Initialize the intel data index to 0 before each iteration.
		*
		**/

		g_usIntelDataIndex = 0;
		cOpcode            = 0;
		ucState            = 0;
		usDelay            = 0;
		usToggle           = 0;
		usContinue		   = 1;

		/*
		*
		* Begin looping through all the VME opcodes.
		*
		*/
		/*
		* 4/1/09 Nguyen replaced the recursive function call codes on
		*        the ispVMLCOUNT function
		*
		*/
		while (usContinue) {
			cOpcode = GetByte();
			switch (cOpcode) {
			case HIR:
			case TIR:
			case HDR:
			case TDR:
				/*
				 * Set the header/trailer of the device in order
				 * to bypass successfully.
				 */

				ispVMAmble(cOpcode);
			break;
			case STATE:

				/*
				 * Step the JTAG state machine.
				 */

				ucState = GetByte();
				/*
				 * Step the JTAG state machine to DRCAPTURE
				 * to support Looping.
				 */

				if ((g_usDataType & LHEAP_IN) &&
					 (ucState == DRPAUSE) &&
					 (g_cCurrentJTAGState == ucState)) {
					ispVMStateMachine(DRCAPTURE);
				}
				ispVMStateMachine(ucState);
#ifdef DEBUG
				printf("LDELAY %s ", GetState(ucState));
#endif /* DEBUG */
				break;
			case SIR:
#ifdef DEBUG
				printf("SIR ");
#endif /* DEBUG */
				/*
				 * Shift in data into the device.
				 */

				cRetCode = ispVMShift(cOpcode);
				break;
			case SDR:

#ifdef DEBUG
				printf("LSDR ");
#endif /* DEBUG */
				/*
				 * Shift in data into the device.
				 */

				cRetCode = ispVMShift(cOpcode);
				break;
			case WAIT:

				/*
				*
				* Observe delay.
				*
				*/

				usDelay = (unsigned short)ispVMDataSize();
				ispVMDelay(usDelay);

#ifdef DEBUG
				if (usDelay & 0x8000) {

					/*
					 * Since MSB is set, the delay time must
					 * be decoded to millisecond. The
					 * SVF2VME encodes the MSB to represent
					 * millisecond.
					 */

					usDelay &= ~0x8000;
					printf("%.2E SEC;\n",
						(float) usDelay / 1000);
				} else {
					/*
					 * Since MSB is not set, the delay time
					 * is given as microseconds.
					 */

					printf("%.2E SEC;\n",
						(float) usDelay / 1000000);
				}
#endif /* DEBUG */
				break;
			case TCK:

				/*
				 * Issue clock toggles.
				 */

				usToggle = (unsigned short)ispVMDataSize();
				ispVMClocks(usToggle);

#ifdef DEBUG
				printf("RUNTEST %d TCK;\n", usToggle);
#endif /* DEBUG */
				break;
			case ENDLOOP:

				/*
				 * Exit point from processing loops.
				 */
				usContinue = 0;
				break;

			case COMMENT:

				/*
				 * Display comment.
				 */

				ispVMComment((unsigned short) ispVMDataSize());
				break;
			case ispEN:
				ucState = GetByte();
				if ((ucState == ON) || (ucState == 0x01))
					writePort(g_ucPinENABLE, 0x01);
				else
					writePort(g_ucPinENABLE, 0x00);
				ispVMDelay(1);
				break;
			case TRST:
				if (GetByte() == 0x01)
					writePort(g_ucPinTRST, 0x01);
				else
					writePort(g_ucPinTRST, 0x00);
				ispVMDelay(1);
				break;
			default:

				/*
				 * Invalid opcode encountered.
				 */

				debug("\nINVALID OPCODE: 0x%.2X\n", cOpcode);

				return VME_INVALID_FILE;
			}
		}
		if (cRetCode >= 0) {
			/*
			 * Break if intelligent programming is successful.
			 */

			break;
		}

	}
	/*
	 * If HEAP_IN flag was temporarily disabled,
	 * re-enable it before exiting
	 */

	if (cRepeatHeap) {
		g_usDataType |= HEAP_IN;
	}

	/*
	 * Set the data type register to not get data from the
	 * intelligent data buffer.
	 */

	g_usDataType &= ~LHEAP_IN;
	return cRetCode;
}
/*
 *
 * ispVMClocks
 *
 * Applies the specified number of pulses to TCK.
 *
 */

void ispVMClocks(unsigned short Clocks)
{
	unsigned short iClockIndex = 0;
	for (iClockIndex = 0; iClockIndex < Clocks; iClockIndex++) {
		sclock();
	}
}

/*
 *
 * ispVMBypass
 *
 * This procedure takes care of the HIR, HDR, TIR, TDR for the
 * purpose of putting the other devices into Bypass mode. The
 * current state is checked to find out if it is at DRPAUSE or
 * IRPAUSE. If it is at DRPAUSE, perform bypass register scan.
 * If it is at IRPAUSE, scan into instruction registers the bypass
 * instruction.
 *
 */

void ispVMBypass(signed char ScanType, unsigned short Bits)
{
	/* 09/11/07 NN added local variables initialization */
	unsigned short iIndex       = 0;
	unsigned short iSourceIndex = 0;
	unsigned char cBitState     = 0;
	unsigned char cCurByte      = 0;
	unsigned char *pcSource    = NULL;

	if (Bits <= 0) {
		return;
	}

	switch (ScanType) {
	case HIR:
		pcSource = g_pucHIRData;
		break;
	case TIR:
		pcSource = g_pucTIRData;
		break;
	case HDR:
		pcSource = g_pucHDRData;
		break;
	case TDR:
		pcSource = g_pucTDRData;
		break;
	default:
		break;
	}

	iSourceIndex = 0;
	cBitState = 0;
	for (iIndex = 0; iIndex < Bits - 1; iIndex++) {
		/* Scan instruction or bypass register */
		if (iIndex % 8 == 0) {
			cCurByte = pcSource[iSourceIndex++];
		}
		cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
			? 0x01 : 0x00);
		writePort(g_ucPinTDI, cBitState);
		sclock();
	}

	if (iIndex % 8 == 0)  {
		cCurByte = pcSource[iSourceIndex++];
	}

	cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
		? 0x01 : 0x00);
	writePort(g_ucPinTDI, cBitState);
}

/*
 *
 * ispVMStateMachine
 *
 * This procedure steps all devices in the daisy chain from a given
 * JTAG state to the next desirable state. If the next state is TLR,
 * the JTAG state machine is brute forced into TLR by driving TMS
 * high and pulse TCK 6 times.
 *
 */

void ispVMStateMachine(signed char cNextJTAGState)
{
	/* 09/11/07 NN added local variables initialization */
	signed char cPathIndex  = 0;
	signed char cStateIndex = 0;

	if ((g_cCurrentJTAGState == cNextJTAGState) &&
		(cNextJTAGState != RESET)) {
		return;
	}

	for (cStateIndex = 0; cStateIndex < 25; cStateIndex++) {
		if ((g_cCurrentJTAGState ==
			 g_JTAGTransistions[cStateIndex].CurState) &&
			(cNextJTAGState ==
				 g_JTAGTransistions[cStateIndex].NextState)) {
			break;
		}
	}

	g_cCurrentJTAGState = cNextJTAGState;
	for (cPathIndex = 0;
		cPathIndex < g_JTAGTransistions[cStateIndex].Pulses;
		cPathIndex++) {
		if ((g_JTAGTransistions[cStateIndex].Pattern << cPathIndex)
			& 0x80) {
			writePort(g_ucPinTMS, (unsigned char) 0x01);
		} else {
			writePort(g_ucPinTMS, (unsigned char) 0x00);
		}
		sclock();
	}

	writePort(g_ucPinTDI, 0x00);
	writePort(g_ucPinTMS, 0x00);
}

/*
 *
 * ispVMStart
 *
 * Enable the port to the device and set the state to RESET (TLR).
 *
 */

void ispVMStart()
{
#ifdef DEBUG
	printf("// ISPVM EMBEDDED ADDED\n");
	printf("STATE RESET;\n");
#endif
	g_usFlowControl	= 0;
	g_usDataType = g_uiChecksumIndex = g_cCurrentJTAGState = 0;
	g_usHeadDR = g_usHeadIR = g_usTailDR = g_usTailIR = 0;
	g_usMaxSize = g_usShiftValue = g_usRepeatLoops = 0;
	g_usTDOSize =  g_usMASKSize = g_usTDISize = 0;
	g_usDMASKSize = g_usLCOUNTSize = g_usHDRSize = 0;
	g_usTDRSize = g_usHIRSize = g_usTIRSize =  g_usHeapSize	= 0;
	g_pLVDSList = NULL;
	g_usLVDSPairCount = 0;
	previous_size = 0;

	ispVMStateMachine(RESET);    /*step devices to RESET state*/
}

/*
 *
 * ispVMEnd
 *
 * Set the state of devices to RESET to enable the devices and disable
 * the port.
 *
 */

void ispVMEnd()
{
#ifdef DEBUG
	printf("// ISPVM EMBEDDED ADDED\n");
	printf("STATE RESET;\n");
	printf("RUNTEST 1.00E-001 SEC;\n");
#endif

	ispVMStateMachine(RESET);   /*step devices to RESET state */
	ispVMDelay(1000);              /*wake up devices*/
}

/*
 *
 * ispVMSend
 *
 * Send the TDI data stream to devices. The data stream can be
 * instructions or data.
 *
 */

signed char ispVMSend(unsigned short a_usiDataSize)
{
	/* 09/11/07 NN added local variables initialization */
	unsigned short iIndex       = 0;
	unsigned short iInDataIndex = 0;
	unsigned char cCurByte      = 0;
	unsigned char cBitState     = 0;

	for (iIndex = 0; iIndex < a_usiDataSize - 1; iIndex++) {
		if (iIndex % 8 == 0) {
			cCurByte = g_pucInData[iInDataIndex++];
		}
		cBitState = (unsigned char)(((cCurByte << iIndex % 8) & 0x80)
			? 0x01 : 0x00);
		writePort(g_ucPinTDI, cBitState);
		sclock();
	}

	if (iIndex % 8 == 0) {
		/* Take care of the last bit */
		cCurByte = g_pucInData[iInDataIndex];
	}

	cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
		? 0x01 : 0x00);

	writePort(g_ucPinTDI, cBitState);
	if (g_usFlowControl & CASCADE) {
		/*1/15/04 Clock in last bit for the first n-1 cascaded frames */
		sclock();
	}

	return 0;
}

/*
 *
 * ispVMRead
 *
 * Read the data stream from devices and verify.
 *
 */

signed char ispVMRead(unsigned short a_usiDataSize)
{
	/* 09/11/07 NN added local variables initialization */
	unsigned short usDataSizeIndex    = 0;
	unsigned short usErrorCount       = 0;
	unsigned short usLastBitIndex     = 0;
	unsigned char cDataByte           = 0;
	unsigned char cMaskByte           = 0;
	unsigned char cInDataByte         = 0;
	unsigned char cCurBit             = 0;
	unsigned char cByteIndex          = 0;
	unsigned short usBufferIndex      = 0;
	unsigned char ucDisplayByte       = 0x00;
	unsigned char ucDisplayFlag       = 0x01;
	char StrChecksum[256]            = {0};
	unsigned char g_usCalculateChecksum = 0x00;

	/* 09/11/07 NN Type cast mismatch variables */
	usLastBitIndex = (unsigned short)(a_usiDataSize - 1);

#ifndef DEBUG
	/*
	 * If mask is not all zeros, then set the display flag to 0x00,
	 * otherwise it shall be set to 0x01 to indicate that data read
	 * from the device shall be displayed. If DEBUG is defined,
	 * always display data.
	 */

	for (usDataSizeIndex = 0; usDataSizeIndex < (a_usiDataSize + 7) / 8;
		usDataSizeIndex++) {
		if (g_usDataType & MASK_DATA) {
			if (g_pucOutMaskData[usDataSizeIndex] != 0x00) {
				ucDisplayFlag = 0x00;
				break;
			}
		} else if (g_usDataType & CMASK_DATA) {
			g_usCalculateChecksum = 0x01;
			ucDisplayFlag = 0x00;
			break;
		} else {
			ucDisplayFlag = 0x00;
			break;
		}
	}
#endif /* DEBUG */

	/*
	*
	* Begin shifting data in and out of the device.
	*
	**/

	for (usDataSizeIndex = 0; usDataSizeIndex < a_usiDataSize;
		usDataSizeIndex++) {
		if (cByteIndex == 0) {

			/*
			 * Grab byte from TDO buffer.
			 */

			if (g_usDataType & TDO_DATA) {
				cDataByte = g_pucOutData[usBufferIndex];
			}

			/*
			 * Grab byte from MASK buffer.
			 */

			if (g_usDataType & MASK_DATA) {
				cMaskByte = g_pucOutMaskData[usBufferIndex];
			} else {
				cMaskByte = 0xFF;
			}

			/*
			 * Grab byte from CMASK buffer.
			 */

			if (g_usDataType & CMASK_DATA) {
				cMaskByte = 0x00;
				g_usCalculateChecksum = 0x01;
			}

			/*
			 * Grab byte from TDI buffer.
			 */

			if (g_usDataType & TDI_DATA) {
				cInDataByte = g_pucInData[usBufferIndex];
			}

			usBufferIndex++;
		}

		cCurBit = readPort();

		if (ucDisplayFlag) {
			ucDisplayByte <<= 1;
			ucDisplayByte |= cCurBit;
		}

		/*
		 * Check if data read from port matches with expected TDO.
		 */

		if (g_usDataType & TDO_DATA) {
			/* 08/28/08 NN Added Calculate checksum support. */
			if (g_usCalculateChecksum) {
				if (cCurBit == 0x01)
					g_usChecksum +=
						(1 << (g_uiChecksumIndex % 8));
				g_uiChecksumIndex++;
			} else {
				if ((((cMaskByte << cByteIndex) & 0x80)
					? 0x01 : 0x00)) {
					if (cCurBit != (unsigned char)
					(((cDataByte << cByteIndex) & 0x80)
						? 0x01 : 0x00)) {
						usErrorCount++;
					}
				}
			}
		}

		/*
		 * Write TDI data to the port.
		 */

		writePort(g_ucPinTDI,
			(unsigned char)(((cInDataByte << cByteIndex) & 0x80)
				? 0x01 : 0x00));

		if (usDataSizeIndex < usLastBitIndex) {

			/*
			 * Clock data out from the data shift register.
			 */

			sclock();
		} else if (g_usFlowControl & CASCADE) {

			/*
			 * Clock in last bit for the first N - 1 cascaded frames
			 */

			sclock();
		}

		/*
		 * Increment the byte index. If it exceeds 7, then reset it back
		 * to zero.
		 */

		cByteIndex++;
		if (cByteIndex >= 8) {
			if (ucDisplayFlag) {

			/*
			 * Store displayed data in the TDO buffer. By reusing
			 * the TDO buffer to store displayed data, there is no
			 * need to allocate a buffer simply to hold display
			 * data. This will not cause any false verification
			 * errors because the true TDO byte has already
			 * been consumed.
			 */

				g_pucOutData[usBufferIndex - 1] = ucDisplayByte;
				ucDisplayByte = 0;
			}

			cByteIndex = 0;
		}
		/* 09/12/07 Nguyen changed to display the 1 bit expected data */
		else if (a_usiDataSize == 1) {
			if (ucDisplayFlag) {

				/*
				 * Store displayed data in the TDO buffer.
				 * By reusing the TDO buffer to store displayed
				 * data, there is no need to allocate
				 * a buffer simply to hold display data. This
				 * will not cause any false verification errors
				 * because the true TDO byte has already
				 * been consumed.
				 */

				/*
				 * Flip ucDisplayByte and store it in cDataByte.
				 */
				cDataByte = 0x00;
				for (usBufferIndex = 0; usBufferIndex < 8;
					usBufferIndex++) {
					cDataByte <<= 1;
					if (ucDisplayByte & 0x01) {
						cDataByte |= 0x01;
					}
					ucDisplayByte >>= 1;
				}
				g_pucOutData[0] = cDataByte;
				ucDisplayByte = 0;
			}

			cByteIndex = 0;
		}
	}

	if (ucDisplayFlag) {

#ifdef DEBUG
		debug("RECEIVED TDO (");
#else
		vme_out_string("Display Data: 0x");
#endif /* DEBUG */

		/* 09/11/07 NN Type cast mismatch variables */
		for (usDataSizeIndex = (unsigned short)
				((a_usiDataSize + 7) / 8);
			usDataSizeIndex > 0 ; usDataSizeIndex--) {
			cMaskByte = g_pucOutData[usDataSizeIndex - 1];
			cDataByte = 0x00;

			/*
			 * Flip cMaskByte and store it in cDataByte.
			 */

			for (usBufferIndex = 0; usBufferIndex < 8;
				usBufferIndex++) {
				cDataByte <<= 1;
				if (cMaskByte & 0x01) {
					cDataByte |= 0x01;
				}
				cMaskByte >>= 1;
			}
#ifdef DEBUG
			printf("%.2X", cDataByte);
			if ((((a_usiDataSize + 7) / 8) - usDataSizeIndex)
				% 40 == 39) {
				printf("\n\t\t");
			}
#else
			vme_out_hex(cDataByte);
#endif /* DEBUG */
		}

#ifdef DEBUG
		printf(")\n\n");
#else
		vme_out_string("\n\n");
#endif /* DEBUG */
		/* 09/02/08 Nguyen changed to display the data Checksum */
		if (g_usChecksum != 0) {
			g_usChecksum &= 0xFFFF;
			sprintf(StrChecksum, "Data Checksum: %.4lX\n\n",
				g_usChecksum);
			vme_out_string(StrChecksum);
			g_usChecksum = 0;
		}
	}

	if (usErrorCount > 0) {
		if (g_usFlowControl & VERIFYUES) {
			vme_out_string(
				"USERCODE verification failed.   "
				"Continue programming......\n\n");
			g_usFlowControl &= ~(VERIFYUES);
			return 0;
		} else {

#ifdef DEBUG
			printf("TOTAL ERRORS: %d\n", usErrorCount);
#endif /* DEBUG */

			return VME_VERIFICATION_FAILURE;
		}
	} else {
		if (g_usFlowControl & VERIFYUES) {
			vme_out_string("USERCODE verification passed.    "
				"Programming aborted.\n\n");
			g_usFlowControl &= ~(VERIFYUES);
			return 1;
		} else {
			return 0;
		}
	}
}

/*
 *
 * ispVMReadandSave
 *
 * Support dynamic I/O.
 *
 */

signed char ispVMReadandSave(unsigned short int a_usiDataSize)
{
	/* 09/11/07 NN added local variables initialization */
	unsigned short int usDataSizeIndex = 0;
	unsigned short int usLastBitIndex  = 0;
	unsigned short int usBufferIndex   = 0;
	unsigned short int usOutBitIndex   = 0;
	unsigned short int usLVDSIndex     = 0;
	unsigned char cDataByte            = 0;
	unsigned char cDMASKByte           = 0;
	unsigned char cInDataByte          = 0;
	unsigned char cCurBit              = 0;
	unsigned char cByteIndex           = 0;
	signed char cLVDSByteIndex         = 0;

	/* 09/11/07 NN Type cast mismatch variables */
	usLastBitIndex = (unsigned short) (a_usiDataSize - 1);

	/*
	*
	* Iterate through the data bits.
	*
	*/

	for (usDataSizeIndex = 0; usDataSizeIndex < a_usiDataSize;
		usDataSizeIndex++) {
		if (cByteIndex == 0) {

			/*
			 * Grab byte from DMASK buffer.
			 */

			if (g_usDataType & DMASK_DATA) {
				cDMASKByte = g_pucOutDMaskData[usBufferIndex];
			} else {
				cDMASKByte = 0x00;
			}

			/*
			 * Grab byte from TDI buffer.
			 */

			if (g_usDataType & TDI_DATA) {
				cInDataByte = g_pucInData[usBufferIndex];
			}

			usBufferIndex++;
		}

		cCurBit = readPort();
		cDataByte = (unsigned char)(((cInDataByte << cByteIndex) & 0x80)
			? 0x01 : 0x00);

		/*
		 * Initialize the byte to be zero.
		 */

		if (usOutBitIndex % 8 == 0) {
			g_pucOutData[usOutBitIndex / 8] = 0x00;
		}

		/*
		 * Use TDI, DMASK, and device TDO to create new TDI (actually
		 * stored in g_pucOutData).
		 */

		if ((((cDMASKByte << cByteIndex) & 0x80) ? 0x01 : 0x00)) {

			if (g_pLVDSList) {
				for (usLVDSIndex = 0;
					 usLVDSIndex < g_usLVDSPairCount;
					usLVDSIndex++) {
					if (g_pLVDSList[usLVDSIndex].
						usNegativeIndex ==
						usDataSizeIndex) {
						g_pLVDSList[usLVDSIndex].
							ucUpdate = 0x01;
						break;
					}
				}
			}

			/*
			 * DMASK bit is 1, use TDI.
			 */

			g_pucOutData[usOutBitIndex / 8] |= (unsigned char)
				(((cDataByte & 0x1) ? 0x01 : 0x00) <<
				(7 - usOutBitIndex % 8));
		} else {

			/*
			 * DMASK bit is 0, use device TDO.
			 */

			g_pucOutData[usOutBitIndex / 8] |= (unsigned char)
				(((cCurBit & 0x1) ? 0x01 : 0x00) <<
				(7 - usOutBitIndex % 8));
		}

		/*
		 * Shift in TDI in order to get TDO out.
		 */

		usOutBitIndex++;
		writePort(g_ucPinTDI, cDataByte);
		if (usDataSizeIndex < usLastBitIndex) {
			sclock();
		}

		/*
		 * Increment the byte index. If it exceeds 7, then reset it back
		 * to zero.
		 */

		cByteIndex++;
		if (cByteIndex >= 8) {
			cByteIndex = 0;
		}
	}

	/*
	 * If g_pLVDSList exists and pairs need updating, then update
	 * the negative-pair to receive the flipped positive-pair value.
	 */

	if (g_pLVDSList) {
		for (usLVDSIndex = 0; usLVDSIndex < g_usLVDSPairCount;
			usLVDSIndex++) {
			if (g_pLVDSList[usLVDSIndex].ucUpdate) {

				/*
				 * Read the positive value and flip it.
				 */

				cDataByte = (unsigned char)
				 (((g_pucOutData[g_pLVDSList[usLVDSIndex].
					usPositiveIndex / 8]
					<< (g_pLVDSList[usLVDSIndex].
					usPositiveIndex % 8)) & 0x80) ?
					0x01 : 0x00);
				/* 09/11/07 NN Type cast mismatch variables */
				cDataByte = (unsigned char) (!cDataByte);

				/*
				 * Get the byte that needs modification.
				 */

				cInDataByte =
				g_pucOutData[g_pLVDSList[usLVDSIndex].
					usNegativeIndex / 8];

				if (cDataByte) {

					/*
					 * Copy over the current byte and
					 * set the negative bit to 1.
					 */

					cDataByte = 0x00;
					for (cLVDSByteIndex = 7;
						cLVDSByteIndex >= 0;
						cLVDSByteIndex--) {
						cDataByte <<= 1;
						if (7 -
						(g_pLVDSList[usLVDSIndex].
							usNegativeIndex % 8) ==
							cLVDSByteIndex) {

							/*
							 * Set negative bit to 1
							 */

							cDataByte |= 0x01;
						} else if (cInDataByte & 0x80) {
							cDataByte |= 0x01;
						}

						cInDataByte <<= 1;
					}

					/*
					 * Store the modified byte.
					 */

					g_pucOutData[g_pLVDSList[usLVDSIndex].
					usNegativeIndex / 8] = cDataByte;
				} else {

					/*
					 * Copy over the current byte and set
					 * the negative bit to 0.
					 */

					cDataByte = 0x00;
					for (cLVDSByteIndex = 7;
						cLVDSByteIndex >= 0;
						cLVDSByteIndex--) {
						cDataByte <<= 1;
						if (7 -
						(g_pLVDSList[usLVDSIndex].
						usNegativeIndex % 8) ==
						cLVDSByteIndex) {

							/*
							 * Set negative bit to 0
							 */

							cDataByte |= 0x00;
						} else if (cInDataByte & 0x80) {
							cDataByte |= 0x01;
						}

						cInDataByte <<= 1;
					}

					/*
					 * Store the modified byte.
					 */

					g_pucOutData[g_pLVDSList[usLVDSIndex].
					usNegativeIndex / 8] = cDataByte;
				}

				break;
			}
		}
	}

	return 0;
}

signed char ispVMProcessLVDS(unsigned short a_usLVDSCount)
{
	unsigned short usLVDSIndex = 0;

	/*
	 * Allocate memory to hold LVDS pairs.
	 */

	ispVMMemManager(LVDS, a_usLVDSCount);
	g_usLVDSPairCount = a_usLVDSCount;

#ifdef DEBUG
	printf("LVDS %d (", a_usLVDSCount);
#endif /* DEBUG */

	/*
	 * Iterate through each given LVDS pair.
	 */

	for (usLVDSIndex = 0; usLVDSIndex < g_usLVDSPairCount; usLVDSIndex++) {

		/*
		 * Assign the positive and negative indices of the LVDS pair.
		 */

		/* 09/11/07 NN Type cast mismatch variables */
		g_pLVDSList[usLVDSIndex].usPositiveIndex =
			(unsigned short) ispVMDataSize();
		/* 09/11/07 NN Type cast mismatch variables */
		g_pLVDSList[usLVDSIndex].usNegativeIndex =
			(unsigned short)ispVMDataSize();

#ifdef DEBUG
		if (usLVDSIndex < g_usLVDSPairCount - 1) {
			printf("%d:%d, ",
				g_pLVDSList[usLVDSIndex].usPositiveIndex,
				g_pLVDSList[usLVDSIndex].usNegativeIndex);
		} else {
			printf("%d:%d",
				g_pLVDSList[usLVDSIndex].usPositiveIndex,
				g_pLVDSList[usLVDSIndex].usNegativeIndex);
		}
#endif /* DEBUG */

	}

#ifdef DEBUG
	printf(");\n");
#endif /* DEBUG */

	return 0;
}