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

/*************************************************************************
 *
 *  File: randtest.c
 *
 *  This file contains the main routine for the I/O random test.
 *
 *  $Header: /root/leakn64/depot/rf/sw/bbplayer/iosim/src/uiotest.c,v 1.2 2002/05/30 05:52:50 whs Exp $
 *
 */

#include <sys/types.h>		
#include <sys/cachectl.h>
#include <sys/sysmp.h>
#include <sys/mman.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <fcntl.h>
#include <getopt.h>
#include <libelf.h>
#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 */
#include <task.h>

#include "trace.h"
#include "sim.h"
#include "simipc.h"
#include "os.h"
#include "rcp.h"
#include "iotest.h"
#include "sync.h"
#include "verify.h"
#include "viint.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	10
#define IO_STATE_CHECK_MID	11
#define IO_STATE_CHECK_F1	12
#define IO_STATE_CHECK_END	13

/*
 * 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;

/* 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;


unsigned int __osRomType	= 0;


/***************************************************************************
 * Extern declarations
 */
extern int __osDpDeviceBusy(void);
extern int __osSiDeviceBusy(void);
extern int __osPiDeviceBusy(void);


/***************************************************************************
 * 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	SpRandIoRd(ThreadCmd *);
void	SpRandIoWrRd(ThreadCmd *);
void	SpRandDma(ThreadCmd *);
void	SpLocDma(ThreadCmd *);

void	DpcRandDma(ThreadCmd *);
void	SpDummyDma(ThreadCmd *);

/* New test procedures for vi, added by kluster */
void	ViMLF2Dma(ThreadCmd *);
void    ViLAN1Dma(ThreadCmd *);
void    ViLAN2Dma(ThreadCmd *);
void    ViLPN1Dma(ThreadCmd *);
void    ViHAN1Dma(ThreadCmd *);
void    ViHPF2Dma(ThreadCmd *);

void    ViModeTest(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,     ViLAN1Dma,      0, 0, 1, 0,     "ViLAN1Dma"     },
  { /* 17 */     0,     ViLAN2Dma,      0, 0, 1, 0,     "ViLAN2Dma"     },
  { /* 18 */     0,     ViLPN1Dma,      0, 0, 1, 0,     "ViLPN1Dma"     },
  { /* 19 */     0,     ViHAN1Dma,      0, 0, 1, 0,     "ViHAN1Dma"     },
  { /* 20 */     0,     ViHPF2Dma,      0, 0, 1, 0,     "ViHPF2Dma"     },

    /* NTSC_LAN1 */
  { /* 21 */     0,     ViModeTest,     0, 0, 0x1002, 0,     "ViModeTest"    },
    /* NTSC_LAN2 */
  { /* 22 */     0,     ViModeTest,     0, 0, 0x2106, 0,     "ViModeTest"    },
    /* NTSC_LPN1 */
  { /* 23 */     0,     ViModeTest,     0, 0, 0x2000, 0,     "ViModeTest"    },
    /* NTSC_HAN1 */
  { /* 24 */     0,     ViModeTest,     0, 0, 0x200A, 0,     "ViModeTest"    },
    /* NTSC_HPF2 */
  { /* 25 */     0,     ViModeTest,     0, 0, 0x210D, 0,     "ViModeTest"    },
    /* NTSC_LAN1 */
  { /* 26 */     0,     ViModeTest,     0, 0, 0x2002, 0,     "ViModeTest"    },

};

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, ret;
	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 
	 */
	ret =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;

	/* If 2nd RDRAM is enabled, we use this thread to test it */
	if (Rdram1Enabled) {
		if ((address == 0) || 
			(address > (RDRAM_1_START+0x80000+RdramTestSize)))
			address = RDRAM_1_START+0x80000 - 4;
	}
	else {
		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;

        stat = osPiGetStatus();
	if (osPiRawReadIo((u32)address, &data) == 0) {
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, data, 0, 0, DONE_STR);
	}
	else {
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, stat, 0, 0, BUSY_STR);
	}

}  /* PiRandIoRd */


void
PiRandIoWrRd(ThreadCmd *thread)
{
	int			ret;
	unsigned int		stat, data;
	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;

	stat = osPiGetStatus();
	ret = osPiRawWriteIo(address, address);
        if (ret == 0) {
        	while (__osPiDeviceBusy()) ;
		if (osPiRawReadIo(address, &data) == 0) {
                    ret = (data == address) ? 0 : -1;
		    fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, data, 0, 0, INSERT_RESULT(ret));
		    return;
		}
	}
	fprintf(LogFp, RTST_MSG_RES2_STR,
		thread - RandThreadTable, thread->name,
		address, stat, 0, 0, BUSY_STR); 

}  /* PiRandIoWrRd */


void
PiRandDma(ThreadCmd *thread)
{
	int			ret;
	unsigned int		stat;
	static	unsigned int	dir     = OS_WRITE;
	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: {
			thread->inProgress = 1;
			/* Toggle direction */
			dir = (dir==OS_WRITE) ? OS_READ:OS_WRITE;

			/* If DMA to RDRAM, then we use the 2 ROM models
	 		 */
			if (dir == OS_READ) {
				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 = osPiRawStartDma(dir, piAddr, ramAddr, nbytes);
			if (ret != -1) {
			    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: {
			stat = osPiGetStatus();
			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)) {
					IO_WRITE(PI_STATUS_REG, PI_CLR_INTR);
					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;

	stat = osSiGetStatus();
#if 0
	if (RandThreadTable[SI_DMA_ID].inProgress == 0) {
#endif
		if (osSiRawReadIo(address, &ret) == 0) {
			if (ret == data) ret = 0;
			else ret = -1;
			fprintf(LogFp, RTST_MSG_RES2_STR,
				thread - RandThreadTable, thread->name,
				address, data, stat, 0, INSERT_RESULT(ret));
			return;
		}
#if 0
	}
#endif
	fprintf(LogFp, RTST_MSG_RES2_STR,
		thread - RandThreadTable, thread->name,
		address, data, stat, 0, BUSY_STR);

}  /* SiRandIoRd */


void
SiRandIoWrRd(ThreadCmd *thread)
{
	int			ret;
	unsigned int		stat, data;
	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;

	/*
	 * 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.
	 */
	stat = osSiGetStatus();
#if 0
	if (RandThreadTable[SI_DMA_ID].inProgress == 0) {
#endif
		ret = osSiRawWriteIo(address, address);
        	if (ret == 0) {
			while (__osSiDeviceBusy()) ;
			if (osSiRawReadIo(address, &data) == 0) {
				ret = (data == address) ? 0 : -1;
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, thread->name,
					address,data,stat,0,INSERT_RESULT(ret));
		    		return;
			}
		}
#if 0
	}
#endif
	fprintf(LogFp, RTST_MSG_RES2_STR,
		thread - RandThreadTable, thread->name,
		address, address, stat, 0, BUSY_STR);

}  /* SiRandIoWrRd */


void
SiRandDma(ThreadCmd *thread)
{
	int			ret;
	unsigned int		stat;
	static	unsigned int	dir     = OS_WRITE;
	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;
			dir = (dir==OS_WRITE) ? OS_READ : OS_WRITE;
			if ((ramAddr == 0) || 
			    (ramAddr > (SiRdramEnd - (nbytes+4))))
				ramAddr = SiRdramStart - 8;
			ramAddr += 8;
			siAddr = SiRamStart;
			siAddr  &= 0xfffffff8;
			ramAddr &= 0xfffffff8;

			ret = osSiRawStartDma(dir, siAddr, ramAddr, nbytes);
			if (ret != -1) {
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SiTestDma",
					dir, siAddr, ramAddr, nbytes,
					INSERT_RESULT(ret));
				thread->state = IO_STATE_CHECK_STATUS;
			} else {
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable, "SiTestDma",
					dir, 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
			 */
			stat = osSiGetStatus();
			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 */
					IO_WRITE(SI_STATUS_REG, 0);
					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 = osAiSetNextBuffer(ramAddr, nbytes);
			dmaCount++;
			fprintf(LogFp, RTST_MSG_RES2_STR,
				thread - RandThreadTable, "AiTestDma",
				ramAddr, nbytes, 0, 0, INSERT_RESULT(ret));
			stat = osAiGetStatus();
			/* If not full, we start another DMA */
			if (!(stat & AI_STATUS_FIFO_FULL) && 
				(dmaCount < MinRunCount)) {
				ret = osAiSetNextBuffer(ramAddr,nbytes);
				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: {
			stat = osAiGetStatus();
			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 = osAiSetNextBuffer(ramAddr,nbytes);
				dmaCount++;
				fprintf(LogFp, RTST_MSG_RES2_STR,
					thread - RandThreadTable,
					"AiTestDma", ramAddr, 
					nbytes, 0, 0, INSERT_RESULT(ret));
			}
			stat = osAiGetStatus();
			_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 */
					IO_WRITE(AI_STATUS_REG, 0);
					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
 */
static void
dumpViRegs(void)
{
    fprintf(LogFp, "VI Regs:\n");
    fprintf(LogFp, "\tcurrent=0x%08x\n", IO_READ(VI_CURRENT_REG));
    fprintf(LogFp, "\torigin=0x%08x\n", IO_READ(VI_ORIGIN_REG));
    fprintf(LogFp, "\twidth =0x%08x\n", IO_READ(VI_WIDTH_REG));
    fprintf(LogFp, "\tvIntr =0x%08x\n", IO_READ(VI_V_INTR_REG));
    fprintf(LogFp, "\tburst =0x%08x\n", IO_READ(VI_BURST_REG));
    fprintf(LogFp, "\tvSync =0x%08x\n", IO_READ(VI_V_SYNC_REG));
    fprintf(LogFp, "\thSync =0x%08x\n", IO_READ(VI_H_SYNC_REG));
    fprintf(LogFp, "\tleap  =0x%08x\n", IO_READ(VI_LEAP_REG));
    fprintf(LogFp, "\thStart=0x%08x\n", IO_READ(VI_H_START_REG));
    fprintf(LogFp, "\tvStart=0x%08x\n", IO_READ(VI_V_START_REG));
    fprintf(LogFp, "\tvBurst=0x%08x\n", IO_READ(VI_V_BURST_REG));
    fprintf(LogFp, "\txScale=0x%08x\n", IO_READ(VI_X_SCALE_REG));
    fprintf(LogFp, "\tyScale=0x%08x\n", IO_READ(VI_Y_SCALE_REG));
    fprintf(LogFp, "\tctrl  =0x%08x\n", IO_READ(VI_CONTROL_REG));
}

static void
dumpViContext(__OSViContext *vc)
{

    fprintf(LogFp, "VI Context\t= 0x%08x\n", vc);
    fprintf(LogFp, "\tstate\t= 0x%08x\n", vc->state);
    fprintf(LogFp, "\tcontrol\t= 0x%08x\n", vc->control);
    fprintf(LogFp, "\tframep\t= 0x%08x\n", vc->framep);
    fprintf(LogFp, "\tmodep\t= 0x%08x\n", vc->modep);
    fprintf(LogFp, "\tmsgq\t= 0x%08x\n", vc->msgq);
    fprintf(LogFp, "\tmsg\t= 0x%08x\n", vc->msg);
    fprintf(LogFp, "\tCommon Regs:\n");
    fprintf(LogFp, "\tctrl\t= 0x%08x\n", vc->modep->comRegs.ctrl);
    fprintf(LogFp, "\twidth\t= 0x%08x\n", vc->modep->comRegs.width);
    fprintf(LogFp, "\tvCurrent\t= 0x%08x\n", vc->modep->comRegs.vCurrent);
    fprintf(LogFp, "\tburst\t= 0x%08x\n", vc->modep->comRegs.burst);
    fprintf(LogFp, "\tvSync\t= 0x%08x\n", vc->modep->comRegs.vSync);
    fprintf(LogFp, "\thSync\t= 0x%08x\n", vc->modep->comRegs.hSync);
    fprintf(LogFp, "\tleap\t= 0x%08x\n", vc->modep->comRegs.leap);
    fprintf(LogFp, "\thStart\t= 0x%08x\n", vc->modep->comRegs.hStart);
    fprintf(LogFp, "\txScale\t= 0x%08x\n", vc->modep->comRegs.xScale);
    fprintf(LogFp, "\tField #1 Regs:\n");
    fprintf(LogFp, "\torigin\t= 0x%08x\n", vc->modep->fldRegs[0].origin);
    fprintf(LogFp, "\tyScale\t= 0x%08x\n", vc->modep->fldRegs[0].yScale);
    fprintf(LogFp, "\tvStart\t= 0x%08x\n", vc->modep->fldRegs[0].vStart);
    fprintf(LogFp, "\tvBurst\t= 0x%08x\n", vc->modep->fldRegs[0].vBurst);
    fprintf(LogFp, "\tvIntr\t= 0x%08x\n",  vc->modep->fldRegs[0].vIntr);
    fprintf(LogFp, "\tField #2 Regs:\n");
    fprintf(LogFp, "\torigin\t= 0x%08x\n", vc->modep->fldRegs[1].origin);
    fprintf(LogFp, "\tyScale\t= 0x%08x\n", vc->modep->fldRegs[1].yScale);
    fprintf(LogFp, "\tvStart\t= 0x%08x\n", vc->modep->fldRegs[1].vStart);
    fprintf(LogFp, "\tvBurst\t= 0x%08x\n", vc->modep->fldRegs[1].vBurst);
    fprintf(LogFp, "\tvIntr\t= 0x%08x\n",  vc->modep->fldRegs[1].vIntr);
    fprintf(LogFp, "\tX-scale:\n");
    fprintf(LogFp, "\tfactor\t= %d\n", vc->x.factor);
    fprintf(LogFp, "\toffset\t= 0x%04x\n", vc->x.offset);
    fprintf(LogFp, "\tscale\t= 0x%08x\n", vc->x.scale);
    fprintf(LogFp, "\tY-scale:\n");
    fprintf(LogFp, "\tfactor\t= %d\n", vc->y.factor);
    fprintf(LogFp, "\toffset\t= 0x%04x\n", vc->y.offset);
    fprintf(LogFp, "\tscale\t= 0x%08x\n", vc->y.scale);
}


void ViModeTest(ThreadCmd *thread)
{
    /*
     * We use thread->state field to tell us which VI mode to run
     * and the frame buffer size (16-bit or 32-bit)
     */
    int			ret;
    unsigned int	stat, line, oldline;
    unsigned short	field, pixel, mode, scale;
    float		xscale, yscale;
    static unsigned int	frameBufPtr  = 0;
    static int          done;
    __OSViContext	*vc;

    thread->inProgress = 1;
		    
    mode  = thread->state & 0x00FF;
    pixel = thread->state & 0x0100;
    scale = (thread->state & 0xF000) >> 12;
    fprintf(LogFp,
        "---- ViModeTest: starting with mode=%d, pixel=%d, line=%d, scale=%d\n",
        mode, pixel, osViGetCurrentLine(), scale);

    /* We want RDRAM address to be near 2KB boundary */
    /* Advance address by 128 bytes to skip the first field */
#if 0
    frameBufPtr = RDRAM_VI_START + 0x400 + 128;  /* 0x001fd7e0 */

    if (pixel) {
        /* frameBufPtr = RDRAM_VI_START + 0x4000; */
        frameBufPtr = 0x1000;	/* Use tri-small32.rdram */
    } 
    else {
        /* frameBufPtr = RDRAM_VI_START + 0x4000; */
        frameBufPtr = 0x1000;	/* Use tri-small16.rdram */
    }
#else
    frameBufPtr = 0x1000;
#endif

    xscale = (float) 1 / scale;
    yscale = (float) 1 / scale;
    fprintf(LogFp,
        "---- ViModeTest: using x_scale=%2.1f, y_scale=%2.1f\n",
        xscale, yscale);

    osViSetMode(&osViModeTable[mode]);
    osViSetXScale(xscale);
    osViSetYScale(yscale);
    osViSwapBuffer((void *)frameBufPtr);
    dumpViRegs();

    fprintf(LogFp, "---- ViModeTest: current field=%d\n", 
		osViGetCurrentField());
    fprintf(LogFp, "---- ViModeTest: calling __osViSwapContext\n");
    field = osViGetCurrentField();
    __osViSwapContext(field);
    dumpViRegs();

    fprintf(LogFp, "---- ViModeTest: dumping CURRENT context\n");
    dumpViContext(__osViGetCurrentContext());

    fprintf(LogFp, "---- ViModeTest: dumping NEXT context\n");
    dumpViContext(__osViGetNextContext());

    fprintf(LogFp, "---- ViModeTest: finished swapping first context!\n");

    vc = __osViGetCurrentContext();

    /* Loop until we hit line number */
    fprintf(LogFp, 
            "---- ViModeTest: in field=%d, current=%d, looping until line %d\n",
            field, osViGetCurrentLine(), vc->modep->fldRegs[field].vIntr);
    oldline = osViGetCurrentLine();
    while (1) {
        line = osViGetCurrentLine();
        if (line >= vc->modep->fldRegs[field].vIntr)
            break;
        if (line != oldline)
            fprintf(LogFp, "---- ViModeTest: in line=%d\n", line);
        oldline = line;
    }

    /* Check for interrupt */
    if (CheckInterrupt(MI_INTR_VI, &stat)) {
        /* Write to VI_CURRENT_REG to clear interrupt */
        IO_WRITE(VI_CURRENT_REG, 0);
        fprintf(LogFp, "---- ViModeTest: interrupt #1 passed, stat=%d !\n", 
		stat);
    }
    else {
        fprintf(LogFp, "---- ViModeTest: interrupt #1 failed, stat=%d !\n", 
		stat);
    }

    /* Wait until V_SYNC occurs and wraps around */
    oldline = osViGetCurrentLine();
    while (1) {
        line = osViGetCurrentLine();
        if (line < vc->modep->fldRegs[field].vIntr)
            break;
        if (line != oldline)
            fprintf(LogFp, "---- ViModeTest: (v_sync) in line=%d\n", line);
        oldline = line;
    }

    /* Swap context to field #2 */

    fprintf(LogFp, "---- ViModeTest: current field=%d\n", 
		osViGetCurrentField());
    fprintf(LogFp, "---- ViModeTest: calling __osViSwapContext\n");
    field = osViGetCurrentField();
    __osViSwapContext(field);

    dumpViRegs();
    fprintf(LogFp, "---- ViModeTest: dumping CURRENT context\n");
    dumpViContext(__osViGetCurrentContext());
    fprintf(LogFp, "---- ViModeTest: dumping NEXT context\n");
    dumpViContext(__osViGetNextContext());

    vc = __osViGetCurrentContext();

    /* Loop until we hit line number */
    fprintf(LogFp, 
            "---- ViModeTest: in field=%d, current=%d, looping until line %d\n",
            field, osViGetCurrentLine(), vc->modep->fldRegs[field].vIntr);
    oldline = osViGetCurrentLine();
    while (1) {
        line = osViGetCurrentLine();
        if (line >= vc->modep->fldRegs[field].vIntr)
            break;
        if (line != oldline)
            fprintf(LogFp, "---- ViModeTest: in line=%d\n", line);
        oldline = line;
    }


    /* Check for interrupt */
    if (CheckInterrupt(MI_INTR_VI, &stat)) {
        /* Write to VI_CURRENT_REG to clear interrupt */
        IO_WRITE(VI_CURRENT_REG, 0);
        fprintf(LogFp, "---- ViModeTest: interrupt #2 passed, stat=%d !\n", 
		stat);
    }
    else {
        fprintf(LogFp, "---- ViModeTest: interrupt #2 failed, stat=%d !\n", 
		stat);
    }

    fprintf(LogFp, "---- ViModeTest: completed at line=%d (0x%08x)\n", 
            osViGetCurrentLine(), osViGetCurrentLine());

    QuickDeath = 1;

}  /* ViModeTest */


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 = RDRAM_VI_START + 0x400 + 128;  /* 0x001fd7e0 */

	    osViSetMode(&osViModeTable[OS_VI_NTSC_LAF2]);
            osViSwapBuffer((void *)(RDRAM_VI_START + 0x400));
	    dumpViRegs();
	    __osViSwapContext(0);
	    dumpViRegs();
	    dumpViContext(__osViGetCurrentContext());
	    dumpViContext(__osViGetNextContext());

	    dmaCount++;

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

	    thread->state = IO_STATE_CHECK_F0;

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

	    break;
	}
 
	case IO_STATE_CHECK_F0:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

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

	    break;
	}
	case IO_STATE_CHECK_MID:
	{
	    /* Wait until DMA counter is back at beginning  */
	    data = osViGetCurrentLine();

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

	    dumpViRegs();
	    dumpViContext(__osViGetCurrentContext());
	    dumpViContext(__osViGetNextContext());
	    __osViSwapContext(1);
	    dumpViRegs();
	    dumpViContext(__osViGetCurrentContext());
	    dumpViContext(__osViGetNextContext());

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

	    break;		    
	}
	case IO_STATE_CHECK_F1:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

	    break;
	}
	case IO_STATE_CHECK_END:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

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

	    break;
	}

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

} /* ViMLF2Dma */

/* 5 new vi tests */

/* void ViMLN1Dma(ThreadCmd *thread) */
void ViLAN1Dma(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);

	    ret = 0;
	    thread->inProgress = 1;

	    ramAddr = RDRAM_VI_START + 0x400;
		    
	    osViSetMode(&osViModeTable[OS_VI_NTSC_LAN1]);
	    osViSwapBuffer((void *)ramAddr);
	    __osViSwapContext(0);
	    dumpViRegs();
	    dumpViContext(__osViGetCurrentContext());
	    dumpViContext(__osViGetNextContext());

	    dmacount++;

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

	    thread->state = IO_STATE_CHECK_F0;

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

	    break;
	}
 
	case IO_STATE_CHECK_F0:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

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

	    break;
	}
	case IO_STATE_CHECK_MID:
	{
	    /* Wait until DMA counter is back at beginning  */
	    data = osViGetCurrentLine();

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

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

	    break;		    
	}
	case IO_STATE_CHECK_F1:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

	    break;
	}
	case IO_STATE_CHECK_END:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

	    if (data < 0x00000028)
		{
		    thread->state = IO_STATE_CHECK_F0;
		    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 */

} /* ViLAN1Dma */


void ViLAN2Dma(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 = RDRAM_VI_START + 0xd00;

	    osViSetMode(&osViModeTable[OS_VI_NTSC_LAN2]);
	    osViSwapBuffer((void *)ramAddr);
	    __osViSwapContext(0);
		    
	    dmacount++;

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

	    thread->state = IO_STATE_CHECK_F0;

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

	    break;
	}
 
	case IO_STATE_CHECK_F0:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

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

	    break;
	}
	case IO_STATE_CHECK_MID:
	{
	    /* Wait until DMA counter is back at beginning  */
	    data = osViGetCurrentLine();

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

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

	    break;		    
	}
	case IO_STATE_CHECK_F1:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

	    break;
	}
	case IO_STATE_CHECK_END:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

	    if (data < 0x00000028)
		{
		    thread->state = IO_STATE_CHECK_F0;
		    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 */

} /* ViLAN2Dma */


void ViLPN1Dma(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 = RDRAM_VI_START + 0x1d00;

	    osViSetMode(&osViModeTable[OS_VI_NTSC_LPN1]);
	    osViSwapBuffer((void *)ramAddr);
	    __osViSwapContext(0);
		    
	    dmacount++;

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

	    thread->state = IO_STATE_CHECK_F0;

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

	    break;
	}
 
	case IO_STATE_CHECK_F0:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

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

	    break;
	}
	case IO_STATE_CHECK_MID:
	{
	    /* Wait until DMA counter is back at beginning  */
	    data = osViGetCurrentLine();

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

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

	    break;		    
	}
	case IO_STATE_CHECK_F1:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

	    break;
	}
	case IO_STATE_CHECK_END:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

	    if (data < 0x00000028)
		{
		    thread->state = IO_STATE_CHECK_F0;
		    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 */

} /* ViLPN1Dma */


void ViHAN1Dma(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 = RDRAM_VI_START + 0x27d0;

	    osViSetMode(&osViModeTable[OS_VI_NTSC_HAN1]);
	    osViSwapBuffer((void *)ramAddr);
	    __osViSwapContext(0);
		    
	    dmacount++;

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

	    thread->state = IO_STATE_CHECK_F0;

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

	    break;
	}
 
	case IO_STATE_CHECK_F0:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

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

	    break;
	}
	case IO_STATE_CHECK_MID:
	{
	    /* Wait until DMA counter is back at beginning  */
	    data = osViGetCurrentLine();

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

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

	    break;		    
	}
	case IO_STATE_CHECK_F1:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

	    break;
	}
	case IO_STATE_CHECK_END:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

	    if (data < 0x00000028)
		{
		    thread->state = IO_STATE_CHECK_F0;
		    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 */

} /* ViHAN1Dma */


void ViHPF2Dma(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 = RDRAM_VI_START + 0x3120;
 
	    osViSetMode(&osViModeTable[OS_VI_NTSC_HPF2]);
	    osViSwapBuffer((void *)ramAddr);
	    __osViSwapContext(0);
		    
	    dmacount++;

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

	    thread->state = IO_STATE_CHECK_F0;

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

	    break;
	}
 
	case IO_STATE_CHECK_F0:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

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

	    break;
	}
	case IO_STATE_CHECK_MID:
	{
	    /* Wait until DMA counter is back at beginning  */
	    data = osViGetCurrentLine();

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

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

	    break;		    
	}
	case IO_STATE_CHECK_F1:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

	    break;
	}
	case IO_STATE_CHECK_END:
	{
	    /* Wait until DMA counter is at end */
	    data = osViGetCurrentLine();

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

	    if (data < 0x00000028)
		{
		    thread->state = IO_STATE_CHECK_F0;
		    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 */

} /* ViHPF2Dma */


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

	sem = IO_READ(SP_SEMAPHORE_REG);

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

static void
releaseSpSemaphore(void)
{
	IO_WRITE(SP_SEMAPHORE_REG, 0);
}

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
	 */
	stat = osSpGetStatus();
	if (((address == SpImemStart) && !(stat & SP_STATUS_HALT))) {
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, data, 0, stat, BUSY_STR);
		return;
	}
	if (osSpRawReadIo(address, &data) == -1) {
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, data, 0, stat, BUSY_STR);
	}
	else {
		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, data;
	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;

	stat = osSpGetStatus();
	if (((address == SpImemStart+8) && !(stat & SP_STATUS_HALT))) {
		fprintf(LogFp, RTST_MSG_RES2_STR,
			thread - RandThreadTable, thread->name,
			address, address, 0, 0, BUSY_STR);
		return;
	}
	ret = osSpRawWriteIo(address, address);
	if (ret == 0) {
		if (osSpRawReadIo(address, &data) == 0) {
			if (data != address)
				ret = -1;
			fprintf(LogFp, RTST_MSG_RES2_STR,
				thread - RandThreadTable, thread->name,
				address, address, 0, stat, INSERT_RESULT(ret));
			return;
		}
	}
	fprintf(LogFp, RTST_MSG_RES2_STR,
		thread - RandThreadTable, thread->name,
		address, address, 0, 0, BUSY_STR);

}  /* SpRandIoWrRd */


void
SpRandDma(ThreadCmd *thread)
{
	int			i, ret;
	unsigned int		stat;
	static	unsigned int	skip, count, nbytes;
	static	unsigned int	dir      = OS_READ;
	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==OS_READ) ? OS_WRITE : OS_READ;

			/* 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 = OS_WRITE; 
			}
			ramAddr = (dir == OS_WRITE) ? 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 */
			stat = osSpGetStatus();
			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 = osSpRawStartDma(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: {
			stat = osSpGetStatus();
			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      = OS_READ;
	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 = OS_READ;

			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 */
			stat = osSpGetStatus();
			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 = osSpRawStartDma(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: {
			stat = osSpGetStatus();
			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
			 */

			stat = osSpGetStatus();
			/* Make sure that SP is halted before setting PC */
			if (stat & SP_STATUS_HALT) {
				if (!init) {
					/* Set the SP PC to 0 */
				    if (dma80Enabled) {
					osSpSetPc(0);
					/* This is a hack to fix the problem
					 * with exception handler executing 
					 * the else case
					 */
				        init = 1;
				    }
			            else {	
					osSpSetPc(0xb24);
				    }
				    init = 1;
				}

				/* Clear halt bit to kick off SP test */
				osSpSetStatus(SP_CLR_HALT);
				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: {
			stat = osSpGetStatus();
			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] */
				    osSpSetStatus(
					(SP_CLR_BROKE | SP_CLR_SIG1 | 
					 SP_CLR_SIG2 | SP_CLR_SIG3)); 
			    }
			    else {
				/* Check for failed test */
				if (stat & SP_STATUS_SIG1) {
				    /* Get test file from RDRAM_IMEM_TEST_ID */
				    testFile = IO_READ(RDRAM_IMEM_TEST_ID);
				    testFile = (testFile & 0x000000ff) >> 3;
				    /* Get test number from DMEM address 0 */
				    osSpRawReadIo( SP_DMEM_START, &testNum);
				    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 */
				    osSpSetStatus(SP_CLR_BROKE | SP_CLR_SIG1);
			    }  /* 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) {
		IO_WRITE(DPC_STATUS_REG, DPC_CLR_XBUS_DMEM_DMA);
		/* 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 {
		IO_WRITE(DPC_STATUS_REG, DPC_SET_XBUS_DMEM_DMA);
		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__
	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
        {
            VerifyInfo tmp;
            int 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+offset+4) ;
        }
#endif
	
        fprintf(LogFp, "VerifyInfo: start=0x%x, count=%d\n", 
		dplStartAddr, count);
	/*
	* 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;
#ifdef _NOUIO
	initDpXbusChannel(dplRingSource);
#endif

	/* Copy from DP list to ring buffer */
	i = 0;
	while ((i < dplRingSize) && 
		(dplCurrAddr < dplEndAddr)) {
		data = IO_READ(dplCurrAddr);
		if (dplRingSource != FROM_RDRAM) {
			osSpRawWriteIo(dplRingStartp+(i*4), data);
		}
		else
			IO_WRITE((dplRingStartp+(i*4)), data);

		dplCurrAddr += 4;
		i++;
	}

	if (dplRingSource == FROM_DMEM2) {	/* Use Evan's test */

		initDpXbusChannel(1);

		/* Fill remaining space with 0 (noop) */
		while (i < dplRingSize) {
			osSpRawWriteIo(dplRingStartp+(i*4), 0);
			i++;
		}
		/* Set sig0 in SP to tell SP that it can perform
		 * DMA to DPC
		 */
		osSpSetStatus(SP_SET_SIG0);
		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;

#ifdef _NOUIO
		/* Write start & end addresses */
		IO_WRITE(DPC_START_REG, dplRingStartp);

		/*
		 * 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) {
			IO_WRITE(DPC_END_REG, dplRingEndp-8);
		}

		/* Reset end pointer */
		IO_WRITE(DPC_END_REG, dplRingEndp);
#else
		osDpSetNextBuffer(dplRingStartp, (i*4));
#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)) {
				data = osSpGetStatus();
				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 */
			stat = osDpGetStatus();

			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) {
				spStat = osSpGetStatus();
		        	if (spStat & SP_STATUS_SIG0) {
				    /* We manually start this thread to 
				     * kick off the DMA from ring buffer to DP
				     */
				    SpLocDma(spThread);
				    break;
				}
			}

			/* Wait until DMA counter is at end */
			data = IO_READ(DPC_CURRENT_REG);
			stat = osDpGetStatus();

			/* 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: {
			stat = osDpGetStatus();
			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 
					 */
					IO_WRITE(MI_INIT_MODE_REG, 
						 MI_CLR_DP_INTR);

					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) {
                w1 = IO_READ(src+(i*4));
                w2 = IO_READ(dst+(i*4));

		_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) {
        	w1 = IO_READ(src+((nwords-1)*4));
        	w2 = IO_READ(dst+((nwords-1)*4));

		_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) 
{
	
	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, "MI:\tStart = 0x%08x\tEnd = 0x%08x\n", 
		MiRdramStart, MiRdramEnd);
	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 */
	fprintf(LogFp, RTST_MSG_RES2_STR,
		0, "RdramInit", Rdram1Enabled, 0, 0, 0, DONE_STR);
	RdramInit(Rdram1Enabled);

	/* Print out hardware version */
	data = IO_READ(MI_VERSION_REG);
	fprintf(LogFp, "Hardware version = 0x%08x\n", data);

#if 0
        /* 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);
#endif

	/* 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");
	IO_WRITE(VI_STATUS_REG, 0);

	fprintf(LogFp, "Initializing refresh signal...\n");
	IO_WRITE(VI_H_SYNC_REG, 0x130);

        /* Init VI internal structures and set default mode */
	fprintf(LogFp, "Initializing VI default mode...\n");

	__osViInit();
	dumpViRegs();

#if 0
	/* 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) {
		/* AiInit(0x44f, 0x0f);	*/
		data = osAiSetFrequency(44100);	
		fprintf(LogFp, RTST_MSG_RES2_STR,
			0, "osAiSetFrequency", 0x44f,0x0f,44100,data,DONE_STR);
	}
	else {
		/* AiInit(0x86, 0x01);	*/
		data = osAiSetFrequency(5000);	
		fprintf(LogFp, RTST_MSG_RES2_STR,
			0, "osAiSetFrequency", 0x86,0x01,5000,data,DONE_STR);
	}

	fprintf(LogFp, "Halting SP...\n");
	osSpSetStatus(SP_SET_HALT);

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

	/* Detect to see which test is loaded in IMEM */
	data = IO_READ(RDRAM_IMEM_TEST_ID);
	dma80Enabled = ((data & 0xff000000) == 0) ? 1 : 0;
	fprintf(LogFp, "IMEM test dma80 is %s...\n", dma80Enabled ? "on":"off");
#endif
	/* Setup all interrupt masks */
	fprintf(LogFp, "Setting all interrupt masks...\n");
	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));

	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++) {
		data = IO_READ(VERIFY_INFO_PHYSADDR+(i*4));
		IO_WRITE(VERIFY_INFO_PHYSADDR+(i*4), data);
	}

}  /* 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;

	ret = IO_READ(MI_INTR_REG);
	*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 */