1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982
|
/* -*- mode: C++; tab-width: 4 -*- */
/* ================================================================================== */
/* Copyright (c) 1998-1999 3Com Corporation or its subsidiaries. All rights reserved. */
/* ================================================================================== */
#include "EmulatorCommon.h"
#include "TrapPatches.h"
#include "ATraps.h" // ATrap::DoingCall
#include "Byteswapping.h" // Canonical
#include "CGremlinsStubs.h" // StubAppEnqueueKey
#include "CPU_REG.h" // LowMem::GetEvtMgrIdle, LowMem::TrapExists, gKeyQueue, DateToDays, gNeedPostLoad
#include "DebugMgr.h" // Debug::SendMessage
#include "ErrorHandling.h" // Errors::SysFatalAlert
#include "Hordes.h" // Hordes::IsOn, Hordes::PostFakeEvent, Hordes::CanSwitchToApp
#include "HostControlPrv.h" // HandleHostControlCall
#include "Logging.h" // LogEvtAddEventToQueue, etc.
#include "MetaMemory.h" // MetaMemory mark functions
#include "Miscellaneous.h" // SetHotSyncUserName, DateToDays, SystemCallContext
#include "Platform.h" // Platform::GetDate
#include "PreferenceMgr.h" // Preference (kPrefKeyUserName)
#include "Profiling.h" // StDisableAllProfiling
#include "RAM_ROM.h" // CEnableFullAccess
#include "EmRPC.h" // RPC::SignalWaiters
#include "SessionFile.h" // SessionFile
#include "SLP.h" // SLP
#include "Startup.h" // Startup::GetAutoLoads
#include "Strings.r.h" // kStr_ values
#include "SystemPacket.h" // SystemPacket::SendMessage
#include "UAE_Utils.h" // uae_memmove, uae_memset
// ======================================================================
// Patches for system functions.
// ======================================================================
class SysHeadpatch
{
public:
static CallROMType RecordTrapNumber (void); // EvtGetEvent & EvtGetPen
static CallROMType DbgMessage (void);
static CallROMType DmInit (void);
static CallROMType ErrDisplayFileLineMsg (void);
static CallROMType EvtAddEventToQueue (void);
static CallROMType EvtAddUniqueEventToQueue(void);
static CallROMType EvtEnqueueKey (void);
static CallROMType EvtEnqueuePenPoint (void);
static CallROMType FrmDrawForm (void);
static CallROMType HostControl (void);
static CallROMType HwrBatteryLevel (void);
static CallROMType HwrDockStatus (void);
static CallROMType HwrDoze (void);
static CallROMType HwrGetROMToken (void);
static CallROMType HwrSleep (void);
static CallROMType KeyCurrentState (void);
static CallROMType PenOpen (void);
static CallROMType SysAppExit (void);
static CallROMType SysAppLaunch (void);
static CallROMType SysBinarySearch (void);
static CallROMType SysEvGroupWait (void);
static CallROMType SysFatalAlert (void);
static CallROMType SysLaunchConsole (void);
static CallROMType SysReset (void);
static CallROMType SysSemaphoreWait (void);
static CallROMType SysUIAppSwitch (void);
static CallROMType SysUIBusy (void);
static CallROMType TimInit (void);
};
class SysTailpatch
{
public:
static void DmGetNextDatabaseByTypeCreator (void);
static void EvtGetEvent (void);
static void EvtGetPen (void);
static void EvtGetSysEvent (void);
static void FtrInit (void);
static void HwrMemReadable (void);
static void HwrSleep (void);
static void SysAppStartup (void);
static void SysBinarySearch (void);
static void TimInit (void);
static void UIInitialize (void);
};
// ======================================================================
// Proto patch table for the system functions. This array will be used
// to create a sparse array at runtime.
// ======================================================================
ProtoPatchTableEntry gProtoSysPatchTable[] =
{
{sysTrapDbgMessage, SysHeadpatch::DbgMessage, NULL},
// sysTrapDmGetNextDatabaseByTypeCreator, NULL, SysTailpatch::DmGetNextDatabaseByTypeCreator,
{sysTrapDmInit, SysHeadpatch::DmInit, NULL},
{sysTrapErrDisplayFileLineMsg, SysHeadpatch::ErrDisplayFileLineMsg, NULL},
{sysTrapEvtAddEventToQueue, SysHeadpatch::EvtAddEventToQueue, NULL},
{sysTrapEvtAddUniqueEventToQueue,SysHeadpatch::EvtAddUniqueEventToQueue, NULL},
{sysTrapEvtEnqueueKey, SysHeadpatch::EvtEnqueueKey, NULL},
{sysTrapEvtEnqueuePenPoint, SysHeadpatch::EvtEnqueuePenPoint, NULL},
{sysTrapEvtGetEvent, SysHeadpatch::RecordTrapNumber, SysTailpatch::EvtGetEvent},
{sysTrapEvtGetPen, SysHeadpatch::RecordTrapNumber, SysTailpatch::EvtGetPen},
{sysTrapEvtGetSysEvent, NULL, SysTailpatch::EvtGetSysEvent},
{sysTrapFrmDrawForm, SysHeadpatch::FrmDrawForm, NULL},
{sysTrapFtrInit, NULL, SysTailpatch::FtrInit},
{sysTrapHostControl, SysHeadpatch::HostControl, NULL},
{sysTrapHwrBatteryLevel, SysHeadpatch::HwrBatteryLevel, NULL},
{sysTrapHwrDockStatus, SysHeadpatch::HwrDockStatus, NULL},
{sysTrapHwrGetROMToken, SysHeadpatch::HwrGetROMToken, NULL},
{sysTrapHwrDoze, SysHeadpatch::HwrDoze, NULL},
{sysTrapHwrMemReadable, NULL, SysTailpatch::HwrMemReadable},
{sysTrapHwrSleep, SysHeadpatch::HwrSleep, SysTailpatch::HwrSleep},
{sysTrapKeyCurrentState, SysHeadpatch::KeyCurrentState, NULL},
{sysTrapPenOpen, SysHeadpatch::PenOpen, NULL},
{sysTrapSysAppExit, SysHeadpatch::SysAppExit, NULL},
{sysTrapSysAppLaunch, SysHeadpatch::SysAppLaunch, NULL},
{sysTrapSysAppStartup, NULL, SysTailpatch::SysAppStartup},
{sysTrapSysBinarySearch, SysHeadpatch::SysBinarySearch, SysTailpatch::SysBinarySearch},
{sysTrapSysEvGroupWait, SysHeadpatch::SysEvGroupWait, NULL},
{sysTrapSysFatalAlert, SysHeadpatch::SysFatalAlert, NULL},
{sysTrapSysLaunchConsole, SysHeadpatch::SysLaunchConsole, NULL},
{sysTrapSysReset, SysHeadpatch::SysReset, NULL},
{sysTrapSysSemaphoreWait, SysHeadpatch::SysSemaphoreWait, NULL},
{sysTrapSysUIAppSwitch, SysHeadpatch::SysUIAppSwitch, NULL},
{sysTrapSysUIBusy, SysHeadpatch::SysUIBusy, NULL},
{sysTrapTimInit, SysHeadpatch::TimInit, SysTailpatch::TimInit},
{sysTrapUIInitialize, NULL, SysTailpatch::UIInitialize},
{0, NULL, NULL}
};
// ======================================================================
// Patches for HtalLib functions
// ======================================================================
class HtalLibHeadpatch
{
public:
static CallROMType HtalLibSendReply (void);
};
#pragma mark -
// ===========================================================================
// ModulePatchTable
// ===========================================================================
// A simple class for managing patches on a module. A "module" is defined
// a the main set of system functions (dispatch numbers 0xA000 - 0xA7FFF) or
// a library (dispatch numbers 0xA800 - 0xAFFF, with a unique refnum to
// select which library).
class ModulePatchTable
{
public:
void Clear ();
void AddProtoPatchTable (ProtoPatchTableEntry protoPatchTable[]);
// B.S. operators for VC++ so that we can put objects of
// this class into STL collections.
bool operator < (const ModulePatchTable& other) const
{
return this < &other;
}
bool operator == (const ModulePatchTable& other) const
{
return this == &other;
}
// Return the patch function for the given module function. The given
// module function *must* be given as a zero-based index. If there is
// no patch function for the modeule function, return NULL.
HeadpatchProc GetHeadpatch (uae_u16 index) const
{
if (index < fHeadpatches.size ())
{
return fHeadpatches[index];
}
return NULL;
}
TailpatchProc GetTailpatch (uae_u16 index) const
{
if (index < fTailpatches.size ())
{
return fTailpatches[index];
}
return NULL;
}
private:
vector<HeadpatchProc> fHeadpatches;
vector<TailpatchProc> fTailpatches;
};
void ModulePatchTable::Clear (void)
{
fHeadpatches.clear ();
fTailpatches.clear ();
}
void ModulePatchTable::AddProtoPatchTable (ProtoPatchTableEntry protoPatchTable[])
{
// Create a fast dispatch table for the managed module. A "fast
// dispatch table" is a table with a headpatch and tailpatch entry
// for each possible function in the module. If the function is
// not head or tailpatched, the corresponding entry is NULL. When
// a patch function is needed, the trap dispatch number is used as
// an index into the table in order to get the right patch function.
//
// For simplicity, "fast patch tables" are created from "proto patch
// tables". A proto patch table is a compact table containing the
// information needed to create a fast patch table. Each entry in
// the proto patch table is a trap-number/headpatch/tailpatch tupple.
// Each tuple is examined in turn. If there is a head or tail patch
// function for the indicated module function, that patch function
// is entered in the fast dispatch table, using the trap number as
// the index.
for (long ii = 0; protoPatchTable[ii].fTrapWord; ++ii)
{
// If there is a headpatch function...
if (protoPatchTable[ii].fHeadpatch)
{
// Get the trap number.
uae_u16 index = SysTrapIndex (protoPatchTable[ii].fTrapWord);
// If the trap number is 0xA800-based, make it zero based.
if (IsLibraryTrap (index))
index -= SysTrapIndex (sysLibTrapBase);
// Resize the fast patch table, if necessary.
if (index >= fHeadpatches.size ())
{
fHeadpatches.resize (index + 1);
}
// Add the headpatch function.
fHeadpatches[index] = protoPatchTable[ii].fHeadpatch;
}
// If there is a tailpatch function...
if (protoPatchTable[ii].fTailpatch)
{
// Get the trap number.
uae_u16 index = SysTrapIndex (protoPatchTable[ii].fTrapWord);
// If the trap number is 0xA800-based, make it zero based.
if (IsLibraryTrap (index))
index -= SysTrapIndex (sysLibTrapBase);
// Resize the fast patch table, if necessary.
if (index >= fTailpatches.size ())
{
fTailpatches.resize (index + 1);
}
// Add the tailpatch function.
fTailpatches[index] = protoPatchTable[ii].fTailpatch;
}
}
}
// ===========================================================================
// TailpatchType
// ===========================================================================
// Structure used to hold tail-patch information.
struct TailpatchType
{
SystemCallContext fContext;
uae_s32 fCount;
TailpatchProc fTailpatch;
// I hate VC++...really I do...
bool operator< (const TailpatchType&) const {return false;}
bool operator> (const TailpatchType&) const {return false;}
bool operator== (const TailpatchType&) const {return false;}
bool operator!= (const TailpatchType&) const {return false;}
};
// ===========================================================================
// Patches
// ===========================================================================
// ======================================================================
// Globals and constants
// ======================================================================
const UInt kMagicRefNum = 0x666; // See comments in HtalLibSendReply.
const ModulePatchTable* kNoPatchTable = (ModulePatchTable*) -1;
static bool gUIInitialized;
static bool gHeapInitialized;
static bool gEvtGetEventCalled;
static bool gHaveJapanese;
static long gSysBinarySearchCount;
extern long gMemMgrCount;
extern long gMemSemaphoreCount;
extern unsigned long gMemSemaphoreReserveTime;
extern ULong gResizeOrigSize;
extern UInt gHeapID;
static UInt gNextAppCardNo;
static LocalID gNextAppDbID;
static QuitStage gQuitStage;
static EmuAppInfoList gCurAppInfo;
static ModulePatchTable gSysPatchTable;
static ModulePatchTable gNetLibPatchTable;
typedef vector<const ModulePatchTable*> PatchTableType;
static PatchTableType gLibPatches;
typedef vector<TailpatchType> TailpatchTableType;
static TailpatchTableType gInstalledTailpatches;
static uae_u16 gLastEvtTrap;
static DWord gOSVersion;
static const DWord kOSUndeterminedVersion = ~0;
#if defined (__MACOS__)
extern Bool gProhibitMemoryAllocation;
#endif
// ======================================================================
// Private functions
// ======================================================================
static string PrvToString (uaecptr s);
static void PrvAutoload (void);
static void PrvSetCurrentDate (void);
// ========================================================================
// Macros for extracting parameters from the emulated stack
// Note that use of these has been superceded by the PARAM_FOO
// macros; we should eventually move over to those macros.
// ========================================================================
#define PARAMETER_SIZE(x) \
(sizeof (((StackFrame*) 0)->x))
#define PARAMETER_OFFSET(x) \
(m68k_areg (regs, 7) + offsetof (StackFrame, x))
#define GET_PARAMETER(x) \
((PARAMETER_SIZE(x) == sizeof (char)) ? get_byte (PARAMETER_OFFSET(x)) : \
(PARAMETER_SIZE(x) == sizeof (short)) ? get_word (PARAMETER_OFFSET(x)) : \
get_long (PARAMETER_OFFSET(x)))
#define SET_PARAMETER(x, v) \
((PARAMETER_SIZE(x) == sizeof (char)) ? put_byte (PARAMETER_OFFSET(x), v) : \
(PARAMETER_SIZE(x) == sizeof (short)) ? put_word (PARAMETER_OFFSET(x), v) : \
put_long (PARAMETER_OFFSET(x), v))
// ========================================================================
// The following functions define a bunch of StackFrame structs.
// These structs need to mirror the format of parameters pushed
// onto the stack by the emulated code, and so need to be packed
// to 2-byte boundaries.
//
// The pragmas are reversed at the end of the file.
// ========================================================================
#include "PalmPack.h"
/***********************************************************************
*
* FUNCTION: Patches::Initialize
*
* DESCRIPTION: Standard initialization function. Responsible for
* initializing this sub-system when a new session is
* created. May also be called from the Load function
* to share common functionality.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::Initialize (void)
{
gSysPatchTable.Clear ();
gSysPatchTable.AddProtoPatchTable (gProtoSysPatchTable);
gSysPatchTable.AddProtoPatchTable (gProtoMemMgrPatchTable);
gNetLibPatchTable.Clear ();
gNetLibPatchTable.AddProtoPatchTable (gProtoNetLibPatchTable);
}
/***********************************************************************
*
* FUNCTION: Patches::Reset
*
* DESCRIPTION: Standard reset function. Sets the sub-system to a
* default state. This occurs not only on a Reset (as
* from the menu item), but also when the sub-system
* is first initialized (Reset is called after Initialize)
* as well as when the system is re-loaded from an
* insufficient session file.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::Reset (void)
{
gLastEvtTrap = 0;
gOSVersion = kOSUndeterminedVersion;
gInstalledTailpatches.clear ();
gUIInitialized = false;
gHeapInitialized = false;
gEvtGetEventCalled = false;
gHaveJapanese = false;
gSysBinarySearchCount = 0;
gMemMgrCount = 0;
gMemSemaphoreCount = 0;
gMemSemaphoreReserveTime = 0;
gResizeOrigSize = 0;
gHeapID = 0;
Patches::SetSwitchApp (0, 0);
Patches::QuitOnAppExit (false);
gLibPatches.clear ();
// Clear out everything we know about the current applications.
gCurAppInfo.clear ();
}
/***********************************************************************
*
* FUNCTION: Patches::Save
*
* DESCRIPTION: Standard save function. Saves any sub-system state to
* the given session file.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::Save (SessionFile& f)
{
const long kCurrentVersion = 2;
Chunk chunk;
ChunkStream s (chunk);
s << kCurrentVersion;
s << gUIInitialized;
s << gHeapInitialized;
s << gSysBinarySearchCount;
s << gMemMgrCount;
s << gMemSemaphoreCount;
s << gMemSemaphoreReserveTime;
s << gResizeOrigSize;
s << gHeapID;
s << gNextAppCardNo;
s << gNextAppDbID;
s << (long) gQuitStage;
s << (long) gCurAppInfo.size ();
EmuAppInfoList::iterator iter1;
for (iter1 = gCurAppInfo.begin (); iter1 != gCurAppInfo.end (); ++iter1)
{
s << iter1->fCmd;
s << (long) iter1->fDB;
s << iter1->fCardNo;
s << iter1->fDBID;
s << iter1->fMemOwnerID;
s << iter1->fStackP;
s << iter1->fStackEndP;
s << iter1->fStackSize;
s << iter1->fName;
s << iter1->fVersion;
}
// s << gSysPatchTable;
// s << gNetLibPatchTable;
// s << gLibPatches;
s << (long) gInstalledTailpatches.size ();
TailpatchTableType::iterator iter2;
for (iter2 = gInstalledTailpatches.begin (); iter2 != gInstalledTailpatches.end (); ++iter2)
{
s << iter2->fContext.fDestPC;
s << iter2->fContext.fExtra;
s << iter2->fContext.fNextPC;
s << iter2->fContext.fPC;
s << iter2->fContext.fTrapIndex;
s << iter2->fContext.fTrapWord;
s << iter2->fCount;
// s << iter2->fTailpatch; // Patched up in ::Load
}
s << gLastEvtTrap;
s << gOSVersion;
// Added in version 2.
s << gHaveJapanese;
f.WritePatchInfo (chunk);
}
/***********************************************************************
*
* FUNCTION: Patches::Load
*
* DESCRIPTION: Standard load function. Loads any sub-system state
* from the given session file.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::Load (SessionFile& f)
{
Chunk chunk;
if (f.ReadPatchInfo (chunk))
{
long temp;
long version;
ChunkStream s (chunk);
s >> version;
if (version >= 1)
{
s >> gUIInitialized;
s >> gHeapInitialized;
gEvtGetEventCalled = false;
s >> gSysBinarySearchCount;
s >> gMemMgrCount;
s >> gMemSemaphoreCount;
s >> gMemSemaphoreReserveTime;
s >> gResizeOrigSize;
s >> gHeapID;
s >> gNextAppCardNo;
s >> gNextAppDbID;
s >> temp; gQuitStage = (QuitStage) temp;
long numApps;
s >> numApps;
long ii;
for (ii = 0; ii < numApps; ++ii)
{
EmuAppInfo info;
s >> info.fCmd;
s >> temp; info.fDB = (VoidPtr) temp;
s >> info.fCardNo;
s >> info.fDBID;
s >> info.fMemOwnerID;
s >> info.fStackP;
s >> info.fStackEndP;
s >> info.fStackSize;
s >> info.fName;
s >> info.fVersion;
gCurAppInfo.push_back (info);
}
long numTailpatches;
s >> numTailpatches;
for (ii = 0; ii < numTailpatches; ++ii)
{
TailpatchType patch;
s >> patch.fContext.fDestPC;
s >> patch.fContext.fExtra;
s >> patch.fContext.fNextPC;
s >> patch.fContext.fPC;
s >> patch.fContext.fTrapIndex;
s >> patch.fContext.fTrapWord;
s >> patch.fCount;
// Patch up the tailpatch proc.
HeadpatchProc dummy;
GetPatches (patch.fContext, dummy, patch.fTailpatch);
gInstalledTailpatches.push_back (patch);
}
s >> gLastEvtTrap;
s >> gOSVersion;
}
if (version >= 2)
{
s >> gHaveJapanese;
}
else
{
gHaveJapanese = false;
}
}
else
{
f.SetCanReload (false);
}
}
/***********************************************************************
*
* FUNCTION: Patches::Dispose
*
* DESCRIPTION: Standard dispose function. Completely release any
* resources acquired or allocated in Initialize and/or
* Load.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::Dispose (void)
{
gUIInitialized = false;
gHeapInitialized = false;
gEvtGetEventCalled = false;
gHaveJapanese = false;
gSysPatchTable.Clear ();
gNetLibPatchTable.Clear ();
}
/***********************************************************************
*
* FUNCTION: Patches::PostLoad
*
* DESCRIPTION: Do some stuff that is normally taken care of during the
* process of resetting the device (autoloading
* applications, setting the device date, installing the
* HotSync user-name, and setting the 'gdbS' feature).
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::PostLoad (void)
{
if (Patches::UIInitialized ())
{
// If we're listening on a socket, install the 'gdbS' feature. The
// existance of this feature causes programs written with the prc tools
// to enter the debugger when they're launched.
if (Debug::ConnectedToTCPDebugger ())
{
FtrSet ('gdbS', 0, 0x12BEEF34);
}
else
{
FtrUnregister ('gdbS', 0);
}
// Install the HotSync user-name.
Preference<string> userName (kPrefKeyUserName);
::SetHotSyncUserName (userName->c_str ());
// Auto-load any files in the Autoload[Foo] directories.
::PrvAutoload ();
// Install the current date.
::PrvSetCurrentDate ();
// Wake up any current application so that they can respond
// to events we pump in at EvtGetEvent time.
::EvtWakeup ();
}
}
/***********************************************************************
*
* FUNCTION: Patches::GetLibPatchTable
*
* DESCRIPTION: .
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
const ModulePatchTable* Patches::GetLibPatchTable (uae_u16 refNum)
{
string libName = ::GetLibraryName (refNum);
if (libName == "Net.lib")
{
return &gNetLibPatchTable;
}
return kNoPatchTable;
}
/***********************************************************************
*
* FUNCTION: Patches::HandleSystemCall
*
* DESCRIPTION: If this is a trap we could possibly have head- or
* tail-patched, handle those cases.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType Patches::HandleSystemCall (const SystemCallContext& context)
{
if (gNeedPostLoad)
{
gNeedPostLoad = false;
Patches::PostLoad ();
}
HeadpatchProc hp;
TailpatchProc tp;
Patches::GetPatches (context, hp, tp);
CallROMType handled = Patches::HandlePatches (context, hp, tp);
return handled;
}
/***********************************************************************
*
* FUNCTION: Patches::GetPatches
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::GetPatches ( const SystemCallContext& context,
HeadpatchProc& hp,
TailpatchProc& tp)
{
const ModulePatchTable* patchTable = NULL;
// If this is in the system function range, check our table of
// system function patches.
if (::IsSystemTrap (context.fTrapWord))
{
patchTable = &gSysPatchTable;
}
// Otherwise, see if this is a call to a library function
else
{
if (context.fExtra == kMagicRefNum) // See comments in HtalLibSendReply.
{
hp = HtalLibHeadpatch::HtalLibSendReply;
tp = NULL;
return;
}
if (context.fExtra >= gLibPatches.size ())
{
gLibPatches.resize (context.fExtra + 1);
}
patchTable = gLibPatches[context.fExtra];
if (patchTable == NULL)
{
patchTable = gLibPatches[context.fExtra] = GetLibPatchTable (context.fExtra);
}
}
// Now that we've got the right patch table for this module, see if
// that patch table has head- or tailpatches for this function.
if (patchTable != kNoPatchTable)
{
hp = patchTable->GetHeadpatch (context.fTrapIndex);
tp = patchTable->GetTailpatch (context.fTrapIndex);
}
else
{
assert (patchTable == kNoPatchTable);
hp = NULL;
tp = NULL;
}
}
/***********************************************************************
*
* FUNCTION: Patches::HandlePatches
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType Patches::HandlePatches (const SystemCallContext& context,
HeadpatchProc hp,
TailpatchProc tp)
{
CallROMType handled = kExecuteROM;
// First, see if we have a SysHeadpatch for this function. If so, call
// it. If it returns true, then that means that the head patch
// completely handled the function.
// !!! May have to mess with PC here in case patches do something
// to enter the debugger.
if (hp)
{
handled = CallHeadpatch (hp);
}
// Next, see if there's a SysTailpatch function for this trap. If
// so, install the TRAP that will cause us to regain control
// after the trap function has executed.
if (tp)
{
if (handled == kExecuteROM)
{
SetupForTailpatch (tp, context);
}
else
{
CallTailpatch (tp);
}
}
return handled;
}
/***********************************************************************
*
* FUNCTION: Patches::HandleCPUBreak
*
* DESCRIPTION: Handle a tail patch, if any is registered for this
* memory location.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::HandleCPUBreak (void)
{
// Get the address of the tailpatch to call. May return NULL if
// there is no tailpatch for this memory location.
TailpatchProc tp = RecoverFromTailpatch (m68k_getpc ());
// Call the tailpatch handler for the trap that just returned.
CallTailpatch (tp);
}
/***********************************************************************
*
* FUNCTION: Patches::InstallCPUBreaks
*
* DESCRIPTION: Set the MetaMemory bit that tells the CPU loop to stop
* when we get to the desired locations.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::InstallCPUBreaks (void)
{
TailpatchTableType::iterator iter = gInstalledTailpatches.begin ();
while (iter != gInstalledTailpatches.end ())
{
MetaMemory::MarkCPUBreak (iter->fContext.fNextPC);
++iter;
}
}
/***********************************************************************
*
* FUNCTION: Patches::RemoveCPUBreaks
*
* DESCRIPTION: Clear the MetaMemory bit that tells the CPU loop to stop
* when we get to the desired locations.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::RemoveCPUBreaks (void)
{
TailpatchTableType::iterator iter = gInstalledTailpatches.begin ();
while (iter != gInstalledTailpatches.end ())
{
MetaMemory::UnmarkCPUBreak (iter->fContext.fNextPC);
++iter;
}
}
/***********************************************************************
*
* FUNCTION: Patches::SetupForTailpatch
*
* DESCRIPTION: Set up the pending TRAP $F call to be tailpatched.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::SetupForTailpatch (TailpatchProc tp, const SystemCallContext& context)
{
// See if this function is already tailpatched. If so, merely increment
// the use-count field.
TailpatchTableType::iterator iter = gInstalledTailpatches.begin ();
while (iter != gInstalledTailpatches.end ())
{
if (iter->fContext.fNextPC == context.fNextPC)
{
++(iter->fCount);
return;
}
++iter;
}
// This function is not already tailpatched, so add a new entry
// for the the PC/opcode we want to save.
TailpatchType newTailpatch;
newTailpatch.fContext = context;
newTailpatch.fCount = 1;
newTailpatch.fTailpatch = tp;
gInstalledTailpatches.push_back (newTailpatch);
#if defined (__MACOS__)
// For the Mac, make sure there's always at least 10 spaces available.
// We use these spaces in times when we can't resize the array, as
// when we're handling debugger packets.
if (!gProhibitMemoryAllocation)
{
if (gInstalledTailpatches.size () + 10 > gInstalledTailpatches.capacity ())
{
gInstalledTailpatches.reserve (gInstalledTailpatches.capacity () + 10);
}
}
#endif
Emulator::InstallCPUBreaks ();
}
/***********************************************************************
*
* FUNCTION: Patches::RecoverFromTailpatch
*
* DESCRIPTION: .
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
TailpatchProc Patches::RecoverFromTailpatch (uaecptr startPC)
{
// Get the current PC so that we can find the record for this tailpatch.
uaecptr patchPC = startPC;
// Find the PC.
TailpatchTableType::iterator iter = gInstalledTailpatches.begin ();
while (iter != gInstalledTailpatches.end ())
{
if (iter->fContext.fNextPC == patchPC)
{
TailpatchProc result = iter->fTailpatch;
// Decrement the use-count. If it reaches zero, remove the
// patch from our list.
if (--(iter->fCount) == 0)
{
gInstalledTailpatches.erase (iter);
Emulator::InstallCPUBreaks ();
}
return result;
}
++iter;
}
return NULL;
}
/***********************************************************************
*
* FUNCTION: Patches::CallHeadpatch
*
* DESCRIPTION: If the given system function is head patched, then call
* the headpatch. Return "handled" (which means whether
* or not to call the ROM function after this one).
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType Patches::CallHeadpatch (HeadpatchProc hp)
{
CallROMType handled = kExecuteROM;
if (hp)
{
if (hp != &SysHeadpatch::HostControl)
{
// Stop all profiling activities. Stop cycle counting and stop the
// recording of function entries and exits. We want our trap patches
// to be as transparent as possible.
StDisableAllProfiling stopper;
handled = hp ();
}
else
{
handled = hp ();
}
}
return handled;
}
/***********************************************************************
*
* FUNCTION: Patches::CallTailpatch
*
* DESCRIPTION: If the given function is tail patched, then call the
* tailpatch.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::CallTailpatch (TailpatchProc tp)
{
if (tp)
{
// Stop all profiling activities. Stop cycle counting and stop the
// recording of function entries and exits. We want our trap patches
// to be as transparent as possible.
StDisableAllProfiling stopper;
tp ();
}
}
/***********************************************************************
*
* FUNCTION: Patches::OSVersion
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
DWord Patches::OSVersion (void)
{
assert (gOSVersion != kOSUndeterminedVersion);
return gOSVersion;
}
/***********************************************************************
*
* FUNCTION: Patches::OSMajorMinorVersion
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
DWord Patches::OSMajorMinorVersion (void)
{
return OSMajorVersion () * 10 + OSMinorVersion ();
}
/***********************************************************************
*
* FUNCTION: Patches::OSMajorVersion
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
DWord Patches::OSMajorVersion (void)
{
assert (gOSVersion != kOSUndeterminedVersion);
return sysGetROMVerMajor (gOSVersion);
}
/***********************************************************************
*
* FUNCTION: Patches::OSMinorVersion
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
DWord Patches::OSMinorVersion (void)
{
assert (gOSVersion != kOSUndeterminedVersion);
return sysGetROMVerMinor (gOSVersion);
}
/***********************************************************************
*
* FUNCTION: SetSwitchApp
*
* DESCRIPTION: Sets an application or launchable document to switch to
* the next time the system can manage it.
*
* PARAMETERS: cardNo - the card number of the app to switch to.
*
* dbID - the database id of the app to switch to.
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::SetSwitchApp (UInt cardNo, LocalID dbID)
{
gNextAppCardNo = cardNo;
gNextAppDbID = dbID;
}
/***********************************************************************
*
* FUNCTION: Patches::SwitchToApp
*
* DESCRIPTION: Switches to the given application or launchable document.
*
* PARAMETERS: cardNo - the card number of the app to switch to.
*
* dbID - the database id of the app to switch to.
*
* RETURNED: Err number of any errors that occur
*
***********************************************************************/
Err Patches::SwitchToApp (UInt cardNo, LocalID dbID)
{
UInt dbAttrs;
ULong type, creator;
Err err = ::DmDatabaseInfo (
cardNo,
dbID,
NULL, /*name*/
&dbAttrs,
NULL, /*version*/
NULL, /*create date*/
NULL, /*modDate*/
NULL, /*backup date*/
NULL, /*modNum*/
NULL, /*appInfoID*/
NULL, /*sortInfoID*/
&type,
&creator);
if (err)
return err;
//---------------------------------------------------------------------
// If this is an executable, call SysUIAppSwitch
//---------------------------------------------------------------------
if (::IsExecutable (type, creator, dbAttrs))
{
err = ::SysUIAppSwitch (cardNo, dbID,
sysAppLaunchCmdNormalLaunch, NULL);
if (err)
return err;
}
//---------------------------------------------------------------------
// else, this must be a launchable data database. Find it's owner app
// and launch it with a pointer to the data database name.
//---------------------------------------------------------------------
else
{
DmSearchStateType searchState;
UInt appCardNo;
LocalID appDbID;
err = ::DmGetNextDatabaseByTypeCreator (true, &searchState,
sysFileTApplication, creator,
true, &appCardNo, &appDbID);
if (err)
return err;
// Create the param block
uaecptr cmdPBP = (uaecptr) ::MemPtrNew (sizeof (SysAppLaunchCmdOpenDBType));
if (cmdPBP == UAE_NULL)
return memErrNotEnoughSpace;
// Fill it in
::MemPtrSetOwner ((VoidPtr) cmdPBP, 0);
put_word (cmdPBP + offsetof (SysAppLaunchCmdOpenDBType, cardNo), cardNo);
put_long (cmdPBP + offsetof (SysAppLaunchCmdOpenDBType, dbID), dbID);
// Switch now
err = ::SysUIAppSwitch (appCardNo, appDbID, sysAppLaunchCmdOpenDB, (Ptr) cmdPBP);
if (err)
return err;
}
if (GetQuitStage () == kWaitingForSysUIAppSwitch)
{
SetQuitStage (kWaitingForSysAppStartup);
}
return errNone;
}
/***********************************************************************
*
* FUNCTION: Patches::QuitOnAppExit
*
* DESCRIPTION: Tells out patching mechanisms that the emulator should
* quit once the current application quits.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::QuitOnAppExit (Bool val)
{
if (val)
SetQuitStage (kWaitingForSysUIAppSwitch);
else
SetQuitStage (kNoQuit);
}
/***********************************************************************
*
* FUNCTION: Patches::GetQuitStage
*
* DESCRIPTION: .
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
QuitStage Patches::GetQuitStage (void)
{
return gQuitStage;
}
/***********************************************************************
*
* FUNCTION: Patches::SetQuitStage
*
* DESCRIPTION: .
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::SetQuitStage (QuitStage q)
{
gQuitStage = q;
}
/***********************************************************************
*
* FUNCTION: Patches::PuppetString
*
* DESCRIPTION: Puppet stringing function for inserting events into
* the system. We want to insert events when:
*
* - Gremlins is running
* - The user types characters
* - The user clicks with the mouse
* - We need to trigger a switch another application
*
* This function is called from headpatches to
* SysEvGroupWait and SysSemaphoreWait. When the Palm OS
* needs an event, it calls EvtGetEvent. EvtGetEvent
* looks in all the usual places for events to return. If
* it doesn't find any, it puts the system to sleep by
* calling SysEvGroupWait (or SysSemaphoreWait on 1.0
* systems). SysEvGroupWait will wake up and return when
* an event is posted via something like EvtEnqueuePenPoint,
* EvtEnqueueKey, or KeyHandleInterrupt.
*
* To puppet-string Palm OS, we therefore patch those
* functions and post events, preventing them from actually
* going to sleep.
*
* PARAMETERS: callROM - return here whether or not the original ROM
* function still needs to be called. Normally, the
* answer is "yes".
*
* clearTimeout - set to true if the "timeout" parameter
* of the function we've patched needs to be prevented
* from being "infinite".
*
* RETURNED: nothing
*
***********************************************************************/
static void PrvForceNilEvent (void)
{
// No event was posted. What we'd like right now is to force
// EvtGetEvent to return a nil event. We can do that by returning
// a non-zero result code from SysEvGroupWait. EvtGetEvent doesn't
// look too closely at the result, but let's try to be as close to
// reality as possible. SysEvGroupWait currently returns "4"
// (CJ_WRTMOUT) to indicate a timeout condition. It should
// probably get turned into sysErrTimeout somewhere along the way,
// but that translation doesn't seem to occur.
m68k_dreg(regs, 0) = 4;
}
void Patches::PuppetString (CallROMType& callROM, Bool& clearTimeout)
{
callROM = kExecuteROM;
clearTimeout = false;
// Set the return value (Err) to zero in case we return
// "true" (saying that we handled the trap).
m68k_dreg (regs, 0) = 0;
// If the low-memory global "idle" is true, then we're being
// called from EvtGetEvent or EvtGetPen, in which case we
// need to check if we need to post some events.
if (LowMem::GetEvtMgrIdle ())
{
// If there's an RPC request waiting for a nilEvent,
// let it know that it happened.
if (gLastEvtTrap == sysTrapEvtGetEvent)
{
RPC::SignalWaiters (hostSignalIdle);
}
// If we're in the middle of calling a Palm OS function ourself,
// and we are someone at the point where the system is about to
// doze, then just return now. Don't let it doze! Interrupts are
// off, and HwrDoze will never return!
if (ATrap::DoingCall())
{
::PrvForceNilEvent();
callROM = kSkipROM;
return;
}
if (Hordes::IsOn ())
{
if (gLastEvtTrap == sysTrapEvtGetEvent)
{
if (!Hordes::PostFakeEvent ())
{
if (LogEnqueuedEvents ())
{
LogAppendMsg ("Hordes::PostFakeEvent did not post an event.");
}
::PrvForceNilEvent();
callROM = kSkipROM;
return;
}
}
else if (gLastEvtTrap == sysTrapEvtGetPen)
{
Hordes::PostFakePenEvent ();
}
else
{
if (LogEnqueuedEvents ())
{
LogAppendMsg ("Last event was 0x%04X, so not posting event.", gLastEvtTrap);
}
}
#if 0
// Ensure that there is an event posted. If there isn't
// a timeout of zero (= forever) would kill us.
assert (get_word (objID + 0x1E) == 1);
#endif
// Never let the timeout be infinite. If the above event-posting
// attempts failed (which could happen, for instance, if we attempted
// to post a pen event with the same coordinates as the previous
// pen event), we'd end up waiting forever.
clearTimeout = true;
}
// Gremlins aren't on; let's see if the user has typed some
// keys that we need to pass on to the Palm device.
else if (gKeyQueue.GetUsed () > 0)
{
StubAppEnqueueKey (gKeyQueue.Get (), 0, 0);
}
// No key events, let's see if there are pen events.
else if (Hardware::HavePenEvent ())
{
PointType pen = { -1, -1 };
if (Hardware::PenIsDown ())
{
pen = Hardware::PenLocation ();
}
Hardware::SetHavePenEvent (false);
StubAppEnqueuePt (&pen);
}
// E. None of the above. Let's see if there's an app
// we're itching to switch to.
else if (gNextAppDbID != 0)
{
Err err = SwitchToApp (gNextAppCardNo, gNextAppDbID);
gNextAppCardNo = 0;
gNextAppDbID = 0;
clearTimeout = true;
}
}
else
{
if (Hordes::IsOn () && LogEnqueuedEvents ())
{
LogAppendMsg ("Event Manager not idle, so not posting an event.");
}
}
}
/***********************************************************************
*
* FUNCTION: Patches::HasWellBehavedMemSemaphoreUsage
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
Bool Patches::HasWellBehavedMemSemaphoreUsage (void)
{
// Palm OS 3.0 and later should not be holding the memory manager
// semaphore for longer than 1 minute. I imagine that older ROMs
// don't hold the semaphore for longer than this, but Roger still
// suggested testing for 3.0.
return gOSVersion != kOSUndeterminedVersion && OSMajorMinorVersion () >= 30;
}
/***********************************************************************
*
* FUNCTION: Patches::EnterMemMgr
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::EnterMemMgr (const char* fnName)
{
UNUSED_PARAM(fnName)
++gMemMgrCount;
assert (gMemMgrCount < 10);
}
/***********************************************************************
*
* FUNCTION: Patches::ExitMemMgr
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::ExitMemMgr (const char* fnName)
{
UNUSED_PARAM(fnName)
--gMemMgrCount;
assert (gMemMgrCount >= 0);
}
/***********************************************************************
*
* FUNCTION: Patches::IsInSysBinarySearch
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
Bool Patches::IsInSysBinarySearch (void)
{
return gSysBinarySearchCount > 0;
}
/***********************************************************************
*
* FUNCTION: Patches::EnterSysBinarySearch
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::EnterSysBinarySearch (void)
{
assert (gSysBinarySearchCount < 10);
++gSysBinarySearchCount;
}
/***********************************************************************
*
* FUNCTION: Patches::ExitSysBinarySearch
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::ExitSysBinarySearch (void)
{
--gSysBinarySearchCount;
assert (gSysBinarySearchCount >= 0);
}
/***********************************************************************
*
* FUNCTION: Patches::UIInitialized
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
Bool Patches::UIInitialized (void)
{
return gUIInitialized;
}
/***********************************************************************
*
* FUNCTION: Patches::SetUIInitialized
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::SetUIInitialized (Bool b)
{
gUIInitialized = b != 0;
}
/***********************************************************************
*
* FUNCTION: Patches::HeapInitialized
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
Bool Patches::HeapInitialized (void)
{
return gHeapInitialized;
}
/***********************************************************************
*
* FUNCTION: Patches::SetHeapInitialized
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void Patches::SetHeapInitialized (Bool b)
{
gHeapInitialized = b != 0;
}
/***********************************************************************
*
* FUNCTION: Patches::TurningJapanese
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
Bool Patches::TurningJapanese (void)
{
return gHaveJapanese;
}
/***********************************************************************
*
* FUNCTION: Patches::EvtGetEventCalled
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
Bool Patches::EvtGetEventCalled (void)
{
return gEvtGetEventCalled;
}
/***********************************************************************
*
* FUNCTION: Patches::CollectCurrentAppInfo
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
EmuAppInfo Patches::CollectCurrentAppInfo (uaecptr appInfoP)
{
EmuAppInfo newAppInfo;
memset (&newAppInfo, 0, sizeof (newAppInfo));
// Scarf some information out of the app info block.
newAppInfo.fDB = (DmOpenRef) get_long (appInfoP + offsetof (SysAppInfoType, dbP));
newAppInfo.fStackP = get_long (appInfoP + offsetof (SysAppInfoType, stackP));
newAppInfo.fMemOwnerID = get_word (appInfoP + offsetof (SysAppInfoType, memOwnerID));
// Determine the current stack range. Under Palm OS 3.0 and later, this information
// is in the DatabaseInfo block. Under earlier OSes, we only get the low-end of the stack
// (that is, the address that was returned by MemPtrNew). To get the high-end of
// the stack, assume that stackP pointed to a chunk of memory allocated by MemPtrNew
// and call MemPtrSize.
if (DatabaseInfoHasStackInfo ())
{
if (newAppInfo.fStackP)
{
ULong stackSize = ::MemPtrSize ((VoidPtr) newAppInfo.fStackP);
if (stackSize)
{
newAppInfo.fStackEndP = newAppInfo.fStackP + stackSize;
}
else
{
newAppInfo.fStackEndP = UAE_NULL;
}
}
}
else
{
newAppInfo.fStackEndP = get_long (appInfoP + offsetof (SysAppInfoType, stackEndP));
}
newAppInfo.fStackSize = newAppInfo.fStackEndP - newAppInfo.fStackP;
// Remember the current application name and version information. We use
// this information when telling users that something has gone haywire. Collect
// this information now instead of later (on demand) as we can't be sure that
// we can make the necessary DataMgr calls after an error occurs.
//
// If the database has a 'tAIN' resource, get the name from there.
// Otherwise, use the database name.
//
// (Write the name into a temporary local variable. The local variable is
// on the stack, which will get "mapped" into the emulated address space so
// that the emulated DmDatabaseInfo can get to it.)
UInt cardNo;
LocalID dbID;
Err err = ::DmOpenDatabaseInfo (newAppInfo.fDB, &dbID, NULL, NULL, &cardNo, NULL);
if (err)
return newAppInfo;
newAppInfo.fCardNo = cardNo;
newAppInfo.fDBID = dbID;
char appName[dmDBNameLength] = {0};
char appVersion[256] = {0}; // <gulp> I hope this is big enough...
// DmOpenRef dbP = DmOpenDatabase (cardNo, dbID, dmModeReadOnly);
// if (dbP)
{
VoidHand strH;
// Get the app name from the 'tAIN' resource.
strH = ::DmGet1Resource (ainRsc, ainID);
if (strH)
{
uaecptr strP = (uaecptr) ::MemHandleLock (strH);
uae_strcpy (appName, strP);
::MemHandleUnlock (strH);
::DmReleaseResource (strH);
}
// Get the version from the 'tver' resource, using ID's 1 and 1000
strH = ::DmGet1Resource (verRsc, appVersionID);
if (strH == NULL)
strH = ::DmGet1Resource (verRsc, appVersionAlternateID);
if (strH)
{
uaecptr strP = (uaecptr) ::MemHandleLock (strH);
uae_strcpy (appVersion, strP);
::MemHandleUnlock (strH);
::DmReleaseResource (strH);
}
// ::DmCloseDatabase (dbP);
}
if (appName[0] == 0) // No 'tAIN' resource, so use database name
{
::DmDatabaseInfo (cardNo, dbID,
appName,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL);
}
// Copy the strings from the stack to their permanent homes.
strcpy (newAppInfo.fName, appName);
strcpy (newAppInfo.fVersion, appVersion);
return newAppInfo;
}
/***********************************************************************
*
* FUNCTION: Patches::GetCurrentAppInfo
*
* DESCRIPTION: Return information on the last application launched
* with SysAppLaunch (and that hasn't exited yet).
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
EmuAppInfo Patches::GetCurrentAppInfo (void)
{
EmuAppInfo result;
memset (&result, 0, sizeof (result));
if (gCurAppInfo.size () > 0)
result = *(gCurAppInfo.rbegin ());
return result;
}
/***********************************************************************
*
* FUNCTION: Patches::GetRootAppInfo
*
* DESCRIPTION: Return information on the last application launched
* with SysAppLaunch and with the launch code of
* sysAppLaunchCmdNormalLaunch (and that hasn't exited yet).
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
EmuAppInfo Patches::GetRootAppInfo (void)
{
EmuAppInfo result;
memset (&result, 0, sizeof (result));
EmuAppInfoList::reverse_iterator iter = gCurAppInfo.rbegin ();
while (iter != gCurAppInfo.rend ())
{
if ((*iter).fCmd == sysAppLaunchCmdNormalLaunch)
{
result = *iter;
break;
}
++iter;
}
return result;
}
#pragma mark -
// ===========================================================================
// SysHeadpatch
// ===========================================================================
/***********************************************************************
*
* FUNCTION: SysHeadpatch::RecordTrapNumber
*
* DESCRIPTION: Record the trap we're executing for our patch to
* SysEvGroupWait later.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::RecordTrapNumber (void)
{
struct StackFrame
{
};
uae_u8* realMem = get_real_address (m68k_getpc ());
assert (do_get_mem_word (realMem - 2) == (m68kTrapInstr + sysDispatchTrapNum));
gLastEvtTrap = do_get_mem_word (realMem);
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::DbgMessage
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::DbgMessage (void)
{
// void DbgMessage(CharPtr aStr);
struct StackFrame
{
CharPtr aStr;
};
uaecptr msg = GET_PARAMETER (aStr);
if (msg)
{
string msgCopy;
size_t msgLen = uae_strlen (msg);
if (msgLen > 0)
{
msgCopy.resize (msgLen);
uae_strcpy (&msgCopy[0], msg);
}
SLP slp (Debug::GetDebuggerSocket ());
SystemPacket::SendMessage (slp, msgCopy.c_str ());
}
return kSkipROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::DmInit
*
* DESCRIPTION: After MemInit is called, we need to sync up with the
* initial state of the heap(s). However, MemInit is not
* called via the trap table, so we can't easily tailpatch
* it. DmInit is the first such function called after
* MemInit, so we headpatch *it* instead of tailpatching
* MemInit.
*
* (Actually, MemHeapCompact is called as one of the last
* things MemInit does which makes it an interesting
* candidate for patching in order to sync up with the
* heap state. However, if we were to do a full sync on
* that call, a full sync would occur on *every* call to
* MemHeapCompact, which we don't really want to do.)
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::DmInit (void)
{
MetaMemory::SyncAllHeaps ();
Patches::SetHeapInitialized (true);
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::ErrDisplayFileLineMsg
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::ErrDisplayFileLineMsg (void)
{
// void ErrDisplayFileLineMsg(CharPtr filename, UInt lineno, CharPtr msg)
CEnableFullAccess munge; // Remove blocks on memory access.
// Force this guy to true. If it's false, ErrDisplayFileLineMsg will
// just try to enter the debugger.
Word sysMiscFlags = LowMem_GetGlobal (sysMiscFlags);
LowMem_SetGlobal (sysMiscFlags, sysMiscFlags | sysMiscFlagUIInitialized);
// Clear this low-memory flag so that we force the dialog to display.
// If this flag is true, ErrDisplayFileLineMsg will just try to enter
// the debugger.
LowMem_SetGlobal (dbgWasEntered, false);
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::EvtAddEventToQueue
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::EvtAddEventToQueue (void)
{
// void EvtAddEventToQueue (const EventPtr event)
struct StackFrame
{
const EventPtr event;
};
uaecptr event = GET_PARAMETER (event);
LogEvtAddEventToQueue (event);
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::EvtAddUniqueEventToQueue
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::EvtAddUniqueEventToQueue (void)
{
// void EvtAddUniqueEventToQueue(const EventPtr eventP, const DWord id, const Boolean inPlace)
struct StackFrame
{
const EventPtr event;
const DWord id;
const Boolean inPlace;
};
uaecptr event = GET_PARAMETER (event);
DWord id = GET_PARAMETER (id);
Boolean inPlace = GET_PARAMETER (inPlace);
LogEvtAddUniqueEventToQueue (event, id, inPlace);
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::EvtEnqueueKey
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::EvtEnqueueKey (void)
{
// Err EvtEnqueueKey(UInt ascii, UInt keycode, UInt modifiers)
struct StackFrame
{
UInt ascii;
UInt keycode;
UInt modifiers;
};
UInt ascii = GET_PARAMETER (ascii);
UInt keycode = GET_PARAMETER (keycode);
UInt modifiers = GET_PARAMETER (modifiers);
LogEvtEnqueueKey (ascii, keycode, modifiers);
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::EvtEnqueuePenPoint
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::EvtEnqueuePenPoint (void)
{
// Err EvtEnqueuePenPoint(PointType* ptP)
struct StackFrame
{
PointType* ptP;
};
uaecptr ptP = GET_PARAMETER (ptP);
LogEvtEnqueuePenPoint (ptP);
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::FrmDrawForm
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::FrmDrawForm (void)
{
// void FrmDrawForm (const FormPtr frm)
struct StackFrame
{
FormPtr frm;
};
FormPtr frm = (FormPtr) GET_PARAMETER (frm);
vector<Word> okObjects;
::CollectOKObjects (frm, okObjects, true);
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::HostControl
*
* DESCRIPTION: This one's kind of odd. Originally, there was
* SysGremlins, which was declared as follows:
*
* DWord SysGremlins(GremlinFunctionType selector,
* GremlinParamsType *params)
*
* Also originally, the only defined selector was
* GremlinIsOn.
*
* Now, SysGremlins is extended to be SysHostControl,
* which allows the Palm environment to access host
* functions if it's actually running under the simulator
* or emulator.
*
* Because of this extension, functions implemented via
* this trap are not limited to pushing a selector and
* parameter block on the stack. Now, they will all push
* on a selector, but what comes after is dependent on the
* selector.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::HostControl (void)
{
return HandleHostControlCall ();
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::HwrBatteryLevel
*
* DESCRIPTION: Return that the battery is always full. HwrBatteryLevel
* is the bottleneck function called to determine the
* battery level. By patching it this way, we don't have
* to emulate the hardware registers that report the
* battery level.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::HwrBatteryLevel (void)
{
// UInt HwrBatteryLevel(void)
struct StackFrame
{
};
m68k_dreg (regs, 0) = 255; // Hardcode a maximum level
return kSkipROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::HwrDockStatus
*
* DESCRIPTION: Always return hwrDockStatusUsingExternalPower. We
* could fake this out by twiddling the right bits in the
* Dragonball and DragonballEZ emulation units, but those
* bits are different for almost every device.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::HwrDockStatus (void)
{
// hwrDockStatusState HwrDockStatus(void)
// (added in Palm OS 3.1)
// (changed later to return UInt16)
struct StackFrame
{
};
// Old enumerated values from Hardware.h:
//
// DockStatusNotDocked = 0,
// DockStatusInModem,
// DockStatusInCharger,
// DockStatusUnknown = 0xFF
// New defines from HwrDock.h
#define hwrDockStatusUndocked 0x0000 // nothing is attached
#define hwrDockStatusModemAttached 0x0001 // some type of modem is attached
#define hwrDockStatusDockAttached 0x0002 // some type of dock is attached
#define hwrDockStatusUsingExternalPower 0x0004 // using some type of external power source
#define hwrDockStatusCharging 0x0008 // internal power cells are recharging
m68k_dreg (regs, 0) = hwrDockStatusUsingExternalPower;
return kSkipROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::HwrDoze
*
* DESCRIPTION: If we're the one responsible for this function being
* called, then return immediately. If HwrDoze were
* called, it would never return, because we have
* interrupts turned off.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::HwrDoze (void)
{
// if (ATrap::DoingCall())
// return kSkipROM;
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::HwrGetROMToken
*
* DESCRIPTION: Patch this guy so that we never return the 'irda' token.
* We should take this out when some sort of IR support is
* added.
*
* NOTE: This patch is useless for diverting the ROM. It
* calls HwrGetROMToken directly, which means that it will
* bypass this patch.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::HwrGetROMToken (void)
{
// Err HwrGetROMToken (Word cardNo, DWord token, BytePtr *dataP, WordPtr sizeP)
struct StackFrame
{
Word cardNo;
DWord token;
BytePtr *dataP;
WordPtr sizeP;
};
Word cardNo = GET_PARAMETER (cardNo);
DWord token = GET_PARAMETER (token);
uaecptr dataP = GET_PARAMETER (dataP);
uaecptr sizeP = GET_PARAMETER (sizeP);
if (cardNo == 0 && token == hwrROMTokenIrda)
{
if (dataP)
put_long (dataP, 0);
if (sizeP)
put_long (sizeP, 0);
m68k_dreg (regs, 0) = ~0; // token not found.
return kSkipROM;
}
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::HwrSleep
*
* DESCRIPTION: Record whether or not we are sleeping and update the
* boolean that determines if low-memory access is OK.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::HwrSleep (void)
{
// void HwrSleep(Boolean untilReset, Boolean emergency)
struct StackFrame
{
Boolean untilReset;
Boolean dummy;
Boolean emergency;
};
// HwrSleep changes the exception vectors for for the interrupts,
// so temporarily unlock those. We'll re-establish them in the
// HwrSleep tailpatch.
MetaMemory::MarkTotalAccess (offsetof (M68KExcTableType, busErr),
offsetof (M68KExcTableType, busErr) + sizeof (uaecptr));
MetaMemory::MarkTotalAccess (offsetof (M68KExcTableType, addressErr),
offsetof (M68KExcTableType, addressErr) + sizeof (uaecptr));
MetaMemory::MarkTotalAccess (offsetof (M68KExcTableType, illegalInstr),
offsetof (M68KExcTableType, illegalInstr) + sizeof (uaecptr));
MetaMemory::MarkTotalAccess (offsetof (M68KExcTableType, autoVec1),
offsetof (M68KExcTableType, trapN[0]));
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::KeyCurrentState
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
// From Roger:
//
// I was thinking a bit more about Gremlins and games yesterday. Games which
// use the buttons as input have been a special case for Gremlins because
// Gremlins only faked events and pen points. Normally games have to fake
// their own button mask, and they get better results if have some buttons
// held down more often than others.
//
// Now I'm thinking different. With Poser, the KeyCurrentState call should
// be stolen when Gremlins is running. All of the keys possible should have
// their button bits on half the time. This will allow users to test games.
// By not tuning how often buttons should be held down, the testing process
// will take longer to excerise all app functionality, but it's better than
// now. App developers can override the default Gremlin values with their
// own for better results.
//
// To successfully test this, A game like SubHunt should play on it's own for
// a least a while. HardBall is an example of a game which would really
// benefit from recognizing Gremlins is running and tune itself to make the
// testing more effective. It should grant infinite balls until after the
// last level is finished.
//
// I actually think this is important enough to be for Acorn because it
// enables users to test a large class of apps which otherwise can't. I
// think it's easy to implement. Basically just have KeyCurrentState return
// the int from the random number generator. Each bit should be on about
// half the time.
//
// Follow-up commentary: it turns out that this patch is not having the
// effect we hoped. SubHunt was so overwhelmed with events from Gremlins
// that it rarely had the chance to call KeyCurrentState. We're thinking
// of Gremlins backing off on posting events if the SysGetEvent sleep time
// is small (i.e., not "forever"), but we'll have to think about the impact
// on other apps first.
CallROMType SysHeadpatch::KeyCurrentState (void)
{
// DWord KeyCurrentState(void)
struct StackFrame
{
};
if (Hordes::IsOn ())
{
// Let's try setting each bit 1/4 of the time.
uae_u32 bits = rand () & rand ();
m68k_dreg (regs, 0) = bits;
return kSkipROM;
}
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::PenOpen
*
* DESCRIPTION: This is where the pen calibration information is read.
* Preflight this call to add the calibration information
* to the preferences database if it doesn't exist. That
* way, we don't have to continually calibrate the screen
* when we boot up.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::PenOpen (void)
{
// Err PenOpen(void)
struct StackFrame
{
};
#if !TIME_STARTUP
::InstallCalibrationInfo ();
#endif
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::SysAppExit
*
* DESCRIPTION: If the application calling SysAppExit was launched as a
* full application, then "forget" any information we have
* about it. When the next application is launched, we'll
* collect information on it in SysAppStartup.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::SysAppExit (void)
{
// Err SysAppExit(SysAppInfoPtr appInfoP, Ptr prevGlobalsP, Ptr globalsP)
struct StackFrame
{
SysAppInfoPtr appInfoP;
Ptr prevGlobalsP;
Ptr globalsP;
};
uaecptr appInfoP = GET_PARAMETER (appInfoP);
// uaecptr prevGlobalsP = GET_PARAMETER (prevGlobalsP);
// uaecptr globalsP = GET_PARAMETER (globalsP);
if (!appInfoP)
return kExecuteROM;
Int cmd = get_word (appInfoP + offsetof (SysAppInfoType, cmd));
if (cmd == sysAppLaunchCmdNormalLaunch)
{
if (Patches::GetQuitStage () == kWaitingForSysAppExit)
{
Patches::SetQuitStage (kTimeToQuit);
}
}
gCurAppInfo.pop_back (); // !!! should probably make sure appInfoP matches
// the top guy on the stack.
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::SysAppLaunch
*
* DESCRIPTION: Log information app launches and action codes.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::SysAppLaunch (void)
{
struct StackFrame
{
UInt cardNo;
LocalID dbID;
UInt launchFlags;
Word cmd;
Ptr cmdPBP;
DWord* resultP;
};
if (false)
{
UInt cardNo = GET_PARAMETER (cardNo);
LocalID dbID = GET_PARAMETER (dbID);
UInt launchFlags = GET_PARAMETER (launchFlags);
Word cmd = GET_PARAMETER (cmd);
// uaecptr cmdPBP = GET_PARAMETER (cmdPBP);
// uaecptr resultP = GET_PARAMETER (resultP);
const char* launchStr = ::LaunchCmdToString (cmd);
LogAppendMsg ("SysAppLaunch called:");
LogAppendMsg (" cardNo: %ld", (long) cardNo);
LogAppendMsg (" dbID: 0x%08X", (long) dbID);
LogAppendMsg (" launchFlags: 0x%08X", (long) launchFlags);
LogAppendMsg (" cmd: %ld (%s)", (long) cmd, launchStr);
switch (cmd)
{
case sysAppLaunchCmdNormalLaunch:
// No parameter block
break;
case sysAppLaunchCmdFind:
{
// FindParamsType
LogAppendMsg (" FindParamsType:");
LogAppendMsg (" dbAccesMode: %ld", (long) cmd);
LogAppendMsg (" recordNum: %ld", (long) cmd);
LogAppendMsg (" more: %ld", (long) cmd);
LogAppendMsg (" strAsTyped: %ld", (long) cmd);
LogAppendMsg (" strToFind: %ld", (long) cmd);
LogAppendMsg (" numMatches: %ld", (long) cmd);
LogAppendMsg (" lineNumber: %ld", (long) cmd);
LogAppendMsg (" continuation: %ld", (long) cmd);
LogAppendMsg (" searchedCaller: %ld", (long) cmd);
LogAppendMsg (" callerAppDbID: %ld", (long) cmd);
LogAppendMsg (" callerAppCardNo: %ld", (long) cmd);
LogAppendMsg (" appDbID: %ld", (long) cmd);
LogAppendMsg (" newSearch: %ld", (long) cmd);
LogAppendMsg (" searchState: %ld", (long) cmd);
LogAppendMsg (" match: %ld", (long) cmd);
break;
}
case sysAppLaunchCmdGoTo:
// GoToParamsType
break;
case sysAppLaunchCmdSyncNotify:
// No parameter block
break;
case sysAppLaunchCmdTimeChange:
// No parameter block
break;
case sysAppLaunchCmdSystemReset:
// SysAppLaunchCmdSystemResetType
break;
case sysAppLaunchCmdAlarmTriggered:
// SysAlarmTriggeredParamType
break;
case sysAppLaunchCmdDisplayAlarm:
// SysDisplayAlarmParamType
break;
case sysAppLaunchCmdCountryChange:
// Not sent?
break;
case sysAppLaunchCmdSyncRequestLocal:
// case sysAppLaunchCmdSyncRequest:
// No parameter block (I think...)
break;
case sysAppLaunchCmdSaveData:
// SysAppLaunchCmdSaveDataType
break;
case sysAppLaunchCmdInitDatabase:
// SysAppLaunchCmdInitDatabaseType
break;
case sysAppLaunchCmdSyncCallApplicationV10:
// SysAppLaunchCmdSyncCallApplicationTypeV10
break;
case sysAppLaunchCmdPanelCalledFromApp:
// Panel specific?
// SvcCalledFromAppPBType
// NULL
break;
case sysAppLaunchCmdReturnFromPanel:
// No parameter block
break;
case sysAppLaunchCmdLookup:
// App-specific (see AppLaunchCmd.h)
break;
case sysAppLaunchCmdSystemLock:
// No parameter block
break;
case sysAppLaunchCmdSyncRequestRemote:
// No parameter block (I think...)
break;
case sysAppLaunchCmdHandleSyncCallApp:
// SysAppLaunchCmdHandleSyncCallAppType
break;
case sysAppLaunchCmdAddRecord:
// App-specific (see AppLaunchCmd.h)
break;
case sysSvcLaunchCmdSetServiceID:
// ServiceIDType
break;
case sysSvcLaunchCmdGetServiceID:
// ServiceIDType
break;
case sysSvcLaunchCmdGetServiceList:
// serviceListType
break;
case sysSvcLaunchCmdGetServiceInfo:
// serviceInfoType
break;
case sysAppLaunchCmdFailedAppNotify:
// SysAppLaunchCmdFailedAppNotifyType
break;
case sysAppLaunchCmdEventHook:
// EventType
break;
case sysAppLaunchCmdExgReceiveData:
// ExgSocketType
break;
case sysAppLaunchCmdExgAskUser:
// ExgAskParamType
break;
case sysDialLaunchCmdDial:
// DialLaunchCmdDialType
break;
case sysDialLaunchCmdHangUp:
// DialLaunchCmdDialType
break;
case sysSvcLaunchCmdGetQuickEditLabel:
// SvcQuickEditLabelInfoType
break;
case sysAppLaunchCmdURLParams:
// Part of the URL
break;
case sysAppLaunchCmdNotify:
// SysNotifyParamType
break;
case sysAppLaunchCmdOpenDB:
// SysAppLaunchCmdOpenDBType
break;
case sysAppLaunchCmdAntennaUp:
// No parameter block
break;
case sysAppLaunchCmdGoToURL:
// URL
break;
}
}
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::SysBinarySearch
*
* DESCRIPTION: There's a bug in pre-3.0 versions of SysBinarySearch
* that cause it to call the user callback function with a
* pointer just past the array to search. Make a note of
* when we enter SysBinarySearch so that we can make
* allowances for that in MetaMemory's memory checking.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::SysBinarySearch (void)
{
Patches::EnterSysBinarySearch ();
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::SysEvGroupWait
*
* DESCRIPTION: We patch SysEvGroupWait as the mechanism for feeding
* the Palm OS new events. See comments in
* Patches::PuppetString.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::SysEvGroupWait (void)
{
// Err SysEvGroupWait(DWord evID, DWord mask, DWord value, SDWord matchType,
// SDWord timeout)
struct StackFrame
{
DWord evID;
DWord mask;
DWord value;
SDWord matchType;
SDWord timeout;
};
// Only do this under 2.0 and later. Under Palm OS 1.0, EvtGetSysEvent
// called SysSemaphoreWait instead. See our headpatch of that function
// for a chunk of pretty similar code.
if (Patches::OSMajorVersion () == 1)
{
return kExecuteROM;
}
CallROMType result;
Bool clearTimeout;
Patches::PuppetString (result, clearTimeout);
// If timeout is infinite, the kernel wants 0.
// If timeout is none, the kernel wants -1.
if (clearTimeout && GET_PARAMETER (timeout) == 0)
{
SET_PARAMETER (timeout, (uae_u32) -1);
}
return result;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::SysFatalAlert
*
* DESCRIPTION: Intercept this and show the user a dialog.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::SysFatalAlert (void)
{
// UInt SysFatalAlert (CharPtr msg)
struct StackFrame
{
CharPtr msg;
};
Preference<bool> pref (kPrefKeyInterceptSysFatalAlert);
if (!*pref)
{
// Palm OS will display a dialog with just a Reset button
// in it. So *always* turn off the Gremlin, as the user
// won't be able to select "Continue".
Hordes::Stop ();
return kExecuteROM;
}
uaecptr msg = GET_PARAMETER (msg);
string msgString;
if (msg)
{
msgString = PrvToString (msg);
}
else
{
msgString = Platform::GetString (kStr_UnknownFatalError);
}
int button = Errors::SysFatalAlert (msgString.c_str ());
switch (button)
{
case Errors::kDebug: m68k_dreg (regs, 0) = fatalEnterDebugger; break;
case Errors::kReset: m68k_dreg (regs, 0) = fatalReset; break;
case Errors::kContinue: m68k_dreg (regs, 0) = fatalDoNothing; break;
case Errors::kNextGremlin: m68k_dreg (regs, 0) = fatalDoNothing; break;
}
if (button == Errors::kNextGremlin)
{
Hordes::ErrorEncountered();
}
else if (button != Errors::kContinue)
{
Hordes::Stop ();
}
return kSkipROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::SysLaunchConsole
*
* DESCRIPTION: Stub this function out so that it doesn't do anything.
* We completely handle the console in DebugMgr, so there's
* no need to get the ROM all heated up. Also, there are
* problems with actually letting the ROM try to launch its
* console task, at least on the Mac. It will try to open
* a serial port socket and do stuff with it. That attempt
* will fail, as much of the serial port processing on the
* Mac is handled at idle time, and idle time processing is
* inhibited when handling debugger packets
* (SysLaunchConsole is usually called by a debugger via
* the RPC packet). Since serial port processing doesn't
* occur, SysLaunchConsole hangs.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::SysLaunchConsole (void)
{
// Err SysLaunchConsole(void)
struct StackFrame
{
};
m68k_dreg (regs, 0) = 0; // no error
return kSkipROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::SysReset
*
* DESCRIPTION: Reset the device our way. This way, we can keep track
* of the fact that we're booting and need to make those
* "special allowances" that are necessary at boot time.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::SysReset (void)
{
// Err SysReset(void)
struct StackFrame
{
};
// Causes us to reset the next time we're in the CPU loop.
regs.spcflags |= SPCFLAG_RESET;
return kSkipROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::SysSemaphoreWait
*
* DESCRIPTION: We patch SysSemaphoreWait as the mechanism for feeding
* the Palm OS new events. See comments in
* Patches::PuppetString.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::SysSemaphoreWait (void)
{
// Err SysSemaphoreWait(DWord smID, DWord priority, SDWord timeout)
struct StackFrame
{
DWord smID;
DWord priority;
SDWord timeout;
};
// Only do this under 1.0. Under Palm OS 2.0 and later, EvtGetSysEvent
// calls SysEvGroupWait instead. See our headpatch of that function
// for a chunk of pretty similar code.
if (Patches::OSMajorVersion () != 1)
{
return kExecuteROM;
}
CallROMType result;
Bool clearTimeout;
Patches::PuppetString (result, clearTimeout);
// If timeout is infinite, the kernel wants 0.
// If timeout is none, the kernel wants -1.
if (clearTimeout && GET_PARAMETER (timeout) == 0)
{
SET_PARAMETER (timeout, (uae_u32) -1);
}
return result;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::SysUIAppSwitch
*
* DESCRIPTION: SysUIAppSwitch is called from the following locations
* for the given reasons. When running Gremlins, we want
* to prevent SysUIAppSwitch from doing its job, which is
* to record information about the application to switch
* to and to then post an appStopEvent to the current
* application.
*
* There are a couple of places where SysUIAppSwitch is
* called to quit and re-run the current application.
* Therefore, we want to stub out SysUIAppSwitch only when
* the application being switched to is not the currently
* running application.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
// Places where this is called:
//
// - LauncherMain.c (AppsViewSwitchApp) to launch new app.
// - PrefApp (PilotMain) to launch a panel.
// - SyncApp (prvLaunchExternalModule) to launch a "service owner"
// - AppLaunchCmd.h (AppLaunchWithCommand) ???
// - ModemPanel.h (ModemPanelLaunch) ???
// - callApp.h (LaunchApp) ???
// - ExgMgr.c (ExgNotifyReceive) to send a sysAppLaunchCmdGoTo message.
// - GraffitiGlue.c (PrvLaunchGraffitiDemo) to launch Graffiti demo.
// - Keyboard.c (PrvLaunchGraffitiDemo) to launch Graffiti demo.
// - Launcher.c (LauncherFormHandleEvent) handles taps on launcher icons.
// - SystemMgr.c (SysHandleEvent) to send sysAppLaunchCmdSystemLock
// in response to seeing a lockChr keyboard message.
// - SystemMgr.c (SysHandleEvent) to switch apps in response to hard#Chr
// keyboard messages.
// - Find.c (Find) to send sysAppLaunchCmdGoTo message.
//
// - ButtonsPanel.c (ButtonsFormHandleEvent) switch to another panel.
// - FormatsPanel.c (FormatsFormHandleEvent) switch to another panel.
// - GeneralPanel.c (GeneralFormHandleEvent) switch to another panel.
// - ModemPanel.c (ModemFormHandleEvent) switch to another panel.
// - NetworkPanel.c (NetworkFormHandleEvent) switch to another panel.
// - OwnerPanel.c (OwnerViewHandleEvent) switch to another panel.
// - ShortCutsPanel.c (ShortCutFormHandleEvent) switch to another panel.
CallROMType SysHeadpatch::SysUIAppSwitch (void)
{
// Err SysUIAppSwitch(UInt cardNo, LocalID dbID, Word cmd, Ptr cmdPBP)
struct StackFrame
{
UInt cardNo;
LocalID dbID;
Word cmd;
Ptr cmdPBP;
};
UInt cardNo = (UInt) GET_PARAMETER (cardNo);
LocalID dbID = (LocalID) GET_PARAMETER (dbID);
// Word cmd = (Word) GET_PARAMETER (cmd);
// We are headpatching SysUIAppSwitch; if we skip the ROM version, we
// need to replicate at least this part of its functionality:
//
// If the last launch attempt failed, release the command parameter block, if
// any. When a launch succeeds, the UIAppShell will clear this global
// and free the chunk itself when the app quits.
{
CEnableFullAccess munge; // Remove blocks on memory access.
uaecptr nextUIAppCmdPBP = LowMem_GetGlobal (nextUIAppCmdPBP);
if (nextUIAppCmdPBP) {
MemPtrFree((VoidPtr) nextUIAppCmdPBP);
LowMem_SetGlobal (nextUIAppCmdPBP, 0);
}
}
// Get the current application card and db. If we are attempting to switch
// to the same app, allow it.
UInt currentCardNo;
LocalID currentDbID;
Err err = SysCurAppDatabase (¤tCardNo, ¤tDbID);
// Got an error? Give up and let default handling occur.
if (err != 0)
return kExecuteROM;
// Switching to current app; let default handling occur.
if ((cardNo == currentCardNo) && (dbID == currentDbID))
return kExecuteROM;
// OK, we're switching to a different application. If Gremlins is running
// and running in "single app" mode, then stub out SysUIAppSwitch to do nothing.
if (Hordes::IsOn () && !Hordes::CanSwitchToApp (cardNo, dbID))
{
m68k_dreg (regs, 0) = 0;
return kSkipROM;
}
// Do the normal app switch.
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::SysUIBusy
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
extern uae_u32 gStart;
CallROMType SysHeadpatch::SysUIBusy (void)
{
// Word SysUIBusy(Boolean set, Boolean value)
struct StackFrame
{
Boolean set;
Boolean value;
};
#if TIME_STARTUP
static calledOnce;
if (!calledOnce)
{
calledOnce = true;
uae_u32 now = Platform::GetMilliseconds ();
uae_u32 elapsed = now - gStart;
char buffer[200];
sprintf (buffer, "startup = %ld milliseconds "
"instructions = %ld "
elapsed, Emulator::GetInstructionCount ());
Platform::ReportString (buffer);
}
#endif
return kExecuteROM;
}
/***********************************************************************
*
* FUNCTION: SysHeadpatch::TimInit
*
* DESCRIPTION: TimInit is where the date is read from non-volatile
* memory into private Time Manager memory. After that,
* the Time Manager uses its private copy to update the
* non-volatile copy. Thus, stashing correct values of
* the date into non-volatile memory should be done just
* before TimInit.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType SysHeadpatch::TimInit (void)
{
// Err TimInit(void)
struct StackFrame
{
};
::PrvSetCurrentDate ();
return kExecuteROM;
}
#pragma mark -
// ===========================================================================
// SysTailpatch
// ===========================================================================
void SysTailpatch::DmGetNextDatabaseByTypeCreator (void)
{
static int recursing;
if (recursing)
return;
struct StackFrame
{
Boolean newSearch;
DmSearchStatePtr stateInfoP;
ULong type;
ULong creator;
Boolean onlyLatestVers;
UIntPtr cardNoP;
LocalID* dbIDP;
};
// Boolean newSearch = GET_PARAMETER (newSearch);
uaecptr stateInfoP = GET_PARAMETER (stateInfoP);
ULong type = GET_PARAMETER (type);
ULong creator = GET_PARAMETER (creator);
Boolean onlyLatestVers = GET_PARAMETER (onlyLatestVers);
uaecptr cardNoP = GET_PARAMETER (cardNoP);
uaecptr dbIDP = GET_PARAMETER (dbIDP);
Err result = m68k_dreg (regs, 0);
if (result == errNone && type == sysFileTExtension && creator == 0)
{
// Make sure we are called from within PrvLoadExtensions.
// If so, get information on the returned cardNoP and dbIDP.
UInt cardNo = get_word (cardNoP);
LocalID dbID = get_long (dbIDP);
ULong actualCreator;
Err err = ::DmDatabaseInfo (cardNo, dbID, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, &actualCreator);
// If the returned database has the creator of 'xxxx'
// call DmGetNextDatabaseByTypeCreator again to skip
// past this entry.
if (err == errNone && actualCreator == 'xxxx')
{
recursing = true;
err = ::DmGetNextDatabaseByTypeCreator (false,
(DmSearchStatePtr) stateInfoP,
type, creator, onlyLatestVers,
(UIntPtr) cardNoP, (LocalID*) dbIDP);
m68k_dreg (regs, 0) = (uae_u32) err;
recursing = false;
}
}
}
/***********************************************************************
*
* FUNCTION: SysTailpatch::EvtGetEvent
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void SysTailpatch::EvtGetEvent (void)
{
// void EvtGetEvent(const EventPtr event, SDWord timeout);
struct StackFrame
{
const EventPtr event;
SDWord timeout;
};
uaecptr event = GET_PARAMETER (event);
SDWord timeout = GET_PARAMETER (timeout);
LogEvtGetEvent (event, timeout);
gEvtGetEventCalled = true;
}
/***********************************************************************
*
* FUNCTION: SysTailpatch::EvtGetPen
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void SysTailpatch::EvtGetPen (void)
{
// void EvtGetPen(SWord *pScreenX, SWord *pScreenY, Boolean *pPenDown)
struct StackFrame
{
SWord *pScreenX;
SWord *pScreenY;
Boolean *pPenDown;
};
uaecptr pScreenX = GET_PARAMETER (pScreenX);
uaecptr pScreenY = GET_PARAMETER (pScreenY);
uaecptr pPenDown = GET_PARAMETER (pPenDown);
LogEvtGetPen (pScreenX, pScreenY, pPenDown);
}
/***********************************************************************
*
* FUNCTION: SysTailpatch::EvtGetSysEvent
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void SysTailpatch::EvtGetSysEvent (void)
{
// void EvtGetSysEvent(EventPtr eventP, Long timeout)
struct StackFrame
{
EventPtr event;
Long timeout;
};
uaecptr event = GET_PARAMETER (event);
Long timeout = GET_PARAMETER (timeout);
LogEvtGetSysEvent (event, timeout);
}
/***********************************************************************
*
* FUNCTION: SysTailpatch::FtrInit
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void SysTailpatch::FtrInit (void)
{
// void FtrInit(void)
struct StackFrame
{
};
// Get information about the current OS so that we know
// what features are implemented (for those cases when we
// can't make other tests, like above where we test for
// the existance of a trap before calling it).
// Read the version into a local variable; the ROM Stub facility
// automatically maps local variables into Palm space so that ROM
// functions can get to them. If we were to pass &gOSVersion,
// the DummyBank functions would complain about an invalid address.
DWord value;
Err err = FtrGet (sysFtrCreator, sysFtrNumROMVersion, &value);
if (err == errNone)
{
gOSVersion = value;
}
else
{
gOSVersion = kOSUndeterminedVersion;
}
err = FtrGet (sysFtrCreator, sysFtrNumEncoding, &value);
gHaveJapanese = (err == errNone);
}
/***********************************************************************
*
* FUNCTION: SysTailpatch::HwrMemReadable
*
* DESCRIPTION: Patch this function so that it returns non-zero if the
* address is in the range of memory that we've mapped in
* to emulated space.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void SysTailpatch::HwrMemReadable (void)
{
// DWord HwrMemReadable(VoidPtr address)
struct StackFrame
{
VoidPtr address;
};
uaecptr address = GET_PARAMETER (address);
DWord result = m68k_dreg (regs, 0);
if (result == 0)
{
void* addrStart;
uae_u32 addrLen;
Memory::GetMappingInfo ((void*) address, &addrStart, &addrLen);
m68k_dreg (regs, 0) = addrLen;
}
}
/***********************************************************************
*
* FUNCTION: SysTailpatch::HwrSleep
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void SysTailpatch::HwrSleep (void)
{
// void HwrSleep(Boolean untilReset, Boolean emergency)
struct StackFrame
{
Boolean untilReset;
Boolean dummy;
Boolean emergency;
};
MetaMemory::MarkLowMemory (offsetof (M68KExcTableType, busErr),
offsetof (M68KExcTableType, busErr) + sizeof (uaecptr));
MetaMemory::MarkLowMemory (offsetof (M68KExcTableType, addressErr),
offsetof (M68KExcTableType, addressErr) + sizeof (uaecptr));
MetaMemory::MarkLowMemory (offsetof (M68KExcTableType, illegalInstr),
offsetof (M68KExcTableType, illegalInstr) + sizeof (uaecptr));
MetaMemory::MarkLowMemory (offsetof (M68KExcTableType, autoVec1),
offsetof (M68KExcTableType, trapN[0]));
}
/***********************************************************************
*
* FUNCTION: SysTailpatch::SysAppStartup
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void SysTailpatch::SysAppStartup (void)
{
// Err SysAppStartup(SysAppInfoPtr* appInfoPP, Ptr* prevGlobalsP, Ptr* globalsPtrP)
struct StackFrame
{
SysAppInfoPtr* appInfoPP;
Ptr* prevGlobalsP;
Ptr* globalsPtrP;
};
if (m68k_dreg (regs, 0) != errNone)
return;
uaecptr appInfoPP = GET_PARAMETER (appInfoPP);
if (!appInfoPP)
return;
uaecptr appInfoP = get_long (appInfoPP);
if (!appInfoP)
return;
Int cmd = get_word (appInfoP + offsetof (SysAppInfoType, cmd));
EmuAppInfo newAppInfo = Patches::CollectCurrentAppInfo (appInfoP);
newAppInfo.fCmd = cmd;
gCurAppInfo.push_back (newAppInfo);
if (cmd == sysAppLaunchCmdNormalLaunch)
{
if (Patches::GetQuitStage () == kWaitingForSysAppStartup)
{
Patches::SetQuitStage (kWaitingForSysAppExit);
}
// Clear the flags that tell us which warnings we've already issued.
// We reset these flags any time a new application is launched.
//
// !!! Put these flags into EmuAppInfo? That way, we can keep
// separate sets for sub-launched applications.
Errors::ClearWarningFlags ();
}
}
/***********************************************************************
*
* FUNCTION: SysTailpatch::SysBinarySearch
*
* DESCRIPTION: There's a bug in pre-3.0 versions of SysBinarySearch
* that cause it to call the user callback function with a
* pointer just past the array to search. Make a note of
* when we enter SysBinarySearch so that we can make
* allowances for that in MetaMemory's memory checking.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void SysTailpatch::SysBinarySearch (void)
{
Patches::ExitSysBinarySearch ();
}
/***********************************************************************
*
* FUNCTION: SysTailpatch::TimInit
*
* DESCRIPTION: TimInit is where a special boolean is set to trigger a
* bunch of RTC bug workarounds in the ROM (that is, there
* are RTC bugs in the Dragonball that the ROM needs to
* workaround). We're not emulating those bugs, so turn
* off that boolean. Otherwise, we'd have to add support
* for the 1-second, 1-minute, and 1-day RTC interrupts in
* order to get those workarounds to work.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void SysTailpatch::TimInit (void)
{
// Err TimInit(void)
struct StackFrame
{
};
// Turn off the RTC bug workaround flag.
uaecptr timGlobalsP = LowMem_GetGlobal (timGlobalsP);
if (get_byte (timGlobalsP + 8) == 0x01)
{
put_byte (timGlobalsP + 8, 0x00);
}
}
/***********************************************************************
*
* FUNCTION: SysTailpatch::UIInitialize
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void SysTailpatch::UIInitialize (void)
{
// Void UIInitialize (void)
struct StackFrame
{
};
Patches::SetUIInitialized (true);
// Prevent the device from going to sleep.
SysSetAutoOffTime (0);
// Can't call PrefSetPreference on 1.0 devices....
if (LowMem::TrapExists (sysTrapPrefSetPreference))
{
PrefSetPreference (prefAutoOffDuration, 0);
}
// Install a 'pose' feature so that people can tell if they're running
// under the emulator or not. We *had* added a HostControl function
// for this, but people can call that only under Poser or under 3.0
// and later ROMs. Which means that there's no way to tell under 2.0
// and earlier ROMs when running on an actual device.
//
// Note that we do this here instead of in a tailpatch to FtrInit because
// of a goofy inter-dependency. FtrInit is called early in the boot process
// before a valid stack has been allocated and switched to. During this time,
// the ROM is using a range of memory that happens to reside in a free
// memory chunk. Routines in MetaMemory know about this and allow the use
// of this unallocate range of memory. However, in order to work, they need
// to know the current stack pointer value. If we were to call FtrSet from
// the FtrInit tailpatch, the stack pointer will point to the stack set up
// by the ATrap object, not the faux stack in use at the time of FtrInit.
// Thus, the MetaMemory routines get confused and end up getting in a state
// where accesses to the temporary OS stack are flagged as invalid. Hence,
// we defer adding these Features until much later in the boot process (here).
FtrSet ('pose', 0, 0);
// If we're listening on a socket, install the 'gdbS' feature. The
// existance of this feature causes programs written with the prc tools
// to enter the debugger when they're launched.
if (Debug::ConnectedToTCPDebugger ())
{
FtrSet ('gdbS', 0, 0x12BEEF34);
}
// Install the HotSync user-name.
Preference<string> userName (kPrefKeyUserName);
::SetHotSyncUserName (userName->c_str ());
// Auto-load any files in the Autoload[Foo] directories.
::PrvAutoload ();
}
#pragma mark -
/***********************************************************************
*
* FUNCTION: HtalLibHeadpatch::HtalLibSendReply
*
* DESCRIPTION: Ohhh...I'm going to Programmer Hell for this one...
* We call DlkDispatchRequest to install the user name in
* our UIInitialize patch. DlkDispatchRequest will
* eventually call HtalLibSendReply to return a result
* code. Well, we haven't fired up the Htal library, and
* wouldn't want it to send a response even if we had.
* Therefore, I'll subvert the whole process by setting
* the HTAL library refNum passed in to the Desktop Link
* Manager to an invalid value. I'll look for this
* value in the SysTrap handling code and no-op the call
* by calling this stub.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
CallROMType HtalLibHeadpatch::HtalLibSendReply (void)
{
m68k_dreg (regs, 0) = errNone;
return kSkipROM;
}
#pragma mark -
/***********************************************************************
*
* FUNCTION: PrvToString
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
string PrvToString (uaecptr s)
{
string result;
size_t sLen = uae_strlen (s);
if (sLen > 0)
{
result.resize (sLen);
uae_strcpy (&result[0], s);
}
return result;
}
/***********************************************************************
*
* FUNCTION: PrvAutoload
*
* DESCRIPTION: Install the files in the various Autoload directories.
* If there are any designated to be run, pick on to run.
* If the emulator should exit when the picked application
* quits, schedule it to do so.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void PrvAutoload (void)
{
// Load all the files in one blow.
FileRefList fileList;
Startup::GetAutoLoads (fileList);
::LoadPalmFileList (fileList);
// Get the application to switch to (if any);
string appToRun = Startup::GetAutoRunApp ();
if (!appToRun.empty())
{
char name[dmDBNameLength];
strcpy (name, appToRun.c_str());
UInt cardNo = 0;
LocalID dbID = ::DmFindDatabase (cardNo, name);
if (dbID != 0)
{
Patches::SetSwitchApp (cardNo, dbID);
// If we're supposed to quit after running this application,
// start that process in motion.
if (Startup::QuitOnExit ())
{
Patches::QuitOnAppExit (true);
}
}
}
}
/***********************************************************************
*
* FUNCTION: PrvSetCurrentDate
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void PrvSetCurrentDate (void)
{
// Get the current date.
long year, month, day;
::GetHostDate (&year, &month, &day);
// Get the current non-volatile variables.
SysNVParamsType params[100]; // Allocate much more memory than we need
// in case the ROM's notion of SysNVParamsType
// is different from ours.
::MemNVParams (false, ¶ms[0]);
// Update the "hours adjustment" value to contain the current date.
params[0].rtcHours = ::DateToDays (year, month, day) * 24;
// Re-write the non-volatile variables.
::MemNVParams (true, ¶ms[0]);
}
#include "PalmPackPop.h"
|