randtest.c 125 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 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777

/*************************************************************************
 *
 *  File: randtest.c
 *
 *  This file contains the main routine for the I/O random test.
 *
 *  $Header: /root/leakn64/depot/rf/sw/bbplayer/iosim/src/randtest.c,v 1.5 2002/11/23 00:22:55 whs Exp $
 *
 */

#include <sys/types.h>		
#ifdef __sgi__
	#include <sys/cachectl.h>
	#include <sys/sysmp.h>
#endif
#include <sys/mman.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <fcntl.h>
#include <getopt.h>
#ifdef __sgi__
	#include <libelf.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>

#include <sys/time.h>		/* Needed for select */
#include <math.h>		/* Needed for random */
#include <sys/prctl.h>		/* Needed for sproc */
#include <signal.h>		/* Needed for signal */
#ifdef __sgi__
	#include <task.h>
#endif

#include "trace.h"
#include "sim.h"
#include "simipc.h"
#include "iomap.h"
#include "iotest.h"
#include "sync.h"
#include "verify.h"
#include "bcptest.h"

/***************************************************************************
 * Definitions
 */
#define	DEFAULT_LOOP_COUNT	500
#define	DEFAULT_RUN_COUNT	1	/* Minimum run count for each thread */
#define	DEFAULT_TEST_SEED	1
#define	DEFAULT_FREQ_SEED	1

/*
 * DP List ring buffer size (in number of 32-bit words)
 */
#define	DPL_RING_SIZE	124	/* 124 32-bit words == 62 64-bit longs */

#define FROM_RDRAM	0
#define FROM_DMEM1	1
#define FROM_DMEM2	2
#define MAX_RINGBUFFERS	3

/*
 * Default address ranges for testing
 *
			RDRAM
	      0 +---------------------+		04000000 +----------------+
		|                     |			 |     DMEM       |
		|                     |			 |                |
		|                     |			 |                |
		|                     |			 +----------------+
		|                     |		         |   128 Bytes    |
       01e7000  +---------------------+         04000fff +----------------+
		|      MI (10 KB)     |         04001000 |     IMEM       |
		+---------------------+                  |                |
		|      PI             |                  |                |
		+---------------------+                  +----------------+
		|      SI             |                  |   128 Bytes    |
		+---------------------+         04001fff +----------------+
		|      AI             |
		+---------------------+
		|      SP             |
		+---------------------+
		|                     |
       01fffff  +---------------------+
 */
#define	RDRAM_RAND_BASE		(RDRAM_0_END+1-(60*1024)) /* 100 KB from end */
#define RDRAM_TEST_SIZE		(3*1024)		   /* 3 KB each */

#define	RDRAM_VI_START		(RDRAM_RAND_BASE)
#define	RDRAM_VI_END		(RDRAM_VI_START+(30*1024)-1)

#define	RDRAM_MI_START		(RDRAM_VI_END+1)
#define	RDRAM_MI_END		(RDRAM_MI_START+RDRAM_TEST_SIZE-1)

#define	RDRAM_PI_START		(RDRAM_MI_END+1)
#define	RDRAM_PI_END		(RDRAM_PI_START+RDRAM_TEST_SIZE-1)

#define	RDRAM_SI_START		(RDRAM_PI_END+1)
#define	RDRAM_SI_END		(RDRAM_SI_START+RDRAM_TEST_SIZE-1)

#define	RDRAM_AI_START		(RDRAM_SI_END+1)
#define	RDRAM_AI_END		(RDRAM_AI_START+RDRAM_TEST_SIZE-1)

#define	RDRAM_SP_START		(RDRAM_AI_END+1)
#define	RDRAM_SP_END		(RDRAM_SP_START+RDRAM_TEST_SIZE-1)

#define	RDRAM_DP_START		((RDRAM_SP_END+1) + 0x3e0) /* +992 */
#define	RDRAM_DP_END		(RDRAM_DP_START+RDRAM_TEST_SIZE-1)

#define	DP_DMEM_TEST_SIZE	(DPL_RING_SIZE*4)  /* 62 64-bit words */
#define DMEM_TEST_SIZE		(128)		   /* 128 bytes */
#define IMEM_TEST_SIZE		(128)		   /* 128 bytes */

#define	DMEM_START		(SP_DMEM_END+1-128)
#define	DMEM_END		(DMEM_START+DMEM_TEST_SIZE-1)

#define	DP_DMEM_START	(SP_DMEM_END+1-(DMEM_TEST_SIZE+DP_DMEM_TEST_SIZE))
#define	DP_DMEM_END		(DP_DMEM_START+DP_DMEM_TEST_SIZE-1)
#define	DP2_DMEM_START		0x04000800	/* Evan's ring buffer */
#define	DP2_DMEM_END		0x040008FF

#define	IMEM_START		(SP_IMEM_END+1-128)
#define	IMEM_END		(IMEM_START+IMEM_TEST_SIZE-1)

#define	PI_ROM1_START		0x08000000	/* 4 MB each */
#define	PI_ROM1_END		0x08400000

#define	PI_ROM2_START		0x10000000	/* 4 MB each */
#define	PI_ROM2_END		0x10400000

#define	PI_WROM_START1		0x09000000	/* 2 MB each */
#define	PI_WROM_END1		0x09200000
#define	PI_WROM_START2		0x09200000	/* 2 MB each */
#define	PI_WROM_END2		0x09400000

#define	SI_ROM_START		0x1fc00000	/* 1984 bytes (4-byte status) */
#define	SI_ROM_END		0x1fc007bc

#define	SI_RAM_START		0x1fc007c0	/* 64 bytes */
#define	SI_RAM_END		0x1fc007ff

#define RDRAM_IMEM_TEST_ID	0x00137000

/*
 * IO/DMA states
 */
#define	IO_STATE_INIT 		1	/* One-time-only init steps */
#define	IO_STATE_START		2	/* Start IO transaction */
#define	IO_STATE_CHECK_STATUS	3	/* Check for IO (i.e. DMA) completion */
#define	IO_STATE_COMPARE	4	/* Compare data */

/*
 * Extra IO/DMA states for doing multi-frame video tests
 */

#define IO_STATE_CHECK_F0_LAN1  6
#define IO_STATE_CHECK_MID_LAN1 7
#define IO_STATE_CHECK_F1_LAN1  8
#define IO_STATE_CHECK_END_LAN1 9

#define IO_STATE_CHECK_F0_LAN2  10
#define IO_STATE_CHECK_MID_LAN2 11
#define IO_STATE_CHECK_F1_LAN2  12
#define IO_STATE_CHECK_END_LAN2 13

#define IO_STATE_CHECK_F0_LPN1  14
#define IO_STATE_CHECK_MID_LPN1 15
#define IO_STATE_CHECK_F1_LPN1  16
#define IO_STATE_CHECK_END_LPN1 17

#define IO_STATE_CHECK_F0_HAN1  18
#define IO_STATE_CHECK_MID_HAN1 19
#define IO_STATE_CHECK_F1_HAN1  20
#define IO_STATE_CHECK_END_HAN1 21

#define IO_STATE_CHECK_F0_HPF2  22
#define IO_STATE_CHECK_MID_HPF2 23
#define IO_STATE_CHECK_F1_HPF2  24
#define IO_STATE_CHECK_END_HPF2 25


/*
 * Messages
 */
 /* test id, description, expected val, actual val, passed/failed */
#define RTST_MSG_RES_STR "Test %3d (%15s: %08x %08x %08x %08x)\n"
#define RTST_MSG_RES2_STR "Test %3d (%15s: %08x %08x %08x %08x) -> %s\n"
#define RTST_ERROR_STR "\t****ERROR: src=%08x:%08x, dst=%08x:%08x\t\t -> %s\n"

#define THREAD_PRINT_MSG	\
    "T: id=%2d, pid=%4d, entry=0x%08x, R=%3d, B=%1d, S=%1d, I=%1d, name=%s\n"

#define	BUSY_STR	"Busy"
#define	DONE_STR	"Done"

/*
 * Macros
 */
#define INSERT_RESULT(ret)	(ret == -1 ? FAIL_STR : PASS_STR)

#define PRINT_RESULT(ret)	{				\
	if (ret == -1) fprintf(LogFp, "-> %s\n", FAIL_STR);	\
	else fprintf(LogFp, "-> %s\n", PASS_STR); }		\

#define RANDLOG_ERROR(src,w1,dst,w2) 				\
		fprintf(LogFp, RTST_ERROR_STR, src, w1, dst, w2, FAIL_STR)

/***************************************************************************
 * External variables
 */
extern unsigned long	Dflags;
extern FILE		*LogFp, *CmdFp;
extern char		*LogFile;


/***************************************************************************
 * Data structures
 */
typedef struct _ThreadCmd {
	int	pid;				/* Thread process id */
	void	(*entry)(struct _ThreadCmd *);	/* Thread entry point */
	int	runCount;			/* Total number of run
							(Parent) */
	int	busy;				/* Busy running (Child)  */
	int	state;				/* IO internal state */
	int	inProgress;			/* IO transaction is still
						   	in progress */
	char	*name;
} ThreadCmd;

typedef	void	(*SPROC_T)(void *);


/***************************************************************************
 * Global variables
 */
/* How many times we need to run */
unsigned int LoopCount	= DEFAULT_LOOP_COUNT;
unsigned int MinRunCount= DEFAULT_RUN_COUNT;

/* Random seed for selecting a test */
unsigned int TestSeed	= DEFAULT_TEST_SEED;

/* Random seed for frequency of run for a test */
unsigned int FreqSeed	= DEFAULT_FREQ_SEED;

/* Size of memory reserved for each IO subsystem for testing */
unsigned int RdramTestSize	= RDRAM_TEST_SIZE;

/* Define RDRAM address range for MI testing */
unsigned int MiRdramStart	= RDRAM_MI_START;
unsigned int MiRdramEnd		= RDRAM_MI_END;

/* Define RDRAM address range for PI testing */
unsigned int PiRdramStart	= RDRAM_PI_START;
unsigned int PiRdramEnd		= RDRAM_PI_END;
unsigned int PiRomStart[2]	= {{PI_ROM1_START}, {PI_ROM2_START}};
unsigned int PiRomEnd[2]	= {{PI_ROM1_END}, {PI_ROM2_END}};
unsigned int PiWRomStart1	= PI_WROM_START1;
unsigned int PiWRomEnd1		= PI_WROM_END1;
unsigned int PiWRomStart2	= PI_WROM_START2;
unsigned int PiWRomEnd2		= PI_WROM_END2;

/* Define RDRAM address range for SI testing */
unsigned int SiRdramStart	= RDRAM_SI_START;
unsigned int SiRdramEnd		= RDRAM_SI_END;
unsigned int SiRomStart		= SI_ROM_START;
unsigned int SiRomEnd		= SI_ROM_END;
unsigned int SiRamStart		= SI_RAM_START;
unsigned int SiRamEnd		= SI_RAM_END;

/* Define RDRAM address range for AI testing */
unsigned int AiRdramStart	= RDRAM_AI_START;
unsigned int AiRdramEnd		= RDRAM_AI_END;

/* Define RDRAM address range for SP testing */
unsigned int SpRdramStart	= RDRAM_SP_START;
unsigned int SpRdramEnd		= RDRAM_SP_END;
unsigned int SpImemStart	= IMEM_START;
unsigned int SpImemEnd		= IMEM_END;
unsigned int SpDmemStart	= DMEM_START;
unsigned int SpDmemEnd		= DMEM_END;

/* Define RDRAM address range for DP testing */
unsigned int DpRdramStart	= RDRAM_DP_START;
unsigned int DpRdramEnd		= RDRAM_DP_END;
unsigned int DpDmemStart	= DP_DMEM_START;
unsigned int DpDmemEnd		= DP_DMEM_END;
unsigned int Dp2DmemStart	= DP2_DMEM_START;
unsigned int Dp2DmemEnd		= DP2_DMEM_END;

/* Define RDRAM address range for VI testing */
unsigned int ViRdramStart	= RDRAM_VI_START;
unsigned int ViRdramEnd		= RDRAM_VI_END;

/* To turn on 2nd RDRAM */
unsigned int Rdram1Enabled	= 1;

/* RDRAM size: 4 MB, 6MB, or 8MB 
 *	1) 4 MB = 2 * 2MB
 *	2) 6 MB = 2MB + (2 * 1MB) + 2MB
 *	3) 8 MB = 4 * 2MB
 */
unsigned int RdramSize		= 4;	/* Use 4MB for default */

/* To kill the iorand environment - can be set in any test */
unsigned int QuickDeath		= 0;

/* To enable DMA compare and disable round-robin scheduing */
unsigned int CompareEnabled	= 1;
unsigned int RoundRobinEnabled	= 0;

/* Flag to tell if IMEM dma80 test is enabled or not */
unsigned int dma80Enabled	= 1;

/* Flag to tell if AI is running at 44.1 KHz */
unsigned int Ai441Enabled	= 0;

/* Only for standalone */
#ifdef STANDALONE
int	errorTotal = 0;
int	errorCount = 0;
#endif

/* Round robin randomizing array */
int *ProcessOrder;

/***************************************************************************
 * Static declarations
 */
unsigned int spLocDmaActive	= 0;
unsigned int dpcRandDmaActive	= 0;

/* Display list parameters */
unsigned int dplStartAddr	= 0;
unsigned int dplEndAddr		= 0;
unsigned int dplCurrAddr	= 0;

/* Display list ring buffer parameters */
unsigned int dplRingSize	= 0;
unsigned int dplRingSource	= 0;
unsigned int dplRingStartp	= 0;
unsigned int dplRingEndp	= 0;


/***************************************************************************
 * Extern declarations
 */


/***************************************************************************
 * Forward declarations
 */
static void setupRingBuffer(void);

void	KillAllThreads(void);
int	CheckInterrupt(unsigned int, unsigned int *);
void	FlushVerifyInfo(void);
void	PrintActiveTable(void);
int	MemSmplCompare(int , int , int , int );
void	rangeExpand(char *);
void    initRoundRobin(void);

ThreadCmd * findActiveThread(void *entry);

void	MiRandIoRd(ThreadCmd *);
void	MiRandIoWrRd(ThreadCmd *);

void	PiRandIoRd(ThreadCmd *);
void	PiRandIoWrRd(ThreadCmd *);
void	PiRandDma(ThreadCmd *);

void	SiRandIoRd(ThreadCmd *);
void	SiRandIoWrRd(ThreadCmd *);
void	SiRandDma(ThreadCmd *);

void	AiRandDma(ThreadCmd *);

void	ViRandDma(ThreadCmd *);
void	ViMLF2Dma(ThreadCmd *);
void    ViBigDma(ThreadCmd *);
void    ViLan2Dma(ThreadCmd *);

void	SpRandIoRd(ThreadCmd *);
void	SpRandIoWrRd(ThreadCmd *);
void	SpRandDma(ThreadCmd *);
void	SpLocDma(ThreadCmd *);

void	DpcRandDma(ThreadCmd *);

/* New test procedures for vi, added by kluster */

void    ViMLN1Dma(ThreadCmd *);
void    ViMLN2Dma(ThreadCmd *);
void    ViMLP1Dma(ThreadCmd *);
void    ViMHN1Dma(ThreadCmd *);
void    ViMHF2Dma(ThreadCmd *);

void    ViDTH2Dma(ThreadCmd *);
   
void	SpDummyDma(ThreadCmd *);

/*
 * IDs for different test threads
 */
#define MI_RAND_BASE		0
#define MI_IOR_ID		(MI_RAND_BASE+0)
#define MI_IOWR_ID		(MI_RAND_BASE+1)

#define PI_RAND_BASE		(MI_IOWR_ID+1)
#define PI_IOR_ID		(PI_RAND_BASE+0)
#define PI_IOWR_ID		(PI_RAND_BASE+1)
#define PI_DMA_ID		(PI_RAND_BASE+2)

#define SI_RAND_BASE		(PI_DMA_ID+1)
#define SI_IOR_ID		(SI_RAND_BASE+0)
#define SI_IOWR_ID		(SI_RAND_BASE+1)
#define SI_DMA_ID		(SI_RAND_BASE+2)

#define AI_RAND_BASE		(SI_DMA_ID+1)
#define AI_DMA_ID		(AI_RAND_BASE+0)

#define VI_RAND_BASE		(AI_DMA_ID+1)
#define VI_DMA_ID		(VI_RAND_BASE+0)

#define SP_RAND_BASE		(VI_DMA_ID+1)
#define SP_IOR_ID		(SP_RAND_BASE+0)
#define SP_IOWR_ID		(SP_RAND_BASE+1)
#define SP_DMA_ID		(SP_RAND_BASE+2)
#define SP_LOC_ID		(SP_RAND_BASE+3)

#define DPC_RAND_BASE		(SP_LOC_ID+1)
#define DPC_DMA_ID		(DPC_RAND_BASE+0)

/***************************************************************************
 * Test Thread Table
 */
ThreadCmd RandThreadTable[] = {
 /*
  *   ID	PID	Entry		R  B  S  I	EntryName
  * ====================================================================
  */
  { /*  0 */   	 0,	MiRandIoRd,	0, 0, 1, 0,	"MiRandIoRd"	},
  { /*  1 */     0,	MiRandIoWrRd,	0, 0, 1, 0,	"MiRandIoWrRd"},

  { /*  2 */     0,	PiRandIoRd,	0, 0, 1, 0,	"PiRandIoRd"	},
  { /*  3 */     0,	PiRandIoWrRd,	0, 0, 1, 0,	"PiRandIoWrRd"},
  { /*  4 */     0,	PiRandDma,	0, 0, 1, 0,	"PiRandDma"	},

  { /*  5 */     0,	SiRandIoRd,	0, 0, 1, 0,	"SiRandIoRd"	},
  { /*  6 */     0,	SiRandIoWrRd,	0, 0, 1, 0,	"SiRandIoWrRd"},
  { /*  7 */     0,	SiRandDma,	0, 0, 1, 0,	"SiRandDma"	},

  { /*  8 */     0,	AiRandDma,	0, 0, 1, 0,	"AiRandDma"	},

  { /*  9 */     0,	ViMLF2Dma,	0, 0, 1, 0,	"ViMLF2Dma"	},

  { /* 10 */     0,	SpRandIoRd,	0, 0, 1, 0,	"SpRandIoRd"	},
  { /* 11 */     0,	SpRandIoWrRd,	0, 0, 1, 0,	"SpRandIoWrRd"},
  { /* 12 */     0,	SpRandDma,	0, 0, 1, 0,	"SpRandDma"	},
  { /* 13 */     0,	SpLocDma,	0, 0, 1, 0,	"SpLocDma"	},

  { /* 14 */     0,	DpcRandDma,	0, 0, 1, 0,	"DpcRandDma"	},

  /* New tests for vi testing */

  { /* 15 */     0,	SpDummyDma,	0, 0, 1, 0,	"SpDummyDma"	},
  { /* 16 */     0,     ViMLN1Dma,      0, 0, 1, 0,     "ViMLN1Dma"     },
  { /* 17 */     0,     ViMLN2Dma,      0, 0, 1, 0,     "ViMLN2Dma"     },
  { /* 18 */     0,     ViMLP1Dma,      0, 0, 1, 0,     "ViMLP1Dma"     },
  { /* 19 */     0,     ViMHN1Dma,      0, 0, 1, 0,     "ViMHN1Dma"     },
  { /* 20 */     0,     ViMHF2Dma,      0, 0, 1, 0,     "ViMHF2Dma"     },

  /* Dither vi test */
  { /* 21 */     0,     ViDTH2Dma,      0, 0, 1, 0,     "ViDTH2Dma"     },

};

int RandTableSize = sizeof(RandThreadTable)/sizeof(RandThreadTable[0]);

ThreadCmd **ActiveThreadTable;

int ActiveTableSize;


/***************************************************************************
 * Main random IO test loop
 *
 * The idea is to have a table containing of various test routines that can
 * be spawned off as a thread (via sproc). The parent scheduler randomly 
 * selects (based on UNIX random() function) which test thread to run. 
 * Each thread contains a main loop where it processes the IO transaction and
 * then blocks itself, waiting to be unblocked by the parent scheduler upon the
 * next invocation. The thread exits only when it receives a SIGINT signal from
 * the parent; this occurs at the end of the all testing. Parent and child 
 * communicate over global data structures. Once started, the child thread
 * sets a busy flag, telling the parent to not invoke this thread again. And 
 * once the child finishes its task, it resets the busy flag and blocks itself. 
 * Based on the random number generated, the parent will most likely select this 
 * thread again, especially when the busy flag is cleared. 
 * 
 * In general, there 3 types of test thread: one to perform only IO read-compare
 * from a memory address range that has been initialized with pattern data, 
 * one to perform IO write-read-compare, and one to perform DMA transaction 
 * between an IO subsytem and RDRAM. The DMA thread needs to have its own 
 * internal state machine to minimize the time it spends hogging CPU and
 * RCP. For example, in state 1, the thread will start a DMA transaction, 
 * records the transaction and returns. In its next invocation, it will enter 
 * state 2 where it checks for DMA completion and returns. The thread will
 * stay in state 2 (after numerous invocation) until the DMA busy bit is off.
 * Once the busy bit is off, the next invocation will put the thread into
 * its last state 3 to compare the data for correctness. Once again, the thread
 * need to distribute its time spent in this state over numerous invocations.
 *
 * Ideally, there should be 2 random seeds: one for selecting a test to run, and
 * the other for choosing the number of times (or frequency) that this test
 * should run. For simplicity, we'll simply use 1 seed to generate random 
 * numbers which only the low 8 bits are used.
 */
int
IoRandTest(char *testRange) 
{

	int 		i, j;
	int 		status, done;
	int 		randomNeeded, stillRunning;
	unsigned int	count;
	unsigned int	testRand  = 0;
	ThreadCmd	*thread;
	struct timeval 	timeout;


	_TRACE(DLOG, printf("IoRandTest: entered\n"));

	rangeExpand(testRange);

	_TRACE(DLOG, printf("RandTableSize = %d\n", RandTableSize));
	_TRACE(DLOG, printf("ActiveTableSize = %d\n", ActiveTableSize));

	/*
	 * Initialize random seeds 
	 */
	srandom(TestSeed);

	initRoundRobin();
	
	randomNeeded = 1;
	done  = 0;
	count = 1;
	while (!done && !QuickDeath) {
		/* 
		 * First, get the random number to select which thread to run 
		 */
		if (randomNeeded) {
			if (RoundRobinEnabled)
				testRand = (testRand+1) % ActiveTableSize;
			else
				testRand = random() % ActiveTableSize;
		}
		else {
		/*
		 * Here, we have finished the testing but there are still
		 * transactions in progress (most likely DMAs). So, we need
		 * to keep activate the in-progress thread to run until 
		 * it's finished. We sequentially search the table for each
		 * unfinished thread and sticks with it.
		 */
			for (i=0; i < ActiveTableSize; i++) {
				if (ActiveThreadTable[i]->inProgress == 1) {
					testRand = i;
					break;
				}  /* if */
			}  /* for */
		}  /* if */

		thread = ActiveThreadTable[ProcessOrder[testRand]];

		_TRACE(DLOG, printf(
			"Count #%4d: thread id=%2d, name=%s\n", 
			count, testRand, thread->name));

		thread->entry(thread);
		thread->runCount++;

		_TRACE(DLOG, printf(THREAD_PRINT_MSG,
			thread - RandThreadTable,
			thread->pid, thread->entry,
			thread->runCount, thread->busy,
			thread->state, thread->inProgress, 
			thread->name));
		count++;

		/* 
		 * Check to see if we're done: 
		 *	1) LoopCount is reached
		 *	2) Every thread has been run at least MinRunCount
		 *	3) No outstanding DMA test
		 */
		if (count > LoopCount) {
			stillRunning = 0;
			for (i=0; i < ActiveTableSize; i++) {
				if (ActiveThreadTable[i]->runCount <
				    MinRunCount) 
					break;
				if (ActiveThreadTable[i]->inProgress == 1) 
					stillRunning = 1;
			}  /* for */
			if (i >= ActiveTableSize) {
				if (stillRunning)
					randomNeeded = 0;
				else 
					done = 1;
			}  /* if */
		}  /* if */

	}  /* while */

	PrintActiveTable();

	return(0);

}  /* IoRandTest */


/***************************************************************************
 * Thread entry routines
 */
void
MiRandIoRd(ThreadCmd *thread)
{
	unsigned int	data;
	int		ret;
	static unsigned int address = 0;

	/* Assume that RDRAM has been initialized with data = address */
	if ((address == 0) || 
		(address > (MiRdramStart+(RdramTestSize/2))))
		address = MiRdramStart - 4;
	address = (address + 4) & 0xfffffffc;
	ret = MemReadCompare(address, address, 0, 0);
	fprintf(LogFp, RTST_MSG_RES2_STR,
		thread - RandThreadTable, thread->name,
		address, address, 0, 0,
		INSERT_RESULT(ret));

}  /* MiRandIoRd */


void
MiRandIoWrRd(ThreadCmd *thread)
{
	int		ret = 0;
	static unsigned int address = 0;
	static unsigned int maxAddress = 0;

	/* If 2nd RDRAM is enabled, we use this thread to test it */
#if 0
	if (Rdram1Enabled) {
		if ((address == 0) || 
			(address > (RDRAM_1_START+0x80000+RdramTestSize)))
			address = RDRAM_1_START+0x80000 - 4;
	}
#endif
	if ((address == 0) || (address > MiRdramEnd))
		address = MiRdramStart+(RdramTestSize/2) - 4; 

	address = (address + 4) & 0xfffffffc;

	ret = MemWriteRead(address, address, address, 0);
	fprintf(LogFp, RTST_MSG_RES2_STR,
		thread - RandThreadTable, thread->name,
		address, address, 0, 0,
		INSERT_RESULT(ret));

}  /* MiRandIoWrRd */


/***************************************************************************
 * PI thread routines
 */
void
PiRandIoRd(ThreadCmd *thread)
{
	unsigned int		data, stat;
	static unsigned int	rom 	= 1;
	static unsigned int	address = 0; 

	/* 
 	 * For PI, we need to check for both DMA/IO busy bits before 
	 * perform IO read/write - If they're busy, we simply exit and 
	 * try again later
 	 */
	/* Assume that ROM 1 & 2 have been initialized with 
	 * data = address 
	 */
	if ((address == 0) || (address > (PiRomStart[rom]+100))) {
		rom = 1 - rom;
		address = PiRomStart[rom] - 4;
	}
	address = (address + 4) & 0xfffffffc;

#ifdef __sgi__	/* K1--BUG */
        stat = *(vu_int *)PHYS_TO_K1(PI_STATUS_REG);  
#else
        stat = (vu_int) (IO_READ(PI_STATUS_REG)); 
#endif
	if (!(stat & (PI_STATUS_DMA_BUSY | PI_STATUS_IO_BUSY))) {

		/*
		data = PiTestIoRead(address, address, 0, 0);
		*/
#ifdef __sgi__  /* K1--BUG */
		data = *(vu_int *)PHYS_TO_K1(address);
#else 
                data = (vu_int)(IO_READ(address)); 
#endif
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, data, 0, stat, DONE_STR);
	}
	else {
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, 0, 0, stat, BUSY_STR);
	}

}  /* PiRandIoRd */


void
PiRandIoWrRd(ThreadCmd *thread)
{
	int			ret;
	unsigned int		stat;
	static unsigned int	address = 0;

	/* 
	 * For PI, we need to check for both DMA/IO busy bits before 
	 * perform IO read/write - If they're busy, we simply exit and 
	 * try again later
	 */
	if ((address == 0) || (address > PiWRomEnd1))
		address = PiWRomStart1 - 4;
	address = (address + 4) & 0xfffffffc;
	
#ifdef __sgi__    /* K1--BUG */
	stat = *(vu_int *)PHYS_TO_K1(PI_STATUS_REG); 
#else
        stat = (vu_int)(IO_READ(PI_STATUS_REG)); 
#endif
	if (!(stat & (PI_STATUS_DMA_BUSY | PI_STATUS_IO_BUSY))) {

		ret = PiTestIoWrite(address, address, 0, 0);
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, address, 0, stat, INSERT_RESULT(ret));
	}
	else {
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, address, 0, stat, BUSY_STR); 
	}

}  /* PiRandIoWrRd */


void
PiRandDma(ThreadCmd *thread)
{
	int			ret;
	unsigned int		stat;
	static	unsigned int	dir     = DIR_FROM_RAM;
	static	unsigned int	piAddr  = 0;
	static	unsigned int	ramAddr = 0;
	static	unsigned int	nbytes	= 0; 
	static	unsigned int	lastStat= 0; 
	static unsigned int	rom 	= 1;

	switch (thread->state) {
		case IO_STATE_INIT:
		case IO_STATE_START: {
#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(PI_STATUS_REG);
#else 
			stat = (vu_int)(IO_READ(PI_STATUS_REG));
#endif 
			if (!(stat & 
				(PI_STATUS_DMA_BUSY | PI_STATUS_IO_BUSY))) {

			    thread->inProgress = 1;
			    /* Toggle direction */
			    dir = (dir==DIR_FROM_RAM) ? DIR_TO_RAM:DIR_FROM_RAM;

			    /* If DMA to RDRAM, then we use the 2 ROM models
	 		     */
			    if (dir == DIR_TO_RAM) {
				rom = 1 - rom;
				piAddr = PiRomStart[rom];
			    }
			    else {	/* we use the WROM model */
				piAddr = PiWRomStart2;
			    }

			    /* 
			     * We randomly pick nbytes to be between 
			     * 128 and 2044 (0x7fc) bytes 
			     */
			    nbytes = (128 + random()) & 0x7fc;

			    /* Set RDRAM address */
			    if ((ramAddr == 0) || 
			    	(ramAddr > (PiRdramEnd - (nbytes+4))))
				ramAddr = PiRdramStart - 128;
			    ramAddr += 128;

			    ret = PiTestDma(dir, piAddr, ramAddr, nbytes);
			    fprintf(LogFp, RTST_MSG_RES2_STR,
			    	thread - RandThreadTable, "PiTestDma",
			    	dir, piAddr, ramAddr, nbytes,
			    	INSERT_RESULT(ret));
			    thread->state = IO_STATE_CHECK_STATUS;
			} else {
			    fprintf(LogFp, RTST_MSG_RES2_STR,
			    	thread - RandThreadTable, "PiTestDma",
			    	dir, piAddr, ramAddr, stat, BUSY_STR); 
			}
			break;
		}
		case IO_STATE_CHECK_STATUS: {
#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(PI_STATUS_REG);
#else
			stat = (vu_int)(IO_READ(PI_STATUS_REG));
#endif
			if (stat != lastStat)
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "PiDmaStatus", 
					PI_STATUS_REG, PI_STATUS_DMA_BUSY, 
					1, stat, DONE_STR);
			lastStat = stat;
			if (!(stat & PI_STATUS_DMA_BUSY)) {
				if (CheckInterrupt(MI_INTR_PI, &stat)) {
#ifdef __sgi__  /* K1--BUG */
					*(vu_int *)PHYS_TO_K1(PI_STATUS_REG) =
						PI_CLR_INTR;
#else 
					IO_WRITE(PI_STATUS_REG, PI_CLR_INTR);
#endif
					fprintf(LogFp, RTST_MSG_RES2_STR,
						thread - RandThreadTable,
						"PiCheckIntr", 
						MI_INTR_REG, MI_INTR_PI, 
						0, stat, PASS_STR);
				}
				else {
					fprintf(LogFp, RTST_MSG_RES2_STR,
						thread - RandThreadTable,
						"PiCheckIntr", 
						MI_INTR_REG, MI_INTR_PI, 
						0, stat, FAIL_STR);
        				errorTotal++;
				}
				if (CompareEnabled)
					thread->state = IO_STATE_COMPARE;
				else {
					thread->state = IO_STATE_START;
					thread->inProgress = 0;
				}
			}
			break;
		}
		case IO_STATE_COMPARE: {
			/* Here we compare the 1st word, last word */
			ret = MemSmplCompare(piAddr, ramAddr, nbytes/4, 0);
			fprintf(LogFp, RTST_MSG_RES2_STR,
				thread - RandThreadTable,
				"PiDmaCompare",
				piAddr, 
				ramAddr, nbytes/4, 0, INSERT_RESULT(ret));
			thread->state = IO_STATE_START;
			thread->inProgress = 0;
			break;
		}
		default: {
                        _TRACE(DERROR,
                        fprintf(LogFp,
                                "\tERROR: Illegal IO state encountered!\n"));
		}
	}  /* switch */

}  /* PiRandDma */


/***************************************************************************
 * SI thread routines
 */
void
SiRandIoRd(ThreadCmd *thread)
{
	int			ret;
	unsigned int		data, stat;
	static unsigned int 	address = 0;

	/* Assume that ROM has been initialized with data = address */
	if ((address == 0) || (address > SiRomEnd)) {
		address = SiRomStart - 4;
	}
	address = (address + 4) & 0xfffffffc;
	data    = (address >> 2) & 0x0000ffff;
#ifdef __sgi__  /* K1--BUG */
	stat = *(vu_int *)PHYS_TO_K1(SI_STATUS_REG);
#else
	stat = (vu_int)(IO_READ(SI_STATUS_REG));
#endif 
	if (!(stat & (SI_STATUS_DMA_BUSY | SI_STATUS_RD_BUSY)) &&
		(RandThreadTable[SI_DMA_ID].inProgress == 0)) {
		ret = SiTestIoRead(address, data, 0, 0);
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, data, 0, stat, INSERT_RESULT(ret));
	}
	else {
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, data, 0, stat, BUSY_STR);
	}

}  /* SiRandIoRd */


void
SiRandIoWrRd(ThreadCmd *thread)
{
	int			ret;
	unsigned int		stat;
	static unsigned int	address = 0;

	/* 
	 * For SI, we need to check for both DMA/IO busy bits before 
	 * perform IO write - If they're busy, we simply exit and try 
	 * again later
	 */
	if ((address == 0) || (address > SiRamEnd))
		address = SiRamStart - 4;
	address = (address + 4) & 0xfffffffc;

#ifdef __sgi__  /* K1--BUG */
	stat = *(vu_int *)PHYS_TO_K1(SI_STATUS_REG);
#else
	stat = (vu_int)(IO_READ(SI_STATUS_REG));
#endif
		
	/*
	 * Need to check for busy bits AND no outstanding DMA
	 * in progress; otherwise, this routine will overwrite
	 * one 32-bit word in the 64 bytes SI RAM and cause DMA
	 * compare to fail.
	 */
	if (!(stat & (SI_STATUS_DMA_BUSY | SI_STATUS_RD_BUSY)) &&
		(RandThreadTable[SI_DMA_ID].inProgress == 0)) {
		ret = SiTestIoWrite(address, address, 0, 0);
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, address, 0, stat, INSERT_RESULT(ret));
	}
	else {
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, address, 0, stat, BUSY_STR);
	}

}  /* SiRandIoWrRd */


void
SiRandDma(ThreadCmd *thread)
{
	int			ret;
	unsigned int		stat;
	static	unsigned int	type    = SI_WR64B;
	static	unsigned int	siAddr  = 0;
	static	unsigned int	ramAddr = 0;
	static	unsigned int	nbytes	= 64;
	static	unsigned int	lastStat= 0;

	switch (thread->state) {
		case IO_STATE_INIT:
		case IO_STATE_START: {
			thread->inProgress = 1;
			type = (type==SI_RD64B) ? SI_WR64B : SI_RD64B;
			if ((ramAddr == 0) || 
			    (ramAddr > (SiRdramEnd - (nbytes+4))))
				ramAddr = SiRdramStart - 8;
			ramAddr += 8;
			siAddr = SiRamStart;
			siAddr  &= 0xfffffff8;
			ramAddr &= 0xfffffff8;

#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(SI_STATUS_REG);
#else
			stat = (vu_int)(IO_READ(SI_STATUS_REG));
#endif
			if (!(stat & 
				(SI_STATUS_DMA_BUSY | SI_STATUS_RD_BUSY))) {
				ret = SiTestDma(type, siAddr, ramAddr, 0);
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SiTestDma",
					type, siAddr, ramAddr, nbytes,
					INSERT_RESULT(ret));
				thread->state = IO_STATE_CHECK_STATUS;
			} else {
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SiTestDma",
					type, siAddr, ramAddr, stat, BUSY_STR);
			}
			break;
		}
		case IO_STATE_CHECK_STATUS: {
			/*
			 * Here, we need to combine 2 states into 1 to
			 * protect against an SI IO WR/RD test to come in
			 * between state 2 & 3 and clobber the data (since
			 * SI RAM only contains 64-bytes
			 */
#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(SI_STATUS_REG);
#else
			stat = (vu_int)(IO_READ(SI_STATUS_REG));
#endif 
			if (stat != lastStat)
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SiDmaStatus",
					SI_STATUS_REG, SI_STATUS_DMA_BUSY, 
					1, stat, DONE_STR);
			lastStat = stat;
			if (!(stat & SI_STATUS_DMA_BUSY)) {
				if (CheckInterrupt(MI_INTR_SI, &stat)) {
					/* Any write clears interrupt */

#ifdef __sgi__  /* K1--BUG */
					*(vu_int *)PHYS_TO_K1(SI_STATUS_REG)=0;
#else
					IO_WRITE(SI_STATUS_REG, 0);
#endif
					fprintf(LogFp, RTST_MSG_RES2_STR,
						thread - RandThreadTable,
						"SiCheckIntr",
						MI_INTR_REG, MI_INTR_SI,
						0, stat, PASS_STR);
				}
				else {
					fprintf(LogFp, RTST_MSG_RES2_STR,
						thread - RandThreadTable,
						"SiCheckIntr",
						MI_INTR_REG, MI_INTR_SI,
						0, stat, FAIL_STR);
					errorTotal++;
				}

				/* Here we compare the 1st word, last word */
				ret = MemSmplCompare(siAddr,ramAddr,nbytes/4,0);
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable,
					"SiDmaCompare", siAddr, 
					ramAddr, nbytes/4, 0,
					INSERT_RESULT(ret));
				thread->state = IO_STATE_START;
				thread->inProgress = 0;
			}
			break;
		}
		default: {
                        _TRACE(DERROR,
                        fprintf(LogFp,
                                "\tERROR: Illegal IO state encountered!\n"));
		}
	}  /* switch */

}  /* SiRandDma */


/***************************************************************************
 * AI thread routines
 */
void
AiRandDma(ThreadCmd *thread)
{
	int			ret;
	unsigned int		stat;
	static	unsigned int	ramAddr = 0;
	static	unsigned int	nbytes	= 400;
	static	unsigned int	dmaCount = 0;
	static	unsigned int	waitCount = 0;
	static	unsigned int	lastStat  = 0;

	switch (thread->state) {
		case IO_STATE_INIT:
		case IO_STATE_START: {
			thread->inProgress = 1;
			ramAddr = AiRdramStart;

			ret = AiTestDma(ramAddr, nbytes, 0, 0);
			dmaCount++;
			fprintf(LogFp, RTST_MSG_RES2_STR,
				thread - RandThreadTable, "AiTestDma",
				ramAddr, nbytes, 0, 0, INSERT_RESULT(ret));
#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(AI_STATUS_REG);
#else
			stat = (vu_int)(IO_READ(AI_STATUS_REG));
#endif
			/* If not full, we start another DMA */
			if (!(stat & AI_STATUS_FIFO_FULL) && 
				(dmaCount < MinRunCount)) {
				ret = AiTestDma(ramAddr, nbytes, 0, 0);
				dmaCount++;
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "AiTestDma",
					ramAddr, nbytes, 0, stat,
					INSERT_RESULT(ret));
			}

			thread->state = IO_STATE_CHECK_STATUS;
			break;
		}
		case IO_STATE_CHECK_STATUS: {
#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(AI_STATUS_REG);
#else
			stat = (vu_int)(IO_READ(AI_STATUS_REG));
#endif
			if (stat != lastStat)
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable,
					"AiDmaStatus", 
					AI_STATUS_REG, AI_STATUS_DMA_BUSY, 
					1, stat, DONE_STR);
			lastStat = stat;
			/* If not full, we start another DMA */
			if (!(stat & AI_STATUS_FIFO_FULL) &&
				(dmaCount < MinRunCount)) {
				ret = AiTestDma(ramAddr, nbytes, 0, 0);
				dmaCount++;
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable,
					"AiTestDma", ramAddr, 
					nbytes, 0, 0, INSERT_RESULT(ret));
			}
#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(AI_STATUS_REG);
#else
			stat = (vu_int)(IO_READ(AI_STATUS_REG));
#endif
			_TRACE(DSTATUS, 
				fprintf(LogFp, "\n\tStatus = 0x%08x\n",stat));
			if (!(stat & (AI_STATUS_DMA_BUSY|AI_STATUS_FIFO_FULL))){
				if (CheckInterrupt(MI_INTR_AI, &stat)) {
					/* Any write clears interrupt */

#ifdef __sgi__  /* K1--BUG */
					*(vu_int *)PHYS_TO_K1(AI_STATUS_REG)=0;
#else
					IO_WRITE(AI_STATUS_REG, 0);
#endif
					fprintf(LogFp, RTST_MSG_RES2_STR,
						thread - RandThreadTable,
						"AiCheckIntr",
						MI_INTR_REG, MI_INTR_AI,
						0, stat, PASS_STR);
				}
				else {
					fprintf(LogFp, RTST_MSG_RES2_STR,
						thread - RandThreadTable,
						"AiCheckIntr",
						MI_INTR_REG, MI_INTR_AI,
						0, stat, FAIL_STR);
					errorTotal++;
				}

				thread->state = IO_STATE_START;
				thread->inProgress = 0;
			}
			break;
		}
		default: {
                        _TRACE(DERROR,
                        fprintf(LogFp,
                                "\tERROR: Illegal IO state encountered!\n"));
		}
	}  /* switch */

}  /* AiRandDma */


/***************************************************************************
 * VI thread routines
 */
void
ViRandDma(ThreadCmd *thread)
{
	int			ret;
	unsigned int		stat, data;
	static	unsigned int	lastLine = 0;
	static	unsigned int	intrLine = 0;
	static	unsigned int	hWidth   = 0;
	static	unsigned int	ramAddr  = 0;
	static	unsigned int	dmaCount = 0;
	static	unsigned int	done     = 0;
	static	unsigned int	lastStat = 0;

	switch (thread->state) {
		case IO_STATE_INIT:
		case IO_STATE_START: {

			thread->inProgress = 1;

			/* We want RDRAM address to be near 2KB boundary */
			ramAddr = ViRdramStart + 0x400;  /* 0x001fd7e0 */

			lastLine = 0x48;
			intrLine = 0xa;		/* to get intr at line 10 */
			hWidth   = 0x20;	/* 32-32 bit picture */

			/* Assume that VI interrupt mask has been set */

			/* Write-read start RDRAM address */
			ret = MemWriteRead(VI_DRAM_ADDR_REG, ramAddr,
				ramAddr, 0);

			/* Set horizontal width (32x32) */
			ret = MemWriteRead(VI_H_WIDTH_REG, hWidth, hWidth, 0);

			/* Set vertical line interrupt */
			ret = MemWriteRead(VI_V_INTR_REG, intrLine, 
				intrLine, 0);

			/* Write-read timing/burst */
			ret = MemWriteRead(VI_TIMING_REG, 0x820404, 
				0x820404, 0);

			/* Write-read vertical sync */
			ret = MemWriteRead(VI_V_SYNC_REG, lastLine+1, 
				lastLine+1, 0);

			/* Write-read horizontal sync */
			ret = MemWriteRead(VI_H_SYNC_REG, 0x130, 0x130, 0);

			/* Write-read horizontal sync leap */
			ret = MemWriteRead(VI_H_SYNC_LEAP_REG, 0x01300130, 
				0x01300130, 0);

			/* Write-read horizontal start */
			ret = MemWriteRead(VI_H_VIDEO_REG, 0x200040, 
				0x200040, 0);

			/* Write-read vertical start */
			ret = MemWriteRead(VI_V_VIDEO_REG, 0x50045, 
				0x50045, 0);

			/* Write-read vertical burst */
			ret = MemWriteRead(VI_V_BURST_REG, 0x50045, 
				0x50045, 0);

			/* Write-read x-scale */
			ret = MemWriteRead(VI_X_SCALE_REG, 0x400, 
				0x400, 0);

			/* Write-read y-scale */
			ret = MemWriteRead(VI_Y_SCALE_REG, 0x400, 
				0x400, 0);

			/* Write-read controller/status */
			/*
			ret = MemWriteRead(VI_STATUS_REG, 0x303, 0x303, 0);
			*/
			ret = MemWriteRead(VI_STATUS_REG, 0x3003, 
				0x3003, 0);

			/* Write vertical current line */
			ret = MemWrite(VI_V_CURRENT_LINE_REG, 0x0, 0x0, 0);

			/* Read vertical current line */
			ret = MemRead(VI_V_CURRENT_LINE_REG, 0x0, 0x0, 0);

			dmaCount++;
			fprintf(LogFp, RTST_MSG_RES2_STR,
				thread - RandThreadTable,
				"ViTestDma", ramAddr, lastLine,
				intrLine, 0, INSERT_RESULT(ret));

			thread->state = IO_STATE_CHECK_STATUS;
			break;
		}
		case IO_STATE_CHECK_STATUS: {
			/* Check for VI interrupt: should occur at line 
			 * intrLine 
			 */
#ifdef __sgi__  /* K1--BUG */
			data = *(vu_int *)PHYS_TO_K1(MI_INTR_REG);
#else
			data = (vu_int)(IO_READ(MI_INTR_REG));
#endif 
			if (data & MI_INTR_VI) {
				/* Write any to vertical current line 
				 * to clear interrupt
				 */
#ifdef __sgi__  /* K1--BUG */
				*(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG) = 0;
#else
				IO_WRITE(VI_V_CURRENT_LINE_REG, 0);
#endif
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable,
					"ViDmaIntr",
					MI_INTR_REG, data, 0,
					0, DONE_STR);
			}

			/* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
			data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
			data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif
			if (data >= (lastLine & 0xffffffff)) {
#ifdef __sgi__  /* K1--BUG */
				data = *(vu_int *)PHYS_TO_K1(
						VI_V_CURRENT_LINE_REG);
#else
				data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif
				thread->state = IO_STATE_START;
				thread->inProgress = 0;
				done = 1;
			}
			fprintf(LogFp, RTST_MSG_RES2_STR,
				thread - RandThreadTable, "ViDmaCurrent",
				VI_V_CURRENT_LINE_REG, data, lastLine,
				0, DONE_STR);

			break;
		}

		default: {
                        _TRACE(DERROR,
                        fprintf(LogFp,
                                "\tERROR: Illegal IO state encountered!\n"));
		}
	}  /* switch */

}  /* ViRandDma */


#define IO_STATE_CHECK_F0_MLF2  26
#define IO_STATE_CHECK_MID_MLF2 27
#define IO_STATE_CHECK_F1_MLF2  28
#define IO_STATE_CHECK_END_MLF2 29


void ViMLF2Dma(ThreadCmd *thread)
{
    int			ret;
    unsigned int	stat, data;
    int                 dmaCount;
    static unsigned int	ramAddr  = 0;
    static int          done;

    fprintf(stdout, "In ViMLF2Dma procedure \n");

    switch (thread->state) {
	case IO_STATE_INIT:
	case IO_STATE_START: 
	{
	    fprintf(stdout, "In IO_STATE_START \n"); 

	    thread->inProgress = 1;
		    
	    /****************************************************************/
		    
	    /* We want RDRAM address to be near 2KB boundary */
	    /* Advance address by 128 bytes to skip the first field */
	    ramAddr = ViRdramStart + 128;  /* 0x001fd7e0 */

	    /* Write-read start RDRAM address */
	    /* 
	     ret = MemWriteRead(VI_DRAM_ADDR_REG,   0x001f6160, 0x001f6160, 0);
	    */
	    ret = MemWriteRead(VI_DRAM_ADDR_REG,   ramAddr, ramAddr, 0);

	    /* Set horizontal width (32x32) */
	    ret = MemWriteRead(VI_H_WIDTH_REG,     0x00000020, 0x00000020, 0);

	    /* Set vertical line interrupt */
	    ret = MemWriteRead(VI_V_INTR_REG,      0x000003ff, 0x000003ff, 0);

	    /* Write-read timing/burst */
	    ret = MemWriteRead(VI_TIMING_REG,      0x00820404, 0x00820404, 0);

	    /* Write-read vertical sync */
	    ret = MemWriteRead(VI_V_SYNC_REG,      0x0000002A, 0x0000002A, 0);

	    /* Write-read horizontal sync */
	    ret = MemWriteRead(VI_H_SYNC_REG,      0x00000110, 0x00000110, 0);

	    /* Write-read horizontal sync leap */
	    ret = MemWriteRead(VI_H_SYNC_LEAP_REG, 0x01100110, 0x01100110, 0);
 
	    /* Write-read horizontal start */
	    ret = MemWriteRead(VI_H_VIDEO_REG,     0x00200044, 0x00200044, 0);

	    /* Write-read vertical start */
	    ret = MemWriteRead(VI_V_VIDEO_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read vertical burst */
	    ret = MemWriteRead(VI_V_BURST_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read x-scale */
	    ret = MemWriteRead(VI_X_SCALE_REG,     0x00000200, 0x00000200, 0);

	    /* Write-read y-scale */
	    ret = MemWriteRead(VI_Y_SCALE_REG,     0x02000400, 0x02000400, 0);

	    /* Write-read controller/status */
            ret = MemWriteRead(VI_STATUS_REG,      0x00003053, 0x00003053, 0);

	    dmaCount++;

	    fprintf(LogFp, RTST_MSG_RES2_STR, thread - RandThreadTable,
		    "ViMLF2Dma", ramAddr, 0x28, 0x3ff, 0, 
		    INSERT_RESULT(ret));

	    thread->state = IO_STATE_CHECK_F0_MLF2;

	    fprintf(stdout, "Leaving IO_STATE_START \n"); 

	    break;
	}
 
	case IO_STATE_CHECK_F0_MLF2:
	{
	    /* Wait until DMA counter is at end */

#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F0_MLF2: 0x%.2x \n", data);

	    if (data >= 0x00000028) 
		thread->state = IO_STATE_CHECK_MID_MLF2;

	    break;
	}
	case IO_STATE_CHECK_MID_MLF2:
	{
	    /* Wait until DMA counter is back at beginning  */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif


	    fprintf(stdout, "In IO_STATE_CHECK_MID_MLF2: 0x%.2x \n", data);

	    if (data < 0x00000028) 
		thread->state = IO_STATE_CHECK_F1_MLF2;

	    break;		    
	}
	case IO_STATE_CHECK_F1_MLF2:
	{
	    /* Wait until DMA counter is at end */

#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)IO_READ((VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F1_MLF2: 0x%.2x \n", data);
		    
	    if (data >= 0x00000029)
		thread->state = IO_STATE_CHECK_END_MLF2;

	    break;
	}
	case IO_STATE_CHECK_END_MLF2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_END_MLF2: 0x%.2x \n", data);

	    if (data < 0x00000029)
		{
		    thread->state = IO_STATE_START;
		    thread->inProgress = 0;
		    done++;
		    fprintf(stdout, "Done = %d \n", done);
		    if (done == 6) 
			{
			    fprintf(stdout, "Finished test \n");
			}
		}

	    break;
	}

	default: 
	{
	    _TRACE(DERROR,
		   fprintf(LogFp,
			   "\tERROR: Illegal IO state encountered!\n"));
	}
	}        /* switch */

} /* ViMLF2Dma */

/* 5 new vi tests */


#define IO_STATE_CHECK_F0_MLN1  5
#define IO_STATE_CHECK_MID_MLN1 6
#define IO_STATE_CHECK_F1_MLN1  7
#define IO_STATE_CHECK_END_MLN1 8

void ViMLN1Dma(ThreadCmd *thread)
{
    int			ret;
    unsigned int	stat, data;
    int                 dmacount;
    static int          done;
    static unsigned int ramAddr = 0;

    switch (thread->state) 
	{
	case IO_STATE_INIT:
	case IO_STATE_START: 
	{
	    fprintf(stdout, "In IO_STATE_START \n"); 
	    fflush(stdout);

	    thread->inProgress = 1;

	    ramAddr = ViRdramStart + 0x400 + 0x40;
		    
	    /****************************************************************/
		    
	    /* Write-read start RDRAM address */
	    ret = MemWriteRead(VI_DRAM_ADDR_REG,      ramAddr,    ramAddr, 0);

	    /* Set horizontal width (32x32) */
	    ret = MemWriteRead(VI_H_WIDTH_REG,     0x00000020, 0x00000020, 0);

	    /* Set vertical line interrupt */
	    ret = MemWriteRead(VI_V_INTR_REG,      0x000003ff, 0x000003ff, 0);

	    /* Write-read timing/burst */
	    ret = MemWriteRead(VI_TIMING_REG,      0x00820404, 0x00820404, 0);

	    /* Write-read vertical sync */
	    ret = MemWriteRead(VI_V_SYNC_REG,      0x00000029, 0x00000029, 0);

	    /* Write-read horizontal sync */
	    ret = MemWriteRead(VI_H_SYNC_REG,      0x00000110, 0x00000110, 0);

	    /* Write-read horizontal sync leap */
	    ret = MemWriteRead(VI_H_SYNC_LEAP_REG, 0x01100110, 0x01100110, 0);
 
	    /* Write-read horizontal start */
	    ret = MemWriteRead(VI_H_VIDEO_REG,     0x00200044, 0x00200044, 0);

	    /* Write-read vertical start */
	    ret = MemWriteRead(VI_V_VIDEO_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read vertical burst */
	    ret = MemWriteRead(VI_V_BURST_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read x-scale */
	    ret = MemWriteRead(VI_X_SCALE_REG,     0x00000200, 0x00000200, 0);

	    /* Write-read y-scale */
	    ret = MemWriteRead(VI_Y_SCALE_REG,     0x00000400, 0x00000400, 0);

	    /* Write-read controller/status */
	    ret = MemWriteRead(VI_STATUS_REG,      0x00003112, 0x00003112, 0);

	    dmacount++;

	    fprintf(LogFp, RTST_MSG_RES2_STR, thread - RandThreadTable,
		    "ViMLN1Dma", 0x1f5820, 0x28, 0x3ff, 0, 
		    INSERT_RESULT(ret));

	    thread->state = IO_STATE_CHECK_F0_MLN1;

	    fprintf(stdout, "Leaving IO_STATE_START \n"); 
	    fflush(stdout);

	    break;
	}
 
	case IO_STATE_CHECK_F0_MLN1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F0_MLN1: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data >= 0x00000028) 
		thread->state = IO_STATE_CHECK_MID_MLN1;

	    break;
	}
	case IO_STATE_CHECK_MID_MLN1:
	{
	    /* Wait until DMA counter is back at beginning  */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)IO_READ((VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_MID_MLN1: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data < 0x00000028) 
		thread->state = IO_STATE_CHECK_F1_MLN1;

	    break;		    
	}
	case IO_STATE_CHECK_F1_MLN1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F1_MLN1: 0x%.2x \n", data);
	    fflush(stdout);
		    
	    if (data >= 0x00000028)
		thread->state = IO_STATE_CHECK_END_MLN1;

	    break;
	}
	case IO_STATE_CHECK_END_MLN1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_END_MLN1: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data < 0x00000028)
		{
		    thread->state = IO_STATE_CHECK_F0_MLN1;
		    thread->inProgress = 0;
		    done++;
		    printf("Done = %d \n", done);
		    if (done == 5) 
			{
			    QuickDeath = 1;
			    printf("Finished test \n");
			}
		}

	    break;
	}

	default: 
	{
	    _TRACE(DERROR,
		   fprintf(LogFp,
			   "\tERROR: Illegal IO state encountered!\n"));
	}
	}        /* switch */

} /* ViMLN1Dma */

#define IO_STATE_CHECK_F0_MLN2  5
#define IO_STATE_CHECK_MID_MLN2 6
#define IO_STATE_CHECK_F1_MLN2  7
#define IO_STATE_CHECK_END_MLN2 8

void ViMLN2Dma(ThreadCmd *thread)
{
    int			ret;
    unsigned int	stat, data;
    int                 dmacount;
    static int          done;
    static unsigned int ramAddr = 0;

    switch (thread->state) 
	{
	case IO_STATE_INIT:
	case IO_STATE_START: 
	{
	    fprintf(stdout, "In IO_STATE_START \n"); 
	    fflush(stdout);

	    thread->inProgress = 1;
	    ramAddr = ViRdramStart + 0xd00 + 0x80;

		    
	    /****************************************************************/
		    
	    /* Write-read start RDRAM address */
	    ret = MemWriteRead(VI_DRAM_ADDR_REG,      ramAddr,    ramAddr, 0);

	    /* Set horizontal width (32x32) */
	    ret = MemWriteRead(VI_H_WIDTH_REG,     0x00000020, 0x00000020, 0);

	    /* Set vertical line interrupt */
	    ret = MemWriteRead(VI_V_INTR_REG,      0x000003ff, 0x000003ff, 0);

	    /* Write-read timing/burst */
	    ret = MemWriteRead(VI_TIMING_REG,      0x00820404, 0x00820404, 0);

	    /* Write-read vertical sync */
	    ret = MemWriteRead(VI_V_SYNC_REG,      0x00000029, 0x00000029, 0);

	    /* Write-read horizontal sync */
	    ret = MemWriteRead(VI_H_SYNC_REG,      0x00000110, 0x00000110, 0);

	    /* Write-read horizontal sync leap */
	    ret = MemWriteRead(VI_H_SYNC_LEAP_REG, 0x01100110, 0x01100110, 0);
 
	    /* Write-read horizontal start */
	    ret = MemWriteRead(VI_H_VIDEO_REG,     0x00200044, 0x00200044, 0);

	    /* Write-read vertical start */
	    ret = MemWriteRead(VI_V_VIDEO_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read vertical burst */
	    ret = MemWriteRead(VI_V_BURST_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read x-scale */
	    ret = MemWriteRead(VI_X_SCALE_REG,     0x00000200, 0x00000200, 0);

	    /* Write-read y-scale */
	    ret = MemWriteRead(VI_Y_SCALE_REG,     0x00000400, 0x00000400, 0);

	    /* Write-read controller/status */
	    ret = MemWriteRead(VI_STATUS_REG,      0x00003013, 0x00003013, 0);

	    dmacount++;

	    fprintf(LogFp, RTST_MSG_RES2_STR, thread - RandThreadTable,
		    "ViMLN2Dma", 0x1f6160, 0x28, 0x3ff, 0, 
		    INSERT_RESULT(ret));

	    thread->state = IO_STATE_CHECK_F0_MLN2;

	    fprintf(stdout, "Leaving IO_STATE_START \n"); 
	    fflush(stdout);

	    break;
	}
 
	case IO_STATE_CHECK_F0_MLN2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F0_MLN2: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data >= 0x00000028) 
		thread->state = IO_STATE_CHECK_MID_MLN2;

	    break;
	}
	case IO_STATE_CHECK_MID_MLN2:
	{
	    /* Wait until DMA counter is back at beginning  */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_MID_MLN2: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data < 0x00000028) 
		thread->state = IO_STATE_CHECK_F1_MLN2;

	    break;		    
	}
	case IO_STATE_CHECK_F1_MLN2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else 
	    data = (vu_int) (IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F1_MLN2: 0x%.2x \n", data);
	    fflush(stdout);
		    
	    if (data >= 0x00000028)
		thread->state = IO_STATE_CHECK_END_MLN2;

	    break;
	}
	case IO_STATE_CHECK_END_MLN2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else 
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_END_MLN2: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data < 0x00000028)
		{
		    thread->state = IO_STATE_CHECK_F0_MLN2;
		    thread->inProgress = 0;
		    done++;
		    printf("Done = %d \n", done);
		    if (done == 5) 
			{
			    QuickDeath = 1;
			    printf("Finished test \n");
			}
		}

	    break;
	}

	default: 
	{
	    _TRACE(DERROR,
		   fprintf(LogFp,
			   "\tERROR: Illegal IO state encountered!\n"));
	}
	}        /* switch */

} /* ViMLN2Dma */

#define IO_STATE_CHECK_F0_MLP1  5
#define IO_STATE_CHECK_MID_MLP1 6
#define IO_STATE_CHECK_F1_MLP1  7
#define IO_STATE_CHECK_END_MLP1 8

void ViMLP1Dma(ThreadCmd *thread)
{
    int			ret;
    unsigned int	stat, data;
    int                 dmacount;
    static int          done;
    static unsigned int ramAddr = 0;

    switch (thread->state) 
	{
	case IO_STATE_INIT:
	case IO_STATE_START: 
	{
	    fprintf(stdout, "In IO_STATE_START \n"); 
	    fflush(stdout);

	    thread->inProgress = 1;
	    ramAddr = ViRdramStart + 0x1d00 + 0x40;
		    
	    /****************************************************************/
		    
	    /* Write-read start RDRAM address */
	    ret = MemWriteRead(VI_DRAM_ADDR_REG,      ramAddr,    ramAddr, 0);

	    /* Set horizontal width (32x32) */
	    ret = MemWriteRead(VI_H_WIDTH_REG,     0x00000020, 0x00000020, 0);

	    /* Set vertical line interrupt */
	    ret = MemWriteRead(VI_V_INTR_REG,      0x000003ff, 0x000003ff, 0);

	    /* Write-read timing/burst */
	    ret = MemWriteRead(VI_TIMING_REG,      0x00820404, 0x00820404, 0);

	    /* Write-read vertical sync */
	    ret = MemWriteRead(VI_V_SYNC_REG,      0x00000029, 0x00000029, 0);

	    /* Write-read horizontal sync */
	    ret = MemWriteRead(VI_H_SYNC_REG,      0x00000100, 0x00000100, 0);

	    /* Write-read horizontal sync leap */
	    ret = MemWriteRead(VI_H_SYNC_LEAP_REG, 0x01000100, 0x01000100, 0);
 
	    /* Write-read horizontal start */
	    ret = MemWriteRead(VI_H_VIDEO_REG,     0x00200040, 0x00200040, 0);

	    /* Write-read vertical start */
	    ret = MemWriteRead(VI_V_VIDEO_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read vertical burst */
	    ret = MemWriteRead(VI_V_BURST_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read x-scale */
	    ret = MemWriteRead(VI_X_SCALE_REG,     0x00000200, 0x00000200, 0);

	    /* Write-read y-scale */
	    ret = MemWriteRead(VI_Y_SCALE_REG,     0x00000400, 0x00000400, 0);

	    /* Write-read controller/status */
	    ret = MemWriteRead(VI_STATUS_REG,      0x00003302, 0x00003302, 0);

	    dmacount++;

	    fprintf(LogFp, RTST_MSG_RES2_STR, thread - RandThreadTable,
		    "ViMLP1Dma", 0x1f7120, 0x28, 0x3ff, 0, 
		    INSERT_RESULT(ret));

	    thread->state = IO_STATE_CHECK_F0_MLP1;

	    fprintf(stdout, "Leaving IO_STATE_START \n"); 
	    fflush(stdout);

	    break;
	}
 
	case IO_STATE_CHECK_F0_MLP1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)IO_READ(VI_V_CURRENT_LINE_REG);
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F0_MLP1: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data >= 0x00000028) 
		thread->state = IO_STATE_CHECK_MID_MLP1;

	    break;
	}
	case IO_STATE_CHECK_MID_MLP1:
	{
	    /* Wait until DMA counter is back at beginning  */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_MID_MLP1: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data < 0x00000028) 
		thread->state = IO_STATE_CHECK_F1_MLP1;

	    break;		    
	}
	case IO_STATE_CHECK_F1_MLP1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F1_MLP1: 0x%.2x \n", data);
	    fflush(stdout);
		    
	    if (data >= 0x00000028)
		thread->state = IO_STATE_CHECK_END_MLP1;

	    break;
	}
	case IO_STATE_CHECK_END_MLP1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif 

	    fprintf(stdout, "In IO_STATE_CHECK_END_MLP1: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data < 0x00000028)
		{
		    thread->state = IO_STATE_CHECK_F0_MLP1;
		    thread->inProgress = 0;
		    done++;
		    printf("Done = %d \n", done);
		    if (done == 5) 
			{
			    QuickDeath = 1;
			    printf("Finished test \n");
			}
		}

	    break;
	}

	default: 
	{
	    _TRACE(DERROR,
		   fprintf(LogFp,
			   "\tERROR: Illegal IO state encountered!\n"));
	}
	}        /* switch */

} /* ViMLP1Dma */

#define IO_STATE_CHECK_F0_MHN1  5
#define IO_STATE_CHECK_MID_MHN1 6
#define IO_STATE_CHECK_F1_MHN1  7
#define IO_STATE_CHECK_END_MHN1 8

void ViMHN1Dma(ThreadCmd *thread)
{
    int			ret;
    unsigned int	stat, data;
    int                 dmacount;
    static int          done;
    static unsigned int ramAddr = 0;

    switch (thread->state) 
	{
	case IO_STATE_INIT:
	case IO_STATE_START: 
	{
	    fprintf(stdout, "In IO_STATE_START \n"); 
	    fflush(stdout);

	    thread->inProgress = 1;
	    ramAddr = ViRdramStart + 0x27d0 + 0x80;

		    
	    /****************************************************************/
		    
	    /* Write-read start RDRAM address */
	    ret = MemWriteRead(VI_DRAM_ADDR_REG,      ramAddr,    ramAddr, 0);

	    /* Set horizontal width (32x32) */
	    ret = MemWriteRead(VI_H_WIDTH_REG,     0x00000040, 0x00000040, 0);

	    /* Set vertical line interrupt */
	    ret = MemWriteRead(VI_V_INTR_REG,      0x000003ff, 0x000003ff, 0);

	    /* Write-read timing/burst */
	    ret = MemWriteRead(VI_TIMING_REG,      0x00820404, 0x00820404, 0);

	    /* Write-read vertical sync */
	    ret = MemWriteRead(VI_V_SYNC_REG,      0x0000002a, 0x0000002a, 0);

	    /* Write-read horizontal sync */
	    ret = MemWriteRead(VI_H_SYNC_REG,      0x00000110, 0x00000110, 0);

	    /* Write-read horizontal sync leap */
	    ret = MemWriteRead(VI_H_SYNC_LEAP_REG, 0x01100110, 0x01100110, 0);
 
	    /* Write-read horizontal start */
	    ret = MemWriteRead(VI_H_VIDEO_REG,     0x00200044, 0x00200044, 0);

	    /* Write-read vertical start */
	    ret = MemWriteRead(VI_V_VIDEO_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read vertical burst */
	    ret = MemWriteRead(VI_V_BURST_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read x-scale */
	    ret = MemWriteRead(VI_X_SCALE_REG,     0x00000400, 0x00000400, 0);

	    /* Write-read y-scale */
	    ret = MemWriteRead(VI_Y_SCALE_REG,     0x00000400, 0x00000400, 0);

	    /* Write-read controller/status */
	    ret = MemWriteRead(VI_STATUS_REG,      0x00003052, 0x00003052, 0);

	    dmacount++;

	    fprintf(LogFp, RTST_MSG_RES2_STR, thread - RandThreadTable,
		    "ViMHN1Dma", 0x1f7c30, 0x28, 0x3ff, 0, 
		    INSERT_RESULT(ret));

	    thread->state = IO_STATE_CHECK_F0_MHN1;

	    fprintf(stdout, "Leaving IO_STATE_START \n"); 
	    fflush(stdout);

	    break;
	}
 
	case IO_STATE_CHECK_F0_MHN1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F0_MHN1: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data >= 0x00000028) 
		thread->state = IO_STATE_CHECK_MID_MHN1;

	    break;
	}
	case IO_STATE_CHECK_MID_MHN1:
	{
	    /* Wait until DMA counter is back at beginning  */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_MID_MHN1: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data < 0x00000028) 
		thread->state = IO_STATE_CHECK_F1_MHN1;

	    break;		    
	}
	case IO_STATE_CHECK_F1_MHN1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F1_MHN1: 0x%.2x \n", data);
	    fflush(stdout);
		    
	    if (data >= 0x00000028)
		thread->state = IO_STATE_CHECK_END_MHN1;

	    break;
	}
	case IO_STATE_CHECK_END_MHN1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_END_MHN1: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data < 0x00000028)
		{
		    thread->state = IO_STATE_CHECK_F0_MHN1;
		    thread->inProgress = 0;
		    done++;
		    printf("Done = %d \n", done);
		    if (done == 5) 
			{
			    QuickDeath = 1;
			    printf("Finished test \n");
			}
		}

	    break;
	}

	default: 
	{
	    _TRACE(DERROR,
		   fprintf(LogFp,
			   "\tERROR: Illegal IO state encountered!\n"));
	}
	}        /* switch */

} /* ViMHN1Dma */

#define IO_STATE_CHECK_F0_MHF2  5
#define IO_STATE_CHECK_MID_MHF2 6
#define IO_STATE_CHECK_F1_MHF2  7
#define IO_STATE_CHECK_END_MHF2 8

void ViMHF2Dma(ThreadCmd *thread)
{
    int			ret;
    unsigned int	stat, data;
    int                 dmacount;
    static int          done;
     static unsigned int ramAddr = 0;

    switch (thread->state) 
	{
	case IO_STATE_INIT:
	case IO_STATE_START: 
	{
	    fprintf(stdout, "In IO_STATE_START \n"); 
	    fflush(stdout);

	    thread->inProgress = 1;
	    ramAddr = ViRdramStart + 0x3120 + 0x80;
 		    
	    /****************************************************************/
		    
	    /* Write-read start RDRAM address */
	    ret = MemWriteRead(VI_DRAM_ADDR_REG,      ramAddr,    ramAddr, 0);

	    /* Set horizontal width (32x32) */
	    ret = MemWriteRead(VI_H_WIDTH_REG,     0x00000020, 0x00000020, 0);

	    /* Set vertical line interrupt */
	    ret = MemWriteRead(VI_V_INTR_REG,      0x000003ff, 0x000003ff, 0);

	    /* Write-read timing/burst */
	    ret = MemWriteRead(VI_TIMING_REG,      0x00820404, 0x00820404, 0);

	    /* Write-read vertical sync */
	    ret = MemWriteRead(VI_V_SYNC_REG,      0x0000002a, 0x0000002a, 0);

	    /* Write-read horizontal sync */
	    ret = MemWriteRead(VI_H_SYNC_REG,      0x00000110, 0x00000110, 0);

	    /* Write-read horizontal sync leap */
	    ret = MemWriteRead(VI_H_SYNC_LEAP_REG, 0x01100110, 0x01100110, 0);
 
	    /* Write-read horizontal start */
	    ret = MemWriteRead(VI_H_VIDEO_REG,     0x00200044, 0x00200044, 0);

	    /* Write-read vertical start */
	    ret = MemWriteRead(VI_V_VIDEO_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read vertical burst */
	    ret = MemWriteRead(VI_V_BURST_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read x-scale */
	    ret = MemWriteRead(VI_X_SCALE_REG,     0x00000400, 0x00000400, 0);

	    /* Write-read y-scale */
	    ret = MemWriteRead(VI_Y_SCALE_REG,     0x02000800, 0x02000800, 0);

	    /* Write-read controller/status */
	    ret = MemWriteRead(VI_STATUS_REG,      0x00003243, 0x00003243, 0);

	    dmacount++;

	    fprintf(LogFp, RTST_MSG_RES2_STR, thread - RandThreadTable,
		    "ViMHF2Dma", 0x1f5820, 0x28, 0x3ff, 0, 
		    INSERT_RESULT(ret));

	    thread->state = IO_STATE_CHECK_F0_MHF2;

	    fprintf(stdout, "Leaving IO_STATE_START \n"); 
	    fflush(stdout);

	    break;
	}
 
	case IO_STATE_CHECK_F0_MHF2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F0_MHF2: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data >= 0x00000028) 
		thread->state = IO_STATE_CHECK_MID_MHF2;

	    break;
	}
	case IO_STATE_CHECK_MID_MHF2:
	{
	    /* Wait until DMA counter is back at beginning  */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
            data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_MID_MHF2: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data < 0x00000028) 
		thread->state = IO_STATE_CHECK_F1_MHF2;

	    break;		    
	}
	case IO_STATE_CHECK_F1_MHF2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F1_MHF2: 0x%.2x \n", data);
	    fflush(stdout);
		    
	    if (data >= 0x00000028)
		thread->state = IO_STATE_CHECK_END_MHF2;

	    break;
	}
	case IO_STATE_CHECK_END_MHF2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_END_MHF2: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data < 0x00000028)
		{
		    thread->state = IO_STATE_CHECK_F0_MHF2;
		    thread->inProgress = 0;
		    done++;
		    printf("Done = %d \n", done);
		    if (done == 5) 
			{
			    QuickDeath = 1;
			    printf("Finished test \n");
			}
		}

	    break;
	}

	default: 
	{
	    _TRACE(DERROR,
		   fprintf(LogFp,
			   "\tERROR: Illegal IO state encountered!\n"));
	}
	}        /* switch */

} /* ViMHF2Dma */

void ViBigDma(ThreadCmd *thread)
{
    int			ret;
    unsigned int		stat, data;
    static	unsigned int	lastLine = 0;
    static	unsigned int	intrLine = 0;
    static	unsigned int	hWidth   = 0;
    static	unsigned int	ramAddr  = 0;
    static	unsigned int	dmaCount = 0;
    static	unsigned int	done     = 0;
    static	unsigned int	lastStat = 0;
    
    printf("In ViBigDma procedure \n");

    switch (thread->state) 
	{
	case IO_STATE_INIT:
	case IO_STATE_START: 
	{
	    fprintf(stdout, "In IO_STATE_START \n"); 
	    fflush(stdout);

	    thread->inProgress = 1;

	    ramAddr = ViRdramStart;  /* 0x001f53e0 */

	    lastLine = 0x28;
	    intrLine = 0x3ff;	/* disable interrupt   */
	    hWidth   = 0x20;	/* 32 pixel wide image */

	    /* Do the global register settings that are going to be
	       constant throughout the test */
		    
	    /* Set vertical line interrupt */
	    ret = MemWriteRead(VI_V_INTR_REG,      0x000003ff, 0x000003ff, 0);

	    /* Write-read timing/burst */
	    ret = MemWriteRead(VI_TIMING_REG,      0x00820404, 0x00820404, 0);

	    /* Write-read vertical burst */
	    ret = MemWriteRead(VI_V_BURST_REG,     0x00050025, 0x00050025, 0);
 
	    /****************************************************************/
	    /* File: vi_lan1.tst */

	    /* Write-read horizontal sync */
	    ret = MemWriteRead(VI_H_SYNC_REG,      0x00000110, 0x00000110, 0);

	    /* Write-read horizontal sync leap */
	    ret = MemWriteRead(VI_H_SYNC_LEAP_REG, 0x01100110, 0x01100110, 0);
 
	    /* Write-read vertical sync */
	    ret = MemWriteRead(VI_V_SYNC_REG,      0x00000029, 0x00000029, 0);

	    /* Set horizontal width (32x32) */
	    ret = MemWriteRead(VI_H_WIDTH_REG,     0x00000020, 0x00000020, 0);

	    /* Write-read horizontal start */
	    ret = MemWriteRead(VI_H_VIDEO_REG,     0x00200044, 0x00200044, 0);

	    /* Write-read vertical start */
	    ret = MemWriteRead(VI_V_VIDEO_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read start RDRAM address */
	    ret = MemWriteRead(VI_DRAM_ADDR_REG,   0x001f5820, 0x001f5820, 0);

	    /* Write-read x-scale */
	    ret = MemWriteRead(VI_X_SCALE_REG,     0x00000200, 0x00000200, 0);

	    /* Write-read y-scale */
	    ret = MemWriteRead(VI_Y_SCALE_REG,     0x00000400, 0x00000400, 0);

	    /* Write-read controller/status */
	    ret = MemWriteRead(VI_STATUS_REG,      0x00000112, 0x00000112, 0);

	    dmaCount++;

	    fprintf(LogFp, RTST_MSG_RES2_STR, thread - RandThreadTable,
		    "ViTestDma", ramAddr, lastLine, intrLine, 0, 
		    INSERT_RESULT(ret));

	    thread->state = IO_STATE_CHECK_F0_LAN1;

	    fprintf(stdout, "Leaving IO_STATE_START \n"); 
	    fflush(stdout);

	    break;
	}
	case IO_STATE_CHECK_F0_LAN1: 
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "IO_STATE_CHECK_F0_LAN1: 0x%.2x \n", data); 
	    fflush(stdout);
			   
		    
	    if (data >= 0x00000028) 
		thread->state = IO_STATE_CHECK_MID_LAN1;

	    break;
	}
	case IO_STATE_CHECK_MID_LAN1:
	{
	    /* Wait until DMA counter is back at beginning  */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else	
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_MID_LAN1: 0x%.2x \n", data); 
	    fflush(stdout);


	    if (data < 0x00000028) 
		thread->state = IO_STATE_CHECK_F1_LAN1;

	    break;
		    
	}
	case IO_STATE_CHECK_F1_LAN1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F1_LAN1: 0x%.2x \n", data); 
	    fflush(stdout);


	    if (data >= 0x00000028)
		thread->state = IO_STATE_CHECK_END_LAN1;

	    break;
	}
	case IO_STATE_CHECK_END_LAN1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_END_LAN1 0x%.2x \n", data);
	    fflush(stdout);

	    if (data < 0x00000028)
		{
		    /* Init registers for lan2 test */

		    /* Write-read start RDRAM address */
		    ret = MemWriteRead(VI_DRAM_ADDR_REG,   0x001f6160, 0x001f6160, 0);

		    /* Write-read controller/status */
		    ret = MemWriteRead(VI_STATUS_REG,      0x00000013, 0x00000013, 0);

		    dmaCount++;

		    fprintf(LogFp, RTST_MSG_RES2_STR, thread - RandThreadTable,
			    "ViTestDma", ramAddr, lastLine, intrLine, 0, 
			    INSERT_RESULT(ret));

		    thread->state = IO_STATE_CHECK_F0_LAN2;
		}

	    break;
	}

	case IO_STATE_CHECK_F0_LAN2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F0_LAN2: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data >= 0x00000028) 
		thread->state = IO_STATE_CHECK_MID_LAN2;

	    break;
	}
	case IO_STATE_CHECK_MID_LAN2:
	{
	    /* Wait until DMA counter is back at beginning  */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_MID_LAN2: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data < 0x00000028) 
		thread->state = IO_STATE_CHECK_F1_LAN2;

	    break;		    
	}
	case IO_STATE_CHECK_F1_LAN2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int )(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F1_LAN2: 0x%.2x \n", data);
	    fflush(stdout);
		    

	    if (data >= 0x00000028)
		thread->state = IO_STATE_CHECK_END_LAN2;

	    break;
	}
	case IO_STATE_CHECK_END_LAN2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_END_LAN2: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data < 0x00000028)
		{
		    /* Init registers for lpn1 test */

		    /* Write-read horizontal sync */
		    ret = MemWriteRead(VI_H_SYNC_REG,      0x00000100, 0x00000100, 0);

		    /* Write-read horizontal sync leap */
		    ret = MemWriteRead(VI_H_SYNC_LEAP_REG, 0x01000100, 0x01000100, 0);
 
		    /* Write-read horizontal start */
		    ret = MemWriteRead(VI_H_VIDEO_REG,     0x00200040, 0x00200040, 0);

		    /* Write-read start RDRAM address */
		    ret = MemWriteRead(VI_DRAM_ADDR_REG,   0x001f7120, 0x001f7120, 0);

		    /* Write-read controller/status */
		    ret = MemWriteRead(VI_STATUS_REG,      0x00000302, 0x00000302, 0);

		    dmaCount++;

		    fprintf(LogFp, RTST_MSG_RES2_STR, thread - RandThreadTable,
			    "ViTestDma", ramAddr, lastLine, intrLine, 0, 
			    INSERT_RESULT(ret));

		    thread->state = IO_STATE_CHECK_F0_LPN1;
		}

	    break;
	}
	case IO_STATE_CHECK_F0_LPN1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F0_LPN1: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data >= 0x00000028) 
		thread->state = IO_STATE_CHECK_MID_LPN1;

	    break;
	}
	case IO_STATE_CHECK_MID_LPN1:
	{
	    /* Wait until DMA counter is back at beginning  */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_MID_LPN1: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data < 0x00000028) 
		thread->state = IO_STATE_CHECK_F1_LPN1;

	    break;		    
	}
	case IO_STATE_CHECK_F1_LPN1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F1_LPN1: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data >= 0x00000028)
		thread->state = IO_STATE_CHECK_END_LPN1;

	    break;
	}
	case IO_STATE_CHECK_END_LPN1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
            data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_END_LPN1: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data < 0x00000028)
		{
		    /* Init registers for han1 test */

		    /* Write-read horizontal sync */
		    ret = MemWriteRead(VI_H_SYNC_REG,      0x00000110, 0x00000110, 0);
			    
		    /* Write-read horizontal sync leap */
		    ret = MemWriteRead(VI_H_SYNC_LEAP_REG, 0x01100110, 0x01100110, 0);

		    /* Write-read vertical sync */
		    ret = MemWriteRead(VI_V_SYNC_REG,      0x0000002A, 0x0000002A, 0);

		    /* Set horizontal width (32x32) */
		    ret = MemWriteRead(VI_H_WIDTH_REG,     0x00000040, 0x00000040, 0);

		    /* Write-read horizontal start */
		    ret = MemWriteRead(VI_H_VIDEO_REG,     0x00200044, 0x00200044, 0);

		    /* Write-read start RDRAM address */
		    ret = MemWriteRead(VI_DRAM_ADDR_REG,   0x001f7c30, 0x001f7c30, 0);

		    /* Write-read x-scale */
		    ret = MemWriteRead(VI_X_SCALE_REG,     0x00000400, 0x00000400, 0);

		    /* Write-read controller/status */
		    ret = MemWriteRead(VI_STATUS_REG,      0x00000052, 0x00000052, 0);

		    dmaCount++;

		    fprintf(LogFp, RTST_MSG_RES2_STR, thread - RandThreadTable,
			    "ViTestDma", ramAddr, lastLine, intrLine, 0, 
			    INSERT_RESULT(ret));

		    thread->state = IO_STATE_CHECK_F0_HAN1;
		}
			
	    break;
	}

	case IO_STATE_CHECK_F0_HAN1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F0_HAN1: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data >= 0x00000028) 
		{
		    /* Write-read start RDRAM address */
		    ret = MemWriteRead(VI_DRAM_ADDR_REG,   0x001f7c70, 0x001f7c70, 0);
			    
		    /* Write-read vertical start */
		    ret = MemWriteRead(VI_V_VIDEO_REG,     0x00070027, 0x00070027, 0);

		    thread->state = IO_STATE_CHECK_MID_HAN1;
		}

	    break;
	}
	case IO_STATE_CHECK_MID_HAN1:
	{
	    /* Wait until DMA counter is back at beginning  */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif
	    fprintf(stdout, "In IO_STATE_CHECK_MID_HAN1: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data < 0x00000028) 
		thread->state = IO_STATE_CHECK_F1_HAN1;

	    break;		    
	}
	case IO_STATE_CHECK_F1_HAN1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F1_HAN1: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data >= 0x00000029)
		thread->state = IO_STATE_CHECK_END_HAN1;

	    break;
	}
	case IO_STATE_CHECK_END_HAN1:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int )(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_END_HAN1: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data < 0x00000029)
		{
		    /* Init registers for hpf2 test */

		    /* Set horizontal width (32x32) */
		    ret = MemWriteRead(VI_H_WIDTH_REG,     0x00000020, 0x00000020, 0);

		    /* Write-read start RDRAM address */
		    ret = MemWriteRead(VI_DRAM_ADDR_REG,   0x001f8580, 0x001f8580, 0);

		    /* Write-read vertical start */
		    ret = MemWriteRead(VI_V_VIDEO_REG,     0x00050025, 0x00050025, 0);

		    /* Write-read y-scale */
		    ret = MemWriteRead(VI_Y_SCALE_REG,     0x02000800, 0x02000800, 0);

		    /* Write-read controller/status */
		    ret = MemWriteRead(VI_STATUS_REG,      0x00000243, 0x00000243, 0);

		    dmaCount++;

		    fprintf(LogFp, RTST_MSG_RES2_STR, thread - RandThreadTable,
			    "ViTestDma", ramAddr, lastLine, intrLine, 0, 
			    INSERT_RESULT(ret));

		    thread->state = IO_STATE_CHECK_F0_HPF2;
		}

	    break;
	}

	case IO_STATE_CHECK_F0_HPF2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F0_HPF2: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data >= 0x00000028) 
		{
		    /* Write-read start RDRAM address */
		    ret = MemWriteRead(VI_DRAM_ADDR_REG,   0x001f8600, 0x001f8600, 0);
			    
		    /* Write-read vertical start */
		    ret = MemWriteRead(VI_V_VIDEO_REG,     0x00070027, 0x00070027, 0);
			   
		    thread->state = IO_STATE_CHECK_MID_HPF2;
		}

	    break;
	}
	case IO_STATE_CHECK_MID_HPF2:
	{
	    /* Wait until DMA counter is back at beginning  */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_MID_HPF2: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data < 0x00000028) 
		thread->state = IO_STATE_CHECK_F1_HPF2;

	    break;		    
	}

	case IO_STATE_CHECK_F1_HPF2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F1_HPF2: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data >= 0x00000029)
		thread->state = IO_STATE_CHECK_END_HPF2;

	    break;
	}
	case IO_STATE_CHECK_END_HPF2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_END_HPF2: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data < 0x00000029) 
		{
		    thread->state = IO_STATE_START;
		    thread->inProgress = 0;
		    done = 1;
		    QuickDeath = 1;
		    printf("Finished test \n");
		}

	    break;
	}
	default: 
	{
	    _TRACE(DERROR, 
		   fprintf(LogFp,
			   "\tERROR: Illegal IO state encountered!\n"));
	}
	}  /* switch */

} /* ViBigDma */


void ViLan2Dma(ThreadCmd *thread)
{
    int			ret;
    unsigned int	stat, data;
    int                 dmacount;
    int                 done;

    printf("In ViLan2Dma procedure \n");

    switch (thread->state) 
	{
	case IO_STATE_INIT:
	case IO_STATE_START: 
	{
	    fprintf(stdout, "In IO_STATE_START \n"); 
	    fflush(stdout);

	    thread->inProgress = 1;
		    
	    /****************************************************************/
	    /* File: vi_lan2.tst */
		    
	    /* Write-read start RDRAM address */
	    ret = MemWriteRead(VI_DRAM_ADDR_REG,   0x001f6160, 0x001f6160, 0);

	    /* Set horizontal width (32x32) */
	    ret = MemWriteRead(VI_H_WIDTH_REG,     0x00000020, 0x00000020, 0);

	    /* Set vertical line interrupt */
	    ret = MemWriteRead(VI_V_INTR_REG,      0x000003ff, 0x000003ff, 0);

	    /* Write-read timing/burst */
	    ret = MemWriteRead(VI_TIMING_REG,      0x00820404, 0x00820404, 0);

	    /* Write-read vertical sync */
	    ret = MemWriteRead(VI_V_SYNC_REG,      0x00000029, 0x00000029, 0);

	    /* Write-read horizontal sync */
	    ret = MemWriteRead(VI_H_SYNC_REG,      0x00000110, 0x00000110, 0);

	    /* Write-read horizontal sync leap */
	    ret = MemWriteRead(VI_H_SYNC_LEAP_REG, 0x01100110, 0x01100110, 0);
 
	    /* Write-read horizontal start */
	    ret = MemWriteRead(VI_H_VIDEO_REG,     0x00200044, 0x00200044, 0);

	    /* Write-read vertical start */
	    ret = MemWriteRead(VI_V_VIDEO_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read vertical burst */
	    ret = MemWriteRead(VI_V_BURST_REG,     0x00050025, 0x00050025, 0);

	    /* Write-read x-scale */
	    ret = MemWriteRead(VI_X_SCALE_REG,     0x00000200, 0x00000200, 0);

	    /* Write-read y-scale */
	    ret = MemWriteRead(VI_Y_SCALE_REG,     0x00000400, 0x00000400, 0);

	    /* Write-read controller/status */
	    ret = MemWriteRead(VI_STATUS_REG,      0x00000013, 0x00000013, 0);

	    dmacount++;

	    fprintf(LogFp, RTST_MSG_RES2_STR, thread - RandThreadTable,
		    "ViLan2Dma", 0x1f6160, 0x28, 0x3ff, 0, 
		    INSERT_RESULT(ret));

	    thread->state = IO_STATE_CHECK_F0_LAN2;

	    fprintf(stdout, "Leaving IO_STATE_START \n"); 
	    fflush(stdout);

	    break;
	}
 
	case IO_STATE_CHECK_F0_LAN2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F0_LAN2: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data >= 0x00000028) 
		thread->state = IO_STATE_CHECK_MID_LAN2;

	    break;
	}
	case IO_STATE_CHECK_MID_LAN2:
	{
	    /* Wait until DMA counter is back at beginning  */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_MID_LAN2: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data < 0x00000028) 
		thread->state = IO_STATE_CHECK_F1_LAN2;

	    break;		    
	}
	case IO_STATE_CHECK_F1_LAN2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F1_LAN2: 0x%.2x \n", data);
	    fflush(stdout);
		    

	    if (data >= 0x00000028)
		thread->state = IO_STATE_CHECK_END_LAN2;

	    break;
	}
	case IO_STATE_CHECK_END_LAN2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int )(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_END_LAN2: 0x%.2x \n", data);
	    fflush(stdout);


	    if (data < 0x00000028)
		{
		    thread->state = IO_STATE_START;
		    thread->inProgress = 0;
		    done = 1;
		    QuickDeath = 1;
		    printf("Finished test \n");
		}

	    break;
	}

	default: 
	{
	    _TRACE(DERROR,
		   fprintf(LogFp,
			   "\tERROR: Illegal IO state encountered!\n"));
	}
	}        /* switch */

} /* ViLan2Dma */

#define IO_STATE_CHECK_F0_DTH2  5
#define IO_STATE_CHECK_MID_DTH2 6
#define IO_STATE_CHECK_F1_DTH2  7
#define IO_STATE_CHECK_END_DTH2 8

void ViDTH2Dma(ThreadCmd *thread)
{
    int			ret;
    unsigned int	stat, data;
    int                 dmacount;
    static int          done;
     static unsigned int ramAddr = 0;

    switch (thread->state) 
	{
	case IO_STATE_INIT:
	case IO_STATE_START: 
	{
	    fprintf(stdout, "In IO_STATE_START \n"); 
	    fprintf(stdout, "Address -> 0x%.8x \n", ViRdramStart);
	    fflush(stdout);

	    thread->inProgress = 1;
	    ramAddr = ViRdramStart;
	    
	    /****************************************************************/
		    
	    /* Write-read start RDRAM address */
	    ret = MemWriteRead(VI_DRAM_ADDR_REG,      ramAddr,    ramAddr, 0);

	    /* Set horizontal width (32x32) */
	    ret = MemWriteRead(VI_H_WIDTH_REG,     0x00000020, 0x00000020, 0);

	    /* Set vertical line interrupt */
	    ret = MemWriteRead(VI_V_INTR_REG,      0x000003ff, 0x000003ff, 0);

	    /* Write-read timing/burst */
	    ret = MemWriteRead(VI_TIMING_REG,      0x00820404, 0x00820404, 0);

	    /* Write-read vertical sync */
	    ret = MemWriteRead(VI_V_SYNC_REG,      0x00000049, 0x00000049, 0);

	    /* Write-read horizontal sync */
	    ret = MemWriteRead(VI_H_SYNC_REG,      0x00000110, 0x00000110, 0);

	    /* Write-read horizontal sync leap */
	    ret = MemWriteRead(VI_H_SYNC_LEAP_REG, 0x01100110, 0x01100110, 0);
 
	    /* Write-read horizontal start */
	    ret = MemWriteRead(VI_H_VIDEO_REG,     0x00200044, 0x00200044, 0);

	    /* Write-read vertical start */
	    ret = MemWriteRead(VI_V_VIDEO_REG,     0x00050045, 0x00050045, 0);

	    /* Write-read vertical burst */
	    ret = MemWriteRead(VI_V_BURST_REG,     0x00050045, 0x00050045, 0);

	    /* Write-read x-scale */
	    ret = MemWriteRead(VI_X_SCALE_REG,     0x00000400, 0x00000400, 0);

	    /* Write-read y-scale */
	    ret = MemWriteRead(VI_Y_SCALE_REG,     0x00000400, 0x00000400, 0);

	    /* Write-read controller/status */
	    ret = MemWriteRead(VI_STATUS_REG,      0x00013003, 0x00013003, 0);

	    dmacount++;

	    fprintf(LogFp, RTST_MSG_RES2_STR, thread - RandThreadTable,
		    "ViDTH2Dma", 0x1f5820, 0x48, 0x3ff, 0, 
		    INSERT_RESULT(ret));

	    thread->state = IO_STATE_CHECK_F0_DTH2;

	    fprintf(stdout, "Leaving IO_STATE_START \n"); 
	    fflush(stdout);

	    break;
	}
 
	case IO_STATE_CHECK_F0_DTH2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_F0_DTH2: 0x%.2x \n", data);
	    fflush(stdout);

	    /* We only do 1 frame, every frame is the same */
	    if (data >= 0x00000048) 
		thread->state = IO_STATE_CHECK_END_DTH2;

	    break;
	}
	case IO_STATE_CHECK_END_DTH2:
	{
	    /* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
	    data = *(vu_int *)PHYS_TO_K1(VI_V_CURRENT_LINE_REG);
#else
	    data = (vu_int)(IO_READ(VI_V_CURRENT_LINE_REG));
#endif

	    fprintf(stdout, "In IO_STATE_CHECK_END_DTH2: 0x%.2x \n", data);
	    fflush(stdout);

	    if (data < 0x00000048)
		{
		    thread->state = IO_STATE_CHECK_F0_DTH2;
		    thread->inProgress = 0;
		    done++;
		    printf("Done = %d \n", done);
		    if (done == 5) 
			{
			    QuickDeath = 1;
			    printf("Finished test \n");
			}
		}

	    break;
	}

	default: 
	{
	    _TRACE(DERROR,
		   fprintf(LogFp,
			   "\tERROR: Illegal IO state encountered!\n"));
	}
	}        /* switch */

} /* ViDTH2Dma */



/***************************************************************************
 * SP thread routines
 */
static int
getSpSemaphore(void)
{
	unsigned int sem;

#ifdef __sgi__  /* K1--BUG */
	sem = *(vu_int *)PHYS_TO_K1(SP_SEMAPHORE_REG);
#else
	sem = (vu_int)(IO_READ(SP_SEMAPHORE_REG));
#endif

	/* If sem is 0, it means that we have the semaphore */
	return((sem == 0) ? 1 : 0);
}

static void
releaseSpSemaphore(void)
{
#ifdef __sgi__  /* K1--BUG */
	*(vu_int *)PHYS_TO_K1(SP_SEMAPHORE_REG) = 0;
#else
	IO_WRITE(SP_SEMAPHORE_REG, 0);
#endif
}

void
SpRandIoRd(ThreadCmd *thread)
{
	unsigned int		data;
	unsigned int		stat;
	char			s1[20];
	static unsigned int 	address = 0;

	/* Assume that ROM has been initialized with data = address */
	if ((address == 0) || (address == SpImemStart)) {
		address = SpDmemStart;
	}
	else if (address == SpDmemStart) {
		address = SpImemStart;
	}
	address = address & 0xfffffffc;

	/* 
	 * For SP, we need to check for IO full bit before perform
	 * IO read - If they're busy, we simply exit and try again 
	 * later
	 */
#ifdef __sgi__  /* K1--BUG */
	stat = *(vu_int *)PHYS_TO_K1(SP_STATUS_REG);
#else
	stat = (vu_int)(IO_READ(SP_STATUS_REG));
#endif
	if ((stat & SP_STATUS_IO_FULL) ||
	    ((address == SpImemStart) && !(stat & SP_STATUS_HALT))) {
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, data, 0, stat, BUSY_STR);
	}
	else {
		data = IoSpRead(address);
		sprintf(s1, "0x%08x", data); 
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, data, 0, stat, s1);
	}

}  /* SpRandIoRd */


void
SpRandIoWrRd(ThreadCmd *thread)
{
	int			ret;
	unsigned int		stat;
	static unsigned int	address = 0;

	/* 
	 * For SP, we need to check for DMA/IO busy bits before perform
	 * IO write - If they're busy, we simply exit and try again 
	 * later
	 */
	/* Reserve the 1st 32-bit for IO read */
	if ((address == 0) || (address == SpImemStart+8)) {
		address = SpDmemStart+8;
	}
	else if (address == SpDmemStart+8) {
		address = SpImemStart+8;
	}
	address = address & 0xfffffffc;

#ifdef __sgi__  /* K1--BUG */
	stat = *(vu_int *)PHYS_TO_K1(SP_STATUS_REG);
#else
	stat = (vu_int)(IO_READ(SP_STATUS_REG));
#endif
	if ((stat & (SP_STATUS_DMA_BUSY | SP_STATUS_IO_FULL)) ||
	    ((address == SpImemStart+8) && !(stat & SP_STATUS_HALT))) {
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, address, 0, 0, BUSY_STR);
	}
	else {
		ret = SpTestIoWrite(address, address, 0, 0);
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, address, 0, stat, INSERT_RESULT(ret));
	}

}  /* SpRandIoWrRd */


void
SpRandDma(ThreadCmd *thread)
{
	int			i, ret;
	unsigned int		stat;
	static	unsigned int	skip, count, nbytes;
	static	unsigned int	dir      = DIR_TO_RAM;
	static	unsigned int	spAddr   = 0;
	static	unsigned int	ramAddr  = 0;
	static	unsigned int	length	 = IMEM_TEST_SIZE - 16;
	static	unsigned int	lastStat = 0;

	switch (thread->state) {
		case IO_STATE_INIT: 
		case IO_STATE_START: {
			thread->inProgress = 1;

			/* Toggle 
			dir = (dir==DIR_TO_RAM) ? DIR_FROM_RAM : DIR_TO_RAM;

			/* Toggle between I/DMEM address 
			 * Reserve the first 2 words (64-bit) for IO W/R
			 */
			if (spAddr == SpImemStart+16)
				spAddr = SpDmemStart+16;
			else {
				spAddr = SpImemStart+16;
				/* For IMEM, we only DMA RDRAM -> IMEM */
				dir = DIR_FROM_RAM; 
			}
			ramAddr = (dir == DIR_FROM_RAM) ? SpRdramStart :
					(SpRdramStart + IMEM_TEST_SIZE);

			ramAddr &= 0xfffffff8;
			spAddr  &= 0xfffffff8;

			/* 
			 * Now, we configure the length to be 
			 * 	(skip[12], count[8], len[12])
			 */
			/* skip = 8, count = 1 (actually 2), nbytes = 48 */
			skip   = 8;
			count  = 1;
			nbytes = 48;
			length = (skip << 20) | (count << 12) | nbytes;	

			/* If DMA is full, we simply return */
#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(SP_STATUS_REG);
#else
			stat = (vu_int)(IO_READ(SP_STATUS_REG));
#endif
			if (stat & SP_STATUS_DMA_FULL) {
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SpTestDma",
					dir, spAddr, ramAddr, stat, BUSY_STR);
			} else {
				/* else start DMA and go to next state */
				if (!getSpSemaphore()) {
					fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SpTestDma",
					dir, spAddr, ramAddr, length, BUSY_STR);
					break;
				}
				ret = SpTestDma(dir, spAddr, ramAddr, length);
				releaseSpSemaphore();
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SpTestDma",
					dir, spAddr, ramAddr, length,
					INSERT_RESULT(ret));
				thread->state = IO_STATE_CHECK_STATUS;
			}
			break;
		}
		case IO_STATE_CHECK_STATUS: {
#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(SP_STATUS_REG);
#else
			stat = (vu_int)(IO_READ(SP_STATUS_REG));
#endif
			if (stat != lastStat)
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SpDmaStatus",
					SP_STATUS_REG, SP_STATUS_DMA_BUSY, 
					1, stat, DONE_STR);
			lastStat = stat;
			if (!(stat & SP_STATUS_DMA_BUSY)) {
				if (CompareEnabled)
					thread->state = IO_STATE_COMPARE;
				else {
					thread->state = IO_STATE_START;
					thread->inProgress = 0;
				}
			}
			break;
		}
		case IO_STATE_COMPARE: {
			/* Here we compare the 1st word, last word */
			/* nbytes = 48; skip = 8; count = 2
			 * length = skip | count | nbytes;	
			 */
			for (i = 0; i < (count+1); i++) {
				ret = MemSmplCompare(spAddr+(i*nbytes), 
					ramAddr+(i*nbytes)+(i*skip), 
					nbytes/4, 0);
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable,
					"SpDmaCompare", 
					spAddr+(i*nbytes), 
					ramAddr+(i*nbytes)+(i*skip), 
					nbytes/4, 0, INSERT_RESULT(ret));
			}
			thread->state = IO_STATE_INIT;
			thread->inProgress = 0;
			break;
		}
		default: {
                        _TRACE(DERROR,
                        fprintf(LogFp,
                                "\tERROR: Illegal IO state encountered!\n"));
		}
	}  /* switch */

}  /* SpRandDma */

/* Dummy sp test used in vi testing */

void
SpDummyDma(ThreadCmd *thread)
{
	int			i, ret;
	unsigned int		stat;
	static	unsigned int	skip, count, nbytes;
	static	unsigned int	dir      = DIR_TO_RAM;
	static	unsigned int	spAddr   = 0;
	static	unsigned int	ramAddr  = 0;
	static	unsigned int	length	 = IMEM_TEST_SIZE - 16;
	static	unsigned int	lastStat = 0;

	switch (thread->state) {
		case IO_STATE_INIT: 
		case IO_STATE_START: {
			thread->inProgress = 1;

			/* Toggle 
			dir = DIR_TO_RAM;

			spAddr = SP_DMEM_START;

			ramAddr = SpRdramStart + IMEM_TEST_SIZE;

			ramAddr &= 0xfffffff8;
			spAddr  &= 0xfffffff8;

			/* 
			 * Now, we configure the length to be 
			 * 	(skip[12], count[8], len[12])
			 */

			skip   = 0;
			count  = 15;
			nbytes = 256;
			length = (skip << 20) | (count << 12) | nbytes;	

			/* If DMA is full, we simply return */
#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(SP_STATUS_REG);
#else
			stat = (vu_int)(IO_READ(SP_STATUS_REG));
#endif
			if (stat & SP_STATUS_DMA_FULL) {
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SpTestDma",
					dir, spAddr, ramAddr, stat, BUSY_STR);
			} else {
				/* else start DMA and go to next state */
				if (!getSpSemaphore()) {
					fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SpTestDma",
					dir, spAddr, ramAddr, length, BUSY_STR);
					break;
				}
				ret = SpTestDma(dir, spAddr, ramAddr, length);
				releaseSpSemaphore();
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SpTestDma",
					dir, spAddr, ramAddr, length,
					INSERT_RESULT(ret));
				thread->state = IO_STATE_CHECK_STATUS;
			}
			break;
		}
		case IO_STATE_CHECK_STATUS: {
#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(SP_STATUS_REG);
#else
			stat = (vu_int )(IO_READ(SP_STATUS_REG));
#endif
			if (stat != lastStat)
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SpDmaStatus",
					SP_STATUS_REG, SP_STATUS_DMA_BUSY, 
					1, stat, DONE_STR);
			lastStat = stat;
			if (!(stat & SP_STATUS_DMA_BUSY)) {
				thread->state = IO_STATE_INIT;
				thread->inProgress = 0;
				releaseSpSemaphore();
			}
			break;
		}
		default: {
                        _TRACE(DERROR,
                        fprintf(LogFp,
                                "\tERROR: Illegal IO state encountered!\n"));
		}
	}  /* switch */

}  /* SpDummyDma */


/*
 * Use Evan's test to exercise DMA transfers to/from RDRAM from/to D/IMEM
 * Use semaphore to guarantee that only one processor (either SP or CPU)
 * can DMA in/out of D/IMEM. Load Evan's test to D/IMEM and DMA RDRAM data
 * from ROM cartrdige (to 0x0012000 - 0x0012700). 
 *
 * Test uses SP signal 0     : 0 - test not done
 *			       1 - test done
 *              signal [3-1] : test that fails (if all passes, should be 0)
 *
 * Should poll for either the SP halt bit and signal 0 bit to be set
 *
 * Test only uses the upper 2KB in both D/IMEM
 */

void
SpLocDma(ThreadCmd *thread)
{
	unsigned int		stat, data;
	ThreadCmd		*dpThread;
	unsigned int		testFile = 0;
	unsigned int		testNum  = 0;
	static unsigned int	testCount = 1;
	static unsigned int	lastStat = 0;
	static unsigned int	done = 0;
	static unsigned int	init = 0;

	spLocDmaActive = 1;
	switch (thread->state) {
		case IO_STATE_INIT: 
		case IO_STATE_START: {
			thread->inProgress = 1;

			/* 
			 * Assume that D/Imem and Rdram has been pre-loaded
			 * with test data
			 */

#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(SP_STATUS_REG);
#else
			stat = (vu_int )(IO_READ(SP_STATUS_REG));
#endif
			/* Make sure that SP is halted before setting PC */
			if (stat & SP_STATUS_HALT) {
				if (!init) {
					/* Set the SP PC to 0 */
				    if (dma80Enabled) {
#ifdef __sgi__  /* K1--BUG */
					*(vu_int *)PHYS_TO_K1(SP_PC_REG) = 0;
#else
					IO_WRITE(SP_PC_REG, 0);
#endif
					/* This is a hack to fix the problem
					 * with exception handler executing 
					 * the else case
					 */
				        init = 1;
				    }
			            else {	
#ifdef __sgi__  /* K1--BUG */
				        *(vu_int *)PHYS_TO_K1(SP_PC_REG) = 0xb24;
#else
					IO_WRITE(SP_PC_REG, 0xb24);
#endif
				    }
				    init = 1;
				}

				/* Clear halt bit to kick off SP test */
#ifdef __sgi__  /* K1--BUG */
				*(vu_int *)PHYS_TO_K1(SP_STATUS_REG) = 
					SP_CLR_HALT;
#else
				IO_WRITE(SP_STATUS_REG, SP_CLR_HALT);
#endif
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable,
					"SpLocStart", 
					SP_STATUS_REG, 0, testCount, 
					stat, DONE_STR);
				thread->state = IO_STATE_CHECK_STATUS;
			}
			else {
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable,
					"SpLocStart", 
					SP_STATUS_REG, 0, testCount, 
					stat, BUSY_STR);
			}
			break;
		}
		case IO_STATE_CHECK_STATUS: {
#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(SP_STATUS_REG);
#else
			stat = (vu_int )(IO_READ(SP_STATUS_REG));
#endif
			if (stat != lastStat)
			    fprintf(LogFp, RTST_MSG_RES2_STR,
				thread - RandThreadTable, "SpLocStatus", 
				SP_STATUS_REG, SP_STATUS_HALT, 
				testCount, stat, DONE_STR);
			lastStat = stat;
			/* Check for halt bit set */
			if (stat & (SP_STATUS_HALT | SP_STATUS_BROKE)) {
			    if (dma80Enabled) {
				/* Check for failed test */
				if (stat & (SP_STATUS_SIG1 | SP_STATUS_SIG2 |
				            SP_STATUS_SIG3)) {
				    fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SpLocCheck", 
					SP_STATUS_REG, testCount,
					(SP_STATUS_SIG1 | SP_STATUS_SIG2 | 
					 SP_STATUS_SIG3), stat, FAIL_STR);
        			    errorTotal++;
				} else
				    fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SpLocCheck", 
					SP_STATUS_REG, testCount,
					(SP_STATUS_SIG1 | SP_STATUS_SIG2 |
					 SP_STATUS_SIG3), stat, PASS_STR);

				/* Clear broke and signal bits [1-3] */
#ifdef __sgi__  /* K1--BUG */
				*(vu_int *)PHYS_TO_K1(SP_STATUS_REG) = 
					(SP_CLR_BROKE | SP_CLR_SIG1 | 
					 SP_CLR_SIG2 | SP_CLR_SIG3); 
#else
				IO_WRITE(SP_STATUS_REG, SP_CLR_BROKE |
                                      SP_CLR_SIG1 | SP_CLR_SIG2 | SP_CLR_SIG3);
#endif
				/*This is a hack to fix the problem
				 * with exception handler executing 
				 * the else case (branch delay slot issue)
				 */
				init = 1;
			    }
			    else {
				/* Check for failed test */
				if (stat & SP_STATUS_SIG1) {
				    /* Get test file from RDRAM_IMEM_TEST_ID */
#ifdef __sgi__  /* K1--BUG */
				    testFile = *(vu_int *)PHYS_TO_K1(
							RDRAM_IMEM_TEST_ID);
#else
				    testFile = (vu_int )(IO_READ(RDRAM_IMEM_TEST_ID));
#endif
				    testFile = (testFile & 0x000000ff) >> 3;
				    /* Get test number from DMEM address 0 */
				    testNum = IoSpRead(SP_DMEM_START);
				    fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SpLocCheck", 
					SP_STATUS_REG, testFile, testNum,
					stat, FAIL_STR);
        			    errorTotal++;
				}
				else {
				    fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SpLocCheck", 
					SP_STATUS_REG, testCount,
					SP_STATUS_SIG1, stat, PASS_STR);
				}
				/* Clear broke and signal 1 */
#ifdef __sgi__  /* K1--BUG */
				*(vu_int *)PHYS_TO_K1(SP_STATUS_REG) = 
					(SP_CLR_BROKE | SP_CLR_SIG1);
#else
				IO_WRITE(SP_STATUS_REG, SP_CLR_BROKE | SP_CLR_SIG1);
#endif
			    }  /* not dma80 test */

			    thread->state = IO_STATE_START;
			    thread->inProgress = 0;
			    testCount++;
			}
			break;
		}
		default: {
                        _TRACE(DERROR,
                        fprintf(LogFp,
                                "\tERROR: Illegal IO state encountered!\n"));
		}
	}  /* switch */

}  /* SpLocDma */


/***************************************************************************
 * DP thread routines
 */
static void
initDpXbusChannel(unsigned int dmemActive)
{
	/* Setup dmem/rdram-xbus channel */
	if (dmemActive == 0) {
#ifdef __sgi__  /* K1--BUG */
		*(vu_int *)PHYS_TO_K1(DPC_STATUS_REG) = DPC_CLR_XBUS_DMEM_DMA;
#else
		IO_WRITE(DPC_STATUS_REG, DPC_CLR_XBUS_DMEM_DMA);
#endif
		/* This is a hack: without the next statement, program ignores 
		 * the else condition and executes the other memory write: 
		 * could the bug be in the exception handler or compiler?
		 */
		dmemActive = 0;	
	} else {
#ifdef __sgi__  /* K1--BUG */
		*(vu_int *)PHYS_TO_K1(DPC_STATUS_REG) = DPC_SET_XBUS_DMEM_DMA;
#else
		IO_WRITE(DPC_STATUS_REG, DPC_SET_XBUS_DMEM_DMA);
#endif
		dmemActive = 1;

	}

	/* Wait for xbus_dmem_dma set/cleared */
	MemPollStatus(DPC_STATUS_REG, 
		DPC_STATUS_XBUS_DMEM_DMA, (dmemActive != 0) ? 0 : 1, 0);

}  /* initDpXbusChannel */

static void 
parseVerifyInfo(void)
{
	VerifyInfo	*verifp;
	int		count;

	/* Get DP list starting address and length count 
	* count is a multiple of 8 (64-bit)
	*/
#ifdef __sgi__  /* K1--BUG */
	verifp=(VerifyInfo *)(PHYS_TO_K1( VERIFY_INFO_PHYSADDR));
	dplStartAddr = verifp->rdpListAddr;

	/*  
	 * The rdpListSize is 64 bit written back by the SP. The count resides in
	 * the lower 32 bit. Since iosim only traps 32 bit mbus reads, we must
	 * read the lower 32 bit.
	 */  
	count        = *(((unsigned int *)&verifp->rdpListSize)+1);

#else
	{
	   int offset;
           VerifyInfo tmp;

           offset = ((void *) &tmp.rdpListAddr) - ((void *) &tmp);
           dplStartAddr = IO_READ(VERIFY_INFO_PHYSADDR + offset);

           offset = ((void *) &tmp.rdpListSize) - ((void *) &tmp);
           count = IO_READ(VERIFY_INFO_PHYSADDR + offset + 4);

        }
#endif

	/*
	* Calculate DP list ending address and set current address
	*/
	dplEndAddr  = dplStartAddr + count;
	dplCurrAddr = dplStartAddr;

}  /* parseVerifyInfo */


static void
setupRingBuffer(void)
{
	unsigned int	i, data;

	dplRingStartp &= 0xfffffff8;
	initDpXbusChannel(dplRingSource);

	/* Copy from DP list to ring buffer */
	i = 0;
	while ((i < dplRingSize) && 
		(dplCurrAddr < dplEndAddr)) {
#ifdef __sgi__  /* K1--BUG */
		data = *(vu_int *)PHYS_TO_K1(dplCurrAddr);
#else
		data = (vu_int)(IO_READ(dplCurrAddr));
#endif
		if (dplRingSource != FROM_RDRAM) {
			IoSpWrite(dplRingStartp+(i*4), data);
		}
		else
#ifdef __sgi__  /* K1--BUG */
			*(vu_int *)PHYS_TO_K1(dplRingStartp+(i*4)) = data;
#else
			IO_WRITE(dplRingStartp+(i*4), data);
#endif

		dplCurrAddr += 4;
		i++;
	}

	if (dplRingSource == FROM_DMEM2) {	/* Use Evan's test */
		/* Fill remaining space with 0 (noop) */
		while (i < dplRingSize) {
			IoSpWrite(dplRingStartp+(i*4), 0);
			i++;
		}
		/* Set sig0 in SP to tell SP that it can perform
		 * DMA to DPC
		 */
#ifdef __sgi__  /* K1--BUG */
		*(vu_int *)PHYS_TO_K1(SP_STATUS_REG) = SP_SET_SIG0;
#else
		IO_WRITE(SP_STATUS_REG, SP_SET_SIG0);
#endif
		dplRingEndp = Dp2DmemEnd & 0xfffffff8;
	}
	else {
		/* Since ring size is an even number of 32-bit words and
	 	 * start/end addresses are 8-byte aligned, we assume 
	 	 * that "i" (after while loop) is an even number
	 	 */
		dplRingEndp = (dplRingStartp+(i*4)) & 0xfffffff8;

		/* Write start & end addresses */
#ifdef __sgi__  /* K1--BUG */
		*(vu_int *)PHYS_TO_K1(DPC_START_REG) = dplRingStartp;
#else
		IO_WRITE(DPC_START_REG, dplRingStartp);
#endif

		/*
		 * If end is far away from start (at least 10 64-bit 
		 * words, we program 2 end pointers to satisfy the case:
	 	 *	3) start -> end (no stalled) -> end
		 */
		if (((dplRingEndp - dplRingStartp)/8) > 10) {  
#ifdef __sgi__  /* K1--BUG */
			*(vu_int *)PHYS_TO_K1(DPC_END_REG) = dplRingEndp-8;
#else
			IO_WRITE(DPC_END_REG, dplRingEndp-8);
#endif
		}

		/* Reset end pointer */
#ifdef __sgi__  /* K1--BUG */
		*(vu_int *)PHYS_TO_K1(DPC_END_REG) = dplRingEndp;
#else
		IO_WRITE(DPC_END_REG, dplRingEndp);
#endif

	}  /* else */

}  /* setupRingBuffer */


void
DpcRandDma(ThreadCmd *thread)
{
	VerifyInfo 		*verifp;
	int			i, ret;
	unsigned int		stat, data, countAddr;
	unsigned int		spStat;
	static	ThreadCmd	*spThread = NULL;
	static	unsigned int	init      = 0;
	static	unsigned int	count	  = 0;
	static	unsigned int	done	  = 0;
	static	unsigned int	lastStat   = 0;

	/*
	 * For DP testing of the command buffer, we pass it a real display
	 * list for processing. By programming the fifo in circular manner
	 * all the different conditions will be exercised.
	 *	1) idle -> start -> end
	 *	2) start -> end (stalled) -> end
	 *	3) start -> end (no stalled) -> end
	 *	4) start -> end -> wrap
	 *
	 * In this test, we have a DP list and a small ring buffer in RDRAM.
	 * We copy 64-bit words from the DP list to the ring buffer and 
	 * kick off DMA to DP Cmd from the ring buffer. We maintain 2 pointers
	 * (start and end) in the ring buffer. By wrapping these pointers 
	 * around the buffer, we will exercise all the 4 conditions mentioned
	 * above.
	 *               RDRAM
	 *            +-----------+
	 *        +-- |  DP List  |
	 *        |   |           |
	 *        |   |           |
	 *        |   +-----------+
	 *        |   |     .     |
	 *        |   |     .     |
	 *        |   |     .     |
	 *        |   |           |
	 *        |   +-----------+ <- Start
	 *        +-> |  Ring     |
	 *            |  Buffer   | 
	 *	      +-----------+ <- End
	 *
	 * NOTE:
	 *	- Need to enlarge the ring buffer size (to 30 64-bit words)
	 *	so that DMA busy bit can be verified in dmem->xbus direction
	 *	(DP takes around 11 cycles to process CMD FIFO; that is why
	 *	with the current size of 11 64-bit words, we can't see busy
	 *	bit to set)
	 *	- Need to put DpRdramStart at an address such that the RDRAM
	 *	ring buffer spans a 2KB-page boundary.
	 */

	/* Here, we kick off DP for DMA data from RDRAM into DP for
	 * processing and writing out results back to RDRAM
	 * Since RDRAM is mmap with a file, the output can be reviewed
	 * once the test is completed
	 */

	/* For now, we only paint the display list ONCE! */
	if (done) {
		return;
	}
	dpcRandDmaActive = 1;
	switch (thread->state) {
		case IO_STATE_INIT:  
		case IO_STATE_START: {
			thread->inProgress = 1;

			if (!init) {
				init = 1;
				/* Get info from VerifyInfo structure */
				parseVerifyInfo();

				/* Find SpLocDma thread */
				spThread = findActiveThread((void *)SpLocDma);

				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "DpcDmaInit",
					dplStartAddr, dplEndAddr, 
					dplEndAddr - dplStartAddr,
					dplRingSize, DONE_STR);
			}  /* if */

			count++;

			/* We alternate between RDRAM and 2 DMEM ring buffers */
			dplRingSource = (dplRingSource == FROM_RDRAM) ?
					FROM_DMEM1 : FROM_RDRAM;

			/* Initialize start pointer and size for ring buffer */
			dplRingStartp = (dplRingSource == FROM_RDRAM) ?
				DpRdramStart : DpDmemStart;
			dplRingStartp &= 0xfffffff8;

			/* 
			 * We randomly pick the ring buffer size to be between 
			 * 4 and 124 32-bit words 
			 */
			dplRingSize = 0;
			while (dplRingSize == 0) {
				dplRingSize = random() & 0x07c;
			}

			/* HACK to use Sp's code to DMA data to CMD FIFO */
			if (dma80Enabled && spThread && 
				((count & 0x3) == 0x3)) {
#ifdef __sgi__  /* K1--BUG */
				data = *(vu_int *)PHYS_TO_K1( SP_STATUS_REG);
#else
				data = (vu_int)(IO_READ( SP_STATUS_REG));
#endif
				if (!(data & SP_STATUS_SIG0)) {
				    dplRingSource = FROM_DMEM2;
				    dplRingStartp = Dp2DmemStart;
				    dplRingSize   = 62; 	/* 248 bytes */
				}
			}

			fprintf(LogFp, RTST_MSG_RES2_STR,
				thread - RandThreadTable, "DpcDmaStart",
				dplRingStartp, dplRingEndp, dplCurrAddr, 
				dplRingSize, "Begin");

			setupRingBuffer();

			/* Read out status */
#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(DPC_STATUS_REG);
#else
			stat = (vu_int)(IO_READ(DPC_STATUS_REG));
#endif

			fprintf(LogFp, RTST_MSG_RES2_STR,
				thread - RandThreadTable, "DpcDmaStart",
				dplRingStartp, dplRingEndp, dplCurrAddr, 
				stat, PASS_STR);

			thread->state = IO_STATE_CHECK_STATUS;
			break;
		}
		case IO_STATE_CHECK_STATUS: {
			/* Make sure that Sp's code has acknowledged SIG0 */
			if (dplRingSource == FROM_DMEM2) {
#ifdef __sgi__  /* K1--BUG */
				spStat = *(vu_int *)PHYS_TO_K1(SP_STATUS_REG);
#else
				spStat = (vu_int )(IO_READ(SP_STATUS_REG));
#endif
		        	if (spStat & SP_STATUS_SIG0) {
				    /* We manually start this thread to hopefully
				     * kick off the DMA from ring buffer to DP
				     */
				    SpLocDma(spThread);
				    break;
				}
			}

			/* Wait until DMA counter is at end */
#ifdef __sgi__  /* K1--BUG */
			data = *(vu_int *)PHYS_TO_K1(DPC_CURRENT_REG);
			stat = *(vu_int *)PHYS_TO_K1(DPC_STATUS_REG);
#else
			data = (vu_int )(IO_READ(DPC_CURRENT_REG));
                        stat = (vu_int )(IO_READ(DPC_STATUS_REG));
#endif

			/* Here, we should check for END_VALID */
			if (data == (dplRingEndp & 0x00ffffff)) {
				if (dplCurrAddr >= dplEndAddr)
					thread->state = IO_STATE_COMPARE;
				else
					thread->state = IO_STATE_START;
			}
			fprintf(LogFp, RTST_MSG_RES2_STR,
				thread - RandThreadTable, "DpcDmaCurrent", 
				DPC_CURRENT_REG, data, dplRingEndp,
				stat, DONE_STR);
			break;
		}
		case IO_STATE_COMPARE: {
#ifdef __sgi__  /* K1--BUG */
			stat = *(vu_int *)PHYS_TO_K1(DPC_STATUS_REG);
#else
			stat = (vu_int )(IO_READ(DPC_STATUS_REG));
#endif
			if (stat != lastStat)
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable,
					"DpcDmaStatus", 
					DPC_STATUS_REG, DPC_STATUS_PIPE_BUSY, 
					1, stat, DONE_STR);
			lastStat = stat;
			if (!(stat & DPC_STATUS_PIPE_BUSY)) {
				if (CheckInterrupt(MI_INTR_DP, &stat)) {
					/* Write to bit[11] of MI_INIT_MODE 
					 * to clear interrupt 
					 */
#ifdef __sgi__  /* K1--BUG */
					*(vu_int *)PHYS_TO_K1(
					MI_INIT_MODE_REG) = MI_CLR_DP_INTR;
#else
				 	IO_WRITE(MI_INIT_MODE_REG, MI_CLR_DP_INTR);
#endif

					fprintf(LogFp, RTST_MSG_RES2_STR,
						thread - RandThreadTable,
						"DpcCheckIntr",
						MI_INTR_REG, MI_INTR_DP,
						0, stat, PASS_STR);
				}
				else {
					fprintf(LogFp, RTST_MSG_RES2_STR,
						thread - RandThreadTable,
						"DpcCheckIntr",
						MI_INTR_REG, MI_INTR_DP,
						0, stat, FAIL_STR);
					errorTotal++;
				}

				done = 1;
				thread->state = IO_STATE_INIT;
				thread->inProgress = 0;
			}
			break;
		}
		default: {
                        _TRACE(DERROR,
                        fprintf(LogFp,
                                "\tERROR: Illegal IO state encountered!\n"));
		}
	}  /* switch */
			 
}  /* DpcRandDma */


/***************************************************************************
 * Memory compare by sampling various 32-bit words in the array
 */
int
MemSmplCompare(int src, int dst, int nwords, int failExpected)
{
	/* Compare 'nwords' bytes between source 'src' and destination 'dst' */
	/* 'nwords' must be even and addresses word-aligned */

        int i;
	unsigned int w1, w2;

	errorCount = 0;

	_TRACE(DCOMPARE, fprintf(LogFp,"\tSRC\t\t\t\tDST\n"));

	src &= 0xfffffffc;	/* Ensure addresses are word-aligned */
	dst &= 0xfffffffc;

	/* Here, we sample 1st word, then every 5th word, then last word 
	 * So, if there are 16 words (64-bytes), then it's a diagonal-line
	 * compare
	 */
	for (i = 0; i < nwords; i += 5) {
#ifdef __sgi__  /* K1--BUG */
                w1 = *(vu_int *)PHYS_TO_K1(src+(i*4));
                w2 = *(vu_int *)PHYS_TO_K1(dst+(i*4));
#else
		w1 = (vu_int )(IO_READ(src+(i*4)));
                w2 = (vu_int )(IO_READ(dst+(i*4)));
#endif

		_TRACE(DCOMPARE, fprintf(LogFp, 
			"\t%08x: %08x\t%08x: %08x\t\n",
			src+(i*4), w1, dst+(i*4), w2));

		if (w1 != w2) {
			errorCount++;
                        _TRACE(DERROR, RANDLOG_ERROR(src+(i*4), w1, 
				dst+(i*4), w2));
			break;
		}
	}
	/* Test the last word */
	if (errorCount == 0) {
#ifdef __sgi__  /* K1--BUG */
        	w1 = *(vu_int *)PHYS_TO_K1(src+((nwords-1)*4));
        	w2 = *(vu_int *)PHYS_TO_K1(dst+((nwords-1)*4));
#else
		w1 = (vu_int )(IO_READ(src+((nwords-1)*4)));
		w2 = (vu_int )(IO_READ(dst+((nwords-1)*4)));
#endif

		_TRACE(DCOMPARE, fprintf(LogFp, 
			"\t%08x: %08x\t%08x: %08x\t\n",
			src+((nwords-1)*4), w1, dst+((nwords-1)*4), w2));

		if (w1 != w2) {
			errorCount++;
			_TRACE(DERROR, RANDLOG_ERROR(src+((nwords-1)*4), w1, 
				dst+((nwords-1)*4), w2));
		}
	}

        errorTotal += errorCount;
        return((errorCount == 0) ? 0 : -1);

}  /* MemSmplCompare */


/***************************************************************************
 * Initialize various IO subsystems (RDRAM, PI, AI)
 */
void
IoRandInit(char *testRange, char *init_file) 
{
	
	extern char	IpcName[];
	unsigned int	data = 0;

	fprintf(LogFp, "\n******** Parameters ********\n");
	fprintf(LogFp, "Ipc name                      = %s\n", IpcName);
	fprintf(LogFp, "Debug flag                    = 0x%08x\n", Dflags);
	fprintf(LogFp, "Log file                      = %s\n", LogFile);
	fprintf(LogFp, "Test range                    = %s\n", 
		(testRange == NULL) ? "all" : testRange);
	fprintf(LogFp, "Random test seed              = %d\n", TestSeed);
	fprintf(LogFp, "Minimum run count per thread  = %d\n", MinRunCount);
	fprintf(LogFp, "Minimum iteration for testing = %d\n", LoopCount);
	fprintf(LogFp, "Data comparison               = %s\n", 
		CompareEnabled ? "on" : "off");
	fprintf(LogFp, "Scheduling method             = %s\n", 
		RoundRobinEnabled ? "round-robin" : "random");
	fprintf(LogFp, "Rdram test size               = 0x%08x\n", 
		RdramTestSize);
	fprintf(LogFp, "I/Dmem test size              = 0x%08x\n\n", 
		IMEM_TEST_SIZE);
	fprintf(LogFp, "Rdram size                    = %d\n", RdramSize);

	MiRdramStart = ((RdramSize-1) * (1024*1024)) - (2*RdramTestSize);
	MiRdramEnd   = MiRdramStart + RdramTestSize;
	fprintf(LogFp, "MI:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		MiRdramStart, MiRdramEnd);

	PiRdramStart = ((RdramSize-1) * (1024*1024)) - 1024;
	PiRdramEnd   = PiRdramStart + (5 *1024);
	fprintf(LogFp, "PI:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		PiRdramStart, PiRdramEnd);

	fprintf(LogFp, "  Rom1:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		PiRomStart[0], PiRomEnd[0]);
	fprintf(LogFp, "  Rom2:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		PiRomStart[1], PiRomEnd[1]);
	fprintf(LogFp, "  WRom1:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		PiWRomStart1, PiWRomEnd1);
	fprintf(LogFp, "  WRom2:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		PiWRomStart2, PiWRomEnd2);
	fprintf(LogFp, "SI:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		SiRdramStart, SiRdramEnd);
	fprintf(LogFp, "  Ram:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		SiRamStart, SiRamEnd);
	fprintf(LogFp, "  Rom:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		SiRomStart, SiRomEnd);
	fprintf(LogFp, "AI:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		AiRdramStart, AiRdramEnd);
	fprintf(LogFp, "SP:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		SpRdramStart, SpRdramEnd);
	fprintf(LogFp, "  Imem:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		SpImemStart, SpImemEnd);
	fprintf(LogFp, "  Dmem:\tStart = 0x%08x\tEnd = 0x%08x\n\n", 
		SpDmemStart, SpDmemEnd);
	fprintf(LogFp, "DP:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		DpRdramStart, DpRdramEnd);
	fprintf(LogFp, "  Dmem:\tStart = 0x%08x\tEnd = 0x%08x\n\n", 
		DpDmemStart, DpDmemEnd);
	fprintf(LogFp, "VI:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		ViRdramStart, ViRdramEnd);
	fprintf(LogFp, "\n******** Parameters ********\n\n");

	fprintf(LogFp, "Initializing random environment...\n");

	/* Config RDRAM ==> DDR */
	InitDDR(0,0,0,0);	
	IO_WRITE(0x04300030, 0x00114245);
        IO_WRITE(0x04300030, 0x00114244);
	CpuWait(0xE80, 0, 0, 0);
	IO_WRITE(0x4300030, 0x02114244);
	BCPInitRand(init_file);

	/* Print out hardware version */
#ifdef __sgi__  /* K1--BUG */
	data = *(vu_int *)PHYS_TO_K1(MI_VERSION_REG);
#else
	data = (vu_int )(IO_READ(MI_VERSION_REG));
#endif
	fprintf(LogFp, "Hardware version = 0x%08x\n", data);

        /* Initialize the Peripheral Interface (PI) controller */
        /* Initial test cause the IO busy bit to always be set? */
	fprintf(LogFp, "Configuring PI Domains...\n");
	/* fprintf(LogFp, RTST_MSG_RES2_STR,
		0, "PiConfigDomain1", 0x80, 0x20, 0x02, 0x03, DONE_STR);
        PiConfigDomain(PI_DOMAIN1_REG, 0x80, 0x20, 0x02, 0x03);

	fprintf(LogFp, RTST_MSG_RES2_STR,
		0, "PiConfigDomain2", 0x80, 0x20, 0x02, 0x03, DONE_STR);
        PiConfigDomain(PI_DOMAIN2_REG, 0x80, 0x20, 0x02, 0x03); */

	/* Init Video Interface (VI) to get proper video clock signal into AI */
	/* t 0102 04400000 00000000 00000000 00000000 */
	fprintf(LogFp, "Initializing VI clock signal...\n");
#ifdef __sgi__  /* K1--BUG */
	*(vu_int *)PHYS_TO_K1(VI_STATUS_REG) = 0;
#else
	IO_WRITE(VI_STATUS_REG, 0);
#endif

	fprintf(LogFp, "Initializing refresh signal...\n");
#ifdef __sgi__  /* K1--BUG */
	*(vu_int *)PHYS_TO_K1(VI_H_SYNC_REG) = 0x130;
#else
	IO_WRITE(VI_H_SYNC_REG, 0x130);
#endif

	/* Config AI: dacRate, bitRate */
	fprintf(LogFp, "Initializing AI...\n");
	/* 
	 * Based on Rambus clock (243.37 MHz) and video clock (48.67 MHz) 
	 * (using cg.v with RAMBUS_CLOCK_PERIOD=4.10897) to get 44.1 KHz 
	 * for audio: DAC rate = 1103 (0x44f), bit rate = 15 (0xf)
	 * For faster sample rate: use DAC rate=0x86, bit rate=0x1
	 */
	if (Ai441Enabled) {
		fprintf(LogFp, RTST_MSG_RES2_STR,
			0, "AiInit", 0x44f, 0x0f, 0x0, 0x0, DONE_STR);
		AiInit(0x44f, 0x0f);	
	}
	else {
		fprintf(LogFp, RTST_MSG_RES2_STR,
			0, "AiInit", 0x86, 0x01, 0x0, 0x0, DONE_STR);
		AiInit(0x86, 0x01);	
	}

	fprintf(LogFp, "Halting SP...\n");
#ifdef __sgi__  /* K1--BUG */
	*(vu_int *)PHYS_TO_K1(SP_STATUS_REG) = SP_SET_HALT;
#else
	IO_WRITE(SP_STATUS_REG, SP_SET_HALT);
#endif

	/* Init D/Imem test area */
	fprintf(LogFp, RTST_MSG_RES2_STR,
		0, "IoSpWrite", SpImemStart, 0x3, 0x0, 0x0, DONE_STR);
	IoSpWrite(SpImemStart, SpImemStart);
	IoSpWrite(SpImemStart+4, SpImemStart+4);
	IoSpWrite(SpImemStart+8, SpImemStart+8);
	IoSpWrite(SpDmemStart, SpDmemStart);
	IoSpWrite(SpDmemStart+4, SpDmemStart+4);
	IoSpWrite(SpDmemStart+8, SpDmemStart+8);

	/* Detect to see which test is loaded in IMEM */
#ifdef __sgi__  /* K1--BUG */
	data = *(vu_int *)PHYS_TO_K1(RDRAM_IMEM_TEST_ID);
#else
	data = (vu_int )(IO_READ(RDRAM_IMEM_TEST_ID));
#endif
	dma80Enabled = ((data & 0xff000000) == 0) ? 1 : 0;
	fprintf(LogFp, "IMEM test dma80 is %s...\n", dma80Enabled ? "on":"off");

	/* Setup all interrupt masks */
	fprintf(LogFp, "Setting all interrupt masks...\n");
#ifdef __sgi__  /* K1--BUG */
	*(vu_int *)PHYS_TO_K1(MI_INTR_MASK_REG) = (MI_INTR_MASK_SET_SP |
		MI_INTR_MASK_SET_SI | MI_INTR_MASK_SET_AI |
		MI_INTR_MASK_SET_VI | MI_INTR_MASK_SET_PI |
		MI_INTR_MASK_SET_DP);
#else
	IO_WRITE(MI_INTR_MASK_REG, (MI_INTR_MASK_SET_SP |
                 MI_INTR_MASK_SET_SI | MI_INTR_MASK_SET_AI |
                 MI_INTR_MASK_SET_VI | MI_INTR_MASK_SET_PI |
                 MI_INTR_MASK_SET_DP));
#endif


  /*  If you define MANUAL_INIT, please port the below part too. XXXX (BCP) */
#ifdef MANUAL_INIT
	/* Now, we initialize RDRAM with some pattern */
	/* Init 1.5KB in MI space, 3KB in AI, 1KB for SP */

	fprintf(LogFp, "Initializing MI region of RDRAM...\n");
	fprintf(LogFp, RTST_MSG_RES2_STR,
		0, "rdramInit", MiRdramStart, 0x300, 0x0, 0x0, DONE_STR);
	rdramInit(MiRdramStart, 0x300, 0, 0);

	fprintf(LogFp, "Initializing SP regions...\n");
	fprintf(LogFp, RTST_MSG_RES2_STR,
		0, "rdramInit", SpRdramStart, 0x300, 0x0, 0x0, DONE_STR);
	rdramInit(SpRdramStart, 0x300, 0x0000000, 0);

	fprintf(LogFp, "Initializing SI region of RDRAM...\n");
	fprintf(LogFp, RTST_MSG_RES2_STR,
		0, "rdramInit", SiRdramStart, 0x300, 0x0, 0x0, DONE_STR);
	rdramInit(SiRdramStart, 0x300, 0x0000000, 0);

	/* DMA from ROM into RDRAM AI region
	 * The first 1024 bytes in ROM are test data, the next 400 bytes 
	 * are audio data
	 */
	fprintf(LogFp, "DMAing data from ROM to AI region of RDRAM...\n");
	fprintf(LogFp, RTST_MSG_RES2_STR,
		0, "IoPiDmaCopy", DIR_TO_RAM, PI_ROM2_START+1024, AiRdramStart, 
		400, DONE_STR);
	IoPiDmaCopy(DIR_TO_RAM, PI_ROM2_START+1024, AiRdramStart, 400-1);
	fprintf(LogFp, RTST_MSG_RES2_STR,
		0, "MemPollStatus", PI_STATUS_REG, PI_STATUS_DMA_BUSY, 
		1, 0, DONE_STR);
	MemPollStatus(PI_STATUS_REG, PI_STATUS_DMA_BUSY, 1, 0);

	/* DMA from ROM into RDRAM/DP Evan's test region
	 * The first 1024 bytes in ROM are test data, the next 400 bytes 
	 * are audio data, the next 28672 bytes are DP test data
	 */
	fprintf(LogFp, "DMAing data from ROM to DP test region of RDRAM...\n");
	fprintf(LogFp, RTST_MSG_RES2_STR,
		0, "IoPiDmaCopy", DIR_TO_RAM, PI_ROM2_START+1024+400, 
		0x00120000, 0x7000, DONE_STR);
	IoPiDmaCopy(DIR_TO_RAM, PI_ROM2_START+1024+400, 0x00120000, 0x7000-1);

	fprintf(LogFp, RTST_MSG_RES2_STR,
		0, "MemPollStatus", PI_STATUS_REG, PI_STATUS_DMA_BUSY, 
		1, 0, DONE_STR);
	MemPollStatus(PI_STATUS_REG, PI_STATUS_DMA_BUSY, 1, 0);
#endif

	fprintf(LogFp, "Initializing random environment completed!\n");

}  /* IoRandInit */


/***************************************************************************
 * Send command to tell verilog server to shutdown
 */
extern int IoCmd(int, int);

void
ShutdownServer(void)
{
	IoCmd(REQ_QUIT, 0);
}


/***************************************************************************
 * Flush VerifyInfo structure to mmap file
 */
void
FlushVerifyInfo(void)
{
	int		i;
	unsigned int	data;

	/* Read-out/write-in VerifyInfo structure for mmap_rdram to work
	 * with pre-loaded RDRAM
	 */
	for (i=0; i < (sizeof(VerifyInfo)/4) + 2; i++) {
#ifdef __sgi__  /* K1--BUG */
		data = *(vu_int *)PHYS_TO_K1(VERIFY_INFO_PHYSADDR+(i*4));
		*(vu_int *)PHYS_TO_K1(VERIFY_INFO_PHYSADDR+(i*4)) = data;
#else
		data = (vu_int )(IO_READ(VERIFY_INFO_PHYSADDR+(i*4)));
                IO_WRITE(VERIFY_INFO_PHYSADDR+(i*4), data);
#endif
	}

}  /* FlushVerifyInfo */


/***************************************************************************
 * Check to see if interrupt is set in MI interrupt register
 * Return 1 if interrupt is set, 0 otherwise.
 */
int
CheckInterrupt(unsigned int intrBit, unsigned int *stat)
{
	unsigned int ret;

#ifdef __sgi__  /* K1--BUG */
	ret = *(vu_int *)PHYS_TO_K1(MI_INTR_REG);
#else
	ret = (vu_int )(IO_READ(MI_INTR_REG));
#endif
	*stat = ret;
	return(ret & intrBit);

}  /* CheckInterrupt */


/***************************************************************************
 * Unblock and kill all children threads
 * Assume that all threads are not busy and have no outstanding transactions
 */
void
KillAllThreads(void)
{
	int	  i;
	ThreadCmd *thread;

	for (i=0; i < ActiveTableSize; i++) {
                thread = ActiveThreadTable[i];
		_TRACE(DLOG, 
			printf("Unblocking and killing thread %d with pid=%d\n",
				thread - RandThreadTable, thread->pid));

		/* Here, we unblock the thread and kill it */
		/* Do we really need to unblock it first? */
		kill(thread->pid, SIGKILL);
	}

}  /* KillAllThreads */


/***************************************************************************
 * Print all entries in active thread table
 */
void
PrintActiveTable(void)
{
	int 	  i, testCount;
	ThreadCmd *thread;

        _TRACE(DLOG, fprintf(LogFp, 
		"\n*************************************************\n"));
        _TRACE(DLOG, fprintf(LogFp, "Thread Table Status:\n"));

	testCount = 0;
        for (i=0; i < ActiveTableSize; i++) {
                thread = ActiveThreadTable[i];
                _TRACE(DLOG, fprintf(LogFp, THREAD_PRINT_MSG,
			thread - RandThreadTable, thread->pid, thread->entry,
			thread->runCount, thread->busy,
			thread->state, thread->inProgress, thread->name));
		testCount += thread->runCount;
        }

        _TRACE(DLOG, fprintf(LogFp,  
		"\n*************************************************\n"));

        fprintf(LogFp, "\n*************************************************\n");
        fprintf(LogFp,
                "\n\tTotal transactions = %d, total error = %d\n",
		testCount, errorTotal);
        fprintf(LogFp, "\n*************************************************\n");

}  /* PrintRandTable */


/***************************************************************************
 * Find and return a specified thread from the active table
 */
ThreadCmd *
findActiveThread(void *entry)
{
	int i;
	for (i = 0; i < ActiveTableSize; i++) {
		if (ActiveThreadTable[i]->entry == entry)
			return(ActiveThreadTable[i]);
	}
	return(NULL);

}  /* findActiveThread */


/***************************************************************************
 * Add a thread to the active table
 */
void
AddActiveTable(int n)
{
	if (n > RandTableSize-1) {
		fprintf (stderr, "ERROR: test %d out of range\n", n);
		exit(1);
	}
	ActiveTableSize++;
	if (ActiveThreadTable == NULL)
	    ActiveThreadTable = malloc(ActiveTableSize * sizeof(ThreadCmd *));
	else
	    ActiveThreadTable = realloc(ActiveThreadTable,
					ActiveTableSize * sizeof(ThreadCmd *));
	ActiveThreadTable[ActiveTableSize-1] = &RandThreadTable[n];
}


/***************************************************************************
/* Create a mapping array of the active table procedure numbers so implement
/* random round robin
 */
void
initRoundRobin()
{
    int i, ArraySize, Pick, Temp;

    ProcessOrder = (int *) malloc(ActiveTableSize * sizeof(int));
    
    for (i=0; i<ActiveTableSize; i++)
	ProcessOrder[i] = i;

    if (RoundRobinEnabled)
	{

	    ArraySize = ActiveTableSize;
    
	    for (i=0; i<ActiveTableSize-1; i++)
		{
		    Pick = random() % ArraySize;

		    Temp = ProcessOrder[ArraySize-1];
		    ProcessOrder[ArraySize-- -1] = ProcessOrder[Pick];
		    ProcessOrder[Pick] = Temp;
		}    
	}
}

/***************************************************************************
 * Expand a range expression into a table of pointers to active tests
 */
void
rangeExpand(char *range)
{
	char   *exp;
	int     n, state, next_state, start, stop, cindx;

	if (range == NULL) {
		for (n = 0; n < RandTableSize; n++)
			AddActiveTable(n);
		return;
	}

	state = 0;
	start = -1;
	stop = -1;
	cindx = 0;

	for (exp = range; *exp != '\0'; exp++) {
		switch (state) {
		case 0: {
			if (isdigit(*exp)) {
			    start = *exp - '0';
			    next_state = 1;
			} else {
		 	    fprintf (stderr,
			       "Syntax Error: character #%d (%c), state = %d\n",
			       cindx, *exp, state);
			    exit (1);
			}
		      break;
		}
		case 1: {
			if (isdigit(*exp)) {
			    start = (start * 10) + (*exp - '0');
			    next_state = 1;
			} else if (*exp == ',') {
			    AddActiveTable(start);
			    start = -1;
			    next_state = 0;
			} else if (*exp == '-') {
			    next_state = 2;
			} else {
			    fprintf (stderr,
			       "Syntax Error: character #%d (%c), state = %d\n",
			       cindx, *exp, state);
			    exit (1);
			}
			break;
		}
		case 2: {
			if (isdigit(*exp)) {
			    stop = *exp - '0';
			    next_state = 3;
			} else {
			    fprintf (stderr,
			       "Syntax Error: character #%d (%c), state = %d\n",
			       cindx, *exp, state);
			    exit (1);
			}
			break;
		}
		case 3: {
			if (isdigit(*exp)) {
			    stop = (stop * 10) + (*exp - '0');
			    next_state = 3;
			} else if (*exp == ',') {
			    if (start >= stop) {
				fprintf (stderr,
				"Syntax Error: in range first number(%d) must be smaller than second(%d)\n",
					start, stop);
				exit (1);
			    }
			    for (n = start; n <= stop; n++)
				AddActiveTable(n);
			    start = stop = -1;
			    next_state = 0;
			} else {
			    fprintf (stderr,
			       "Syntax Error: character #%d (%c), state = %d\n",
			       cindx, *exp, state);
			    exit (1);
			}
			break;
		}
		}
		state = next_state;
		cindx++;
	}

	/*
	 *  Cleanup
	 */

	if (start != -1) {
		if (stop == -1) {
		    AddActiveTable(start);
		} else {
		    if (start >= stop) {
			fprintf (stderr,
				"Syntax Error: in range first number(%d) must be smaller than second(%d)\n",
				start, stop);
			exit (1);
		    }
		    for (n = start; n <= stop; n++)
			AddActiveTable(n);
		}
	}

}  /* rangeExpand */