parse_rdp.c 61.2 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
/**************************************************************************
 *                                                                        *
 *               Copyright (C) 1995, Silicon Graphics, Inc.               *
 *                                                                        *
 *  These coded instructions, statements, and computer programs  contain  *
 *  unpublished  proprietary  information of Silicon Graphics, Inc., and  *
 *  are protected by Federal copyright  law.  They  may not be disclosed  *
 *  to  third  parties  or copied or duplicated in any form, in whole or  *
 *  in part, without the prior written consent of Silicon Graphics, Inc.  *
 *                                                                        *
 *************************************************************************/

/*
 * File:      parser_rdp.c
 *
 * Interpret rdp commands, display them, and check for errors.
 *
 *  Error Checks:
 *              o  Alignment of pointers
 *              o  Color & Z outside first 2 Meg
 *              o  Clr,Tex,Z ot of 3Meg range
 *              o  Syncs missing
 *              o  Illegal Triangle coefficients
 *              o  Unscissored rectangles
 *              o  LoadBlock dxt
 *
 *  Options:
 *              o  print info about triangle area
 *              o  accumulate span size histogram XXX
 *              o  print verbose messages (XXX some)
 *
 *  Bugs:
 *              o  No load clocks estimate
 *              o  Sync clocks
 *              o  Sync checks not fool proof
 *              o  Area calc sometimes bogus
 *              o  Estimated clocks does not take into account RDRAM latency,
 *                  number of cycles, RMW mode, etc.
 */

#include <stdlib.h>
#include <stdio.h>
#include "ultra64.h"
#include "dlprint.h"

#ifndef ABS
#define ABS(x)          ((x) > 0 ? (x) : (-(x)))
#endif

#ifndef MAX
#define MAX(x, y)       ((x) > (y) ? (x) : (y))
#endif

#ifndef INRANGE
#define INRANGE(x, min, max)        (((x) >= (min)) && ((x) <= (max)))
#endif

/*
 *  Color and Z should be in First 2 Megs
 *  Max system memory is 3 Meg
 */
#define MAX_CZ_ADDRESS      ((1 << 21) - 1)
#define MAX_ANY_ADDRESS     (((1 << 20) + (1 << 21)) - 1)
#define MAX_SCREEN_LINES    240

/*
 *  Number of clocks in RDP pipeline
 */
#define MAX_HISTORY         45
#define FULLSYNC_CLOCKS     45  /*
                                 * * XXX 
                                 */
#define PIPESYNC_CLOCKS     45  /*
                                 * * XXX 
                                 */
#define TILESYNC_CLOCKS     25  /*
                                 * * XXX 
                                 */
#define LOADSYNC_CLOCKS     30  /*
                                 * * XXX 
                                 */

/*
 *  Fractional precision of attributes
 */
#define ST_PREC         (1 << 21)
#define W_PREC          ((unsigned)(1 << 31))
#define Z_PREC          (1 << 16)
#define RGBA_PREC       (1 << 16)

#define ST2F32(s)       ((f32)s / (f32)ST_PREC)
#define W2F32(w)        ((f32)w / (f32)W_PREC)
#define Z2F32(z)        ((f32)z / (f32)Z_PREC)
#define RGBA2F32(r)     ((f32)r / (f32)RGBA_PREC)

/*
 *  Edge walker coordinate ranges
 */
#define MAX_COORD_X     ((float) (1 << 11) - 1)
#define MIN_COORD_X     (- (float) (1 << 11))
#define MAX_COORD_Y     ((float) (1 << 11) - 1)
#define MIN_COORD_Y     (- (float) (1 << 11))

/*
 *  Field decode strings for color combiner
 */
#define PR_SEPARATOR        "----------------------------------------"
#define D_COMBINE           "combine"
#define D_TEX0              "tex0"
#define D_TEX1              "tex1"
#define D_PRIM              "prim"
#define D_SHADE             "shade"
#define D_ENV               "env"
#define D_CENTER            "center"
#define D_SCALE             "scale"
#define D_COMB_ALPHA        "combined_alpha"
#define D_TEX0_ALPHA        "tex0_alpha"
#define D_TEX1_ALPHA        "tex1_alpha"
#define D_PRIM_ALPHA        "prim_alpha"
#define D_SHADE_ALPHA       "shade_alpha"
#define D_ENV_ALPHA         "env_alpha"
#define D_LOD_FRAC          "lod_frac"
#define D_PRIM_LOD_FRAC     "prim_lod_frac"
#define D_NOISE             "noise"
#define D_CONV_K4           "k4"
#define D_CONV_K5           "k5"
#define D_ONE_PT_OH         "1.0"
#define D_ZERO              "0.0"

/*
 * blender inputs 
 */
#define BL_PIXEL_CLR        "pixel clr"
#define BL_BLENDED_CLR      "blended clr"
#define BL_MEMORY_CLR       "memory clr"
#define BL_BLEND_CLR        "blend reg clr"
#define BL_FOG_CLR          "fog reg clr"
#define BL_PIXEL_ALPHA      "pixel alpha"
#define BL_FOG_ALPHA        "fog alpha"
#define BL_SHADE_ALPHA      "shade alpha"
#define BL_ZERO             "0.0"

/*
 * texture formats 
 */
#define TEX_FORMAT_RGBA     "RGBA"
#define TEX_FORMAT_YUV      "YUV"
#define TEX_FORMAT_CI       "CI"
#define TEX_FORMAT_IA       "IA"
#define TEX_FORMAT_I        "I"
#define TEX_FORMAT_INV      "Invalid"

/*
 * texture sizes 
 */
#define TEX_SIZE_4b     "4b"
#define TEX_SIZE_8b     "8b"
#define TEX_SIZE_16b    "16b"
#define TEX_SIZE_32b    "32b"
#define TEX_SIZE_INV    "Invalid"

/*
 *    T y p e s
 */

typedef enum {
    RDP_CLR_IMAGE, RDP_Z_IMAGE, RDP_TEX_IMAGE
} RdpImageType;

typedef struct {
    struct {
        u32             cmd:8;  /*
                                 * * * command 
                                 */
        u32             tile:3; /*
                                 * * * tile number 
                                 */
        u32             npa:1;  /*
                                 * * * non-primitive attribute command 
                                 */
        u32             prim:1; /*
                                 * * * primitive command 
                                 */
        u32             lbt:1;  /*
                                 * * * load block/tile command 
                                 */
        u32             sts:1;  /*
                                 * * * set tile/tile size command 
                                 */
    } pc[MAX_HISTORY];          /*
                                 * * previous commands, circular buffer 
                                 */
    s32             curr_cmd;   /*
                                 * * index of current command 
                                 */
    s32             n_cmd;      /*
                                 * * number of commands processed so far 
                                 */
} SyncWarn;

/*
 *   G l o b a l s
 */

static SyncWarn SyncW;
static u8       do_verbose = FALSE;
static u8       do_pr_area = FALSE;
static u8       do_pr_histo = FALSE;
static u32      NumPrimitives = 0;
static u32      nWarnings = 0;
static u32      nErrors = 0;
static u32      nPrimClocks = 0;
static u32      nAttClocks = 0;
static u32      nLoadClocks = 0;
static u32      nSyncClocks = 0;
static u64     *RdpDl;

static f32      Scissorulx,
                Scissoruly,
                Scissorlrx,
                Scissorlry;
static f32      _yh,
                _ym,
                _yl,
                _delxm;

static f32      Trix[3],
                Triy[3];

/*
 *   F u n c t i o n s
 */

static char    *
decode_tex_fmt(u32 fmt)
{
    switch (fmt) {
        case 0:
            return (TEX_FORMAT_RGBA);
        case 1:
            return (TEX_FORMAT_YUV);
        case 2:
            return (TEX_FORMAT_CI);
        case 3:
            return (TEX_FORMAT_IA);
        case 4:
            return (TEX_FORMAT_I);
        default:
            return (TEX_FORMAT_INV);
    }
}

static char    *
decode_tex_size(u32 size)
{
    switch (size) {
        case 0:
            return (TEX_SIZE_4b);
        case 1:
            return (TEX_SIZE_8b);
        case 2:
            return (TEX_SIZE_16b);
        case 3:
            return (TEX_SIZE_32b);
        default:
            return (TEX_SIZE_INV);
    }
}

static u32
decode_size_nibbles(u32 size)
{
    switch (size) {
        case 0:
            return (1);
        case 1:
            return (2);
        case 2:
            return (4);
        case 3:
            return (8);
        default:
            return (0);
    }
}

/*
 *  suba_decode: convert select to string
 */
static char    *
suba_decode(u32 sel)
{
    switch (sel) {
        case 0:
            return (D_COMBINE);
        case 1:
            return (D_TEX0);
        case 2:
            return (D_TEX1);
        case 3:
            return (D_PRIM);
        case 4:
            return (D_SHADE);
        case 5:
            return (D_ENV);
        case 6:
            return (D_ONE_PT_OH);
        case 7:
            return (D_NOISE);
        default:
            return (D_ZERO);
    }
}

/*
 *  subb_decode: convert select to string
 */
static char    *
subb_decode(u32 sel)
{
    switch (sel) {
        case 0:
            return (D_COMBINE);
        case 1:
            return (D_TEX0);
        case 2:
            return (D_TEX1);
        case 3:
            return (D_PRIM);
        case 4:
            return (D_SHADE);
        case 5:
            return (D_ENV);
        case 6:
            return (D_CENTER);
        case 7:
            return (D_CONV_K4);
        default:
            return (D_ZERO);
    }
}

/*
 *  mul_decode: convert select to string
 */
static char    *
mul_decode(u32 sel)
{
    switch (sel) {
        case 0:
            return (D_COMBINE);
        case 1:
            return (D_TEX0);
        case 2:
            return (D_TEX1);
        case 3:
            return (D_PRIM);
        case 4:
            return (D_SHADE);
        case 5:
            return (D_ENV);
        case 6:
            return (D_SCALE);
        case 7:
            return (D_COMB_ALPHA);
        case 8:
            return (D_TEX0_ALPHA);
        case 9:
            return (D_TEX1_ALPHA);
        case 10:
            return (D_PRIM_ALPHA);
        case 11:
            return (D_SHADE_ALPHA);
        case 12:
            return (D_ENV_ALPHA);
        case 13:
            return (D_LOD_FRAC);
        case 14:
            return (D_PRIM_LOD_FRAC);
        case 15:
            return (D_CONV_K5);
        default:
            return (D_ZERO);
    }
}

/*
 *  add_decode: convert select to string
 */
static char    *
add_decode(u32 sel)
{
    switch (sel & 7) {
        case 0:
            return (D_COMBINE);
        case 1:
            return (D_TEX0);
        case 2:
            return (D_TEX1);
        case 3:
            return (D_PRIM);
        case 4:
            return (D_SHADE);
        case 5:
            return (D_ENV);
        case 6:
            return (D_ONE_PT_OH);
        case 7:
            return (D_ZERO);
    }
    return "??";
}

/*
 *  a_subab_decode: convert select to string
 */
static char    *
a_subab_decode(u32 sel)
{
    switch (sel & 7) {
        case 0:
            return (D_COMB_ALPHA);
        case 1:
            return (D_TEX0_ALPHA);
        case 2:
            return (D_TEX1_ALPHA);
        case 3:
            return (D_PRIM_ALPHA);
        case 4:
            return (D_SHADE_ALPHA);
        case 5:
            return (D_ENV_ALPHA);
        case 6:
            return (D_ONE_PT_OH);
        case 7:
            return (D_ZERO);
    }
    return "??";
}

/*
 *  a_mul_decode: convert select to string
 */
static char    *
a_mul_decode(u32 sel)
{
    switch (sel & 7) {
        case 0:
            return (D_LOD_FRAC);
        case 1:
            return (D_TEX0_ALPHA);
        case 2:
            return (D_TEX1_ALPHA);
        case 3:
            return (D_PRIM_ALPHA);
        case 4:
            return (D_SHADE_ALPHA);
        case 5:
            return (D_ENV_ALPHA);
        case 6:
            return (D_PRIM_LOD_FRAC);
        case 7:
            return (D_ZERO);
    }
    return "??";
}

/*
 *  a_add_decode: convert select to string
 */
static char    *
a_add_decode(u32 sel)
{
    switch (sel & 7) {
        case 0:
            return (D_COMB_ALPHA);
        case 1:
            return (D_TEX0_ALPHA);
        case 2:
            return (D_TEX1_ALPHA);
        case 3:
            return (D_PRIM_ALPHA);
        case 4:
            return (D_SHADE_ALPHA);
        case 5:
            return (D_ENV_ALPHA);
        case 6:
            return (D_ONE_PT_OH);
        case 7:
            return (D_ZERO);
    }
    return "??";
}

static char    *
blender_p_decode(u32 sel, u32 two_cycle, u32 mux)
{
    switch (sel) {
        case 0:
            if (two_cycle && mux)
                return (BL_BLENDED_CLR);
            else
                return (BL_PIXEL_CLR);
            break;
        case 1:
            return (BL_MEMORY_CLR);
            break;
        case 2:
            return (BL_BLEND_CLR);
            break;
        case 3:
            return (BL_FOG_CLR);
            break;
    }
    return "??";
}

static char    *
blender_a_decode(u32 sel)
{
    switch (sel) {
        case 0:
            return (BL_PIXEL_ALPHA);
            break;
        case 1:
            return (BL_FOG_ALPHA);
            break;
        case 2:
            return (BL_SHADE_ALPHA);
            break;
        case 3:
            return (BL_ZERO);
            break;
    }
    return "??";
}

static char    *
decode_rgb_dither_select(u32 sel)
{
    switch (sel) {
        case 0:
            return ("magic square");
        case 1:
            return ("bayer");
        case 2:
            return ("noise");
        case 3:
            return ("no dither");
    }
    return "??";
}

static char    *
decode_alpha_dither_select(u32 sel)
{
    switch (sel) {
        case 0:
            return ("pattern");
        case 1:
            return ("~pattern");
        case 2:
            return ("noise");
        case 3:
            return ("no dither");
    }
    return "??";
}

/*
 *  Classify command as non-primitive attribute, which are
 *  not buffered and so must be explicitly synced.
 */
static int
is_npa_cmd(u32 cmd)
{
    switch (cmd) {
        case G_SETCIMG:
        case G_SETZIMG:
        case G_SETTIMG:
        case G_SETCOMBINE:
        case G_SETENVCOLOR:
        case G_SETBLENDCOLOR:
        case G_SETFOGCOLOR:
        case G_SETFILLCOLOR:
        case G_RDPSETOTHERMODE:
            /*
             * case G_SETSCISSOR: 
             * case G_NOOP:
             */
        case G_SETCONVERT:
        case G_SETKEYR:
        case G_SETKEYGB:
            return (1);
        default:
            return (0);
    }
}

/*
 *  Classify as triangle command 
 */
static int
is_tri_cmd(u32 cmd)
{
    switch (cmd) {
        case G_TRI_FILL:
        case G_TRI_SHADE:
        case G_TRI_TXTR:
        case G_TRI_SHADE_TXTR:
        case G_TRI_FILL_ZBUFF:
        case G_TRI_SHADE_ZBUFF:
        case G_TRI_TXTR_ZBUFF:
        case G_TRI_SHADE_TXTR_ZBUFF:
            return (1);
        default:
            return (0);
    }
}

/*
 *  classify as texture rectangle command
 */
static int
is_tex_rect_cmd(u32 cmd)
{
    switch (cmd) {
        case G_TEXRECTFLIP:
        case G_TEXRECT:
            return (1);
        default:
            return (0);
    }
}

/*
 *  Classify as rectangle command
 */
static int
is_rect_cmd(u32 cmd)
{
    if (is_tex_rect_cmd(cmd) || cmd == G_FILLRECT)
        return (1);
    else
        return (0);
}

/*
 *  Classify command as load block or load tile 
 */
static int
is_lbt_cmd(u32 cmd)
{
    switch (cmd) {
        case G_LOADTILE:
        case G_LOADBLOCK:
        case G_LOADTLUT:
            return (1);
        default:
            return (0);
    }
}

/*
 * classify comand as primitive
 */
static int
is_prim_cmd(u32 cmd)
{
    if (is_rect_cmd(cmd) || is_tri_cmd(cmd) || is_lbt_cmd(cmd))
        return (1);
    else
        return (0);
}

/*
 *  Classify command as set_tile or set_tile_size
 */
static int
is_sts_cmd(int cmd)
{
    switch (cmd) {
        case G_SETTILE:
        case G_SETTILESIZE:
            return (1);
        default:
            return (0);
    }
}

/*
 *  Search through last MAX_HISTORY commands, looking for primitive
 *  command and noting if pipe or full sync command was found first.
 */
static int
no_sync_since_prim(void)
{
    int             indx,
                    i,
                    sync_found = 0,
                    prim_found = 0;

    if (SyncW.n_cmd < 1)
        return (0);

    indx = SyncW.curr_cmd - 1;
    if (indx < 0)
        indx = MAX_HISTORY - 1;

    for (i = 0; i < SyncW.n_cmd; i++) {
        if (do_verbose)
            PRINTF("SyncW.pc[%d].cmd = %d\n", indx, SyncW.pc[indx].cmd);

        if (SyncW.pc[indx].cmd == G_RDPFULLSYNC ||
            SyncW.pc[indx].cmd == G_RDPPIPESYNC)
            ++sync_found;
        else if (SyncW.pc[indx].prim) {
            ++prim_found;
            break;
        }
        --indx;
        if (indx < 0)
            indx = MAX_HISTORY - 1;
    }
    if (do_verbose)
        PRINTF("sync_found = %d\n", sync_found);
    return (!sync_found && prim_found);
}

/*
 *  Search through last MAX_HISTORY commands, looking for primitive
 *  command and noting if load sync command was found first.
 */
static int
no_load_sync_since_prim(void)
{
    int             indx,
                    i,
                    sync_found = 0,
                    prim_found = 0;

    if (SyncW.n_cmd < 1)
        return (0);

    indx = SyncW.curr_cmd - 1;
    if (indx < 0)
        indx = MAX_HISTORY - 1;

    for (i = 0; i < SyncW.n_cmd; i++) {
        if (do_verbose)
            PRINTF("SyncW.pc[%d].cmd = %d\n", indx, SyncW.pc[indx].cmd);

        if (SyncW.pc[indx].cmd == G_RDPLOADSYNC ||
            SyncW.pc[indx].cmd == G_RDPFULLSYNC ||
            SyncW.pc[indx].cmd == G_RDPPIPESYNC)
            ++sync_found;
        else if (SyncW.pc[indx].prim) {
            ++prim_found;
            break;
        }
        --indx;
        if (indx < 0)
            indx = MAX_HISTORY - 1;
    }

    if (do_verbose)
        PRINTF("sync_found = %d\n", sync_found);

    return (!sync_found && prim_found);
}

/*
 *  Search through last MAX_HISTORY commands, looking for primitive
 *  command and noting if tile sync command was found before primitive
 *  referencing given tile number.
 */
static int
no_tile_sync_since_prim(u32 tile)
{
    int             indx,
                    i,
                    sync_found = 0,
                    prim_found = 0;

    if (SyncW.n_cmd < 1)
        return (0);

    indx = SyncW.curr_cmd - 1;
    if (indx < 0)
        indx = MAX_HISTORY - 1;

    for (i = 0; i < SyncW.n_cmd; i++) {
        if (do_verbose)
            PRINTF("SyncW.pc[%d].cmd = %d\n", indx, SyncW.pc[indx].cmd);

        if (SyncW.pc[indx].cmd == G_RDPTILESYNC ||
            SyncW.pc[indx].cmd == G_RDPFULLSYNC ||
            SyncW.pc[indx].cmd == G_RDPPIPESYNC ||
            SyncW.pc[indx].cmd == G_RDPLOADSYNC)
            ++sync_found;
        else if (SyncW.pc[indx].prim && (SyncW.pc[indx].tile == tile)) {
            ++prim_found;
            break;
        }
        --indx;
        if (indx < 0)
            indx = MAX_HISTORY - 1;
    }

    if (do_verbose)
        PRINTF("sync_found = %d\n", sync_found);

    return (!sync_found && prim_found);
}

/*
 *  check_for_syncs:
 *
 *  Check current command against recent history of commands
 */
static void
check_for_syncs(void)
{
    int             cur_cmd;
    int             tile;

    cur_cmd = SyncW.pc[SyncW.curr_cmd].cmd;
    tile = SyncW.pc[SyncW.curr_cmd].tile;

    /*
     * if current command is non-prim attribute, and no pipe/full sync
     * since last primitive, print warning. 
     */
    if (is_npa_cmd(cur_cmd) && no_sync_since_prim()) {
        PRINTF("vv WARNING: no pipe or full sync between last primitive\n");
        PRINTF("vv          and current attribute change.\n\n");
        ++nWarnings;
    }
    /*
     * if current command is load block/tile, and no load sync between last 
     * primitive and current command, print warning. 
     */
    if (is_lbt_cmd(cur_cmd) && no_load_sync_since_prim()) {
        PRINTF("vv WARNING: no load sync between last primitive\n");
        PRINTF("vv          and current load.\n\n");
        ++nWarnings;
    }
    /*
     * if current command is set tile/tile_size, and previous primitive
     * used that tile, and no tile sync before current command, print
     * warning. 
     */
    if (is_sts_cmd(cur_cmd) && no_tile_sync_since_prim(tile)) {
        PRINTF("vv WARNING: no tile sync between last primitive that\n");
        PRINTF("vv used this tile(%d) and current tile setting.\n\n", tile);
        ++nWarnings;
    }
}

static void
dump_command(char *cmd, u64 word0)
{
    PRINTF("\t%s ", cmd);
    PRINTF("\t\t\t%016llx\n", word0);
}

/*
 *  return dram pointer for alignment check 
 */
void
dump_img(u64 word0, RdpImageType type)
{
    Gsetimg        *ip = (Gsetimg *) RdpDl;
    u32             dp,
                    end_address,
                    nbytes_line;

    dp = ip->dram & 0x03ffffff;

    PRINTF("\t\tfmt = %s\n", decode_tex_fmt(ip->fmt));
    PRINTF("\t\tsiz = %s\n", decode_tex_size(ip->siz));
    PRINTF("\t\twid = %d\n", ip->wd);
    PRINTF("\t\tptr = %08x\n", (int)dp);

    if ((type == RDP_CLR_IMAGE || type == RDP_Z_IMAGE)) {
        nbytes_line = decode_size_nibbles(ip->siz) * ip->wd / 2;
        end_address = dp + nbytes_line * MAX_SCREEN_LINES;
        if (end_address > MAX_CZ_ADDRESS) {
            PRINTF("^^ ERROR: Color/Z image should lie in first 0x%x bytes.\n",
                   MAX_CZ_ADDRESS);
            ++nErrors;
        }
    }
    if (dp > MAX_ANY_ADDRESS) {
        PRINTF("^^ ERROR: Image pointer exceeds max memory (0x%x bytes).\n",
               MAX_ANY_ADDRESS);
        ++nErrors;
    }
    if (dp & 0x7) {
        PRINTF("^^ ERROR: Image not aligned to 8 Bytes.\n");
        ++nErrors;
    }
    if ((type == RDP_CLR_IMAGE || type == RDP_Z_IMAGE) && (dp & 0x3f)) {
        PRINTF("^^ WARNING: Image not aligned to 64 Bytes.\n");
        PRINTF("^^          Only necessary if RMW color AND Z.\n");
        ++nWarnings;
    }
    ++nAttClocks;
    ++RdpDl;
}

static void
dump_setcimg(u64 word0)
{
    dump_command("SETCIMG", word0);
    dump_img(word0, RDP_CLR_IMAGE);
}

static void
dump_setzimg(u64 word0)
{
    dump_command("SETZIMG", word0);
    dump_img(word0, RDP_Z_IMAGE);
}

static void
dump_settimg(u64 word0)
{
    dump_command("SETTIMG", word0);
    dump_img(word0, RDP_TEX_IMAGE);
}

static void
dump_setcolor(u64 word0)
{
    Gsetcolor      *sc = (Gsetcolor *) RdpDl;

    PRINTF("\t\tred   = %02x\n", (int)(sc->color >> 24) & 0xff);
    PRINTF("\t\tgreen = %02x\n", (int)(sc->color >> 16) & 0xff);
    PRINTF("\t\tblue  = %02x\n", (int)(sc->color >> 8) & 0xff);
    PRINTF("\t\talpha = %02x\n", (int)sc->color & 0xff);
    ++nAttClocks;
    RdpDl++;
}

static void
dump_setenvcolor(u64 word0)
{
    dump_command("SETENVCOLOR", word0);
    dump_setcolor(word0);
}

static void
dump_setprimcolor(u64 word0)
{
    dump_command("SETPRIMCOLOR", word0);
    dump_setcolor(word0);
    PRINTF("\t\tPrim Min Level = %02x (%f)\n",
           (int)(word0 >> 40) & 0xff, (f32) ((word0 >> 40) & 0xff) / 256.0);
    PRINTF("\t\tPrim Level Frac = %02x (%f)\n",
           (int)(word0 >> 32) & 0xff, (f32) ((word0 >> 32) & 0xff) / 256.0);
}

static void
dump_setprimdepth(u64 word0)
{
    dump_command("SETPRIMDEPTH", word0);
    PRINTF("\t\tprimZ    = %04x\n", (int)(word0 >> 16) & 0xffff);
    PRINTF("\t\tprimDelZ = %04x\n", (int)word0 & 0xffff);
    ++nAttClocks;
    RdpDl++;
}

static void
dump_setblendcolor(u64 word0)
{
    dump_command("SETBLENDCOLOR", word0);
    dump_setcolor(word0);
}

static void
dump_setfogcolor(u64 word0)
{
    dump_command("SETFOGCOLOR", word0);
    dump_setcolor(word0);
}

static void
dump_setfillcolor(u64 word0)
{
    dump_command("SETFILLCOLOR", word0);
    dump_setcolor(word0);
}

static void
dump_fillrect(u64 word0)
{
    Gfillrect      *fp = (Gfillrect *) RdpDl;

    dump_command("FILLRECT", word0);
    PRINTF("\t\tulx = %d\n", fp->x1);
    PRINTF("\t\tuly = %d\n", fp->y1);
    PRINTF("\t\tlrx = %d\n", fp->x0);
    PRINTF("\t\tlry = %d\n", fp->y0);

    nPrimClocks = (int) MAX((fp->x0 - fp->x1) * (fp->y0 - fp->y1),
                            9 * (fp->y0 - fp->y1));
    RdpDl++;
}

static void
dump_settile(u64 word0)
{
    Gsettile       *tp = (Gsettile *) RdpDl;

    dump_command("SETTILE", word0);
    PRINTF("\t\ttile   = %d\n", tp->tile);
    PRINTF("\t\tfmt    = %s (%d)\n", decode_tex_fmt(tp->fmt), tp->fmt);
    PRINTF("\t\tsize   = %s (%d)\n", decode_tex_size(tp->siz), tp->siz);
    PRINTF("\t\tline   = %d\n", tp->line);
    PRINTF("\t\ttmem   = %03x\n", tp->tmem);
    PRINTF("\t\tpal    = %d\n", tp->palette);
    PRINTF("\t\tclmpt  = %d\n", tp->ct);
    PRINTF("\t\tmirrt  = %d\n", tp->mt);
    PRINTF("\t\tmaskt  = %d\n", tp->maskt);
    PRINTF("\t\tshiftt = %d\n", tp->shiftt);
    PRINTF("\t\tclmps  = %d\n", tp->cs);
    PRINTF("\t\tmirrs  = %d\n", tp->ms);
    PRINTF("\t\tmasks  = %d\n", tp->masks);
    PRINTF("\t\tshifts = %d\n", tp->shifts);
    ++nAttClocks;
    ++RdpDl;
}

static void
dump_settilesize(u64 word0)
{
    Gsettilesize   *sp = (Gsettilesize *) RdpDl;

    dump_command("SETTILESIZE", word0);
    PRINTF("\t\ttile = %d\n", sp->tile);
    PRINTF("\t\tsl = %f  tl = %f\n", (f32) sp->sl / 4.0, (f32) sp->tl / 4.0);
    PRINTF("\t\tsh = %f  th = %f\n", (f32) sp->sh / 4.0, (f32) sp->th / 4.0);
    ++nAttClocks;
    ++RdpDl;
}

static void
dump_loadtile(u64 word0)
{
    Gloadtile      *lp = (Gloadtile *) RdpDl;

    dump_command("LOADTILE", word0);
    PRINTF("\t\tprim #%04d\n", (int)NumPrimitives++);
    PRINTF("\t\ttile = %d\n", lp->tile);
    PRINTF("\t\tsl = %f  tl = %f\n", (f32) lp->sl / 4.0, (f32) lp->tl / 4.0);
    PRINTF("\t\tsh = %f  th = %f\n", (f32) lp->sh / 4.0, (f32) lp->th / 4.0);

    RdpDl++;
    /*
     * XXX figure out number of load clocks 
     */
}

static void
dump_loadblock(u64 word0)
{
    Gloadblock     *lp = (Gloadblock *) RdpDl;

    dump_command("LOADBLOCK", word0);
    PRINTF("\t\tprim #%04d\n", (int)NumPrimitives++);
    PRINTF("\t\ttile = %d\n", lp->tile);
    PRINTF("\t\tsl = %f  tl  = %f\n", (f32) lp->sl, (f32) lp->tl);
    PRINTF("\t\tsh = %f  dxt = %f\n", (f32) lp->sh,
           (f32) lp->th / (f32) (1 << 11));

    RdpDl++;
    /*
     * XXX figure out number of load clocks 
     */
}

static void
dump_loadtlut(u64 word0)
{
    Gloadtlut      *lp = (Gloadtlut *) RdpDl;

    dump_command("LOADTLUT", word0);
    PRINTF("\t\tprim #%04d\n", (int)NumPrimitives++);
    PRINTF("\t\ttile = %d\n", lp->tile);
    PRINTF("\t\ttable index  = %d\n", lp->tl);
    PRINTF("\t\tlow index    = %d\n", lp->sl);
    PRINTF("\t\thigh index   = %d\n", lp->sh);
    RdpDl++;
}

static void
dump_setcombine(u64 word0)
{
    union {
        struct {
            u32             pad0:2;
            u32             cmd:6;
            u32             suba_r0:4;
            u32             mult_r0:5;
            u32             suba_a0:3;
            u32             mult_a0:3;
            u32             suba_r1:4;
            u32             mult_r1:5;
            u32             subb_r0:4;
            u32             subb_r1:4;
            u32             suba_a1:3;
            u32             mult_a1:3;
            u32             adds_r0:3;
            u32             subb_a0:3;
            u32             adds_a0:3;
            u32             adds_r1:3;
            u32             subb_a1:3;
            u32             adds_a1:3;
        } bf;
        u64             word;
    } w0;

    u32             suba,
                    subb,
                    mul,
                    add;
    char           *dsuba,
                   *dsubb,
                   *dmul,
                   *dadd;

    w0.word = word0;

    dump_command("SETCOMBINE", word0);

    suba = w0.bf.suba_r0;
    subb = w0.bf.subb_r0;
    mul = w0.bf.mult_r0;
    add = w0.bf.adds_r0;

    PRINTF("\n\t    Red/Green/Blue Controls, Cycle 0:\n");
    dsuba = suba_decode(suba);
    dsubb = subb_decode(subb);
    dmul = mul_decode(mul);
    dadd = add_decode(add);
    PRINTF("\t\t    (%s - %s) * %s + %s\n", dsuba, dsubb, dmul, dadd);

    suba = w0.bf.suba_r1;
    subb = w0.bf.subb_r1;
    mul = w0.bf.mult_r1;
    add = w0.bf.adds_r1;

    PRINTF("\n\t    Red/Green/Blue Controls, Cycle 1:\n");
    dsuba = suba_decode(suba);
    dsubb = subb_decode(subb);
    dmul = mul_decode(mul);
    dadd = add_decode(add);
    PRINTF("\t\t    (%s - %s) * %s + %s\n", dsuba, dsubb, dmul, dadd);

    suba = w0.bf.suba_a0;
    subb = w0.bf.subb_a0;
    mul = w0.bf.mult_a0;
    add = w0.bf.adds_a0;

    PRINTF("\n\t    Alpha Controls, Cycle 0:\n");
    dsuba = a_subab_decode(suba);
    dsubb = a_subab_decode(subb);
    dmul = a_mul_decode(mul);
    dadd = a_add_decode(add);
    PRINTF("\t\t    (%s - %s) * %s + %s\n", dsuba, dsubb, dmul, dadd);

    suba = w0.bf.suba_a1;
    subb = w0.bf.subb_a1;
    mul = w0.bf.mult_a1;
    add = w0.bf.adds_a1;

    PRINTF("\n\t    Alpha Controls, Cycle 1:\n");
    dsuba = a_subab_decode(suba);
    dsubb = a_subab_decode(subb);
    dmul = a_mul_decode(mul);
    dadd = a_add_decode(add);
    PRINTF("\t\t    (%s - %s) * %s + %s\n", dsuba, dsubb, dmul, dadd);

    ++nAttClocks;
    RdpDl++;
}

/*
 *  Dump other modes
 */
static void
dump_setothermode(u64 word0)
{
    union {
        struct {
            u32             pad0:2;
            u32             cmd:6;
            u32             atomic:1;
            u32             reserved0:1;
            u32             cycle_type:2;
            u32             persp_tex_en:1;
            u32             detail_tex_en:1;
            u32             sharpen_tex_en:1;
            u32             tex_lod_en:1;
            u32             en_tlut:1;
            u32             tlut_type:1;
            u32             sample_type:1;
            u32             mid_texel:1;
            u32             bi_lerp_0:1;
            u32             bi_lerp_1:1;
            u32             convert_one:1;
            u32             key_en:1;
            u32             rgb_dither_select:2;
            u32             alpha_dither_select:2;
            u32             reserved1:4;
            u32             b_m1a_0:2;
            u32             b_m1a_1:2;
            u32             b_m1b_0:2;
            u32             b_m1b_1:2;
            u32             b_m2a_0:2;
            u32             b_m2a_1:2;
            u32             b_m2b_0:2;
            u32             b_m2b_1:2;
            u32             reserved2:1;
            u32             force_blend:1;
            u32             alpha_cvg_select:1;
            u32             cvg_times_alpha:1;
            u32             z_mode:2;
            u32             cvg_dest:2;
            u32             color_on_cvg:1;
            u32             image_read_en:1;
            u32             z_update_en:1;
            u32             z_compare_en:1;
            u32             antialias_en:1;
            u32             z_source_sel:1;
            u32             dither_alpha_en:1;
            u32             alpha_compare_en:1;
        } bf;
        u64             word;
    } w0;

    w0.word = word0;

    dump_command("RDPSETOTHERMODE", word0);

    PRINTF("\t\t    atomic primitive .....  %d\n", w0.bf.atomic);
    PRINTF("\t\t    cycle_type............  %d ", w0.bf.cycle_type);

    if (w0.bf.cycle_type == 0)
        PRINTF("(one cycle)\n");
    else if (w0.bf.cycle_type == 1)
        PRINTF("(two cycle)\n");
    else if (w0.bf.cycle_type == 2)
        PRINTF("(copy)\n");
    else
        PRINTF("(fill)\n");

    PRINTF("\t\t    persp_tex_en..........  %d\n", w0.bf.persp_tex_en);
    PRINTF("\t\t    detail_tex_en.........  %d\n", w0.bf.detail_tex_en);
    PRINTF("\t\t    sharpen_tex_en........  %d\n", w0.bf.sharpen_tex_en);
    PRINTF("\t\t    tex_lod_en............  %d\n", w0.bf.tex_lod_en);
    PRINTF("\t\t    en_tlut...............  %d\n", w0.bf.en_tlut);
    PRINTF("\t\t    tlut_type.............  %d\n", w0.bf.tlut_type);
    PRINTF("\t\t    sample_type...........  %d\n", w0.bf.sample_type);
    PRINTF("\t\t    mid_texel.............  %d\n", w0.bf.mid_texel);
    PRINTF("\t\t    bi_lerp_0.............  %d\n", w0.bf.bi_lerp_0);
    PRINTF("\t\t    bi_lerp_1.............  %d\n", w0.bf.bi_lerp_1);
    PRINTF("\t\t    convert_one...........  %d\n", w0.bf.convert_one);
    PRINTF("\t\t    key_en................  %d\n", w0.bf.key_en);
    PRINTF("\t\t    rgb dither select.....  %d, %s\n", w0.bf.rgb_dither_select,
           decode_rgb_dither_select(w0.bf.rgb_dither_select));
    PRINTF("\t\t    alpha dither select...  %d, %s\n",
           w0.bf.alpha_dither_select,
           decode_alpha_dither_select(w0.bf.alpha_dither_select));

    PRINTF("\n\t    Blender Equation: (1a*1b + 2a*2b) / (1b + 2b)\n");

    PRINTF("\n\t    Blend Mux Controls, Cycle 0: ");
    PRINTF("(%d, %d, %d, %d)\n", w0.bf.b_m1a_0, w0.bf.b_m1b_0, w0.bf.b_m2a_0,
           w0.bf.b_m2b_0);

    PRINTF("\t\t    blend mult 1a.........  ");
    PRINTF("%s\n", blender_p_decode(w0.bf.b_m1a_0, w0.bf.cycle_type & 1, 0));

    PRINTF("\t\t    blend mult 1b.........  ");
    PRINTF("%s\n", blender_a_decode(w0.bf.b_m1b_0));

    PRINTF("\t\t    blend mult 2a.........  ");
    PRINTF("%s\n", blender_p_decode(w0.bf.b_m2a_0, w0.bf.cycle_type & 1, 0));

    PRINTF("\t\t    blend mult 2b.........  ");

    switch (w0.bf.b_m2b_0) {
        case 0:
            PRINTF("1.0 - %s\n", blender_a_decode(w0.bf.b_m1b_0));
            break;
        case 1:
            PRINTF("memory alpha\n");
            break;
        case 2:
            PRINTF("1.0\n");
            break;
        case 3:
            PRINTF("0.0\n");
            break;
    }

    PRINTF("\n\t    Blend Mux Controls, Cycle 1: ");
    PRINTF("(%d, %d, %d, %d)\n", w0.bf.b_m1a_1, w0.bf.b_m1b_1, w0.bf.b_m2a_1,
           w0.bf.b_m2b_1);

    PRINTF("\t\t    blend mult 1a.........  ");
    PRINTF("%s\n", blender_p_decode(w0.bf.b_m1a_1, w0.bf.cycle_type & 1, 1));

    PRINTF("\t\t    blend mult 1b.........  ");
    PRINTF("%s\n", blender_a_decode(w0.bf.b_m1b_1));

    PRINTF("\t\t    blend mult 2a.........  ");
    PRINTF("%s\n", blender_p_decode(w0.bf.b_m2a_1, w0.bf.cycle_type & 1, 0));

    PRINTF("\t\t    blend mult 2b.........  ");

    switch (w0.bf.b_m2b_1) {
        case 0:
            PRINTF("1.0 - %s\n", blender_a_decode(w0.bf.b_m1b_1));
            break;
        case 1:
            PRINTF("memory alpha\n");
            break;
        case 2:
            PRINTF("1.0\n");
            break;
        case 3:
            PRINTF("0.0\n");
            break;
    }

    PRINTF("\n");

    PRINTF("\t\t    force_blend...........  %d\n", w0.bf.force_blend);
    PRINTF("\t\t    alpha_cvg_select......  %d\n", w0.bf.alpha_cvg_select);
    PRINTF("\t\t    cvg_times_alpha.......  %d\n", w0.bf.cvg_times_alpha);

    PRINTF("\t\t    z_mode................  ");
    switch (w0.bf.z_mode) {
        case 0:
            PRINTF("opaque\n");
            break;
        case 1:
            PRINTF("interpenetrating\n");
            break;
        case 2:
            PRINTF("transparent\n");
            break;
        case 3:
            PRINTF("decal\n");
            break;
    }

    PRINTF("\t\t    cvg_dest..............  ");
    switch (w0.bf.cvg_dest) {
        case 0:
            PRINTF("clamp(normal)\n");
            break;
        case 1:
            PRINTF("wrap\n");
            break;
        case 2:
            PRINTF("zap(force to full cvg)\n");
            break;
        case 3:
            PRINTF("save(don't overwrite mem cvg)\n");
            break;
    }

    PRINTF("\t\t    color_on_cvg..........  %d\n", w0.bf.color_on_cvg);
    PRINTF("\t\t    image_read_en.........  %d\n", w0.bf.image_read_en);
    PRINTF("\t\t    z_update_en...........  %d\n", w0.bf.z_update_en);
    PRINTF("\t\t    z_compare_en..........  %d\n", w0.bf.z_compare_en);
    PRINTF("\t\t    antialias_en..........  %d\n", w0.bf.antialias_en);
    PRINTF("\t\t    z_source_sel..........  %s\n",
           w0.bf.z_source_sel ? "prim z" : "pixel z");
    PRINTF("\t\t    dither_alpha_en.......  %d\n", w0.bf.dither_alpha_en);
    PRINTF("\t\t    alpha_compare_en......  %d\n", w0.bf.alpha_compare_en);
    ++nAttClocks;
    RdpDl++;
}

static void
dump_setscissor(u64 word0)
{

    Scissorulx = (f32) ((word0 >> 44) & 0xfff) / 4.0;
    Scissoruly = (f32) ((word0 >> 32) & 0xfff) / 4.0;
    Scissorlrx = (f32) ((word0 >> 12) & 0xfff) / 4.0;
    Scissorlry = (f32) (word0 & 0xfff) / 4.0;

    dump_command("SETSCISSOR", word0);
    PRINTF("\t\txH (ulx) = %.3f  yH (uly) = %.3f\n", Scissorulx, Scissoruly);
    PRINTF("\t\txL (lrx) = %.3f  yL (lry) = %.3f\n", Scissorlrx, Scissorlry);

    if (word0 & 0x02000000) {
        PRINTF("\t\tscissor field ON, ");
        if (word0 & 0x01000000)
            PRINTF("keep odd lines\n");
        else
            PRINTF("keep even lines\n");
    } else {
        PRINTF("\t\tscissor field OFF\n");
    }
    ++nAttClocks;
    RdpDl++;
}

static void
dump_setconvert(u64 word0)
{
    dump_command("SETCONVERT", word0);
    PRINTF("\t\tK0 = %03x\n", (int)(word0 >> 45) & 0x1ff);
    PRINTF("\t\tK1 = %03x\n", (int)(word0 >> 36) & 0x1ff);
    PRINTF("\t\tK2 = %03x\n", (int)(word0 >> 27) & 0x1ff);
    PRINTF("\t\tK3 = %03x\n", (int)(word0 >> 18) & 0x1ff);
    PRINTF("\t\tK4 = %03x\n", (int)(word0 >> 9) & 0x1ff);
    PRINTF("\t\tK5 = %03x\n", (int)word0 & 0x1ff);
    ++nAttClocks;
    RdpDl++;
}

static void
dump_setkeyr(u64 word0)
{
    dump_command("SETKEYR", word0);
    PRINTF("\t\tRed width  = %03x\n", (int)(word0 >> 16) & 0xfff);
    PRINTF("\t\tRed center = %02x\n", (int)(word0 >> 8) & 0xff);
    PRINTF("\t\tRed scale  = %02x\n", (int)word0 & 0xff);
    ++nAttClocks;
    RdpDl++;
}

static void
dump_setkeygb(u64 word0)
{
    dump_command("SETKEYGB", word0);
    PRINTF("\t\tGrn width  = %03x\n", (int)(word0 >> 44) & 0xfff);
    PRINTF("\t\tGrn center = %02x\n", ((int)word0 >> 24) & 0xff);
    PRINTF("\t\tGrn scale  = %02x\n", (int)(word0 >> 16) & 0xff);
    PRINTF("\t\tBlu width  = %03x\n", (int)(word0 >> 32) & 0xfff);
    PRINTF("\t\tBlu center = %02x\n", (int)(word0 >> 8) & 0xff);
    PRINTF("\t\tBlu scale  = %02x\n", (int)word0 & 0xff);
    ++nAttClocks;
    RdpDl++;
}

static void
dump_fullsync(u64 word0)
{
    dump_command("RDPFULLSYNC", word0);
    nSyncClocks += FULLSYNC_CLOCKS;
    RdpDl++;
}

static void
dump_tilesync(u64 word0)
{
    dump_command("RDPTILESYNC", word0);
    nSyncClocks += TILESYNC_CLOCKS;
    RdpDl++;
}

static void
dump_pipesync(u64 word0)
{
    dump_command("RDPPIPESYNC", word0);
    nSyncClocks += PIPESYNC_CLOCKS;
    RdpDl++;
}

static void
dump_loadsync(u64 word0)
{
    dump_command("RDPLOADSYNC", word0);
    nSyncClocks += LOADSYNC_CLOCKS;
    RdpDl++;
}

static void
dump_noop(u64 word0)
{
    dump_command("NOOP", word0);
    nAttClocks++;
    RdpDl++;
}

static void
dump_trect(u64 word0)
{
    Gtexrect       *tp = (Gtexrect *) RdpDl;

    f32             xl,
                    yl,
                    xh,
                    yh,
                    area;

    xl = (f32) tp->xl / 4.0;
    xh = (f32) tp->xh / 4.0;
    yl = (f32) tp->yl / 4.0;
    yh = (f32) tp->yh / 4.0;

    /*
     *  Check for unscissored rectangles, user is suppossed
     *  to scissor.
     */
    if (!INRANGE(xl, Scissorulx, Scissorlrx) ||
        !INRANGE(xh, Scissorulx, Scissorlrx) ||
        !INRANGE(yl, Scissoruly, Scissorlry) ||
        !INRANGE(yh, Scissoruly, Scissorlry)) {
        PRINTF("\n\t\tvv ERROR, rectangle to scissored\n");
        ++nErrors;
    }
    PRINTF("\t\ttile = %d\n", tp->tile);
    PRINTF("\t\txl = %f  yl = %f\n", xl, yl);
    PRINTF("\t\txh = %f  yh = %f\n", xh, yh);
    PRINTF("\t\ts  = %f  t  = %f\n",
           (f32) tp->s / 32.0, (f32) tp->t / 32.0);
    PRINTF("\t\tdsdx = %f  dtdy = %f\n", (f32) tp->dsdx / 1024.0,
           (f32) tp->dtdy / 1024.0);

    area = (xl - xh) * (yl - yh);

    if (do_pr_area)
        PRINTF("\t\tarea =  %f\n", area);

    nPrimClocks += area;
    RdpDl += 2;
}

static void
dump_texrectflip(u64 word0)
{
    dump_command("TEXRECTFLIP", word0);
    PRINTF("\t\tprim #%04d\n", (int)NumPrimitives++);
    dump_trect(word0);
}

static void
dump_texrect(u64 word0)
{
    dump_command("TEXRECT", word0);
    PRINTF("\t\tprim #%04d\n", (int)NumPrimitives++);
    dump_trect(word0);
}

static void
dump_edge_coef(u64 word0)
{
    u32            *buffer;
    u8              dir,
                    level,
                    tile;
    s16             YL,
                    YM,
                    YH;
    f32             yl,
                    ym,
                    yh;
    s32             XL,
                    XH,
                    XM;
    f32             xl,
                    xh,
                    xm;
    s32             DxLDy,
                    DxHDy,
                    DxMDy;
    f32             dxhdy,
                    dxldy,
                    dxmdy,
                    area,
                    real_area,
                    x1,
                    x2,
                    dxmid;
    f32             span,
                    span0,
                    span1,
                    span2,
                    span3;
    s32             i,
                    worst_clks,
                    small_spans;

    buffer = (u32 *) RdpDl;

    dir = (buffer[0] & 0x00800000) >> 23;
    level = (buffer[0] & 0x00380000) >> 19;
    tile = (buffer[0] & 0x00070000) >> 25;

    YL = buffer[0] & 0x0000ffff;
    YM = (buffer[1] & 0xffff0000) >> 16;
    YH = buffer[1] & 0x0000ffff;

    XL = *((s32 *) &(buffer[2]));
    DxLDy = *((s32 *) &(buffer[3]));
    XH = *((s32 *) &(buffer[4]));
    DxHDy = *((s32 *) &(buffer[5]));
    XM = *((s32 *) &(buffer[6]));
    DxMDy = *((s32 *) &(buffer[7]));

    _yl = yl = (f32) YL / 4.0;
    _ym = ym = (f32) YM / 4.0;
    _yh = yh = (f32) YH / 4.0;

    xl = (f32) XL / 65536.0;
    xm = (f32) XM / 65536.0;
    xh = (f32) XH / 65536.0;

    dxhdy = (f32) DxHDy / 65536.0;
    dxldy = (f32) DxLDy / 65536.0;
    dxmdy = (f32) DxMDy / 65536.0;

    _delxm = xl - (xh + (ym - yh) * dxhdy);

    PRINTF("\n\t    Edge coefficients:\n");
    PRINTF("\t\tdir = %d level = %d tile = %d\n\n", dir, level, tile);

    PRINTF("\t\tYH     = %04x (%8.3f)\n", YH, yh);
    PRINTF("\t\tYM     = %04x (%8.3f)\n", YM, ym);
    PRINTF("\t\tYL     = %04x (%8.3f)\n", YL, yl);

    PRINTF("\t\tXH     = %08x (%8.3f)\n", (int)XH, xh);
    PRINTF("\t\tXM     = %08x (%8.3f)\n", (int)XM, xm);
    PRINTF("\t\tXL     = %08x (%8.3f)\n", (int)XL, xl);

    PRINTF("\t\tDxHDy  = %08x (%8.3f)\n", (int)DxHDy, dxhdy);
    PRINTF("\t\tDxMDy  = %08x (%8.3f)\n", (int)DxMDy, dxmdy);
    PRINTF("\t\tDxLDy  = %08x (%8.3f)\n", (int)DxLDy, dxldy);

    Triy[0] = yh;
    Triy[1] = ym;
    Triy[2] = yl;

    Trix[0] = xh + (yh - (int) yh) * dxhdy;
    Trix[1] = xl;
    Trix[2] = Trix[0] + dxhdy * (yl - yh);

    for (area = i = 0; i < 3; i++)
        area += Trix[i] * Triy[(i + 1) % 3] - Triy[i] * Trix[(i + 1) % 3];
    area /= 2.0;

    area = ABS(area);

    /*
     *  find area one line at a time, figure out which spans are
     *  nine clock minimum 
     */

    real_area = 0;
    x1 = xh;
    x2 = xm;
    dxmid = dxmdy;
    small_spans = 0;

    for (i = (int) yh; i < (int) yl; i++) {
        if (i == (int) ym)
            dxmid = dxldy;

        span0 = ABS(x1 - x2);
        span1 = ABS(x1 - x2 + 0.75 * dxmid);
        span2 = ABS(x1 + 0.75 * dxhdy - x2);
        span3 = ABS(x1 + 0.75 * dxhdy - x2 + 0.75 * dxmid);

        span = MAX(span0, MAX(span1, MAX(span2, span3)));

        if (span < 9.0) {
            span = 9.0;
            small_spans++;
        }
        real_area += span;

        x2 += dxmid;
        x1 += dxhdy;
    }

    worst_clks = 22 + real_area;
    nPrimClocks += worst_clks;

    if (do_pr_area) {
        PRINTF("\n");
        PRINTF("\t\t     x0 (%f) y0 (%f)\n", Trix[0], Triy[0]);
        PRINTF("\t\t     x1 (%f) y1 (%f)\n", Trix[1], Triy[1]);
        PRINTF("\t\t     x2 (%f) y2 (%f)\n", Trix[2], Triy[2]);

        PRINTF("\t\t     number of spans.............  %d\n", (int) yl - (int) yh);
        PRINTF("\t\t     number of spans @ 9 clks....  %d\n", (int)small_spans);
        PRINTF("\t\t     area........................  %f\n", area);
        PRINTF("\t\t     appx. num clocks............  %d\n", (int)worst_clks);
        PRINTF("\t\t     %% overhead..................  %f\n",
               (f32) (worst_clks - area) / (f32) worst_clks * 100.0);
    }
    /*
     *   check for illegal X/Y magnitude
     */
    if (xl > MAX_COORD_X || xl < MIN_COORD_X) {
        PRINTF("\n\t\t^^ ERROR, xl(%f) out of range (%f,%f)\n",
               xl, MIN_COORD_X, MAX_COORD_X);
        nErrors++;
    }
    if (xh > MAX_COORD_X || xh < MIN_COORD_X) {
        PRINTF("\n\t\t^^ ERROR, xh(%f) out of range (%f,%f)\n",
               xh, MIN_COORD_X, MAX_COORD_X);
        nErrors++;
    }
    if (xm > MAX_COORD_X || xm < MIN_COORD_X) {
        PRINTF("\n\t\t^^ ERROR, xm(%f) out of range (%f,%f)\n",
               xm, MIN_COORD_X, MAX_COORD_X);
        nErrors++;
    }
    if (yl > MAX_COORD_Y || yl < MIN_COORD_Y) {
        PRINTF("\n\t\t^^ ERROR, yl(%f) out of range (%f,%f)\n",
               yl, MIN_COORD_Y, MAX_COORD_Y);
        nErrors++;
    }
    if (yh > MAX_COORD_Y || yh < MIN_COORD_Y) {
        PRINTF("\n\t\t^^ ERROR, yh(%f) out of range (%f,%f)\n",
               yh, MIN_COORD_Y, MAX_COORD_Y);
        nErrors++;
    }
    if (ym > MAX_COORD_Y || ym < MIN_COORD_Y) {
        PRINTF("\n\t\t^^ ERROR, ym(%f) out of range (%f,%f)\n",
               ym, MIN_COORD_Y, MAX_COORD_Y);
        nErrors++;
    }
    /*
     *   check for illegal slope/yh combinations
     */

    if (dir == 1) {             /*
                                 * * * left major  
                                 */
        if ((dxmdy < dxhdy) && (YH != YM)) {
            PRINTF("\n\t\t^^ WARNING, dxmdy < dxhdy for left major triangle\n");
            ++nWarnings;
        }
        if ((dxldy > dxmdy) && (YH != YM)) {
            PRINTF("\n\t\t^^ WARNING, dxldy > dxmdy for left major triangle\n");
            ++nWarnings;
        }
    } else
        /*
         * right major 
         */
    {
        if ((dxhdy < dxmdy) && (YH != YM)) {
            PRINTF("\n\t\t^^ WARNING, dxhdy < dxmdy for right major triangle\n");
            ++nWarnings;
        }
        if ((dxmdy > dxldy) && (YH != YM)) {
            PRINTF("\n\t\t^^ WARNING, dxmdy > dxldy for right major triangle\n");
            ++nWarnings;
        }
    }

    if (YM < YH) {
        PRINTF("\n\t\t^^ WARNING, YM < YH\n");
        ++nWarnings;
    }
    if (YL < YH) {
        PRINTF("\n\t\t^^ WARNING, YL < YH\n");
        ++nWarnings;
    }
    if (YL < YM) {
        PRINTF("\n\t\t^^ WARNING, YL < YM\n");
        ++nWarnings;
    }
    RdpDl += 4;
}

static f32
get_shade_vert1(f32 base, f32 ym, f32 yh, f32 dade, f32 delxm, f32 dadx)
{
    return (RGBA2F32(base) + (f32) ((int) ym - (int) yh) *
            RGBA2F32(dade) + (f32) delxm * RGBA2F32(dadx));
}

static f32
get_shade_vert2(f32 base, f32 yl, f32 yh, f32 dade)
{
    return (RGBA2F32(base) + (f32) ((int) yl - (int) yh) * RGBA2F32(dade));
}

static void
dump_shade_coef(void)
{
    u16            *buffer;
    s32             red,
                    green,
                    blue,
                    alpha;
    s32             DrDx,
                    DgDx,
                    DbDx,
                    DaDx;
    s32             DrDe,
                    DgDe,
                    DbDe,
                    DaDe;
    s32             DrDy,
                    DgDy,
                    DbDy,
                    DaDy;

    buffer = (u16 *) RdpDl;

    red = buffer[0] << 16;
    red |= buffer[8];
    green = buffer[1] << 16;
    green |= buffer[9];
    blue = buffer[2] << 16;
    blue |= buffer[10];
    alpha = buffer[3] << 16;
    alpha |= buffer[11];

    DrDx = buffer[4] << 16;
    DrDx |= buffer[12];
    DgDx = buffer[5] << 16;
    DgDx |= buffer[13];
    DbDx = buffer[6] << 16;
    DbDx |= buffer[14];
    DaDx = buffer[7] << 16;
    DaDx |= buffer[15];

    DrDe = buffer[16] << 16;
    DrDe |= buffer[24];
    DgDe = buffer[17] << 16;
    DgDe |= buffer[25];
    DbDe = buffer[18] << 16;
    DbDe |= buffer[26];
    DaDe = buffer[19] << 16;
    DaDe |= buffer[27];

    DrDy = buffer[20] << 16;
    DrDy |= buffer[28];
    DgDy = buffer[21] << 16;
    DgDy |= buffer[29];
    DbDy = buffer[22] << 16;
    DbDy |= buffer[30];
    DaDy = buffer[23] << 16;
    DaDy |= buffer[31];

    PRINTF("\n\t    Shade coefficients:\n");
    PRINTF("\t\tred  %08x (%8.6f)  green %08x (%8.6f)\n",
           (int)red, RGBA2F32(red), (int)green, RGBA2F32(green));
    PRINTF("\t\tblue %08x (%8.6f)  alpha %08x (%8.6f)\n",
           (int)blue, RGBA2F32(blue), (int)alpha, RGBA2F32(alpha));
    PRINTF("\n");

    PRINTF("\t\tDrDe %08x (%8.6f)  DgDe %08x (%8.6f)\n",
           (int)DrDe, RGBA2F32(DrDe), (int)DgDe, RGBA2F32(DgDe));
    PRINTF("\t\tDbDe %08x (%8.6f)  DaDe %08x (%8.6f)\n",
           (int)DbDe, RGBA2F32(DbDe), (int)DaDe, RGBA2F32(DaDe));
    PRINTF("\n");

    PRINTF("\t\tDrDx %08x (%8.6f)  DgDx %08x (%8.6f)\n",
           (int)DrDx, RGBA2F32(DrDx), (int)DgDx, RGBA2F32(DgDx));
    PRINTF("\t\tDbDx %08x (%8.6f)  DaDx %08x (%8.6f)\n",
           (int)DbDx, RGBA2F32(DbDx), (int)DaDx, RGBA2F32(DaDx));
    PRINTF("\n");

    PRINTF("\t\tDrDy %08x (%8.6f)  DgDy %08x (%8.6f)\n",
           (int)DrDy, RGBA2F32(DrDy), (int)DgDy, RGBA2F32(DgDy));
    PRINTF("\t\tDbDy %08x (%8.6f)  DaDy %08x (%8.6f)\n",
           (int)DbDy, RGBA2F32(DbDy), (int)DaDy, RGBA2F32(DaDy));
    PRINTF("\n");

    PRINTF("\t\tR0 %5.2f\tG0 %5.2f\tB0 %5.2f\tA0 %5.2f\n",
           RGBA2F32(red),
           RGBA2F32(green),
           RGBA2F32(blue),
           RGBA2F32(alpha));
    PRINTF("\t\tR1 %5.2f\tG1 %5.2f\tB1 %5.2f\tA1 %5.2f\n",
           get_shade_vert1(red, _ym, _yh, DrDe, _delxm, DrDx),
           get_shade_vert1(green, _ym, _yh, DgDe, _delxm, DgDx),
           get_shade_vert1(blue, _ym, _yh, DbDe, _delxm, DbDx),
           get_shade_vert1(alpha, _ym, _yh, DaDe, _delxm, DaDx));
    PRINTF("\t\tR2 %5.2f\tG2 %5.2f\tB2 %5.2f\tA2 %5.2f\n",
           get_shade_vert2(red, _yl, _yh, DrDe),
           get_shade_vert2(green, _yl, _yh, DgDe),
           get_shade_vert2(blue, _yl, _yh, DbDe),
           get_shade_vert2(alpha, _yl, _yh, DaDe));
    PRINTF("\n");

    RdpDl += 8;
}

static f32
get_txtr_vert1(f32 base, f32 ym, f32 yh, f32 dade, f32 delxm, f32 dadx)
{
    return (ST2F32(base) + (f32) ((int) ym - (int) yh) *
            ST2F32(dade) + (f32) delxm * ST2F32(dadx));
}

static f32
get_txtr_vert2(f32 base, f32 yl, f32 yh, f32 dade)
{
    return (ST2F32(base) + (f32) ((int) yl - (int) yh) * ST2F32(dade));
}

static f32
get_w_vert1(f32 base, f32 ym, f32 yh, f32 dade, f32 delxm, f32 dadx)
{
    return (W2F32(base) + (f32) ((int) ym - (int) yh) *
            W2F32(dade) + (f32) delxm * W2F32(dadx));
}

static f32
get_w_vert2(f32 base, f32 yl, f32 yh, f32 dade)
{
    return (W2F32(base) + (f32) ((int) yl - (int) yh) * W2F32(dade));
}

static void
dump_txtr_coef(void)
{
    u16            *buffer;
    s32             s,
                    t,
                    w;
    s32             dxs,
                    dxt,
                    dxw;
    s32             des,
                    det,
                    dew;
    s32             dys,
                    dyt,
                    dyw;

    buffer = (u16 *) RdpDl;

    s = buffer[0] << 16;
    s |= buffer[8];
    t = buffer[1] << 16;
    t |= buffer[9];
    w = buffer[2] << 16;
    w |= buffer[10];

    dxs = buffer[4] << 16;
    dxs |= buffer[12];
    dxt = buffer[5] << 16;
    dxt |= buffer[13];
    dxw = buffer[6] << 16;
    dxw |= buffer[14];

    des = buffer[16] << 16;
    des |= buffer[24];
    det = buffer[17] << 16;
    det |= buffer[25];
    dew = buffer[18] << 16;
    dew |= buffer[26];

    dys = buffer[20] << 16;
    dys |= buffer[28];
    dyt = buffer[21] << 16;
    dyt |= buffer[29];
    dyw = buffer[22] << 16;
    dyw |= buffer[30];

    PRINTF("\n\t    Texture coefficients:\n");
    PRINTF("\t\tS    %08x (%8.6f)  T    %08x (%8.6f)\n",
           (int)s, ST2F32(s), (int)t, ST2F32(t));
    PRINTF("\t\tdSdE %08x (%8.6f)  dTdE %08x (%8.6f)\n",
           (int)des, ST2F32(des), (int)det, ST2F32(det));
    PRINTF("\t\tdSdX %08x (%8.6f)  dTdX %08x (%8.6f)\n",
           (int)dxs, ST2F32(dxs), (int)dxt, ST2F32(dxt));
    PRINTF("\t\tdSdY %08x (%8.6f)  dTdY %08x (%8.6f)\n",
           (int)dys, ST2F32(dys), (int)dyt, ST2F32(dyt));
    PRINTF("\n");

    PRINTF("\t\tW    %08x (%8.6f)\n", (int)w, W2F32(w));
    PRINTF("\t\tdWdE %08x (%8.6f)\n", (int)dew, W2F32(dew));
    PRINTF("\t\tdWdX %08x (%8.6f)\n", (int)dxw, W2F32(dxw));
    PRINTF("\t\tdWdY %08x (%8.6f)\n", (int)dyw, W2F32(dyw));
    PRINTF("\n");

    PRINTF("\t\tS0 %5.2f\tT0 %5.2f\tW0 %9.6f\n",
           ST2F32(s), ST2F32(t), ST2F32(w));
    PRINTF("\t\tS1 %5.2f\tT1 %5.2f\tW1 %9.6f\n",
           get_txtr_vert1(s, _ym, _yh, des, _delxm, dxs),
           get_txtr_vert1(t, _ym, _yh, det, _delxm, dxt),
           get_w_vert1(w, _ym, _yh, dew, _delxm, dxw));
    PRINTF("\t\tS2 %5.2f\tT2 %5.2f\tW2 %9.6f\n",
           get_txtr_vert2(s, _yl, _yh, des),
           get_txtr_vert2(t, _yl, _yh, det),
           get_w_vert2(w, _yl, _yh, dew));
    PRINTF("\n");

    RdpDl += 8;
}

f32
get_z_vert1(f32 base, f32 ym, f32 yh, f32 dade, f32 delxm, f32 dadx)
{
    return (Z2F32(base) + (f32) ((int) ym - (int) yh) *
            Z2F32(dade) + (f32) delxm * Z2F32(dadx));
}

f32
get_z_vert2(f32 base, f32 yl, f32 yh, f32 dade)
{
    return (Z2F32(base) + (f32) ((int) yl - (int) yh) * Z2F32(dade));
}

static void
dump_zbuff_coef(void)
{
    u32            *buffer;
    s32             z,
                    dxz,
                    dez,
                    dyz;

    buffer = (u32 *) RdpDl;

    z = buffer[0];
    dxz = buffer[1];
    dez = buffer[2];
    dyz = buffer[3];

    PRINTF("\n\t    Z-buffer coefficients:\n");
    PRINTF("\t\tZ    %08x (%8.6f)\n", (int)z, Z2F32(z));
    PRINTF("\t\tdZdE %08x (%8.6f)\n", (int)dez, Z2F32(dez));
    PRINTF("\t\tdZdX %08x (%8.6f)\n", (int)dxz, Z2F32(dxz));
    PRINTF("\t\tdZdY %08x (%8.6f)\n", (int)dyz, Z2F32(dyz));
    PRINTF("\n");

    PRINTF("\t\tZ0 %8.6f\n", Z2F32(z));
    PRINTF("\t\tZ1 %8.6f\n", get_z_vert1(z, _ym, _yh, dez, _delxm, dxz));
    PRINTF("\t\tZ2 %8.6f\n", get_z_vert2(z, _yl, _yh, dez));
    PRINTF("\n");

    RdpDl += 2;
}

static void
dump_tri_fill(u64 word0)
{
    dump_command("G_TRI_FILL", word0);
    PRINTF("\t\tprim #%04d\n", (int)NumPrimitives++);
    dump_edge_coef(word0);
}

static void
dump_tri_shade(u64 word0)
{
    dump_command("G_TRI_SHADE", word0);
    PRINTF("\t\tprim #%04d\n", (int)NumPrimitives++);
    dump_edge_coef(word0);
    dump_shade_coef();
}

static void
dump_tri_txtr(u64 word0)
{
    dump_command("G_TRI_TXTR", word0);
    PRINTF("\t\tprim #%04d\n", (int)NumPrimitives++);
    dump_edge_coef(word0);
    dump_txtr_coef();
}

static void
dump_tri_shade_txtr(u64 word0)
{
    dump_command("G_TRI_SHADE_TXTR", word0);
    PRINTF("\t\tprim #%04d\n", (int)NumPrimitives++);
    dump_edge_coef(word0);
    dump_shade_coef();
    dump_txtr_coef();
}

static void
dump_tri_fill_zbuff(u64 word0)
{
    dump_command("G_TRI_FILL_ZBUFF", word0);
    PRINTF("\t\tprim #%04d\n", (int)NumPrimitives++);
    dump_edge_coef(word0);
    dump_zbuff_coef();
}

static void
dump_tri_shade_zbuff(u64 word0)
{
    dump_command("G_TRI_SHADE_ZBUFF", word0);
    PRINTF("\t\tprim #%04d\n", (int)NumPrimitives++);
    dump_edge_coef(word0);
    dump_shade_coef();
    dump_zbuff_coef();
}

static void
dump_tri_txtr_zbuff(u64 word0)
{
    dump_command("G_TRI_TXTR_ZBUFF", word0);
    PRINTF("\t\tprim #%04d\n", (int)NumPrimitives++);
    dump_edge_coef(word0);
    dump_txtr_coef();
    dump_zbuff_coef();
}

static void
dump_tri_shade_txtr_zbuff(u64 word0)
{
    dump_command("G_TRI_SHADE_TXTR_ZBUFF", word0);
    PRINTF("\t\tprim #%04d\n", (int)NumPrimitives++);
    dump_edge_coef(word0);
    dump_shade_coef();
    dump_txtr_coef();
    dump_zbuff_coef();
}

/*
 * Parse and dump a RDP-type display list command.
 */
void
read_rdp(u64 word0)
{
    u8              op;

    /*
     *  Command ID
     */
    op = (u8) ((word0 & 0xff00000000000000LLU) >> 56);

    /*
     *  Classify command for next sync check
     */
    SyncW.pc[SyncW.curr_cmd].cmd = op;
    SyncW.pc[SyncW.curr_cmd].npa = is_npa_cmd(op);
    SyncW.pc[SyncW.curr_cmd].prim = is_prim_cmd(op);
    SyncW.pc[SyncW.curr_cmd].sts = is_sts_cmd(op);
    SyncW.pc[SyncW.curr_cmd].lbt = is_lbt_cmd(op);

    /*
     * save tile number 
     */
    if (is_tex_rect_cmd(op) || is_lbt_cmd(op) || is_sts_cmd(op))
        SyncW.pc[SyncW.curr_cmd].tile = ((word0 & 0x0000000007000000LLU) >> 24);
    else if (is_tri_cmd(op))
        SyncW.pc[SyncW.curr_cmd].tile = ((word0 & 0x0007000000000000LLU) >> 48);
    else
        SyncW.pc[SyncW.curr_cmd].tile = 0;

    check_for_syncs();

    /*
     * increment/clamp indexes 
     */
    ++SyncW.curr_cmd;
    if (SyncW.curr_cmd > (MAX_HISTORY - 1))
        SyncW.curr_cmd = 0;

    ++SyncW.n_cmd;
    if (SyncW.n_cmd > MAX_HISTORY)
        SyncW.n_cmd = MAX_HISTORY;

    /*
     *  Formatted Dump, error checking of each command
     */
    switch (op) {

        case G_SETCIMG:
            dump_setcimg(word0);
            break;

        case G_SETZIMG:
            dump_setzimg(word0);
            break;

        case G_SETTIMG:
            dump_settimg(word0);
            break;

        case G_SETCOMBINE:
            dump_setcombine(word0);
            break;

        case G_SETENVCOLOR:
            dump_setenvcolor(word0);
            break;

        case G_SETPRIMCOLOR:
            dump_setprimcolor(word0);
            break;

        case G_SETBLENDCOLOR:
            dump_setblendcolor(word0);
            break;

        case G_SETFOGCOLOR:
            dump_setfogcolor(word0);
            break;

        case G_SETFILLCOLOR:
            dump_setfillcolor(word0);
            break;

        case G_FILLRECT:
            dump_fillrect(word0);
            break;

        case G_SETTILE:
            dump_settile(word0);
            break;

        case G_LOADTILE:
            dump_loadtile(word0);
            break;

        case G_LOADBLOCK:
            dump_loadblock(word0);
            break;

        case G_SETTILESIZE:
            dump_settilesize(word0);
            break;

        case G_LOADTLUT:
            dump_loadtlut(word0);
            break;

        case G_RDPSETOTHERMODE:
            dump_setothermode(word0);
            break;

        case G_SETPRIMDEPTH:
            dump_setprimdepth(word0);
            break;

        case G_SETSCISSOR:
            dump_setscissor(word0);
            break;

        case G_SETCONVERT:
            dump_setconvert(word0);
            break;

        case G_SETKEYR:
            dump_setkeyr(word0);
            break;

        case G_SETKEYGB:
            dump_setkeygb(word0);
            break;

        case G_RDPFULLSYNC:
            dump_fullsync(word0);
            break;

        case G_RDPTILESYNC:
            dump_tilesync(word0);
            break;

        case G_RDPPIPESYNC:
            dump_pipesync(word0);
            break;

        case G_RDPLOADSYNC:
            dump_loadsync(word0);
            break;

        case G_NOOP:
            dump_noop(word0);
            break;

        case G_TEXRECTFLIP:
            dump_texrectflip(word0);
            break;

        case G_TEXRECT:
            dump_texrect(word0);
            break;

        case G_TRI_FILL:
            dump_tri_fill(word0);
            break;

        case G_TRI_SHADE:
            dump_tri_shade(word0);
            break;

        case G_TRI_TXTR:
            dump_tri_txtr(word0);
            break;

        case G_TRI_SHADE_TXTR:
            dump_tri_shade_txtr(word0);
            break;

        case G_TRI_FILL_ZBUFF:
            dump_tri_fill_zbuff(word0);
            break;

        case G_TRI_SHADE_ZBUFF:
            dump_tri_shade_zbuff(word0);
            break;

        case G_TRI_TXTR_ZBUFF:
            dump_tri_txtr_zbuff(word0);
            break;

        case G_TRI_SHADE_TXTR_ZBUFF:
            dump_tri_shade_txtr_zbuff(word0);
            break;

        default:
            PRINTF("\tUNKNOWN %02x ", op);
            PRINTF("\t\t\t%016llx\n", word0);
            PRINTF("\n\t\t** ERROR, unknown command\n");
            nErrors++;
            nAttClocks++;
            RdpDl++;
            break;
    }

    PRINTF("\n");
}

void DumpRdpDL(u32* rdp_dl, u32 nbytes)
{
    u32   curInts = 0;
    u32   numInts = nbytes >> 2;

    while(curInts < numInts)
    {
      PRINTF("%05d: 0x",(int)curInts >> 1);   
      PRINTF("%08x", (int)rdp_dl[curInts]);
      PRINTF("%08x \n", (int)rdp_dl[curInts+1]);
      curInts += 2;
    }
}

/*
 * external interface to rdp display list dump
 */
void ParseRdpDL(u64 *rdp_dl, u32 nbytes, u8 flags)
{
    f32             total_clks;
    u32             ncommands,
                    nwords;

    ncommands = 0;
    nwords = nbytes / sizeof(u64);

    do_verbose = flags & GU_PARSERDP_VERBOSE;
    do_pr_area = flags & GU_PARSERDP_PRAREA;
    do_pr_histo = flags & GU_PARSERDP_PRHISTO;
    if(flags & GU_PARSERDP_DUMPONLY)
    {
        DumpRdpDL((u32*)rdp_dl,nbytes);
        return;
    }
    /*
     *  Global pointer, updated by function that parse commands
     */
    RdpDl = rdp_dl;

    PRINTF("%s\n", PR_SEPARATOR);

    while (RdpDl != (rdp_dl + nwords)) {
        read_rdp(*RdpDl);
        ++ncommands;
    }

    PRINTF("%s\n\n", PR_SEPARATOR);

    total_clks = nSyncClocks + nAttClocks + nPrimClocks + nLoadClocks;

    if (total_clks == 0)
        return;

    PRINTF("Number of Words in Display List:     %d\n", (int)nwords);
    PRINTF("Number of Commands in Display List:  %d\n", (int)ncommands);
    PRINTF("Number of Warnings:                  %d\n", (int)nWarnings);
    PRINTF("Number of Errors:                    %d\n\n", (int)nErrors);

    PRINTF("Number of Load Clocks (approx): Not Implemented\n");
    PRINTF("Number of Sync Clocks (approx):  %8d (%%%f)\n",
           (int)nSyncClocks, (f32) nSyncClocks / total_clks * 100.0);
    PRINTF("Number of Att  Clocks (approx):  %8d (%%%f)\n",
           (int)nAttClocks, (f32) nAttClocks / total_clks * 100.0);
    PRINTF("Number of Prim Clocks (approx):  %8d (%%%f)\n",
           (int)nPrimClocks, (f32) nPrimClocks / total_clks * 100.0);
    PRINTF("                                ----------\n");
    PRINTF("Number of Total Clocks (approx): %8d\n", (int) total_clks);

}