mi_tests.v 69.4 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
// mi_tests.v v1 Frank Berndt
// mi tests;
// :set tabstop=4

`timescale 1ns/1ns

// definitions for address space support;

`define	MI_MEM_X64L			// lower x64 space (0x01xxxxxx) is implemented;
`define	MI_MEM_X64H			// upper x64 space (0x8xxxxxxx) is implemented;

`define	MI_NA_BROM		14		// # of addr bits on brom;
`define	MI_NA_BRAM		16		// # of addr bits on brom;
`define	MI_NA_IRAM		15		// # of addr bits on brom;
`define	MI_NA_VMEM0		6		// # of addr bits on v0;
`define	MI_NA_VMEM1		6		// # of addr bits on v1;
`define	MI_NA_VMEM2		8		// # of addr bits on v2;
`define	MI_NA_REGS		20		// # of addr bits in register spaces;
`define	MI_NA_RDCFG		24		// # of addr bits in RDRAM cfg space;

`define	MI_N_SUBBLK		8		// # of subblock order random tests;
`define	MI_N_BLKERR		8		// # of random block error tests;
`define	MI_N_MEMRAND	256		// # of random memory tests;

// define MI_ONE_MCFG to run with only one random ddr configuration;

// copy of rom content;
// file is reset every time cpu comes out of reset;

reg [31:0] mi_brom_data [0:4095];
reg [31:0] mi_boot_word;

// pattern seeds for virage cachelines;

reg [31:0] mi_vpat [0:15];

// setup brom_data from initialization file;

initial
	$readmemh("tests/brom.dat", mi_brom_data);

// wire hooks into rtl or gate-level;

	wire [2:0] bb_v_porst;

`ifdef	SIMGATE
	assign bb_v_porst = { vsim.bb.v_porst_2_, vsim.bb.v_porst_1_, vsim.bb.v_porst_0_ };
`else	// SIMGATE
	assign bb_v_porst = vsim.bb.v_porst;
`endif	// SIMGATE

// monitor virage charge-pump porst;

reg [2:0] mi_porst;

initial
	mi_porst = 0;
always @(negedge bb_v_porst[0])
	mi_porst[0] = 1;
always @(negedge bb_v_porst[1])
	mi_porst[1] = 1;
always @(negedge bb_v_porst[2])
	mi_porst[2] = 1;

// time of button push for button tests;

reg [63:0] mi_but_time;

// setup mi cbus request monitor;

task mi_cbus_mon;
	input write;		// cbus writes ok;
	input read;			// cbus reads ok;
	input dma;			// cbus dma ok;
	begin
		vsim.cbus_mon.mi_req.write_ok = write;
		vsim.cbus_mon.mi_req.read_ok = read;
		vsim.cbus_mon.mi_req.dma_ok = dma;
	end
endtask

// check state of reset;

task mi_check_rst;
	input cold;			// expected cold reset;
	input warm;			// expected warm reset;
	input [2:0] div;	// expected divide mode;
	begin
		if(coldrst_l !== cold)
			$display("ERROR: %t: %M: coldrst_l %b exp %b", $time, coldrst_l, cold);
		if(warmrst_l !== warm)
			$display("ERROR: %t: %M: warmrst_l %b exp %b", $time, warmrst_l, warm);
		if(divmode !== div)
			$display("ERROR: %t: %M: divmode %b exp %b", $time, divmode, div);
	end
endtask

// check state of interrupt;

task mi_check_intr;
	input [2:0] intr;	// which intr bit to check;
	input val;			// expected interrupt;
	reg xintr;
	begin
		xintr = int_l >> intr;
		if(xintr !== val)
			$display("ERROR: %t: %M: int_l[%0d] %b exp %b", $time, intr, xintr, val);
	end
endtask

// check state of nmi;

task mi_check_nmi;
	input val;			// expected nmi;
	input [2:0] spc;	// nmi latency in hardware;
	begin
		req_spacing(spc);
		if(nmi_l !== val)
			$display("ERROR: %t: %M: nmi_l %b exp %b", $time, nmi_l, val);
	end
endtask

// check state of secure mode;

task mi_check_secure;
	input val;
	reg secure;
	begin
		secure = vsim.bb.bcp.mi.secure;
		if(secure !== val)
			$display("ERROR: %t: %M: secure mode %b exp %b", $time, secure, val);
	end
endtask

// check secure timer enable;

task mi_check_sec_timer;
	input val;
	reg stim_on;
	begin
		stim_on = vsim.bb.bcp.mi.stim_on;
		if(stim_on !== val)
			$display("ERROR: %t: %M: secure timer %b exp %b", $time, stim_on, val);
	end
endtask

// check status of ai/vi reset;

task mi_avrst_check;
	input exp;			// expected value;
	reg rst;			// signal to check;
	begin
		rst = vsim.bb.bcp.ai.reset_l;
		if(rst !== exp)
			$display("ERROR: %t: %M: ai.reset_l %b exp %b", $time, rst, exp);
		rst = vsim.bb.bcp.ai.reset_l;
		if(rst !== exp)
			$display("ERROR: %t: %M: vi.reset_l %b exp %b", $time, rst, exp);
	end
endtask

// test mi defaults after reset;

task mi_after_reset;
	input [2:0] div;		// expected divmode;
	reg [31:0] wdata;		// write data;
	reg [31:0] rdata;		// read data;
	reg [31:0] xdata;		// other data;
	reg [31:0] exp;			// expected data;
	begin
		// boot word must be from brom after reset;

		mi_boot_word = mi_brom_data[0];

		// test interrupt lines;
		// INTR_ERR may be active;

		mi_check_intr(`INTR_RCP, 1);
		mi_check_intr(`INTR_DEV, 1);
		mi_check_intr(`INTR_BUT, 1);
		mi_check_intr(`INTR_RSVD, 1);

		// test compatible registers;

		$display("test: %M: MI_MODE");
		sread(`MI_MODE, `CPU_SIZE_4, rdata);
		exp = 32'd0;
		rd_check(0, exp, 32'hffff_ffff, 0);

		$display("test: %M: MI_VERSION");
		sread(`MI_VERSION, `CPU_SIZE_4, rdata);
		exp = 32'h02_02_b0_b0;
		rd_check(0, exp, 32'hffff_ffff, 0);

		$display("test: %M: MI_INTR");
		sread(`MI_INTR, `CPU_SIZE_4, rdata);
		exp = 32'd0;
		rd_check(0, exp, 32'hffff_ffff, 0);

		$display("test: %M: MI_MASK");
		sread(`MI_MASK, `CPU_SIZE_4, rdata);
		exp = 32'd0;
		rd_check(0, exp, 32'hffff_ffff, 0);

		// test new registers;

		$display("test: %M: MI_CTRL");
		sread(`MI_CTRL, `CPU_SIZE_4, rdata);
		exp = { 12'd0, 7'b0000000, 2'b00, div, 8'd0 };
		rd_check(0, exp, 32'hffff_ffff, 0);

		// reset armed the secure mode trigger, but secure mode
		// is not on yet, nmi must not be asserted;
		// single fetch from 1fc0_0000 turns on secure mode;

		$display("test: %M: MI_SEC_MODE");
		mi_sec_mode_enter(1);
		sread(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
		exp = { 5'd0, 3'd0, 16'd0, 8'b0000_0011 };
		rd_check(0, exp, 32'hffff_ffff, 0);

		// leave secure timer off;
		// check that timer value and count are the same;

		$display("test: %M: MI_SEC_TIMER");
		mi_check_sec_timer(0);
		wdata[31:16] = 16'd0;
		wdata[15:0] = $random;
		swrite(`MI_SEC_TIMER, `CPU_SIZE_4, wdata);
		sread(`MI_SEC_TIMER, `CPU_SIZE_4, rdata);
		exp = { wdata[15:0], wdata[15:0] };
		rd_check(0, exp, 32'hffff_ffff, 0);
		mi_check_sec_timer(0);

		// error info register;

		$display("test: %M: MI_ERR_INFO");
		sread(`MI_ERR_INFO, `CPU_SIZE_4, rdata);
		exp = 32'd0;
		rd_check(0, exp, 32'hffff_fff0, 0);

		// random bit register;

		$display("test: %M: MI_RANDOM");
		sread(`MI_RANDOM, `CPU_SIZE_4, rdata);
		exp = 32'd0;
		rd_check(0, exp, 32'hffff_fffe, 0);

		// video control register;

		$display("test: %M: MI_AVCTRL");
		mi_avrst_check(0);
		sread(`MI_AVCTRL, `CPU_SIZE_4, rdata);
		exp = { 6'd0, 26'h010_0003 };
		rd_check(0, exp, 32'h03ff_ffff, 0);

		// test extended intr register;

		$display("test: %M: MI_EINTR");
		sread(`MI_EINTR, `CPU_SIZE_4, rdata);
		exp = 32'd0;
		exp[25] = vsim.fl_md;
		exp[24] = vsim.button;
		rd_check(0, exp, 32'hf8ff_ffff, 0);

		// test extended mask register;

		$display("test: %M: MI_EMASK");
		sread(`MI_EMASK, `CPU_SIZE_4, rdata);
		exp = 32'd0;
		rd_check(0, exp, 32'hffff_ffff, 0);

		// virage time base;
		// check that divider decrements;

		$display("test: %M: MI_SEC_VTIME");
		sread(`MI_SEC_VTIME, `CPU_SIZE_4, rdata);
		exp = 32'd62;
		rd_check(0, exp, 32'h0000_00ff, 0);

		// test virage control registers;
		// test virage nms registers;

		vx_after_reset(0);
		vx_after_reset(1);
		vx_after_reset(2);
	end
endtask

// test virage time base divider;
// give mi 8 sysclk per read;
// test does not work if divider is smaller than two cpu reads;

task mi_sec_vtime;
	input [7:0] div;		// expected divider;
	reg [31:0] wdata;		// write data;
	reg [31:0] rdata;		// read data;
	reg [31:0] xdata;		// second read data;
	integer n;
	begin
		// write to timer register;
		// this shouls restart the timer clock;

		$display("test: %M: MI_SEC_VTIME divide %0d", div);
		wdata = { 24'bx, div };
		swrite(`MI_SEC_VTIME, `CPU_SIZE_4, wdata);
		sread(`MI_SEC_VTIME, `CPU_SIZE_4, rdata);
		rd_check(0, wdata, 32'h0000_00ff, 0);
		if(rdata[16] !== 1'b0)
			$display("ERROR: %t: %M: vtimer clock %b exp 0", $time, rdata[16]);

		// wait for timer clock to change to 1;

		for(n = div / 8; n > 0; n = n - 1) begin
			sread(`MI_SEC_VTIME, `CPU_SIZE_4, xdata);
			rd_check(0, wdata, 32'h0000_00ff, 0);
			if(rdata[15:8] === xdata[15:8])
				$display("ERROR: %t: %M: vtimer not decrementing, 0x%h", $time, xdata);
			if(xdata[16] === 1'b1)
				n = -1;
		end
		if(n == 0)
			$display("ERROR: %t: %M: vtimer clock %b exp 1", $time, xdata[16]);
	end
endtask

// test mi video control register;

task mi_avctrl;
	reg [31:0] rdata;		// read data;
	reg [31:0] exp;			// expected data;
	begin
		// take av out of reset;

		$display("test: %M: MI_AVCTRL, rw");
		exp = { 6'bx, 26'h2aa_aaaa };
		swrite(`MI_AVCTRL, `CPU_SIZE_4, exp);
		sread(`MI_AVCTRL, `CPU_SIZE_4, rdata);
		rd_check(0, exp, 32'h03ff_ffff, 0);
		mi_avrst_check(1);

		// put av back into reset;

		$display("test: %M: MI_AVCTRL, rw");
		exp = { 6'bx, 26'h155_5555 };
		swrite(`MI_AVCTRL, `CPU_SIZE_4, exp);
		sread(`MI_AVCTRL, `CPU_SIZE_4, rdata);
		rd_check(0, exp, 32'h03ff_ffff, 0);
		mi_avrst_check(0);
	end
endtask


// test mi control register;

task mi_ctrl;
	reg [31:0] wdata;		// write data;
	reg [2:0] div;			// new divmode;
	begin
		// test rw bits;

		test_reg(`MI_CTRL, 32'h000f_e000, 32'hffff_e7ff, 32'd0);
		div = 3'b010;

		// test soft reset;

		$display("test: %M: soft reset");
		wdata = { 12'd0, 7'b0000000, 2'b10, 3'bxxx, 8'd0 };
		swrite(`MI_CTRL, `CPU_SIZE_4, wdata);
		swrite(`MI_CTRL, `CPU_SIZE_4, wdata);
		mi_check_rst(1, 0, 3'b000);
		wait_out_of_reset;
		mi_after_reset(3'b000);

		// test hard reset;

		$display("test: %M: hard reset");
		wdata = { 12'd0, 7'b0000000, 2'bx1, div, 8'd0 };
		swrite(`MI_CTRL, `CPU_SIZE_4, wdata);
		swrite(`MI_CTRL, `CPU_SIZE_4, wdata);
		mi_check_rst(0, 0, div);
		wait_out_of_reset;
		mi_after_reset(div);

	end
endtask

// test interrupt mask register;
// word sfor both MI_MASK or MI_EMASK;

task mi_intr_mask;
	input [31:0] addr;		// register address;
	input [4:0] nmask;		// # of mask bits to test;
	reg [31:0] wdata;		// write data;
	reg [31:0] mask;		// expected intr mask;
	reg [31:0] rdata;		// read data;
	reg [4:0] n;
	begin
		// clear all bits at once;

		$display("test: %M: addr 0x%h", addr);
		$display("test: %M: clear all");
		wdata = 32'h5555_5555;
		swrite(addr, `CPU_SIZE_4, wdata);
		sread(addr, `CPU_SIZE_4, rdata);
		rd_check(0, 32'd0, 32'hffff_ffff, 0);

		// set each bit, one by one;

		mask = 0;
		for(n = 0; n <= 13; n = n + 1) begin
			$display("test: %M: set mask %0d", n);
			wdata = 1 << ((n * 2) + 1);
			swrite(addr, `CPU_SIZE_4, wdata);
			if(n < nmask)
				mask = mask | (1 << n);
			sread(addr, `CPU_SIZE_4, rdata);
			rd_check(0, mask, 32'hffff_ffff, 0);
		end

		// clear all mask bits, on by one;

		for(n = 0; n <= 13; n = n + 1) begin
			$display("test: %M: clear mask %0d", n);
			wdata = 1 << (n * 2);
			swrite(addr, `CPU_SIZE_4, wdata);
			if(n < nmask)
				mask = mask & ~(1 << n);
			sread(addr, `CPU_SIZE_4, rdata);
			rd_check(0, mask, 32'hffff_ffff, 0);
		end

		// set all mask bits at once;
		// test that all 0s and all 1s do not change mask;

		$display("test: %M: no modification");
		wdata = 32'h0aaa_aaaa;
		swrite(addr, `CPU_SIZE_4, wdata);
		swrite(addr, `CPU_SIZE_4, 32'h0000_0000);
		swrite(addr, `CPU_SIZE_4, 32'hffff_ffff);
		sread(addr, `CPU_SIZE_4, rdata);
		mask = 32'hffff_ffff >> (32 - nmask);
		rd_check(0, mask, 32'hffff_ffff, 0);
	end
endtask

// check kick of secure mode;

task mi_sec_mode_kick;
	input exp_nmi;			// expected nmi;
	reg [31:0] rdata;		// read data;
	begin
		$display("%t: %M", $time);
		mi_check_nmi(exp_nmi, 0);
		mi_check_secure(0);
		sread(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
		rd_check(0, 32'd0, 32'hffff_ffff, 0);
		mi_check_nmi(0, 0);
		mi_check_secure(0);
	end
endtask

// check entering of secure mode;

task mi_sec_mode_enter;
	input exp_nmi;			// expected nmi;
	reg [31:0] rdata;		// read data;
	begin
		// nmi should be as expected, but secure mode not on yet;
		// single read from 1fc0_0000 should turn on secure mode;
		// read must return first word from boot space;

		$display("%t: %M", $time);
		mi_check_nmi(exp_nmi, 0);
		mi_check_secure(0);
		sread(`BASE_BOOT, `CPU_SIZE_4, rdata);
		rd_check(0, mi_boot_word, 32'hffff_ffff, 0);
		mi_check_nmi(exp_nmi, 0);
		mi_check_secure(1);
	end
endtask

// set boot word;

task mi_set_boot_word;
	input reset;
	begin
		if(reset)
			mi_boot_word = mi_brom_data[0];
		else
			sread(`MI_BRAM_FLIP, `CPU_SIZE_4, mi_boot_word);
	end
endtask

// check leaving of secure mode;

task mi_sec_mode_leave;
	reg [31:0] mode;		// secure mode data;
	begin
		// nmi should be deasserted after leaving;
		// do not touch IRAM_EN, SBUT_EN and RESET bits;
		// update boot word based on RESET bit;

		$display("%t: %M", $time);
		sread(`MI_SEC_MODE, `CPU_SIZE_4, mode);
		mode[7:2] = 6'd0;
		mode[0] = 0;
		mi_set_boot_word(mode[1]);
		swrite(`MI_SEC_MODE, `CPU_SIZE_4, mode);
		mi_check_nmi(1, 2);
		mi_check_secure(0);
	end
endtask

// check software assertion of nmi;

task mi_check_nmi_force;
	input [7:2] wdata;		// secure mode reg write data;
	reg [31:0] rdata;		// read data;
	reg [31:0] exp;			// expected data;
	reg exp_nmi;			// expect a nmi;
	begin
`ifdef	SIMGATE
		$display("test: %M: Warning: not done on gate-level");
`else	// SIMGATE
		$display("test: %M: 0x%h", wdata);
		exp_nmi = ~|wdata;
		vsim.bb.bcp.mi.sec_mode[7:2] = wdata;
		swrite(`MI_SEC_MODE, `CPU_SIZE_4, { 5'bx, 3'd0, 16'bx, 6'b111111, 2'b01 });
		mi_check_nmi(exp_nmi, 2);
		sread(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
		exp = { 5'd0, 3'd0, 16'd0, wdata, 2'b01 };
		rd_check(0, exp, 32'hffff_ffff, 0);
		mi_check_nmi(exp_nmi, 0);
`endif	// SIMGATE
	end
endtask

// test secure mode functionality;
// returns in secure mode;

task mi_sec_mode;
	reg [31:0] wdata;		// write data;
	reg [31:0] rdata;		// read data;
	begin
		// check that 0ing SFATAL, STIMER and SAPP deassert nmi;
		// clear out all state, except secure mode;

		$display("test: %M: check nmi re-enable");
		wdata = { 5'bx, 3'd0, 16'bx, 8'b0000_0001 };
		swrite(`MI_SEC_MODE, `CPU_SIZE_4, wdata);
		mi_check_nmi(1, 2);

		// check that reading MI_SEC_MODE returns current state
		// and that it does not trigger entering of secure mode
		// in secure mode;

		sread(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
		wdata = { 5'd0, 3'd0, 16'd0, 8'b0000_0001 };
		rd_check(0, wdata, 32'hffff_ffff, 0);
		mi_check_nmi(1, 0);

		// check that setting SMD, SBUT, STRAP, SFATAL, STIMER or SAPP assert nmi;

		mi_check_nmi_force(6'b100000);
		mi_check_nmi_force(6'b000000);
		mi_check_nmi_force(6'b010000);
		mi_check_nmi_force(6'b000000);
		mi_check_nmi_force(6'b001000);
		mi_check_nmi_force(6'b000000);
		mi_check_nmi_force(6'b000100);
		mi_check_nmi_force(6'b000000);
		mi_check_nmi_force(6'b000010);
		mi_check_nmi_force(6'b000000);
		mi_check_nmi_force(6'b000001);
		mi_check_nmi_force(6'b000000);
	end
endtask

// check button sts/intr in MI_EINTR;

task mi_button_check;
	input exp_sts;		// expected button status;
	input exp_intr;		// expected button intr;
	reg [31:0] rdata;		// read data;
	begin
		sread(`MI_EINTR, `CPU_SIZE_4, rdata);
		if(rdata[24] !== exp_sts) begin
			$display("ERROR: %t: %M: MI_EINTR button sts %b exp %b",
				$time, rdata[24], exp_sts);
		end
		if(rdata[12] !== exp_intr) begin
			$display("ERROR: %t: %M: MI_EINTR button intr %b exp %b",
				$time, rdata[12], exp_intr);
		end
	end
endtask

// test button interrupt/pre-nmi;
// must be in secure mode to test;

task mi_button_test;
	input trap;				// test button trap;
	reg [31:0] wdata;		// write data;
	reg [31:0] rdata;		// read data;
	integer n;
	begin
		// check button timer;
		// release button press that powered on system;
		// set si joy channel clock to fastest;
		// clear button mask to clear previous interrupts;

		$display("test: %M");
		vsim.button = 0;
		mi_cbus_mon(1, 0, 0);
		swrite(`SI_CONFIG, `CPU_SIZE_4, 32'h0);
		swrite(`MI_EMASK, `CPU_SIZE_4, 32'h0100_0000);
		swrite(`MI_EMASK, `CPU_SIZE_4, 32'h0200_0000);
		mi_check_intr(`INTR_BUT, 1);
		mi_cbus_mon(0, 0, 0);

		// enable secure button trap;
		// assumed to be called in non-secure mode;

		if(trap) begin
			if(vsim.bb.bcp.mi.secure === 0)
				mi_sec_mode_enter(0);
			sread(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
			rdata[25] = 1;
			rdata[6] = 0;
			swrite(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
			sread(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
			if(rdata[25] !== 1)
				$display("ERROR: %t: %M: MI_SEC_MODE SBUT_EN %b, exp 1", $time, rdata[25]);
			if(rdata[6] !== 0)
				$display("ERROR: %t: %M: MI_SEC_MODE SBUT %b, exp 0", $time, rdata[6]);
		end

		// push button;
		// check button intr assertion;
		// check MI_EINTR button and interrupt status;

		$display("test: %M: button push");
		vsim.button = 1;
		mi_but_time = $time;
		repeat(5) @(posedge sysclk);
		mi_check_intr(`INTR_BUT, 0);
		mi_button_check(1, 1);

		// release button;
		// check button intr deassertion;
		// check MI_EINTR button and interrupt status;

		$display("test: %M: button release");
		vsim.button = 0;
		repeat(5) @(posedge sysclk);
		mi_check_intr(`INTR_BUT, 0);
		mi_button_check(0, 1);

		// test secure trap;

		if(trap)
			mi_button_finish;

		// clear button interrupt status;
		// check MI_EINTR button and interrupt status;
		// not for button trap testing;

		$display("test: %M: button intr clear");
		swrite(`MI_EMASK, `CPU_SIZE_4, 32'h0100_0000);
		mi_button_check(0, 0);
		mi_check_intr(`INTR_BUT, 1);
	end
endtask

// finish button test;
// this must be run before the button timer expires;

task mi_button_finish;
	reg [31:0] wdata;		// write data;
	reg [31:0] rdata;		// read data;
	reg [63:0] dt;			// time delta;
	integer n;
	begin
		// leave secure mode;
		// wait for timer to trap, nmi is only indication;
		// timer must tick for 2^20 clocks;

		$display("test: %M: waiting for button trap");
		dt = $time - mi_but_time;
		dt = ((dt * 1000) / vsim.sysclk_period) >> 10;
		if(vsim.bb.bcp.mi.secure === 1)
			mi_sec_mode_leave;
		for(n = 1025 - dt; n >= 0; n = n - 1) begin
			if((nmi_l === 1'b0) & (n > 1))
				$display("ERROR: %t: %M: premature button trap, n=%0d", $time, n);
			repeat(1024) @(posedge sysclk);
		end

		// reenter secure mode;
		// check that button trap is set;

		mi_sec_mode_enter(0);
		sread(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
		if(rdata[6] !== 1)
			$display("ERROR: %t: %M: SBUT %b exp 1", $time, rdata[6]);

		// disable button timer;

		$display("test: %M: disabling button trap");
		rdata[25] = 0;
		rdata[6] = 0;
		swrite(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
		sread(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
		mi_check_nmi(1, 0);
		if(rdata[25] !== 0)
			$display("ERROR: %t: %M: MI_SEC_MODE SBUT_EN %b, exp 0", $time, rdata[25]);
		if(rdata[6] !== 0)
			$display("ERROR: %t: %M: MI_SEC_MODE SBUT %b, exp 0", $time, rdata[6]);
	end
endtask

// test secure mode timer;
// must be called in secure mode;

task mi_sec_timer;
	reg [31:0] wdata;		// write data;
	reg [31:0] rdata;		// read data;
	reg tdec;				// timer decrements;
	begin
		// initialize secure timer;
		// set fastest pre-scaler, x2;
		// set timer so that the non-secure tests finish
		// before the timer goes off and triggers nmi;

		$display("test: %M: secure timer trigger");
		wdata[31:16] = 16'd1;
		wdata[15:0] = 16'd16;
		swrite(`MI_SEC_TIMER, `CPU_SIZE_4, wdata);

		// check that timer value is corect;
		// check that timer counter decrements;

		req_spacing(4);
		sread(`MI_SEC_TIMER, `CPU_SIZE_4, rdata);
		mi_check_sec_timer(1);
		if(rdata[15:0] !== wdata[15:0]) begin
			$display("ERROR: %t: %M: secure timer value 0x%h exp 0x%h",
				$time, rdata[15:0], wdata[15:0]);
		end
		tdec = (rdata[31:16] < wdata[15:0]) & (rdata[31:16] >= 16'd0);
		if( !tdec) begin
			$display("ERROR: %t: %M: secure timer decrement 0x%h from 0x%h", 
				$time, rdata[31:16], wdata[15:0]);
		end

		// leave secure mode;
		// writes to MI_SEC_TIMER should be dropped;
		// reads must return 0;

		mi_sec_mode_leave;
		swrite(`MI_SEC_TIMER, `CPU_SIZE_4, 32'bx);
		sread(`MI_SEC_TIMER, `CPU_SIZE_4, rdata);
		rd_check(0, 32'd0, 32'hffff_ffff, 0);

		// secure timer should have gone off;
		// enter secure mode by doing the boot fetch;
		// write in non-secure should have been ignored;

		mi_check_sec_timer(1);
		mi_check_nmi(1, 0);
		repeat(16*2) @(posedge sysclk);
		mi_sec_mode_enter(0);
		sread(`MI_SEC_TIMER, `CPU_SIZE_4, rdata);
		rd_check(0, wdata, 32'h0000_ffff, 0);

		// disable secure timer;

		wdata[31:16] = 16'd0;
		wdata[15:0] = $random;
		swrite(`MI_SEC_TIMER, `CPU_SIZE_4, wdata);
		sread(`MI_SEC_TIMER, `CPU_SIZE_4, rdata);
		mi_check_sec_timer(0);
		rd_check(0, wdata, 32'h0000_ffff, 0);
	end
endtask

// targeted test for single requests to memory;

task mi_mem_sglwalk;
	input [31:0] space;			// address space;
	input [5:0] nabits;			// # of address bits;
	input [1:0] size;			// data size;
	input reqspc;				// use random request spacing;
	reg [31:0] addr;			// address;
	reg [31:0] amask;			// address mask;
	reg [31:0] saddr [2:29];	// single address;
	reg [31:0] sdata [2:29];	// single data;
	reg [31:0] smask [2:29];	// single data mask;
	integer n;
	begin
		// test writes;

		$display("test: %M: space 0x%h, size %0d, spacing %b", space, size + 1, reqspc);
		amask = 32'hffff_ffff >> (32 - nabits);
		amask[1:0] = 2'b00;

		// walk one address bit;
		// randomize request spacing and data;

		$display("test: %M: write, addr walking 1");
		for(n = 2; n < nabits; n = n + 1) begin
			addr = space | (1 << n);
			addr[1:0] = sgl_addr(size);
			saddr[n] = addr;
			sdata[n] = $random;
			smask[n] = sgl_mask(addr[1:0], size);
			if(reqspc)
				req_spacing($random);
			swrite(addr, size, sdata[n]);
		end

		$display("test: %M: write, addr walking 0");
		for(n = 2; n < nabits; n = n + 1) begin
			addr = saddr[n];
			addr = addr ^ amask;
			if(reqspc)
				req_spacing($random);
			swrite(addr, size, ~sdata[n]);
		end

		// read back single data and check;

		$display("test: %M: compare, addr walking 1");
		for(n = 2; n < nabits; n = n + 1) begin
			addr = saddr[n];
			if(reqspc)
				req_spacing($random);
			sread(addr, size, data[0]);
			rd_check(0, sdata[n], smask[n], 0);
		end

		$display("test: %M: compare, addr walking 0");
		for(n = 2; n < nabits; n = n + 1) begin
			addr = saddr[n];
			addr = addr ^ amask;
			if(reqspc)
				req_spacing($random);
			sread(addr, size, data[0]);
			rd_check(0, ~sdata[n], smask[n], 0);
		end
	end
endtask

// targeted test for single requests to memory;

task mi_mem_blkwalk;
	input [31:0] space;			// address space;
	input [5:0] nabits;			// # of address bits;
	input [1:0] size;			// data size;
	input reqspc;				// use random request spacing;
	reg [31:0] addr;			// address;
	reg [31:0] amask;			// address mask;
	reg [31:0] baddr [2:29];	// block address;
	reg [31:0] bdata [2:29];	// block data;
	integer n;
	begin
		// test writes;

		$display("test: %M: space 0x%h, size %0d, spacing %b", space, 8 << size, reqspc);
		amask = 32'hffff_ffff >> (32 - nabits);
		amask[4:0] = 5'd0;

		// walk one address bit;
		// randomize request spacing and data;

		$display("test: %M: write, addr walking 1");
		for(n = 5; n < nabits; n = n + 1) begin
			addr = space | (1 << n);
			addr[4:2] = blk_addr(size);
			baddr[n] = addr;
			bdata[n] = $random;
			if(reqspc)
				req_spacing($random);
			blk_data(bdata[n]);
			bwrite(addr, size);
		end

		$display("test: %M: write, addr walking 0");
		for(n = 5; n < nabits; n = n + 1) begin
			addr = baddr[n];
			addr = addr ^ amask;
			if(reqspc)
				req_spacing($random);
			blk_data(~bdata[n]);
			bwrite(addr, size);
		end

		// read back block data and check;
		// read through same space it was written into;

		$display("test: %M: compare, addr walking 1");
		for(n = 5; n < nabits; n = n + 1) begin
			addr = baddr[n];
			if(reqspc)
				req_spacing($random);
			bread(addr, size);
			blk_check(size, bdata[n], 0, 3'b000);
		end

		$display("test: %M: compare, addr walking 0");
		for(n = 5; n < nabits; n = n + 1) begin
			addr = baddr[n];
			addr = addr ^ amask;
			if(reqspc)
				req_spacing($random);
			bread(addr, size);
			blk_check(size, ~bdata[n], 0, 3'b000);
		end
	end
endtask

// test sub-block order;
// the r4300 aligns physical addresses on doubleword boundaries;
// address bits [2:0] are always 0;

task mi_mem_subblk;
	input [31:0] space;			// address space;
	input [5:0] nabits;			// # of address bits;
	input [1:0] size;			// data size;
	input reqspc;				// use random request spacing;
	reg [31:0] addr;			// block address;
	reg [31:0] amask;			// address mask;
	reg [4:2] waddr;			// write word address;
	reg [31:0] bdata;			// block data;
	integer nx;					// # of random tests;
	integer n, ndw;				// double word index;
	begin
		// only for block reads;
		// block writes are always sequential;
		// use fixed number of random cachelines;

		$display("test: %M: space 0x%h, size %0d, spacing %b", space, 8 << size, reqspc);
		amask = 32'hffff_ffff >> (32 - nabits);
		amask[4:0] = 5'd0;

		// write sequential;
		// pick random address and data;

		for(nx = 0; nx < `MI_N_SUBBLK; nx = nx + 1) begin
			addr = space | ($random & amask);
			waddr = blk_addr(size);
			addr[4:2] = waddr;
			$display("test: %M: write sequential, addr 0x%h", addr);
			bdata = $random;
			blk_data(bdata);
			if(reqspc)
				req_spacing($random);
			bwrite(addr, size);

			// read and compare all subblock orders;

			ndw = (1 << size);
			for(n = 0; n < ndw; n = n + 1) begin
				addr[4:2] = waddr | (n << 1);
				$display("test: %M: read subblock %h", addr[4:3]);
				if(reqspc)
					req_spacing($random);
				bread(addr, size);
				blk_check(size, bdata, 0, n << 1);
			end
		end
	end
endtask

// check rom with single reads;

task mi_rom_sgl;
	input [31:0] space;		// address space;
	input [5:0] nabits;		// # of address bits;
	input [1:0] size;		// data size;
	input reqspc;			// use random request spacing;
	reg [31:0] addr;		// address;
	reg [31:0] amask;		// address mask;
	reg [31:0] smask;		// byte mask;
	reg [13:2] raddr;		// rom address;
	integer n;
	begin
		$display("test: %M: size %0d, spacing %b", size + 1, reqspc);
		amask = 32'hffff_ffff >> (32 - nabits);
		amask[1:0] = 2'b00;

		$display("test: %M: compare, addr walking 1");
		for(n = 2; n < nabits; n = n + 1) begin
			addr = space | (1 << n);
			addr[1:0] = sgl_addr(size);
			smask = sgl_mask(addr[1:0], size);
			raddr = addr[13:2];
			if(reqspc)
				req_spacing($random);
			sread(addr, size, data[0]);
			rd_check(0, mi_brom_data[raddr], smask, 0);
		end

		$display("test: %M: compare, addr walking 0");
		for(n = 2; n < nabits; n = n + 1) begin
			addr = space | (1 << n);
			addr[1:0] = sgl_addr(size);
			addr = addr ^ amask;
			smask = sgl_mask(addr[1:0], size);
			raddr = addr[13:2];
			if(reqspc)
				req_spacing($random);
			sread(addr, size, data[0]);
			rd_check(0, mi_brom_data[raddr], smask, 0);
		end
	end
endtask

// check rom block response;

task mi_rom_blk_check;
	input [1:0] size;		// data size;
	input [13:2] raddr;		// rom address;
	input err;				// expect error;
	integer n;
	begin
		if(size == `CPU_SIZE_RSVD)
			$display("ERROR: %t: %M: blk size %b", $time, size);
		rd_check(0, mi_brom_data[raddr ^ 0], 32'hffffffff, err);
		rd_check(1, mi_brom_data[raddr ^ 1], 32'hffffffff, err);
		if((size == `CPU_SIZE_16) | (size == `CPU_SIZE_32)) begin
			rd_check(2, mi_brom_data[raddr ^ 2], 32'hffffffff, err);
			rd_check(3, mi_brom_data[raddr ^ 3], 32'hffffffff, err);
		end
		if(size == `CPU_SIZE_32) begin
			rd_check(4, mi_brom_data[raddr ^ 4], 32'hffffffff, err);
			rd_check(5, mi_brom_data[raddr ^ 5], 32'hffffffff, err);
			rd_check(6, mi_brom_data[raddr ^ 6], 32'hffffffff, err);
			rd_check(7, mi_brom_data[raddr ^ 7], 32'hffffffff, err);
		end
	end
endtask

// check rom with block reads;

task mi_rom_blk;
	input [31:0] space;		// address space;
	input [5:0] nabits;		// # of address bits;
	input [1:0] size;		// data size;
	input reqspc;			// use random request spacing;
	reg [31:0] addr;		// address;
	reg [31:0] amask;		// address mask;
	reg [13:2] raddr;		// rom address;
	integer n;
	begin
		$display("test: %M: size %0d, spacing %b", size + 1, reqspc);
		amask = 32'hffff_ffff >> (32 - nabits);
		amask[4:0] = 5'b00;

		// read back block data and check;
		// read through same space it was written into;

		$display("test: %M: compare, addr walking 1");
		for(n = 5; n < nabits; n = n + 1) begin
			addr = space | (1 << n);
			addr[4:2] = blk_addr(size);
			if(reqspc)
				req_spacing($random);
			bread(addr, size);
			mi_rom_blk_check(size, addr[13:2], 0);
		end

		$display("test: %M: compare, addr walking 0");
		for(n = 5; n < nabits; n = n + 1) begin
			addr = space | (1 << n);
			addr[4:2] = blk_addr(size);
			addr = addr ^ amask;
			if(reqspc)
				req_spacing($random);
			bread(addr, size);
			mi_rom_blk_check(size, addr[13:2], 0);
		end
	end
endtask

// test rom sub-block order;

task mi_rom_subblk;
	input [31:0] space;			// address space;
	input [5:0] nabits;			// # of address bits;
	input [1:0] size;			// data size;
	input reqspc;				// use random request spacing;
	reg [31:0] addr;			// block address;
	reg [31:0] amask;			// address mask;
	reg [4:2] waddr;			// write word address;
	integer nx;					// # of random tests;
	integer n, ndw;				// double word index;
	begin
		// only for block reads;
		// block writes are always sequential;
		// use fixed number of random cachelines;

		$display("test: %M: space 0x%h, size %0d, spacing %b", space, 8 << size, reqspc);
		amask = 32'hffff_ffff >> (32 - nabits);
		amask[4:0] = 5'd0;

		// pick random addresses;
		// read and compare all subblock orders;

		for(nx = 0; nx < `MI_N_SUBBLK; nx = nx + 1) begin
			addr = space | ($random & amask);
			waddr = blk_addr(size);
			ndw = (1 << size);
			for(n = 0; n < ndw; n = n + 1) begin
				addr[4:2] = waddr | (n << 1);
				$display("test: %M: read subblock %h", addr[4:3]);
				if(reqspc)
					req_spacing($random);
				bread(addr, size);
				mi_rom_blk_check(size, addr[13:2], 0);
			end
		end
	end
endtask

// fill memory with pattern;
// increasing number of 1s in address;
// fill with d-cache writes (sequential);
// used for touch test;

task mi_mem_fill_pat;
	input [31:0] addr;		// address space;
	input [5:0] nabits;		// # of address bits;
	input [31:0] pxor;		// patterrn xor;
	integer n;
	begin
		$display("test: %M: space 0x%h", addr);
		addr = addr & (32'hffff_ffff << nabits);
		for(n = 4; n <= nabits; n = n + 1) begin
			req_spacing($random);
			blk_data(addr ^ pxor);
			bwrite(addr, `CPU_SIZE_16);
			addr = addr | (1 << n);
			pxor = { pxor[30:0], pxor[31] };
		end
	end
endtask

// compare memory with pattern;
// increasing number of 1s in address;
// read d-cache size with random sub-block order;
// used for touch test;

task mi_mem_cmp_pat;
	input [31:0] addr;		// address space;
	input [5:0] nabits;		// # of address bits;
	input [31:0] pxor;		// patterrn xor;
	reg [2:0] sbo;			// subblock order;
	integer n;
	begin
		$display("test: %M: space 0x%h", addr);
		addr = addr & (32'hffff_ffff << nabits);
		sbo[2] = 0;
		for(n = 4; n <= nabits; n = n + 1) begin
			sbo[1:0] = $random;
			req_spacing($random);
			addr[3:2] = sbo[1:0];
			bread(addr, `CPU_SIZE_16);
			addr[3:2] = 2'b00;
			blk_check(`CPU_SIZE_16, addr ^ pxor, 0, sbo);
			addr = addr | (1 << n);
			pxor = { pxor[30:0], pxor[31] };
		end
	end
endtask

// check that memory pattern is 0;
// increasing number of 1s in address;
// block reads with random size and sub-block order;
// randomize lower address bits for cover more space;
// used for touch test;

task mi_mem_cmp_zero;
	input [31:0] addr;		// address space;
	input [5:0] nabits;		// # of address bits;
	reg [1:0] size;			// request size;
	integer n, w;
	begin
		$display("test: %M: space 0x%h", addr);
		addr = addr & (32'hffff_ffff << nabits);
		for(n = 5; n <= nabits; n = n + 1) begin
			size = $random;
			while(size == 2'b11)
				size = $random;
			addr = addr ^ ($random & (32'hffff_ffff >> (32 - n)));
			addr[2:0] = 3'd0;
			req_spacing($random);
			bread(addr, size);
			addr[4:3] = 2'b00;
			for(w = 0; w < (2 << size); w = w + 1)
				rd_check(0, 32'd0, 32'hffffffff, 0);
			addr = addr | (1 << n);
		end
	end
endtask

// check access rights of memory region;
// must be called in secure mode;

task mi_mem_access;
	input [31:0] addr;		// address space;
	input [5:0] nabits;		// # of address bits;
	input nsena;			// device enabled in non-secure mode;
	input [31:0] pxor;		// patterrn xor;
	reg [31:0] rdata;		// read data;
	begin
		// setup test pattern;
		// leave secure mode;

		$display("test: %M: space 0x%h, nsena %b", addr, nsena);
		mi_mem_fill_pat(addr, nabits, pxor);
		mi_sec_mode_leave;

		// if device is enabled in non-secure mode;
		// then pattern should match;

		if(nsena)
			mi_mem_cmp_pat(addr, nabits, pxor);

		// if device is disabled in non-secure mode;
		// writes with new test pattern should be ignored;
		// reads from region should return 0;

		else begin
			mi_mem_fill_pat(addr, nabits, $random);
			mi_mem_cmp_zero(addr, nabits);
		end

		// enter secure mode by application;
		// check that original pattern is unmodified;

		mi_sec_mode_kick(1);
		mi_sec_mode_enter(0);
		mi_mem_cmp_pat(addr, nabits, pxor);
	end
endtask

// check access rights of rom region;
// must be called in secure mode;

task mi_rom_access;
	input [31:0] addr;		// address space;
	input [5:0] nabits;		// # of address bits;
	input [31:0] pxor;		// patterrn xor;
	reg [31:0] rdata;		// read data;
	begin
		// leave secure mode;

		$display("test: %M: space 0x%h", addr);
		mi_sec_mode_leave;

		// writes should be dropped in secure mode;
		// reads from region should return 0;

		mi_mem_cmp_zero(addr, nabits);

		// enter secure mode by application;
		// check that original pattern is unmodified;

		mi_sec_mode_kick(1);
		mi_sec_mode_enter(0);
	end
endtask

// test brom;
// must be in secure mode;

task mi_brom;
	reg [31:0] wdata;	// write data;
	integer sz;			// request size;
	begin
		// set RESET bit in MI_SEC_MODE register;
		// test bram in reset space;

		wdata = { 5'bx, 3'd0, 16'bx, 8'b0000_0011 };
		swrite(`MI_SEC_MODE, `CPU_SIZE_4, wdata);

		$display("test: %M: reset space, single reads");
		for(sz = 0; sz < 4; sz = sz + 1) begin
			mi_rom_sgl(`MI_BROM_RST, `MI_NA_BROM, sz, 0);
			mi_rom_sgl(`MI_BROM_RST, `MI_NA_BROM, sz, 1);
		end

		$display("test: %M: reset space, block reads");
		for(sz = 0; sz < 3; sz = sz + 1) begin
			mi_rom_blk(`MI_BROM_RST, `MI_NA_BROM, sz, 0);
			mi_rom_blk(`MI_BROM_RST, `MI_NA_BROM, sz, 1);
		end

		$display("test: %M: reset space, subblock order");
		for(sz = 0; sz < 3; sz = sz + 1) begin
			mi_rom_subblk(`MI_BROM_RST, `MI_NA_BROM, sz, 0);
			mi_rom_subblk(`MI_BROM_RST, `MI_NA_BROM, sz, 1);
		end

		// check access restrictions;

		mi_rom_access(`MI_BROM_RST, `MI_NA_BROM, $random);

		// clear RESET bit to flip address spaces;
		// test bram in flip space;

		wdata = { 5'bx, 3'd0, 16'bx, 8'b0000_0001 };
		swrite(`MI_SEC_MODE, `CPU_SIZE_4, wdata);

		$display("test: %M: flip space, single requests");
		for(sz = 0; sz < 4; sz = sz + 1) begin
			mi_rom_sgl(`MI_BROM_FLIP, `MI_NA_BROM, sz, 0);
			mi_rom_sgl(`MI_BROM_FLIP, `MI_NA_BROM, sz, 1);
		end

		$display("test: %M: flip space, block requests");
		for(sz = 0; sz < 3; sz = sz + 1) begin
			mi_rom_blk(`MI_BROM_FLIP, `MI_NA_BROM, sz, 0);
			mi_rom_blk(`MI_BROM_FLIP, `MI_NA_BROM, sz, 1);
		end

		$display("test: %M: flip space, subblock order");
		for(sz = 0; sz < 3; sz = sz + 1) begin
			mi_rom_subblk(`MI_BROM_FLIP, `MI_NA_BROM, sz, 0);
			mi_rom_subblk(`MI_BROM_FLIP, `MI_NA_BROM, sz, 1);
		end

		// check access restrictions;

		mi_rom_access(`MI_BROM_FLIP, `MI_NA_BROM, $random);
	end
endtask

// test bram;
// must be in secure mode;

task mi_bram;
	reg [31:0] wdata;	// write data;
	integer sz;			// request size;
	begin
		// set RESET bit in MI_SEC_MODE register;
		// test bram in reset space;

		wdata = { 5'bx, 3'd0, 16'bx, 8'b0000_0011 };
		swrite(`MI_SEC_MODE, `CPU_SIZE_4, wdata);

		$display("test: %M: reset space, single requests");
		for(sz = 0; sz < 4; sz = sz + 1) begin
			mi_mem_sglwalk(`MI_BRAM_RST, `MI_NA_BRAM, sz, 0);
			mi_mem_sglwalk(`MI_BRAM_RST, `MI_NA_BRAM, sz, 1);
		end

		$display("test: %M: reset space, block requests");
		for(sz = 0; sz < 3; sz = sz + 1) begin
			mi_mem_blkwalk(`MI_BRAM_RST, `MI_NA_BRAM, sz, 0);
			mi_mem_blkwalk(`MI_BRAM_RST, `MI_NA_BRAM, sz, 1);
		end

		$display("test: %M: reset space, subblock order");
		for(sz = 0; sz < 3; sz = sz + 1) begin
			mi_mem_subblk(`MI_BRAM_RST, `MI_NA_BRAM, sz, 0);
			mi_mem_subblk(`MI_BRAM_RST, `MI_NA_BRAM, sz, 1);
		end

		// check access restrictions;

		mi_mem_access(`MI_BRAM_RST, `MI_NA_BRAM, 0, $random);

		// clear RESET bit to flip address spaces;
		// test bram in flip space;

		wdata = { 5'bx, 3'd0, 16'bx, 8'b0000_0001 };
		swrite(`MI_SEC_MODE, `CPU_SIZE_4, wdata);

		$display("test: %M: flip space, single requests");
		for(sz = 0; sz < 4; sz = sz + 1) begin
			mi_mem_sglwalk(`MI_BRAM_FLIP, `MI_NA_BRAM, sz, 0);
			mi_mem_sglwalk(`MI_BRAM_FLIP, `MI_NA_BRAM, sz, 1);
		end

		$display("test: %M: flip space, block requests");
		for(sz = 0; sz < 3; sz = sz + 1) begin
			mi_mem_blkwalk(`MI_BRAM_FLIP, `MI_NA_BRAM, sz, 0);
			mi_mem_blkwalk(`MI_BRAM_FLIP, `MI_NA_BRAM, sz, 1);
		end

		$display("test: %M: flip space, subblock order");
		for(sz = 0; sz < 3; sz = sz + 1) begin
			mi_mem_subblk(`MI_BRAM_FLIP, `MI_NA_BRAM, sz, 0);
			mi_mem_subblk(`MI_BRAM_FLIP, `MI_NA_BRAM, sz, 1);
		end

		// check access restrictions;

		mi_mem_access(`MI_BRAM_FLIP, `MI_NA_BRAM, 0, $random);
	end
endtask

// test iram;

task mi_iram;
	integer sz;			// request size;
	reg [31:0] val;		// read/write value;
	begin
		// check access types;

		$display("test: %M: single requests");
		for(sz = 0; sz < 4; sz = sz + 1) begin
			mi_mem_sglwalk(`MI_IRAM, `MI_NA_IRAM, sz, 0);
			mi_mem_sglwalk(`MI_IRAM, `MI_NA_IRAM, sz, 1);
		end

		$display("test: %M: block requests");
		for(sz = 0; sz < 3; sz = sz + 1) begin
			mi_mem_blkwalk(`MI_IRAM, `MI_NA_IRAM, sz, 0);
			mi_mem_blkwalk(`MI_IRAM, `MI_NA_IRAM, sz, 1);
		end

		$display("test: %M: subblock order");
		for(sz = 0; sz < 3; sz = sz + 1) begin
			mi_mem_subblk(`MI_IRAM, `MI_NA_IRAM, sz, 0);
			mi_mem_subblk(`MI_IRAM, `MI_NA_IRAM, sz, 1);
		end

		// check access restrictions;
		// based on IRAM_EN bit in MI_SEC_MODE;

		sread(`MI_SEC_MODE, `CPU_SIZE_4, val);
		val[24] = 0;
		swrite(`MI_SEC_MODE, `CPU_SIZE_4, val);
		mi_mem_access(`MI_IRAM, `MI_NA_IRAM, 0, $random);
		val[24] = 1;
		swrite(`MI_SEC_MODE, `CPU_SIZE_4, val);
		mi_mem_access(`MI_IRAM, `MI_NA_IRAM, 1, $random);
	end
endtask

// test virage registers after reset;

task vx_after_reset;
	input [1:0] vspc;	// virage space;
	reg [31:0] addr;	// address;
	reg [31:0] exp;		// expected data;
	reg [31:0] rdata;	// read data;
	begin
		// test control register default;
		// charge-pump porst has gone inactive before here;
		// nms reset to charge-pump must be 1;

		$display("test: %M: MI_VCTRL%0d", vspc);
		if(mi_porst[vspc] !== 1)
			$display("ERROR: %t: %M: v_porst[%0d] did not pulse", $time, vspc);
		addr = `MI_VCTRL0;
		addr[17:16] = vspc;
		sread(addr, `CPU_SIZE_4, rdata);
		if(vspc == 2) begin
			exp = 32'b10x0_0000_xx10_x000_0001_10xx_xxxx_0000;
			rd_check(0, exp, 32'hdf37_fc0f, 0);
		end else begin
			exp = 32'b00x0_0000_xx00_x000_0001_10xx_xxxx_0000;
			rd_check(0, exp, 32'hdf37_fc0f, 0);
		end

		// clear v2 bypass mode;

		if(vspc == 2)
			swrite(addr, `CPU_SIZE_4, 32'd0);

		// test nms register defaults;
		// this works independent of nms bypass;

		addr = `MI_VREG0;
		addr[17:16] = vspc;

		$display("test: %M: MI_VREG%0d CRSTO_0", vspc);
		addr[4:2] = 0;
		sread(addr, `CPU_SIZE_4, rdata);
		rd_check(0, 32'b0000_0001, 32'h0000_00ff, 0);

		$display("test: %M: MI_VREG%0d CRSTO_1", vspc);
		addr[4:2] = 1;
		sread(addr, `CPU_SIZE_4, rdata);
		rd_check(0, 32'b0001_0010, 32'h0000_00ff, 0);

		$display("test: %M: MI_VREG%0d CRM_0", vspc);
		addr[4:2] = 2;
		sread(addr, `CPU_SIZE_4, rdata);
		rd_check(0, 32'b1001_0000, 32'h0000_00ff, 0);

		$display("test: %M: MI_VREG%0d CRM_1", vspc);
		addr[4:2] = 3;
		sread(addr, `CPU_SIZE_4, rdata);
		rd_check(0, 32'b1001_0110, 32'h0000_00ff, 0);

		$display("test: %M: MI_VREG%0d CRM_2", vspc);
		addr[4:2] = 4;
		sread(addr, `CPU_SIZE_4, rdata);
		rd_check(0, 32'b0101_1001, 32'h0000_00ff, 0);

		$display("test: %M: MI_VREG%0d CRM_3", vspc);
		addr[4:2] = 5;
		sread(addr, `CPU_SIZE_4, rdata);
		rd_check(0, 32'b0001_0101, 32'h0000_00ff, 0);
	end
endtask

// test virage memories;

task mi_vmem;
	input [1:0] vspc;	// virage space;
	input [5:0] nabits;	// # of address bits;
	reg [31:0] addr;	// address;
	integer sz;			// request size;
	begin
		// only 32-bit single requests are supported;

		$display("test: %M: v%0d, single requests", vspc);
		addr = `MI_VMEM0;
		addr[17:16] = vspc;
		mi_mem_sglwalk(addr, nabits, 3, 0);
		mi_mem_sglwalk(addr, nabits, 3, 1);

		$display("test: %M: block requests");
		for(sz = 0; sz < 3; sz = sz + 1) begin
			mi_mem_blkwalk(addr, nabits, sz, 0);
			mi_mem_blkwalk(addr, nabits, sz, 1);
		end

		$display("test: %M: subblock order");
		for(sz = 0; sz < 3; sz = sz + 1) begin
			mi_mem_subblk(addr, nabits, sz, 0);
			mi_mem_subblk(addr, nabits, sz, 1);
		end

		// check access restrictions;

		mi_mem_access(addr, nabits, 0, $random);
	end
endtask

// fill virage sram with randoms;

task mi_fill_vmem;
	input [31:0] addr;	// virage address;
	input save_pat;		// save pattern seeds;
	reg [1:0] vspc;		// virage space;
	integer ndc;		// # of data cachelines;
	integer n;
	begin
		vspc = addr[17:16];
		$display("%t: %M: v%0d", $time, vspc);
		ndc = vspc[1]? 16 : 4;
		addr[15:0] = `MI_VMEM;
		for(n = 0; n < ndc; n = n + 1) begin
			mi_vpat[n] = $random;
			if(save_pat)
				blk_data(mi_vpat[n]);
			bwrite(addr, `CPU_SIZE_16);
			addr = addr + 16;
		end
	end
endtask

// compare virage sram with pattern;

task mi_cmp_vmem;
	input [31:0] addr;	// virage address;
	reg [1:0] vspc;		// virage space;
	integer ndc;		// # of data cachelines;
	integer n;
	begin
		vspc = addr[17:16];
		$display("%t: %M: v%0d", $time, vspc);
		ndc = vspc[1]? 16 : 4;
		addr[15:0] = `MI_VMEM;
		for(n = 0; n < ndc; n = n + 1) begin
			bread(addr, `CPU_SIZE_16);
			blk_check(`CPU_SIZE_16, mi_vpat[n], 0, 3'b000);
			addr = addr + 16;
		end
	end
endtask

// wait for nms ready;

task mi_nms_wait;
	input [1:0] vspc;	// virage space;
	input [31:0] to;	// timeout in time base ticks;
	reg [31:0] addr;	// address;
	reg [31:0] rdata;	// read data;
	integer n;
	begin
		$display("%t: %M: v%0d, to %0d ticks", $time, vspc, to);
		addr = `MI_VCTRL0;
		addr[17:16] = vspc;
		for(n = to; n > 0; n = n - 1) begin
			sread(addr, `CPU_SIZE_4, rdata);
			if(rdata[30] === 1) begin
				$display("%t: %M: nms ready", $time);
				n = 0;
			end else begin
				@(posedge vsim.bb.v_time);
			end
		end
		if(rdata[30] !== 1)
			$display("ERROR: %t: %M: nms ready timeout", $time);
	end
endtask

// test one nms command;

task mi_nms_cmd;
	input [1:0] vspc;	// virage space;
	input [2:0] cmd;	// command to test;
	input [31:0] to;	// ready timeout, us;
	input pass;			// expected pass status;
	reg [31:0] addr;	// address of ctrl reg;
	reg [31:0] wdata;	// write data;
	reg [31:0] rdata;	// read data;
	reg keep;			// keep mode expected;
	integer n;
	begin
		// check nms status;

		$display("test: %M: v%0d, cmd %0d", vspc, cmd);
		addr = `MI_VCTRL0;
		addr[17:16] = vspc;
		sread(addr, `CPU_SIZE_4, rdata);
		if(rdata[31] !== 0)
			$display("ERROR: %t: %M: nms bypass %b", $time, rdata[31]);
		if(rdata[30] !== 1)
			$display("ERROR: %t: %M: nms ready %b exp 1", $time, rdata[30]);

		// issue command;
		// check controller busy;

		wdata[31:24] = { 5'd0, cmd };
		wdata[23:0] = $random;
		addr[13] = 1;
		swrite(addr, `CPU_SIZE_4, wdata);
		sread(addr, `CPU_SIZE_4, rdata);
		if(rdata[30] !== 0)
			$display("ERROR: %t: %M: nms ready %b exp 0", $time, rdata[30]);

		// wait for nms ready;
		// check status;

		mi_nms_wait(vspc, to);
		sread(addr, `CPU_SIZE_4, rdata);
		if(rdata[29] !== pass)
			$display("ERROR: %t: %M: nms pass %b exp %b", $time, rdata[29], pass);
		keep = (cmd == `VCMD_KEEP);
		if(rdata[28] !== keep)
			$display("ERROR: %t: %M: nms keep %b exp %b", $time, rdata[28], keep);
	end
endtask

// test virage controllers;

task mi_vx_ctrl;
	input [1:0] vspc;	// virage space;
	input [5:0] nabits;	// # of address bits;
	reg [31:0] addr;	// address;
	reg [31:0] wdata;	// write data;
	reg [31:0] rdata;	// read data;
	reg [31:0] cmd;		// nms command;
	integer n;
	begin
		// set virage time base;

		$display("test: %M: v%0d", vspc);
		wdata = 1000000 / vsim.sysclk_period;
		$display("%t: %M: setting vtime divider %0d", $time, wdata);
		swrite(`MI_SEC_VTIME, `CPU_SIZE_4, wdata);

		// wait for charge-pump power-on reset to drop;
		// wait up to 1msec according to virage spec;

		addr = `MI_VCTRL0;
		addr[17:16] = vspc;
		for(n = 1000; n > 0; n = n - 1) begin
			sread(addr, `CPU_SIZE_4, rdata);
			if(rdata[0] === 0) begin
				$display("%t: %M: v%0d charge-pump out of reset", $time, vspc);
				n = 0;
			end else begin
				@(posedge vsim.bb.v_time);
			end
		end
		if(rdata[0] !== 0)
			$display("ERROR: %M: v%0d charge-pump porst %b", $time, vspc, rdata[0]);

		// wait for nms ready;
		// delay is 8 time base clocks according to virage spec;

		mi_nms_wait(vspc, 50);

		// test nms idle command;
		// check that sram stays unmodified;

		$display("test: %M: v%0d: nms idle cmd", vspc);
		addr = `MI_VCTRL0;
		addr[17:16] = vspc;
		mi_fill_vmem(addr, 1);
		mi_nms_cmd(vspc, `VCMD_IDLE, 10, 1);
		mi_cmp_vmem(addr);

		// keep mode should not modify sram;
		// writes to sram should be dropped in keep mode;

		$display("test: %M: v%0d: nms keep mode on", vspc);
		mi_nms_cmd(vspc, `VCMD_KEEP, 40, 1);
		mi_cmp_vmem(addr);
		mi_fill_vmem(addr, 0);
		mi_cmp_vmem(addr);

		$display("test: %M: v%0d: nms keep mode off", vspc);
		mi_nms_cmd(vspc, `VCMD_IDLE, 10, 1);
		mi_cmp_vmem(addr);

		// store to novea;
		// check that sram stays unmodified;

		$display("test: %M: v%0d: nms full store", vspc);
		mi_nms_cmd(vspc, `VCMD_FSTORE, 8000, 1);
		mi_cmp_vmem(addr);

		// compare novea with sram;
		// check that sram stays unmodified;

		$display("test: %M: v%0d: nms normal compare", vspc);
		mi_nms_cmd(vspc, `VCMD_NCOMP, 10, 1);
		mi_cmp_vmem(addr);

		// fill sram with new pattern, then recall;
		// original data pattern should be restored;

		$display("test: %M: v%0d: nms normal recall", vspc);
		mi_fill_vmem(addr, 0);
		mi_nms_cmd(vspc, `VCMD_NRECALL, 10, 1);
		mi_cmp_vmem(addr);
	end
endtask

// test dram single requests;
// in all address spaces, all sizes;
// witout and with random request spacing;

task mi_dram_sgl;
	input [5:0] nabits;		// # of address bits in x64 space;
	integer sz;				// current size;
	begin
		// test all sizes through all spaces;

		for(sz = 0; sz < 4; sz = sz + 1) begin
			mi_mem_sglwalk(`BASE_DRAM36, nabits - 1, sz, 0);
			mi_mem_sglwalk(`BASE_DRAM36, nabits - 1, sz, 1);
`ifdef	MI_MEM_X64L
			mi_mem_sglwalk(`BASE_DRAM64L, nabits, sz, 0);
			mi_mem_sglwalk(`BASE_DRAM64L, nabits, sz, 1);
`endif	// MI_MEM_X64L
`ifdef	MI_MEM_X64H
			mi_mem_sglwalk(`BASE_DRAM64H, nabits, sz, 0);
			mi_mem_sglwalk(`BASE_DRAM64H, nabits, sz, 1);
`endif	// MI_MEM_X64L
		end
	end
endtask

// test dram block requests;
// in all address spaces, all sizes;
// witout and with random request spacing;

task mi_dram_blk;
	input [5:0] nabits;		// # of address bits in x64 space;
	integer sz;				// current size;
	begin
		// test all sizes through all spaces;

		for(sz = 0; sz < 3; sz = sz + 1) begin
			mi_mem_blkwalk(`BASE_DRAM36, nabits - 1, sz, 0);
			mi_mem_blkwalk(`BASE_DRAM36, nabits - 1, sz, 1);
`ifdef	MI_MEM_X64L
			mi_mem_blkwalk(`BASE_DRAM64L, nabits, sz, 0);
			mi_mem_blkwalk(`BASE_DRAM64L, nabits, sz, 1);
`endif	// MI_MEM_X64L
`ifdef	MI_MEM_X64H
			mi_mem_blkwalk(`BASE_DRAM64H, nabits, sz, 0);
			mi_mem_blkwalk(`BASE_DRAM64H, nabits, sz, 1);
`endif	// MI_MEM_X64H
		end
	end
endtask

// test dram subblock order;
// in all address spaces, all sizes;
// witout and with random request spacing;

task mi_dram_subblk;
	input [5:0] nabits;		// # of address bits in x64 space;
	integer sz;				// current size;
	begin
		// test all sizes through all spaces;

		for(sz = 0; sz < 3; sz = sz + 1) begin
			mi_mem_subblk(`BASE_DRAM36, nabits - 1, sz, 0);
			mi_mem_subblk(`BASE_DRAM36, nabits - 1, sz, 1);
`ifdef	MI_MEM_X64L
			mi_mem_subblk(`BASE_DRAM64L, nabits, sz, 0);
			mi_mem_subblk(`BASE_DRAM64L, nabits, sz, 1);
`endif	// MI_MEM_X64L
`ifdef	MI_MEM_X64H
			mi_mem_subblk(`BASE_DRAM64H, nabits, sz, 0);
			mi_mem_subblk(`BASE_DRAM64H, nabits, sz, 1);
`endif	// MI_MEM_X64H
		end
	end
endtask

// measure cacheline read/write bandwidth;
// write pattern is address;
// read pattern is checked, so do write before read;

task mi_rwbw;
	input [3:0] mcfg;		// memory configuration;
	input [31:0] addr;		// address space;
	input write;			// measure write bandwidth;
	input [1:0] size;		// block size;
	integer nb;				// # of byte in cacheline;
	integer t0, t;			// time stamps;
	integer n;				// loop count;
	reg [31:0] addr;		// cacheline address;
	begin
		nb = 8 << size;
		$display("test: %M: mcfg %0d, addr 0x%h, size %0d, %s bw",
			mcfg, addr, nb, write? "write" : "read");
		t0 = $time;
		for(n = 0; n < 64; n = n + 1) begin
			if(write) begin
				blk_data(addr);
				bwrite(addr, size);
			end else begin
				bread(addr, size);
				blk_check(size, addr, 0, 3'b000);
			end
			addr = addr + nb;
		end
		t = $time - t0;
		$display("test: %M: addr 0x%h, size %0d, %s bw %0dMB/sec",
			addr, nb, write? "write" : "read", (64 * nb * 1000) / t);
	end
endtask

// setup for error testing;

task mi_err_setup;
	input [5:0] ena;		// error enables;
	input exitsec;			// exit secure mode;
	reg [31:0] rdata;		// read data;
	begin
		$display("%t: %M: pif %b, bnm %b", $time, ena[5:3], ena[2:0]);
		sread(`MI_CTRL, `CPU_SIZE_4, rdata);
		rdata[18:13] = ena;
		swrite(`MI_CTRL, `CPU_SIZE_4, rdata);

		// read MI_ERR_INFO to enable capture;
		// clear any pending secure mode triggers;

		sread(`MI_ERR_INFO, `CPU_SIZE_4, rdata);
		sread(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
		rdata[7:2] = 6'd0;
		if(exitsec)
			rdata[0] = 0;
		mi_set_boot_word(rdata[1]);
		swrite(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
		mi_check_nmi(1, 2);
		mi_check_secure(~exitsec);
	end
endtask

// read test for bus errors;

task mi_bus_err;
	input [31:0] space;		// address space;
	input [5:0] nabits;		// # of address bits;
	input exp_berr;			// expect bus error;
	input wen;				// writeable space;
	input [1:0] size;		// block size;
	reg [31:0] addr;		// address;
	reg [31:0] amask;		// address mask;
	reg [31:0] bdata;		// block data pattern;
	reg [2:0] sbo;			// subblock order;
	integer n;
	begin
		// use random addresses within space;
		// fill and compare data if writeable space;
		// else, just check the error bit;

		$display("test: %M: space 0x%h, nabits %0d, size %0d, berr %b",
			space, nabits, 8 << size, exp_berr);
		amask = 32'hffff_ffff >> (32 - nabits);
		amask[4:0] = 5'd0;
		for(n = 0; n < `MI_N_BLKERR; n = n + 1) begin
			addr = space | ($random & amask);
			addr[4:2] = blk_addr(size);
			if(wen) begin
				bdata = $random;
				blk_data(bdata);
				bwrite(addr, size);
			end
			sbo = sbo_addr(size);
			addr[4:2] = addr[4:2] ^ sbo;
			req_spacing($random);
			bread(addr, size);
			if(wen)
				blk_check(size, bdata, 0, sbo);
			else
				berr_check(exp_berr);
		end
	end
endtask

// test block reads of all request sizes;

task mi_blk_rdacc;
	input [31:0] addr;		// address space;
	input [5:0] nabits;		// # of address bits;
	input exp_berr;			// expect bus error;
	input wen;				// writeable space;
	integer sz;
	begin
		for(sz = 0; sz < 3; sz = sz + 1)
			mi_bus_err(addr, nabits, exp_berr, wen, sz);
	end
endtask

// test legality of block reads;

task mi_blk_reads;
	input exp_berr;			// expect bus error;
	input [5:0] nadram;		// # of memory address bits, x64 space;
	begin
		// setup mi control register;

		$display("test: %M: berr %b", exp_berr);
		mi_cbus_mon(0, 0, 0);
		mi_err_setup({ 3'b000, 2'b00, exp_berr }, 0);

		// cacheable memory spaces should never return an erorr;
		// only dma request should be on cbus;

		mi_cbus_mon(0, 0, 1);
		mi_blk_rdacc(`BASE_DRAM36, nadram - 1, 0, 1);
`ifdef	MI_MEM_X64L
		mi_blk_rdacc(`BASE_DRAM64L, nadram, 0, 1);
`endif	// MI_MEM_X64L
`ifdef	MI_MEM_X64H
		mi_blk_rdacc(`BASE_DRAM64H, nadram, 0, 1);
`endif	// MI_MEM_X64L
		mi_cbus_mon(0, 0, 0);
		mi_blk_rdacc(`MI_BROM_FLIP, `MI_NA_BROM, 0, 0);
		mi_blk_rdacc(`MI_BRAM_FLIP, `MI_NA_BRAM, 0, 1);
		mi_blk_rdacc(`MI_IRAM, `MI_NA_IRAM, 0, 1);
		mi_blk_rdacc(`MI_VMEM0, `MI_NA_VMEM0, 0, 1);
		mi_blk_rdacc(`MI_VMEM1, `MI_NA_VMEM1, 0, 1);
		mi_blk_rdacc(`MI_VMEM2, `MI_NA_VMEM2, 0, 1);

		// test old rdram config space and register spaces;

		mi_blk_rdacc(`BASE_RDCFG, `MI_NA_RDCFG, exp_berr, 0);
		mi_blk_rdacc(`BASE_SP, `MI_NA_REGS, exp_berr, 0);
		mi_blk_rdacc(`BASE_CMD, `MI_NA_REGS, exp_berr, 0);
		mi_blk_rdacc(`BASE_SPAN, `MI_NA_REGS, exp_berr, 0);
		mi_blk_rdacc(`BASE_MI, `MI_NA_REGS, exp_berr, 0);
		mi_blk_rdacc(`BASE_VI, `MI_NA_REGS, exp_berr, 0);
		mi_blk_rdacc(`BASE_AI, `MI_NA_REGS, exp_berr, 0);
		mi_blk_rdacc(`BASE_PI, `MI_NA_REGS, exp_berr, 0);
		mi_blk_rdacc(`BASE_RI, `MI_NA_REGS, exp_berr, 0);
		mi_blk_rdacc(`BASE_SI, `MI_NA_REGS, exp_berr, 0);
		mi_blk_rdacc(`BASE_USB0, `MI_NA_REGS, exp_berr, 0);
		mi_blk_rdacc(`BASE_USB1, `MI_NA_REGS, exp_berr, 0);

		// test io spaces;

		mi_blk_rdacc(`BASE_IO05, 24, exp_berr, 0);
		mi_blk_rdacc(`BASE_IO06, 25, exp_berr, 0);
		mi_blk_rdacc(`BASE_IO08, 27, exp_berr, 0);
		mi_blk_rdacc(`BASE_IO10, 27, exp_berr, 0);

		// none of the above should have triggered a write error;

		mi_check_nmi(1, 2);
		mi_check_intr(`INTR_ERR, 1);
	end
endtask

// write test for write errors;

task mi_write_err;
	input [31:0] space;		// address space;
	input [5:0] nabits;		// # of address bits;
	input exp_wei;			// expect write error interrupt;
	input exp_ski;			// expect secure kernel trap;
	input wen;				// writeable space;
	input [1:0] size;		// block size;
	reg [31:0] addr;		// address;
	reg [31:0] amask;		// address mask;
	reg mult;				// force multiple error;
	reg [31:0] bdata;		// block data;
	reg [31:0] rdata;		// read data;
	reg [31:0] info;		// error info;
	reg [2:0] sbo;			// subblock order;
	integer n;
	begin
		// use random addresses within space;

		$display("test: %M: space 0x%h, nabits %0d, size %0d, wei %b, ski %b",
			space, nabits, 8 << size, exp_wei, exp_ski);
		amask = 32'hffff_ffff >> (32 - nabits);
		amask[4:0] = 5'd0;

		// run write tests;

		for(n = 0; n < `MI_N_BLKERR; n = n + 1) begin
			addr = space | ($random & amask);
			addr[4:2] = blk_addr(size);
			bdata = $random;
			blk_data(bdata);
			bwrite(addr, size);
			mult = $random;
			if(mult)
				bwrite(addr, size);

			// if writeable space then check read data;

			if(wen) begin
				sbo = sbo_addr(size);
				addr[4:2] = addr[4:2] ^ sbo;
				bread(addr, size);
				blk_check(size, bdata, 0, sbo);
			end

			// check write error interrupt and secure trap;
			// error registers must be updated for either;

			mi_check_nmi(~exp_ski, 2);
			mi_check_intr(`INTR_ERR, ~exp_wei);

			if(exp_wei | exp_ski) begin
				sread(`MI_ERR_ADDR, `CPU_SIZE_4, rdata);
				rd_check(0, addr, 32'hffff_ffff, 0);
				sread(`MI_ERR_INFO, `CPU_SIZE_4, rdata);
				info = { 27'd0, 1'b1, mult, 1'b1, size };
				rd_check(0, info, 32'hffff_ffff, 0);
			end

			// check write error intr clearance;
			// reading MI_ERR_INFO above should have cleared write error intr;
			// reading it again should have the WEVAL bit cleared;

			if(exp_wei) begin
				mi_check_intr(`INTR_ERR, 1);
				sread(`MI_ERR_INFO, `CPU_SIZE_4, rdata);
				info = { 27'd0, 1'b0, 1'bx, 1'bx, 2'bx };
				rd_check(0, info, 32'hffff_fff0, 0);
			end

			// check secure trap bit;
			// clear secure triggers to re-enable nmi;

			if(exp_ski) begin
				sread(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
				if(rdata[5] !== 1)
					$display("ERROR: %t: %M: STRAP %b exp 1", $time, rdata[5]);
				rdata[7:2] = 6'd0;
				swrite(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
				mi_check_nmi(1, 2);
			end

		end
	end
endtask

// test block writes of all request sizes;

task mi_blk_wracc;
	input [31:0] addr;		// address space;
	input [5:0] nabits;		// # of address bits;
	input exp_wei;			// expect write error interrupt;
	input exp_ski;			// expect secure kernel trap;
	input wen;				// writeable space;
	integer sz;
	begin
		for(sz = 0; sz < 3; sz = sz + 1)
			mi_write_err(addr, nabits, exp_wei, exp_ski, wen, sz);
	end
endtask

// test legality of block writes;

task mi_blk_writes;
	input exp_wei;			// expect write error intr;
	input exp_ski;			// expect secure kernel trap;
	input [5:0] nadram;		// # of memory address bits, x64 space;
	reg [31:0] rdata;		// read data;
	begin
		// setup mi control register;

		$display("test: %M: wei %b, ski %b", exp_wei, exp_ski);
		mi_cbus_mon(0, 0, 0);
		mi_err_setup({ 3'b000, exp_ski, exp_wei, 1'b0 }, 0);

		// cacheable memory spaces should never cause an erorr;
		// only dma request should be on cbus;

		mi_cbus_mon(0, 0, 1);
		mi_blk_wracc(`BASE_DRAM36, nadram - 1, 0, 0, 1);
`ifdef	MI_MEM_X64L
		mi_blk_wracc(`BASE_DRAM64L, nadram, 0, 0, 1);
`endif	// MI_MEM_X64L
`ifdef	MI_MEM_X64H
		mi_blk_wracc(`BASE_DRAM64H, nadram, 0, 0, 1);
`endif	// MI_MEM_X64L
		mi_cbus_mon(0, 0, 0);
		mi_blk_wracc(`MI_BROM_FLIP, `MI_NA_BROM, 0, 0, 0);
		mi_blk_wracc(`MI_BRAM_FLIP, `MI_NA_BRAM, 0, 0, 1);
		mi_blk_wracc(`MI_IRAM, `MI_NA_IRAM, 0, 0, 1);
		mi_blk_wracc(`MI_VMEM0, `MI_NA_VMEM0, 0, 0, 1);
		mi_blk_wracc(`MI_VMEM1, `MI_NA_VMEM1, 0, 0, 1);
		mi_blk_wracc(`MI_VMEM2, `MI_NA_VMEM2, 0, 0, 1);

		// test old rdram config space and register spaces;

		mi_blk_wracc(`BASE_RDCFG, `MI_NA_RDCFG, exp_wei, exp_ski, 0);
		mi_blk_wracc(`BASE_SP, `MI_NA_REGS, exp_wei, exp_ski, 0);
		mi_blk_wracc(`BASE_CMD, `MI_NA_REGS, exp_wei, exp_ski, 0);
		mi_blk_wracc(`BASE_SPAN, `MI_NA_REGS, exp_wei, exp_ski, 0);
		mi_blk_wracc(`BASE_MI, `MI_NA_REGS, exp_wei, exp_ski, 0);
		mi_blk_wracc(`BASE_VI, `MI_NA_REGS, exp_wei, exp_ski, 0);
		mi_blk_wracc(`BASE_AI, `MI_NA_REGS, exp_wei, exp_ski, 0);
		mi_blk_wracc(`BASE_PI, `MI_NA_REGS, exp_wei, exp_ski, 0);
		mi_blk_wracc(`BASE_RI, `MI_NA_REGS, exp_wei, exp_ski, 0);
		mi_blk_wracc(`BASE_SI, `MI_NA_REGS, exp_wei, exp_ski, 0);
		mi_blk_wracc(`BASE_USB0, `MI_NA_REGS, exp_wei, exp_ski, 0);
		mi_blk_wracc(`BASE_USB1, `MI_NA_REGS, exp_wei, exp_ski, 0);

		// test io spaces;

		mi_blk_wracc(`BASE_IO05, 24, exp_wei, exp_ski, 0);
		mi_blk_wracc(`BASE_IO06, 25, exp_wei, exp_ski, 0);
		mi_blk_wracc(`BASE_IO08, 27, exp_wei, exp_ski, 0);
		mi_blk_wracc(`BASE_IO10, 27, exp_wei, exp_ski, 0);

	end
endtask

// pif read access test;

task mi_pif_rdacc;
	input exp_berr;			// expect bus error;
	input blkreq;			// block request;
	input [1:0] size;		// block size;
	reg [31:0] addr;		// address;
	reg [31:0] rdata;		// block data pattern;
	integer n;
	begin
		// use random addresses within space;
		// just check the error bit;

		$display("test: %M: size %0d, berr %b",
			blkreq? (8 << size) : (size + 1), exp_berr);
		addr = `BASE_PIF;
		addr[5:2] = $random;
		addr[1:0] = 2'b00;
		if(blkreq) begin
			addr[2] = 0;
			bread(addr, size);
		end else
			sread(addr, size, rdata);
		berr_check(exp_berr);
	end
endtask

// test pif read trapping;

task mi_pif_reads;
	input exp_berr;			// expect bus error;
	begin
		// non of below requests should go on cbus;

		mi_cbus_mon(0, 0, 0);

		// check no trapping in secure mode;

		mi_err_setup({ 2'b00, exp_berr, 3'b000 }, 0);
		mi_pif_rdacc(0, 0, `CPU_SIZE_1);
		mi_pif_rdacc(0, 0, `CPU_SIZE_2);
		mi_pif_rdacc(0, 0, `CPU_SIZE_3);
		mi_pif_rdacc(0, 0, `CPU_SIZE_4);
		mi_pif_rdacc(0, 1, `CPU_SIZE_8);
		mi_pif_rdacc(0, 1, `CPU_SIZE_16);
		mi_pif_rdacc(0, 1, `CPU_SIZE_32);

		// check trapping in non-secure mode;

		mi_err_setup({ 2'b00, exp_berr, 3'b000 }, 1);
		mi_pif_rdacc(exp_berr, 0, `CPU_SIZE_1);
		mi_pif_rdacc(exp_berr, 0, `CPU_SIZE_2);
		mi_pif_rdacc(exp_berr, 0, `CPU_SIZE_3);
		mi_pif_rdacc(exp_berr, 0, `CPU_SIZE_4);
		mi_pif_rdacc(exp_berr, 1, `CPU_SIZE_8);
		mi_pif_rdacc(exp_berr, 1, `CPU_SIZE_16);
		mi_pif_rdacc(exp_berr, 1, `CPU_SIZE_32);

		// re-enter secure mode;

		mi_sec_mode_kick(1);
		mi_sec_mode_enter(0);
	end
endtask

// pif write access test;

task mi_pif_wracc;
	input exp_ski;			// expect secure kernel trap;
	input exp_wei;			// expect write error interrupt;
	input blkreq;			// block request;
	input [1:0] size;		// block size;
	input exitsec;			// exit secure mode;
	reg [31:0] addr;		// address;
	reg [31:0] maddr;		// multiple address;
	reg mult;				// force multiple error;
	reg [31:0] wdata;		// write data;
	reg [31:0] rdata;		// read data;
	reg [31:0] info;		// error info;
	integer n;
	begin
		// leave secure mode is required;

		$display("test: %M: size %0d, ski %b, wei %b",
			blkreq? (8 << size) : (size + 1), exp_ski, exp_wei);
		if(exitsec)
			mi_sec_mode_leave;

		// use random addresses within space;
		// issue different write type for multiple test;

		addr = `BASE_PIF;
		addr[5:2] = $random;
		mult = $random;
		wdata = $random;

		if(blkreq) begin
			addr[4:3] = blk_addr(size);
			addr[2:0] = 0;
			blk_data(wdata);
			bwrite(addr, size);
			if(mult) begin
				maddr = addr;
				maddr[5] = ~maddr[5];
				swrite(maddr, ~size, $random);
			end
		end else begin
			addr[1:0] = sgl_addr(size);
			swrite(addr, size, wdata);
			if(mult) begin
				maddr = addr;
				maddr[5] = ~maddr[5];
				maddr[4:3] = blk_addr(`CPU_SIZE_16);
				maddr[2:0] = 0;
				blk_data($random);
				bwrite(maddr, `CPU_SIZE_16);
			end
		end

		// check write error interrupt and secure trap;
		// error registers must be updated for either;

		mi_check_nmi(~exp_ski, 2);
		mi_check_intr(`INTR_ERR, ~exp_wei);

		if(exp_wei | exp_ski) begin
			sread(`MI_ERR_ADDR, `CPU_SIZE_4, rdata);
			rd_check(0, addr, 32'hffff_ffff, 0);
			sread(`MI_ERR_INFO, `CPU_SIZE_4, rdata);
			info = { 27'd0, 1'b1, mult, blkreq, size };
			rd_check(0, info, 32'hffff_ffff, 0);
			if( ~blkreq) begin
				sread(`MI_ERR_DATA, `CPU_SIZE_4, rdata);
				rd_check(0, wdata, 32'hffff_ffff, 0);
			end
		end

		// check secure trap bit;
		// re-enter secure mode;
		// clear secure triggers to re-enable nmi;

		if(exitsec) begin
			mi_sec_mode_kick(~exp_ski);
			mi_sec_mode_enter(0);
		end
		if(exp_ski) begin
			sread(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
			if(rdata[5] !== 1)
				$display("ERROR: %t: %M: STRAP %b exp 1", $time, rdata[5]);
			if(rdata[2] !== 1)
				$display("ERROR: %t: %M: SAPP %b exp 1", $time, rdata[2]);
			rdata[7:2] = 6'd0;
			swrite(`MI_SEC_MODE, `CPU_SIZE_4, rdata);
			mi_check_nmi(1, 2);
		end
	end
endtask

// test pif write trapping;

task mi_pif_writes;
	input exp_ski;			// expect secure kernel trap;
	input exp_wei;			// expect write error interrupt;
	integer n;
	begin
		// setup error handling;
		// non of below requests should go on cbus;

		mi_cbus_mon(0, 0, 0);
		mi_err_setup({ exp_ski, exp_wei, 1'b0, 3'b000 }, 0);

		// check no trapping in secure mode;

		mi_pif_wracc(0, 0, 0, `CPU_SIZE_1, 0);
		mi_pif_wracc(0, 0, 0, `CPU_SIZE_2, 0);
		mi_pif_wracc(0, 0, 0, `CPU_SIZE_3, 0);
		mi_pif_wracc(0, 0, 0, `CPU_SIZE_4, 0);
		mi_pif_wracc(0, 0, 1, `CPU_SIZE_8, 0);
		mi_pif_wracc(0, 0, 1, `CPU_SIZE_16, 0);
		mi_pif_wracc(0, 0, 1, `CPU_SIZE_32, 0);

		// check trapping in non-secure mode;

		mi_pif_wracc(exp_ski, exp_wei, 0, `CPU_SIZE_1, 1);
		mi_pif_wracc(exp_ski, exp_wei, 0, `CPU_SIZE_2, 1);
		mi_pif_wracc(exp_ski, exp_wei, 0, `CPU_SIZE_3, 1);
		mi_pif_wracc(exp_ski, exp_wei, 0, `CPU_SIZE_4, 1);
		mi_pif_wracc(exp_ski, exp_wei, 1, `CPU_SIZE_8, 1);
		mi_pif_wracc(exp_ski, exp_wei, 1, `CPU_SIZE_16, 1);
		mi_pif_wracc(exp_ski, exp_wei, 1, `CPU_SIZE_32, 1);
	end
endtask

// random memory test;
// for all r/w memories that support single and block requests;

task mi_mem_random;
	input [3:0] mcfg;		// memory configuration;
	input [5:0] nadram;		// # of dram address bits, x64 space;
	reg [2:0] spc;			// address space;
	reg [31:0] addr;		// address;
	reg [31:0] amask;		// address mask;
	reg [5:0] nabits;		// # of address bits;
	reg blkreq;				// block request;
	reg wrreq;				// write to fill data;
	reg [1:0] size;			// request size;
	reg [31:0] wdata;		// write data;
	reg [2:0] sbo;			// subblock order;
	reg [31:0] smask;		// single compare mask;
	integer n;
	begin
		// run over the following memory spaces;
		// x36, x64l, x64h, bram, iram, v0, v1, v2;

		$display("test: %M: mcfg %0d, nadram %0d", mcfg, nadram);
		for(n = 0; n < `MI_N_MEMRAND; n = n + 1) begin

			// pick random space;
			// make random address within that space;

			spc = $random;
			case(spc)
				0: { addr, nabits } = { `BASE_DRAM36, nadram - 1 };
				1: { addr, nabits } = { `BASE_DRAM64L, nadram };
				2: { addr, nabits } = { `BASE_DRAM64H, nadram };
				3: { addr, nabits } = { `MI_BRAM_FLIP, `MI_NA_BRAM };
				4: { addr, nabits } = { `MI_IRAM, `MI_NA_IRAM };
				5: { addr, nabits } = { `MI_VMEM0, `MI_NA_VMEM0 };
				6: { addr, nabits } = { `MI_VMEM1, `MI_NA_VMEM1 };
				7: { addr, nabits } = { `MI_VMEM2, `MI_NA_VMEM2 };
			endcase
			amask = 32'hffff_ffff >> (32 - nabits);
			amask[1:0] = 2'b00;
			addr = addr | ($random & amask);

			// pick random request type;

			{ size, blkreq, wrreq } = $random;
			while(blkreq & (size == 2'b11))
				size = $random;

			// write random data for wrreq;

			if(wrreq) begin
				wdata = $random;
				if(blkreq) begin
					addr[4:2] = blk_addr(size);
					blk_data(wdata);
					bwrite(addr, size);
				end else
					swrite(addr, size, wdata);
			end

			// read back data;
			// if block request, the use random subblock order;
			// check only if write had been issued;

			if(blkreq) begin
				sbo = sbo_addr(size);
				addr[4:2] = addr[4:2] ^ sbo;
				bread(addr, size);
				if(wrreq)
					blk_check(size, wdata, 0, sbo);
			end else begin
				sread(addr, size, data[0]);
				if(wrreq) begin
					smask = sgl_mask(addr[1:0], size);
					rd_check(0, wdata, smask, 0);
				end
			end
		end
	end
endtask

// test mi debug interface;

task mi_debug;
	begin
		$display("test: %M: XXX");
	end
endtask

// run all mi tests;

task test_mi;
	reg [31:0] pxor;		// test pattern xor;
	reg [5:0] nadram;		// # of dram address bits, x64 space;
	integer mcfg;			// current ddr configuration id;
	begin
		// run register related tests;
		// no cbus requests should be issued;

		mi_cbus_mon(0, 0, 0);
		mi_after_reset(3'b000);			// test mi defaults after reset;
		mi_avctrl;						// test audio/video control register;
		mi_ctrl;						// test control register;
		mi_intr_mask(`MI_MASK, 6);		// test interrupt mask register;
		mi_intr_mask(`MI_EMASK, 14);	// test extended mask register;
		mi_sec_mode;					// test secure mode functionality;
		mi_sec_timer;					// test secure timer;
		mi_sec_vtime(62);				// test virage timer;
		mi_sec_vtime(100);				// test virage timer;
		mi_button_test(0);				// test button intr;

		// test mi internal memories;
		// test single and block requests, subblock order;
		// no cbus requests should be issued;

		mi_cbus_mon(0, 0, 0);
		mi_brom;				// test brom;
		mi_bram;				// test bram;
		mi_iram;				// test iram;
		mi_vmem(0, 6);			// test virage 0, 512bits;
		mi_vmem(1, 6);			// test virage 1, 512bits;
		mi_vmem(2, 8);			// test virage 2, 2kbits;

		// fill all mi internal memories with address pattern;
		// then read all back and compare;
		// this tests read/write interference among those memories;

		pxor = $random;
		mi_mem_fill_pat(`MI_BRAM_FLIP, `MI_NA_BRAM, pxor);
		mi_mem_fill_pat(`MI_IRAM, `MI_NA_IRAM, pxor);
		mi_mem_fill_pat(`MI_VMEM0, `MI_NA_VMEM0, pxor);
		mi_mem_fill_pat(`MI_VMEM1, `MI_NA_VMEM1, pxor);
		mi_mem_fill_pat(`MI_VMEM2, `MI_NA_VMEM2, pxor);

		mi_mem_cmp_pat(`MI_BRAM_FLIP, `MI_NA_BRAM, pxor);
		mi_mem_cmp_pat(`MI_IRAM, `MI_NA_IRAM, pxor);
		mi_mem_cmp_pat(`MI_VMEM0, `MI_NA_VMEM0, pxor);
		mi_mem_cmp_pat(`MI_VMEM1, `MI_NA_VMEM1, pxor);
		mi_mem_cmp_pat(`MI_VMEM2, `MI_NA_VMEM2, pxor);

		// mi internal performance tests;
		// measure write bandwidth first, which fills patterns;
		// then read and measure read bandwidth;

		mi_rwbw(0, `MI_IRAM, 1, `CPU_SIZE_16);
		mi_rwbw(0, `MI_IRAM, 0, `CPU_SIZE_16);
		mi_rwbw(0, `MI_IRAM, 1, `CPU_SIZE_32);
		mi_rwbw(0, `MI_IRAM, 0, `CPU_SIZE_32);

		// test main memory of all configurations;
		// block read tests needs main memory;

		nadram = vsim.MEM_NADDR;
		for(mcfg = 0; mcfg < 16; mcfg = mcfg + 1) begin
`ifdef	MI_ONE_MCFG
			mcfg = $random;
`endif	// MI_ONE_MCFG
			mi_cbus_mon(1, 1, 1);
			ri_config_ddr(mcfg, $random);

			// test request error detection;

			mi_blk_reads(0, nadram);		// block reads without bus error;
			mi_blk_reads(1, nadram);		// block reads with bus error;
			mi_blk_writes(0, 0, nadram);	// block writes dropped;
			mi_blk_writes(0, 1, nadram);	// block writes with write error intr;
			mi_blk_writes(1, 0, nadram);	// block writes with secure kernel trap;

			// test pif emulation trapping;

			mi_pif_reads(0);				// reads without bus error;
			mi_pif_reads(1);				// reads with bus error;
			mi_pif_writes(0, 0);			// writes dropped;
			mi_pif_writes(0, 1);			// writes with write error intr;
			mi_pif_writes(1, 0);			// writes with secure kernel trap;

			// test mi access to main memory (ri);

			mi_cbus_mon(0, 0, 1);
			mi_dram_sgl(nadram);			// targeted single requests;
			mi_dram_blk(nadram);			// targeted block requests;
			mi_dram_subblk(nadram);			// targeted sub-block orders;

			//XXX targeted test through x64 mirror spaces;
			// write one space, read from another;

			// random memory tests;
			// random address and request types;

			mi_mem_random(mcfg, nadram);

			// mi dram performance tests;
			// measure write bandwidth first, which fills patterns;
			// then measure read bandwidth;

			mi_rwbw(mcfg, `BASE_DRAM36, 1, `CPU_SIZE_16);
			mi_rwbw(mcfg, `BASE_DRAM36, 0, `CPU_SIZE_16);
			mi_rwbw(mcfg, `BASE_DRAM36, 1, `CPU_SIZE_32);
			mi_rwbw(mcfg, `BASE_DRAM36, 0, `CPU_SIZE_32);

			mi_rwbw(mcfg, `BASE_DRAM64L, 1, `CPU_SIZE_16);
			mi_rwbw(mcfg, `BASE_DRAM64L, 0, `CPU_SIZE_16);
			mi_rwbw(mcfg, `BASE_DRAM64L, 1, `CPU_SIZE_32);
			mi_rwbw(mcfg, `BASE_DRAM64L, 0, `CPU_SIZE_32);

`ifdef	MI_ONE_MCFG
			mcfg = 100;
`endif	// MI_ONE_MCFG
		end

		// button trap test;

		mi_button_test(1);				// launch button timer trap;

		// test virage store controllers and bypass;
		// run as late as possible for charge-pump to
		// finish 1msec power-on reset;

`ifdef	XXX
		mi_vx_ctrl(0, 6);			// test virage 0, 512bits;
		mi_vx_ctrl(1, 6);			// test virage 1, 512bits;
		mi_vx_ctrl(2, 8);			// test virage 2, 2kbits;

		// test rcp interrupts; XXX

		// test new device interrupts; XXX

		// test debug interface;

		mi_debug;
`endif
	end
endtask

// run only the memory random stress test;
// mainly to help debug the ri;

task test_mem_rand;
	reg [5:0] nadram;		// # of dram address bits, x64 space;
	integer mcfg;			// current ddr configuration id;
	begin
		// test main memory of all configurations;
		// block read tests needs main memory;

		nadram = vsim.MEM_NADDR;
		for(mcfg = 0; mcfg < 16; mcfg = mcfg + 1) begin
			mi_cbus_mon(1, 1, 1);
			ri_config_ddr(mcfg, $random);
			mi_mem_random(mcfg, nadram);
		end
	end
endtask