summaryrefslogtreecommitdiffstats
path: root/private/ntos/nthals/halr94a/mips/jxdisp.c
blob: fdf7ebb064f1a10731124f7b9447af0de4cbbad2 (plain) (blame)
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
/* #pragma comment(exestr, "@(#) NEC(MIPS) jxdisp.c 1.2 95/10/17 01:18:22" ) */
/*++

Copyright (c) 1995 NEC Corporation
Copyright (c) 1991-1993  Microsoft Corporation

Module Name:

    jxdisp.c

Abstract:

    This module implements the HAL display initialization and output routines
    for a R4400 R94A system.

History:


--*/

/*
 * New Code for 3.51
 *
 * M001 kuriyama@oa2.kb.nec.co.jp Fri Jul 14 11:46:06 JST 1995
 * 	-change textmode 80x50
 *
 * M002 kuriyama@oa2.kb.nec.co.jp Tue Jul 18 15:36:23 JST 1995
 *      -change for fw interface
 *
 * M003 kuriyama@oa2.kb.nec.co.jp Fri Jul 21 14:14:54 JST 1995
 *      -add  call HalpResetDisplayParameters
 *            for bug fix DPI board panic message
 *
 * M004 kuriyama@oa2.kb.nec.co.jp Wed Aug  2 14:28:28 JST 1995
 *      -add ESM critical error logging
 *
 * S005 kuriyama@oa2.kb.nec.co.jp Wed Aug 23 23:42:59 JST 1995
 *	-change for scroll bug fix
 *
 * S006 nishi@oa2.kb.nec.co.jp Mon Sep 4 18:42:00 JST 1995
 *	-change for GLINT new revision board
 *		R01=00,R02=02
 * M007 nishi@oa2.kb.nec.co.jp Mon Sep 18 18:42:00 JST 1995
 *	- Add Software Power Off, when system panic is occured
 *
 * M008 nishi@oa2.kb.nec.co.jp Mon Sep 18 18:42:00 JST 1995
 *	- Change Logic for resume fixed TLB for DMA
 *
 * M009 kuriyama@oa2.kbnes.nec.co.jp Mon Sep 18 19:58:22 JST 1995
 *      - bug fix for tga 800x600 72Hz
 *
 * M010 v-akitk@microsoft.com
 *      - Change for Software Power Supply(R96B)
 *
 */


#include "halp.h"
#include "jazzvdeo.h"
#include "jzvxl484.h"
#include <jaginit.h>
#include "cirrus.h"
#include "modeset.h"
#include "mode542x.h"
#include <tga.h>
#include <glint.h>
#include <rgb525.h>

#include "string.h"
#include "pci.h"
/* START M007 */
#define HEADER_FILE
#include "kxmips.h"
/* START M007 */
//
// Put all code for HAL initialization in the INIT section. It will be
// deallocated by memory management when phase 1 initialization is
// completed.
//

#if defined(ALLOC_PRAGMA)

#pragma alloc_text(INIT, HalpInitializeDisplay0)
#pragma alloc_text(INIT, HalpInitializeDisplay1)

#endif

//
// for x86bios emulate
//
PCHAR K351UseBios=NULL;
VOID HalpCopyROMs(VOID);

PHAL_RESET_DISPLAY_PARAMETERS HalpResetDisplayParameters;  // M003

typedef
VOID
(*PHALP_CONTROLLER_SETUP) (
    VOID
    );

typedef
VOID
(*PHALP_DISPLAY_CHARACTER) (
    UCHAR
    );
typedef
VOID
(*PHALP_SCROLL_SCREEN) (
    UCHAR
    );

#define    TAB_SIZE            4
#define    TEXT_ATTR           0x1F

//
// Define forward referenced procedure prototypes.
//
VOID
HalpDisplayINT10Setup (
VOID);

VOID HalpOutputCharacterINT10 (
    IN UCHAR Character );

VOID HalpScrollINT10 (
    IN UCHAR line
    );

VOID HalpDisplayCharacterVGA (
    IN UCHAR Character );

BOOLEAN
HalpInitializeX86DisplayAdapter(
    VOID
    );


PHALP_DISPLAY_CHARACTER HalpDisplayCharacter = NULL;

//
// Define forward referenced procedure prototypes.
//

VOID
HalpDisplayCharacterOld (
    IN UCHAR Character
    );

VOID
HalpOutputCharacterOld(
    IN PUCHAR Glyph
    );

VOID
HalpDisplayCirrusSetup (
    VOID
    );

BOOLEAN
HalpCirrusInterpretCmdStream (
    PUSHORT pusCmdStream
    );

VOID
HalpDisplayTgaSetup (
    VOID
    );

/*
VOID
RGB525_WRITE(
    ULONG dac,
    ULONG offset,
    UCHAR data);

VOID
RGB525_SET_REG(
    ULONG dac,
    ULONG index,
    UCHAR data);
*/

VOID
HalpDisplayGlintSetup(
    VOID
    );

VOID
Write_Dbg_Uchar(
PUCHAR,
UCHAR
);

//
// Must use 32Bit transfer. RtlMoveMemory() uses 64Bit transfer.
//

VOID
HalpMoveMemory32 (
   PUCHAR Destination,
   PUCHAR Source,
   ULONG Length
);
/* Start M007 */
VOID
HalpMrcModeChange(
   UCHAR Mode
);
/*End M007 */

//
// Define virtual address of the video memory and control registers.
//

#define VIDEO_MEMORY_BASE 0x40000000

//
//  Define memory access constants for VXL
//

#define CIRRUS_BASE ((PJAGUAR_REGISTERS)0x403ff000)
#define CIRRUS_OFFSET ((PUSHORT)0x3b0)

#define TGA_REGISTER_BASE       ((ULONG)0x403ff000)
#define R94A_PCI_ADDR_REG       ((PULONG)(0x80000518 | 0xffffc000))
#define R94A_PCI_DATA_REG       ((PULONG)(0x80000520 | 0xffffc000))

#define GLINT_CONTROL_REGISTER_BASE     ((ULONG)0x403ff000) // M021
#define GLINT_VIDEO_REGISTER_BASE       ((ULONG)0x403fe000)
#define RGB525_REGISTER_BASE    ((ULONG)0x403fd000)

//
// Define controller setup routine type.
//

typedef
VOID
(*PHALP_CONTROLLER_SETUP) (
    VOID
    );

//
// Define OEM font variables.
//

ULONG HalpBytesPerRow;
ULONG HalpCharacterHeight;
ULONG HalpCharacterWidth;
ULONG HalpColumn;
ULONG HalpDisplayText;
ULONG HalpDisplayWidth;
POEM_FONT_FILE_HEADER HalpFontHeader;
ULONG HalpRow;
ULONG HalpScrollLength;
ULONG HalpScrollLine;

ULONG HalpGlintRevisionId;      // S006

//
// Define display variables.
//

BOOLEAN HalpDisplayOwnedByHal;
ENTRYLO HalpDisplayPte;
ULONG HalpDisplayControlBase = 0;
ULONG HalpDisplayMemoryBase = 0; // M021
ULONG HalpDisplayResetRegisterBase = 0;
ULONG HalpDisplayVxlClockRegisterBase = 0;
ULONG HalpDisplayVxlBt484RegisterBase = 0;
ULONG HalpDisplayVxlJaguarRegisterBase = 0;
PHALP_CONTROLLER_SETUP HalpDisplayControllerSetup = NULL;
MONITOR_CONFIGURATION_DATA HalpMonitorConfigurationData;
BOOLEAN HalpDisplayTypeUnknown = FALSE;
ULONG HalpG364Type = 0;

LARGE_INTEGER HalpCirrusPhigicalVideo = {0,0};

LARGE_INTEGER HalpTgaPhigicalVideo = {0,0};
LARGE_INTEGER HalpTgaPhigicalVideoCont = {0,0};

LARGE_INTEGER HalpGlintPhygicalVideo = {0,0};
LARGE_INTEGER HalpGlintPhygicalVideoCont = {0,0};
ULONG HalpDisplayPCIDevice = FALSE;
ULONG HalpDisplayType16BPP = FALSE;

PVOID HalpMrcControlBase = NULL;        // M007
BOOLEAN HalpMrcControlMapped = FALSE;   // M007

typedef struct _R94A_PCI_CONFIGURATION_ADDRESS_REG{
    ULONG Reserved2: 2;
    ULONG RegisterNumber: 6;
    ULONG FunctionNumber: 3;
    ULONG DeviceNumber: 5;
    ULONG BusNumber: 8;
    ULONG Reserved1: 7;
    ULONG ConfigEnable: 1;
} R94A_PCI_CONFIGURATION_ADDRESS_REG, *PR94A_PCI_CONFIGURATION_ADDRESS_REG;

VOID
HalpReadPCIConfigUlongByOffset (
    IN PCI_SLOT_NUMBER Slot,
    IN PULONG Buffer,
    IN ULONG Offset
   );

BOOLEAN
HalpCheckPCIDevice (
    IN PCI_SLOT_NUMBER Slot
   );

BOOLEAN
HalpInitializeDisplay0 (
    IN PLOADER_PARAMETER_BLOCK LoaderBlock
    )

/*++

Routine Description:

    This routine maps the video memory and control registers into the user
    part of the idle process address space, initializes the video control
    registers, and clears the video screen.

Arguments:

    LoaderBlock - Supplies a pointer to the loader parameter block.

Return Value:

    If the initialization is successfully completed, than a value of TRUE
    is returned. Otherwise, a value of FALSE is returned.

--*/

{

    PCONFIGURATION_COMPONENT_DATA Child;
    PCONFIGURATION_COMPONENT_DATA ConfigurationEntry;
    POEM_FONT_FILE_HEADER FontHeader;
    ULONG Index;
    ULONG MatchKey;
    PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor;
    PLIST_ENTRY NextEntry;
    PENTRYLO PageFrame;
    ENTRYLO Pte;
    ULONG StartingPfn;
    PCI_SLOT_NUMBER Slot;
    ULONG ReadValue;
    PCHAR Options;

    //
    // Set the address of the font file header and compute display variables.
    //
    // N.B. The font information suppled by the OS Loader is used during phase
    //      0 initialization. During phase 1 initialization, a pool buffer is
    //      allocated and the font information is copied from the OS Loader
    //      heap into pool.
    //

    FontHeader = (POEM_FONT_FILE_HEADER)LoaderBlock->OemFontFile;
    HalpFontHeader = FontHeader;
    HalpBytesPerRow = (FontHeader->PixelWidth + 7) / 8;
    HalpCharacterHeight = FontHeader->PixelHeight;
    HalpCharacterWidth = FontHeader->PixelWidth;

#if defined(_X86_DBG_)
    DbgPrint("\n DISP 0\n");  //DBGDBG
#endif // _X86_DBG_

    //
    // Find the configuration entry for the first display controller.
    //

    MatchKey = 0;
    ConfigurationEntry = KeFindConfigurationEntry(LoaderBlock->ConfigurationRoot,
                                                  ControllerClass,
                                                  DisplayController,
                                                  &MatchKey);
    if (ConfigurationEntry == NULL) {
        MatchKey = 1;
        ConfigurationEntry = KeFindConfigurationEntry(LoaderBlock->ConfigurationRoot,
                                                     ControllerClass,
                                                     DisplayController,
                                                     &MatchKey);
        if (ConfigurationEntry == NULL) {
            return FALSE;
        }
    }


    //
    // Determine which video controller is present in the system.
    // Copy the display controller and monitor parameters in case they are
    // needed later to reinitialize the display to output a message.
    //

    if (LoaderBlock->LoadOptions != NULL) {
      Options = LoaderBlock->LoadOptions;
      _strupr(Options);
      K351UseBios = strstr(Options, "USEBIOS");
    }
    if(K351UseBios!=NULL){
        DbgPrint("\nUSEBIOS---\n");
        HalpDisplayControllerSetup = HalpDisplayINT10Setup;
        HalpDisplayCharacter = HalpDisplayCharacterVGA;
        HalpDisplayControlBase = 0x90000000;

    }else if (!strcmp(ConfigurationEntry->ComponentEntry.Identifier,
                      "necvdfrb")) {

        HalpDisplayControllerSetup = HalpDisplayCirrusSetup;
        HalpDisplayCharacter = HalpDisplayCharacterOld;
        HalpDisplayControlBase = 0x90000000;

    } else if (!strcmp(ConfigurationEntry->ComponentEntry.Identifier,"10110004")) {
        //
        // for DEC21030 PCI
        //

        HalpDisplayControllerSetup = HalpDisplayTgaSetup;
        HalpDisplayCharacter = HalpDisplayCharacterOld;
        HalpDisplayPCIDevice = TRUE;

        Slot.u.bits.FunctionNumber = 0;

        for (Slot.u.bits.DeviceNumber = 3; Slot.u.bits.DeviceNumber < 6; Slot.u.bits.DeviceNumber++){
            HalpReadPCIConfigUlongByOffset(Slot,&ReadValue,0);

            if (ReadValue == 0x00041011){
                HalpReadPCIConfigUlongByOffset(Slot,&ReadValue,0x4);

                if ((ReadValue & 0x00000002) == 0x2){
                    HalpReadPCIConfigUlongByOffset(Slot,&ReadValue, 0x10);
                    HalpDisplayControlBase =
                        (ReadValue & 0xfffffff0) + TGA_REG_SPC_OFFSET;

                } else {
                    return FALSE;

                }
            }
        }

    } else if (!strcmp(ConfigurationEntry->ComponentEntry.Identifier,"3D3D0001")) {

        //
        // for GLINT 300SX PCI
        //

        HalpDisplayControllerSetup = HalpDisplayGlintSetup;
        HalpDisplayCharacter = HalpDisplayCharacterOld;
        HalpDisplayPCIDevice = TRUE;
        HalpDisplayType16BPP = TRUE;

        //
        // FunctionNumber always zero
        //

        Slot.u.bits.FunctionNumber = 0;

        for (Slot.u.bits.DeviceNumber = 3; Slot.u.bits.DeviceNumber < 6; Slot.u.bits.DeviceNumber++){
            HalpReadPCIConfigUlongByOffset(Slot,&ReadValue,0);

            if (ReadValue == 0x00013d3d){
                HalpReadPCIConfigUlongByOffset(Slot,&ReadValue,0x4);

                //
                // GLINT 300SX has no I/O space regions
                //

                if (ReadValue & 0x2){

                    //
                    // Control Registers
                    //

                    HalpReadPCIConfigUlongByOffset(Slot,&ReadValue, 0x10);
                    HalpDisplayControlBase = (ReadValue & 0xfffffff0);

                    //
                    // Framebuffer
                    //

                    HalpReadPCIConfigUlongByOffset(Slot,&ReadValue, 0x18);
                        HalpDisplayMemoryBase = (ReadValue & 0xfffffff0);

		   //
  		   //  Revision ID
		   //

		    HalpReadPCIConfigUlongByOffset(Slot,&ReadValue, 0x08);      // S006
		    HalpGlintRevisionId = (ReadValue & 0x000000ff);

                } else {
                    return FALSE;

                }
            }
        }
    } else {
	// M002 +++
        HalpDisplayControllerSetup = HalpDisplayINT10Setup;
        HalpDisplayCharacter = HalpDisplayCharacterVGA;
        HalpDisplayControlBase = 0x90000000;
        HalpDisplayTypeUnknown = TRUE;
	// M002 ---
    }

    Child = ConfigurationEntry->Child;

    RtlMoveMemory((PVOID)&HalpMonitorConfigurationData,
                  Child->ConfigurationData,
                  Child->ComponentEntry.ConfigurationDataLength);

    //
    // Compute character output display parameters.
    //

    HalpDisplayText =
        HalpMonitorConfigurationData.VerticalResolution / HalpCharacterHeight;

    if(HalpDisplayControllerSetup == HalpDisplayCirrusSetup){
        HalpScrollLine =
            1024 * HalpCharacterHeight;
    } else if (HalpDisplayType16BPP) {

        HalpScrollLine =
         HalpMonitorConfigurationData.HorizontalResolution * HalpCharacterHeight * sizeof(USHORT);

    } else {
        HalpScrollLine =
         HalpMonitorConfigurationData.HorizontalResolution * HalpCharacterHeight;
    }

    HalpScrollLength = HalpScrollLine * (HalpDisplayText - 1);

    if(HalpDisplayControllerSetup == HalpDisplayCirrusSetup){
        HalpDisplayWidth =
                1024 / HalpCharacterWidth;

    }else{

        HalpDisplayWidth =
         HalpMonitorConfigurationData.HorizontalResolution / HalpCharacterWidth;
    }

    //
    // Scan the memory allocation descriptors and allocate a free page
    // to map the video memory and control registers, and initialize the
    // PDE entry.
    //

    NextEntry = LoaderBlock->MemoryDescriptorListHead.Flink;
    while (NextEntry != &LoaderBlock->MemoryDescriptorListHead) {
        MemoryDescriptor = CONTAINING_RECORD(NextEntry,
                                             MEMORY_ALLOCATION_DESCRIPTOR,
                                             ListEntry);

        if ((MemoryDescriptor->MemoryType == LoaderFree) &&
            (MemoryDescriptor->PageCount > 1)) {
            StartingPfn = MemoryDescriptor->BasePage;
            MemoryDescriptor->BasePage += 1;
            MemoryDescriptor->PageCount -= 1;
            break;
        }

        NextEntry = NextEntry->Flink;
    }

    ASSERT(NextEntry != &LoaderBlock->MemoryDescriptorListHead);

    Pte.X1 = 0;
    Pte.PFN = StartingPfn;
    Pte.G = 0;
    Pte.V = 1;
    Pte.D = 1;

#if defined(R3000)

    Pte.N = 1;

#endif

#if defined(R4000)

    Pte.C = UNCACHED_POLICY;

#endif

    //
    // Save the page table page PTE for use in displaying information and
    // map the appropriate PTE in the current page directory page to address
    // the display controller page table page.
    //

    HalpDisplayPte = Pte;
    *((PENTRYLO)(PDE_BASE |
                    ((VIDEO_MEMORY_BASE >> (PDI_SHIFT - 2)) & 0xffc))) = Pte;

    //
    // Initialize the page table page.
    //

    PageFrame = (PENTRYLO)(PTE_BASE |
                    (VIDEO_MEMORY_BASE >> (PDI_SHIFT - PTI_SHIFT)));

    if (HalpDisplayControllerSetup == HalpDisplayINT10Setup) {

        HalpCirrusPhigicalVideo.HighPart = 1;
        HalpCirrusPhigicalVideo.LowPart = 0;

        Pte.PFN = (HalpCirrusPhigicalVideo.HighPart >> PAGE_SHIFT) &
                      (0x7fffffff >> PAGE_SHIFT-1) |
                      HalpCirrusPhigicalVideo.HighPart << (32 - PAGE_SHIFT);
    }else if (HalpDisplayControllerSetup == HalpDisplayCirrusSetup) {
        HalpCirrusPhigicalVideo.HighPart = 1;
        HalpCirrusPhigicalVideo.LowPart = MEM_VGA;
        Pte.PFN = (HalpCirrusPhigicalVideo.LowPart >> PAGE_SHIFT) &
                                                  (0x7fffffff >> PAGE_SHIFT-1) |
                    HalpCirrusPhigicalVideo.HighPart << (32 - PAGE_SHIFT);
    } else if (HalpDisplayControllerSetup == HalpDisplayTgaSetup) {
            HalpTgaPhigicalVideo.HighPart = 1;
            HalpTgaPhigicalVideo.LowPart = HalpDisplayControlBase - TGA_REG_SPC_OFFSET +  TGA_DSP_BUF_OFFSET;
            Pte.PFN = (HalpTgaPhigicalVideo.LowPart >> PAGE_SHIFT) &
                                                    (0x7fffffff >> PAGE_SHIFT-1) |
                        HalpTgaPhigicalVideo.HighPart << (32 - PAGE_SHIFT);

    } else if (HalpDisplayControllerSetup == HalpDisplayGlintSetup) { // M021
            HalpGlintPhygicalVideo.HighPart = 1;
            HalpGlintPhygicalVideo.LowPart = HalpDisplayMemoryBase;
            Pte.PFN = (HalpGlintPhygicalVideo.LowPart >> PAGE_SHIFT) &
                                                    (0x7fffffff >> PAGE_SHIFT-1) |
                        HalpGlintPhygicalVideo.HighPart << (32 - PAGE_SHIFT);
    }

    Pte.G = 0;
    Pte.V = 1;
    Pte.D = 1;

#if defined(R3000)

    Pte.N = 1;

#endif

#if defined(R4000)

    Pte.C = UNCACHED_POLICY;

#endif

    //
    // Page table entries of the video memory.
    //

    for (Index = 0; Index < ((PAGE_SIZE / sizeof(ENTRYLO)) - 1); Index += 1) {
        *PageFrame++ = Pte;
        Pte.PFN += 1;
    }

    if (HalpDisplayControllerSetup == HalpDisplayTgaSetup) {
        HalpTgaPhigicalVideoCont.HighPart = 1;
        HalpTgaPhigicalVideoCont.LowPart = HalpDisplayControlBase;
        Pte.PFN = (HalpTgaPhigicalVideoCont.LowPart >> PAGE_SHIFT) &
            (0x7fffffff >> PAGE_SHIFT-1) |
                HalpTgaPhigicalVideoCont.HighPart << (32 - PAGE_SHIFT);
        *PageFrame = Pte;

    } else if (HalpDisplayControllerSetup == HalpDisplayGlintSetup) {

        HalpGlintPhygicalVideoCont.HighPart = 1;
        HalpGlintPhygicalVideoCont.LowPart = HalpDisplayControlBase;

        //
        // IBM RGB525
        //

        Pte.PFN = ((HalpGlintPhygicalVideoCont.LowPart + 0x4000) >> PAGE_SHIFT) &
            (0x7fffffff >> PAGE_SHIFT-1) |
                HalpGlintPhygicalVideoCont.HighPart << (32 - PAGE_SHIFT);
        *(PageFrame - 2) = Pte;

        //
        // GLINT 300SX Internal Video Registers
        //

        Pte.PFN = ((HalpGlintPhygicalVideoCont.LowPart + 0x3000) >> PAGE_SHIFT) &
            (0x7fffffff >> PAGE_SHIFT-1) |
                HalpGlintPhygicalVideoCont.HighPart << (32 - PAGE_SHIFT);
        *(PageFrame - 1) = Pte;

        //
        // GLINT 300SX Control Status Registers
        //

        Pte.PFN = (HalpGlintPhygicalVideoCont.LowPart >> PAGE_SHIFT) &
            (0x7fffffff >> PAGE_SHIFT-1) |
                HalpGlintPhygicalVideoCont.HighPart << (32 - PAGE_SHIFT);
        *PageFrame = Pte;

    } else if (HalpDisplayControllerSetup == HalpDisplayINT10Setup){

#if defined(_X86_DBG_)
	DbgPrint("Map x86 and cirrus control register\n");
#endif // _X86_DBG_

        Pte.PFN = ((ULONG)HalpDisplayControlBase + 0xffff) >> PAGE_SHIFT;

	for (Index = 0; Index < (0x10000 / PAGE_SIZE ); Index++) {
	    *PageFrame--  = Pte;
#if defined(_X86_DBG_)
	    DbgPrint("Map index %x pfn %x\n",Index,Pte.PFN);
#endif // _X86_DBG_
	    Pte.PFN -= 1;
	}
	
    } else {

        //
        // If we have a 'NEC-cirrus GD5428'
        // use the page before last to map the reset register.
        //

        //
        // Page table for the video registers.
        //

        Pte.PFN = ((ULONG)HalpDisplayControlBase) >> PAGE_SHIFT;
        *PageFrame = Pte;
    }

    //
    // M004
    // ESM critical error logging setup.
    //

    HalpInitDisplayStringIntoNvram();

    //
    // Initialize the display controller.
    //

#if defined(_X86_DBG_)
        DbgPrint("\n x86adp init GOOO\n");  //DBGDBG
#endif // _X86_DBG

    if(HalpDisplayControllerSetup == HalpDisplayINT10Setup){
        if (HalpInitializeX86DisplayAdapter()) {
#if defined(_X86_DBG_)
            DbgPrint("\n x86adp init OK\n");  //DBGDBG
#endif // _X86_DBG
//            HalpDisplayControllerSetup(); // initialize twice bugbug

        }else{

            return FALSE;
        }
    }

    HalpDisplayControllerSetup();

    return TRUE;
}

BOOLEAN
HalpInitializeDisplay1 (
    IN PLOADER_PARAMETER_BLOCK LoaderBlock
    )

/*++

Routine Description:

    This routine allocates pool for the OEM font file and copies the font
    information from the OS Loader heap into the allocated pool.

Arguments:

    LoaderBlock - Supplies a pointer to the loader parameter block.

Return Value:

    If the initialization is successfully completed, than a value of TRUE
    is returned. Otherwise, a value of FALSE is returned.

--*/

{

    PVOID FontHeader;

    //
    // Allocate a pool block and copy the OEM font information from the
    // OS Loader heap into the pool block.
    //

    FontHeader = ExAllocatePool(NonPagedPool, HalpFontHeader->FileSize);
    if (FontHeader == NULL) {
        return FALSE;
    }

    RtlMoveMemory(FontHeader, HalpFontHeader, HalpFontHeader->FileSize);
    HalpFontHeader = (POEM_FONT_FILE_HEADER)FontHeader;
    return TRUE;
}

VOID
HalAcquireDisplayOwnership (
    IN PHAL_RESET_DISPLAY_PARAMETERS  ResetDisplayParameters
    )

/*++

Routine Description:

    This routine switches ownership of the display away from the HAL to
    the system display driver. It is called when the system has reached
    a point during bootstrap where it is self supporting and can output
    its own messages. Once ownership has passed to the system display
    driver any attempts to output messages using HalDisplayString must
    result in ownership of the display reverting to the HAL and the
    display hardware reinitialized for use by the HAL.

Arguments:

    ResetDisplayParameters - if non-NULL the address of a function
    the hal can call to reset the video card.  The function returns
    TRUE if the display was reset.

Return Value:

    None.

--*/

{

    //
    // Set HAL ownership of the display to false.
    //

    HalpResetDisplayParameters=ResetDisplayParameters;// M003
    HalpDisplayOwnedByHal = FALSE;

    //
    // M010
    // Set for Software Power supply control
    //

#if defined (_MRCPOWER_)
    HalpMrcModeChange((UCHAR)0x1);    // M007
#endif // _MRCPOWER_

    //
    // M006
    // ESM critical logging success set success startup.
    //

    HalpSuccessOsStartUp();

    return;
}

VOID
HalpDisplayCirrusSetup(
    VOID
    )
/*++

Routine Description:

    This routine initializes the Cirrus VGA display controlleer.

Arguments:

    None

Return Value:

    None

--*/

{
    PULONG              Buffer;
    LONG                Limit;
    LONG                Index;
    ULONG               dac_address, dac_reg;

    ULONG               verticalFrequency;
    ULONG               horizontalTotal;
    ULONG               verticalTotal;

    //
    // Calculate vertical frequency.
    //

    horizontalTotal = ( HalpMonitorConfigurationData.HorizontalDisplayTime
                       +HalpMonitorConfigurationData.HorizontalBackPorch
                       +HalpMonitorConfigurationData.HorizontalFrontPorch
                       +HalpMonitorConfigurationData.HorizontalSync );

    verticalTotal = ( HalpMonitorConfigurationData.VerticalResolution
                     +HalpMonitorConfigurationData.VerticalBackPorch
                     +HalpMonitorConfigurationData.VerticalFrontPorch
                     +HalpMonitorConfigurationData.VerticalSync );

    verticalFrequency = 1000000000 / ( horizontalTotal * verticalTotal);

    switch (HalpMonitorConfigurationData.HorizontalResolution) {
    case 640:
        if( verticalFrequency < 66 ) {
            HalpCirrusInterpretCmdStream(CL542x_640x480_256_60);
            HalpCirrusInterpretCmdStream(HalpCirrus_MODESET_1K_WIDE);
        } else {
            HalpCirrusInterpretCmdStream(CL542x_640x480_256_72);
            HalpCirrusInterpretCmdStream(HalpCirrus_MODESET_1K_WIDE);
        }
        break;

    case 800:
        if( verticalFrequency < 58 ) {
            HalpCirrusInterpretCmdStream(CL542x_800x600_256_56);
            HalpCirrusInterpretCmdStream(HalpCirrus_MODESET_1K_WIDE);
        } else if( verticalFrequency < 66 ) {
            HalpCirrusInterpretCmdStream(CL542x_800x600_256_60);
            HalpCirrusInterpretCmdStream(HalpCirrus_MODESET_1K_WIDE);
        } else {
            HalpCirrusInterpretCmdStream(CL542x_800x600_256_72);
            HalpCirrusInterpretCmdStream(HalpCirrus_MODESET_1K_WIDE);
        }
        break;

    case 1024:

        if( verticalFrequency < 47 ) {
            HalpCirrusInterpretCmdStream(CL542x_1024x768_256_87);
            HalpCirrusInterpretCmdStream(HalpCirrus_MODESET_1K_WIDE);
        } else if ( verticalFrequency < 65) {
            HalpCirrusInterpretCmdStream(CL542x_1024x768_256_60);
            HalpCirrusInterpretCmdStream(HalpCirrus_MODESET_1K_WIDE);
        } else {
            HalpCirrusInterpretCmdStream(CL542x_1024x768_256_70);
            HalpCirrusInterpretCmdStream(HalpCirrus_MODESET_1K_WIDE);
        }
        break;
    default:
        HalpCirrusInterpretCmdStream(CL542x_640x480_256_60);
        HalpCirrusInterpretCmdStream(HalpCirrus_MODESET_1K_WIDE);
        return;
    }

    //
    // Initialize color pallete.
    //

    dac_address = (ULONG)CIRRUS_BASE + 0x3b0 + DAC_ADDRESS_WRITE_PORT;
    dac_reg = (ULONG)CIRRUS_BASE + 0x3b0 + DAC_DATA_REG_PORT;

    Write_Dbg_Uchar((PUCHAR)dac_address, (UCHAR)0x0);

    Write_Dbg_Uchar((PUCHAR)dac_reg, (UCHAR)0x3f);
    Write_Dbg_Uchar((PUCHAR)dac_reg, (UCHAR)0x3f);
    Write_Dbg_Uchar((PUCHAR)dac_reg, (UCHAR)0x3f);

    Write_Dbg_Uchar((PUCHAR)dac_address, (UCHAR)0x1);
    Write_Dbg_Uchar((PUCHAR)dac_reg, (UCHAR)0x00);
    Write_Dbg_Uchar((PUCHAR)dac_reg, (UCHAR)0x00);
    Write_Dbg_Uchar((PUCHAR)dac_reg, (UCHAR)(0x90 >> 2));

    //
    // Set the video memory to address color one.
    //
    Buffer = (PULONG)VIDEO_MEMORY_BASE;
    Limit = (1024 *
            HalpMonitorConfigurationData.VerticalResolution) / sizeof(ULONG);

    for (Index = 0; Index < Limit; Index += 1) {
        *Buffer++ = 0x01010101;
    }

    //
    // Initialize the current display column, row, and ownership values.
    //

    HalpColumn = 0;
    HalpRow = 0;
    HalpDisplayOwnedByHal = TRUE;
    return;
}

BOOLEAN
HalpCirrusInterpretCmdStream(
    PUSHORT pusCmdStream
    )

/*++

Routine Description:

    Interprets the appropriate command array to set up VGA registers for the
    requested mode. Typically used to set the VGA into a particular mode by
    programming all of the registers

Arguments:


    pusCmdStream - array of commands to be interpreted.

Return Value:

    The status of the operation (can only fail on a bad command); TRUE for
    success, FALSE for failure.

Revision History:
--*/



{
    ULONG ulCmd;
    ULONG ulPort;
    UCHAR jValue;
    USHORT usValue;
    ULONG culCount;
    ULONG ulIndex;
    ULONG ulBase;
    if (pusCmdStream == NULL) {

        return TRUE;
    }

    ulBase = (ULONG)CIRRUS_BASE+0x3b0;

    //
    // Now set the adapter to the desired mode.
    //

    while ((ulCmd = *pusCmdStream++) != EOD) {

        //
        // Determine major command type
        //

        switch (ulCmd & 0xF0) {

            //
            // Basic input/output command
            //

        case INOUT:

            //
            // Determine type of inout instruction
            //

            if (!(ulCmd & IO)) {

                //
                // Out instruction. Single or multiple outs?
                //

                if (!(ulCmd & MULTI)) {

                    //
                    // Single out. Byte or word out?
                    //

                    if (!(ulCmd & BW)) {

                        //
                        // Single byte out
                        //

                        ulPort = *pusCmdStream++;
                        jValue = (UCHAR) *pusCmdStream++;


                        Write_Dbg_Uchar((PUCHAR)(ulBase+ulPort),
                                        jValue);

                    } else {

                        //
                        // Single word out
                        //

                        ulPort = *pusCmdStream++;
                        usValue = *pusCmdStream++;

                        Write_Dbg_Uchar((PUCHAR)(ulBase+ulPort), (UCHAR)(usValue & 0x00ff));

                        Write_Dbg_Uchar((PUCHAR)(ulBase+ulPort+1  ), (UCHAR)(usValue >> 8));


                    }

                } else {

                    //
                    // Output a string of values
                    // Byte or word outs?
                    //

                    if (!(ulCmd & BW)) {

                        //
                        // String byte outs. Do in a loop; can't use
                        // VideoPortWritePortBufferUchar because the data
                        // is in USHORT form
                        //

                        ulPort = ulBase + *pusCmdStream++;
                        culCount = *pusCmdStream++;

                        while (culCount--) {
                            jValue = (UCHAR) *pusCmdStream++;

                            Write_Dbg_Uchar((PUCHAR)ulPort,
                                            jValue);

                        }

                    } else {

                        //
                        // String word outs
                        //

                        ulPort = *pusCmdStream++;
                        culCount = *pusCmdStream++;
                        //
                        // Buffering out is not use on the Miniport Driver for R96 machine.
                        //


                        while(culCount--)
                    {
                        usValue = *pusCmdStream++;

                        Write_Dbg_Uchar((PUCHAR)
                                        (ulBase + ulPort), (UCHAR) (usValue & 0x00ff));
                        Write_Dbg_Uchar((PUCHAR)
                                        (ulBase + ulPort+1), (UCHAR) (usValue >> 8));

                    }

                    }
                }

            } else {

                // In instruction
                //
                // Currently, string in instructions aren't supported; all
                // in instructions are handled as single-byte ins
                //
                // Byte or word in?
                //

                if (!(ulCmd & BW)) {
                    //
                    // Single byte in
                    //

                    ulPort = *pusCmdStream++;
                    jValue = READ_REGISTER_UCHAR((PUCHAR)ulBase+ulPort);

                } else {

                    //
                    // Single word in
                    //

                    ulPort = *pusCmdStream++;
                    usValue = READ_REGISTER_USHORT((PUSHORT)
                                                   (ulBase+ulPort));
                }

            }

            break;

            //
            // Higher-level input/output commands
            //

        case METAOUT:

            //
            // Determine type of metaout command, based on minor
            // command field
            //
            switch (ulCmd & 0x0F) {

                //
                // Indexed outs
                //

            case INDXOUT:

                ulPort = ulBase + *pusCmdStream++;
                culCount = *pusCmdStream++;
                ulIndex = *pusCmdStream++;

                while (culCount--) {

                    usValue = (USHORT) (ulIndex +
                                        (((ULONG)(*pusCmdStream++)) << 8));

                    Write_Dbg_Uchar((PUCHAR)ulPort, (UCHAR) (usValue & 0x00ff));

                    Write_Dbg_Uchar((PUCHAR)ulPort+1, (UCHAR) (usValue >> 8));

                    ulIndex++;

                }

                break;

                //
                // Masked out (read, AND, XOR, write)
                //

            case MASKOUT:

                ulPort = *pusCmdStream++;
                jValue = READ_REGISTER_UCHAR((PUCHAR)ulBase+ulPort);
                jValue &= *pusCmdStream++;
                jValue ^= *pusCmdStream++;

                Write_Dbg_Uchar((PUCHAR)ulBase + ulPort,
                                jValue);

                break;

                //
                // Attribute Controller out
                //

            case ATCOUT:

                ulPort = ulBase + *pusCmdStream++;
                culCount = *pusCmdStream++;
                ulIndex = *pusCmdStream++;

                while (culCount--) {

                    // Write Attribute Controller index

                    Write_Dbg_Uchar((PUCHAR)ulPort,
                                    (UCHAR)ulIndex);

                    // Write Attribute Controller data
                    jValue = (UCHAR) *pusCmdStream++;

                    Write_Dbg_Uchar((PUCHAR)ulPort, jValue);

                    ulIndex++;

                }

                break;

                //
                // None of the above; error
                //
            default:

                return FALSE;

            }


            break;

            //
            // NOP
            //

        case NCMD:

            break;

            //
            // Unknown command; error
            //

        default:

            return FALSE;

        }

    }

    return TRUE;

} // end HalpCirrusInterpretCmdStream()


VOID
HalpDisplayTgaSetup(
    VOID
    )
/*++

  Routine Description:

    This routine initializes the Tga(DEC21030) Graphics accelerator.

Arguments:

    None

Return Value:

    None

--*/

{

    PUCHAR      PLLbits;
    ULONG       i, j;
    ULONG       PLLdata;
    ULONG       ColorData;
    PULONG      Buffer;
    LONG        Limit;
    LONG        Index;
    ULONG       VerticalFrequency;
    ULONG       horizontalTotal;
    ULONG       verticalTotal;

// M019+++
//
//      support for nec dec-ga(tga) board.
//      parameter change.
//
#if 0
    const UCHAR PLLbits640x480_72[7] = { 0x80, 0x08, 0x80, 0x24, 0xb0, 0x20, 0xc8 };
    const UCHAR PLLbits640x480_60[7] = { 0x80, 0x08, 0x80, 0x24, 0x88, 0x80, 0x78 };
    const UCHAR PLLbits800x600_72[7] = { 0x80, 0x00, 0x80, 0x24, 0x88, 0x80, 0x78 };
    const UCHAR PLLbits800x600_60[7] = { 0x80, 0x00, 0x80, 0x24, 0x70, 0xa0, 0x84 };
    const UCHAR PLLbits1024x768_60[7] = { 0x80, 0x00, 0x80, 0x24, 0x48, 0x20, 0x98 };
#else
    const UCHAR PLLbits640x480_72[7] = { 0x80, 0x04, 0x80, 0xa4, 0x51, 0x80, 0x70 };
    const UCHAR PLLbits640x480_60[7] = { 0x80, 0x04, 0x80, 0xa5, 0xc4, 0x10, 0x78 };
    const UCHAR PLLbits800x600_72[7] = { 0x80, 0x08, 0x80, 0x24, 0xf1, 0x60, 0x38 }; // S007
    const UCHAR PLLbits800x600_60[7] = { 0x80, 0x04, 0x80, 0xa5, 0x78, 0x20, 0x08 };
    const UCHAR PLLbits1024x768_60[7] = { 0x80, 0x00, 0x80, 0x24, 0x48, 0x20, 0x98 };
#endif // _NECDEC_
// M019 ---

    const UCHAR Vga_Ini_ColorTable[48] =
//      { VGA_INI_PALETTE_WHITE_R, VGA_INI_PALETTE_WHITE_G, VGA_INI_PALETTE_WHITE_B,
        { VGA_INI_PALETTE_HI_WHITE_R, VGA_INI_PALETTE_HI_WHITE_G, VGA_INI_PALETTE_HI_WHITE_B, // M010
          VGA_INI_PALETTE_BLUE_R, VGA_INI_PALETTE_BLUE_G, VGA_INI_PALETTE_BLUE_B,
          VGA_INI_PALETTE_GREEN_R, VGA_INI_PALETTE_GREEN_B, VGA_INI_PALETTE_GREEN_G,
          VGA_INI_PALETTE_YELLOW_R, VGA_INI_PALETTE_YELLOW_G, VGA_INI_PALETTE_YELLOW_B,
          VGA_INI_PALETTE_RED_R, VGA_INI_PALETTE_RED_G, VGA_INI_PALETTE_RED_B,
          VGA_INI_PALETTE_MAGENTA_R, VGA_INI_PALETTE_MAGENTA_G, VGA_INI_PALETTE_MAGENTA_B,
          VGA_INI_PALETTE_CYAN_R, VGA_INI_PALETTE_CYAN_G, VGA_INI_PALETTE_CYAN_B,
          VGA_INI_PALETTE_BLACK_R, VGA_INI_PALETTE_BLACK_G, VGA_INI_PALETTE_BLACK_B,
//        VGA_INI_PALETTE_HI_WHITE_R, VGA_INI_PALETTE_HI_WHITE_G, VGA_INI_PALETTE_HI_WHITE_B,
          VGA_INI_PALETTE_WHITE_R, VGA_INI_PALETTE_WHITE_G, VGA_INI_PALETTE_WHITE_B, // M010
          VGA_INI_PALETTE_HI_BLUE_R, VGA_INI_PALETTE_HI_BLUE_G, VGA_INI_PALETTE_HI_BLUE_B,
          VGA_INI_PALETTE_HI_GREEN_R, VGA_INI_PALETTE_HI_GREEN_G, VGA_INI_PALETTE_HI_GREEN_B,
          VGA_INI_PALETTE_HI_YELLOW_R, VGA_INI_PALETTE_HI_YELLOW_G, VGA_INI_PALETTE_HI_YELLOW_B,
          VGA_INI_PALETTE_HI_RED_R, VGA_INI_PALETTE_HI_RED_G, VGA_INI_PALETTE_HI_RED_B,
          VGA_INI_PALETTE_HI_MAGENTA_R, VGA_INI_PALETTE_HI_MAGENTA_G, VGA_INI_PALETTE_HI_MAGENTA_B,
          VGA_INI_PALETTE_HI_CYAN_R, VGA_INI_PALETTE_HI_CYAN_G, VGA_INI_PALETTE_HI_CYAN_B,
          VGA_INI_PALETTE_HI_BLACK_R, VGA_INI_PALETTE_HI_BLACK_G, VGA_INI_PALETTE_HI_BLACK_B
        };

    //
    // Calculate vertical frequency.
    //

    horizontalTotal = ( HalpMonitorConfigurationData.HorizontalDisplayTime
                       +HalpMonitorConfigurationData.HorizontalBackPorch
                       +HalpMonitorConfigurationData.HorizontalFrontPorch
                       +HalpMonitorConfigurationData.HorizontalSync );

    verticalTotal = ( HalpMonitorConfigurationData.VerticalResolution
                     +HalpMonitorConfigurationData.VerticalBackPorch
                     +HalpMonitorConfigurationData.VerticalFrontPorch
                     +HalpMonitorConfigurationData.VerticalSync );

    VerticalFrequency = 1000000000 / ( horizontalTotal * verticalTotal);

    //
    // Write the PLL
    //

    // Select PLL Data
    if( HalpMonitorConfigurationData.HorizontalResolution == 640
            && HalpMonitorConfigurationData.VerticalResolution == 480 ){
        if( VerticalFrequency > 66 ){
            PLLbits = (PVOID)PLLbits640x480_72;  // S012
        } else {
            PLLbits = (PVOID)PLLbits640x480_60;  // S012
        }
    } else if( HalpMonitorConfigurationData.HorizontalResolution == 800
                   && HalpMonitorConfigurationData.VerticalResolution == 600 ){
        if( VerticalFrequency > 66 ){
            PLLbits = (PVOID)PLLbits800x600_72;  // S012
        } else {
            PLLbits = (PVOID)PLLbits800x600_60;  // S012
        }
    } else if( HalpMonitorConfigurationData.HorizontalResolution == 1024
                   && HalpMonitorConfigurationData.VerticalResolution == 768 ){
        PLLbits = (PVOID)PLLbits1024x768_60;  // S012
    } else {
        PLLbits = (PVOID)PLLbits640x480_60;  // S012
    }

    // Set PLL Data
    for( i = 0; i <= 6; i++ ){
        for( j = 0;  j <= 7;  j++ ){
            PLLdata = (PLLbits[i] >> (7-j)) & 1;
            if( i == 6 && j == 7 )
                PLLdata |= 2;   // Set ~HOLD bit on last write
            WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + CLOCK), PLLdata);
        }
    }

    // Verify 21030 is idle ( check busy bit on Command Status Register )
    while( (READ_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + COMMAND_STATUS) ) & 1) == 1 ){
    }

    // Set to Deep Register
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + DEEP), 0x00014000 );

    // Verify 21030 is idle ( check busy bit on Command Status Register )
    while( (READ_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + COMMAND_STATUS) ) & 1) == 1 ){
    }

    // Set to Video Base Address Register
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + VIDEO_BASE), 0x00000000 );

    // Set to Plane Mask Register
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + PLANE_MASK), 0xffffffff );

    // Set to Pixel Mask Register
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + ONE_SHOT_PIXEL_MASK), 0xffffffff );

    // Set to Raster Operation
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RASTER_OP), 0x03 );

    // Set to Mode Register
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + MODE), 0x0000200d );

    // Set to Block Color Register 0
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + BLK_COLOR_R0), 0x12345678 );

    // Set to Block Color Register 1
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + BLK_COLOR_R1), 0x12345678 );

    //
    // Init. video timing registers for each resolution
    //

    if( HalpMonitorConfigurationData.HorizontalResolution == 640
            && HalpMonitorConfigurationData.VerticalResolution == 480 ){
        if( VerticalFrequency > 66 ){
            WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + H_CONT), 0x03c294a0 ); // M023
            WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + V_CONT), 0x070349e0 );
        } else {
            WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + H_CONT), 0x00e64ca0 ); // M023
            WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + V_CONT), 0x064211e0 );
        }
    } else if( HalpMonitorConfigurationData.HorizontalResolution == 800
                   && HalpMonitorConfigurationData.VerticalResolution == 600 ){
        if( VerticalFrequency > 66 ){
            WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + H_CONT), 0x01a7a4c8 ); // M023
            WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + V_CONT), 0x05c6fa58 );
        } else {
            WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + H_CONT), 0x02681cc8 ); // M023
            WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + V_CONT), 0x05c40a58 );
        }
    } else if( HalpMonitorConfigurationData.HorizontalResolution == 1024
                   && HalpMonitorConfigurationData.VerticalResolution == 768 ){
            WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + H_CONT), 0x04889300 ); // M023
            WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + V_CONT), 0x07461b00 );
    } else {
        WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + H_CONT), 0x00e64ca0 ); // M023
        WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + V_CONT), 0x064211e0 );
    }

    // Set to Raster Operation Register
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RASTER_OP), 0x03 );

    // Set to Mode Register
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + MODE), 0x00002000 );

    // M019 +++

    //
    // wait for 10 msec for nec dec-ga support
    //

    KeStallExecutionProcessor(10000L);

    // M019 ---

    // Set to Palette and DAC Setup & Data Register
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_SETUP), 0x0c );
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_DATA), 0x0ca2 );
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_SETUP), 0x10 );
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_DATA), 0x1040 );
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_SETUP), 0x12 );
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_DATA), 0x1220 );
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_SETUP), 0x00 );
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_DATA), 0x01 );
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_SETUP), 0x14 );
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_DATA), 0x1410 );

    //
    // set pass thru on off & on again to verify operation
    //

    // EEPROM Write Register
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + EEPROM_WRITE), 0x00000001 );

    //
    // Fill palette
    //

    // Set to Palette and DAC Setup & Data Register
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_SETUP), 0x00 );
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_DATA), 0x00 );
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_SETUP), 0x02 );

    for( i = 0; i < 48; i++ ){
        ColorData = Vga_Ini_ColorTable[i];
        ColorData |= 0x200;
        WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_DATA), ColorData );
    }

    for( i = 48; i < 768; i++ ){
        WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + RAMDAC_DATA), 0x200 );
    }

    // Set to Video Valid Register
    WRITE_REGISTER_ULONG( (PULONG)(TGA_REGISTER_BASE + VIDEO_VALID), 0x01 );

    //
    // Set Video Memory to address color one.
    //

    Buffer = (PULONG)VIDEO_MEMORY_BASE;
    Limit = (HalpMonitorConfigurationData.HorizontalResolution *
            HalpMonitorConfigurationData.VerticalResolution) / sizeof(ULONG);

    for (Index = 0; Index < Limit; Index += 1) {
        *Buffer++ = 0x01010101;
    }

    //
    // Initialize the current display column, row, and ownership values.
    //

    HalpColumn = 0;
    HalpRow = 0;
    HalpDisplayOwnedByHal = TRUE;
    return;

}

VOID
HalDisplayString (
    PUCHAR String
    )

/*++

Routine Description:

    This routine displays a character string on the display screen.


Arguments:

    String - Supplies a pointer to the characters that are to be displayed.

Return Value:

    None.

--*/

{

    KIRQL OldIrql;
    ENTRYLO SavedPte;
    /* Start D001 */
    ULONG       NowDisplayControlBase;
    ULONG       NowDisplayMemoryBase; // M021
    PENTRYLO    PageFrame;
    ENTRYLO     Pte;
    LONG        Index;
    /* End D001 */
    PCI_SLOT_NUMBER Slot; // M014
    ULONG ReadValue; // M014

    //
    // Raise IRQL to the highest level, acquire the display adapter spin lock,
    // flush the TB, and map the display frame buffer into the address space
    // of the current process.
    //

    KeRaiseIrql(HIGH_LEVEL, &OldIrql);

#if defined(_DUO_)

    KiAcquireSpinLock(&HalpDisplayAdapterLock);

#endif

    SavedPte = *((PENTRYLO)(PDE_BASE |
                            ((VIDEO_MEMORY_BASE >> (PDI_SHIFT - 2)) & 0xffc)));

    KeFlushCurrentTb();
    *((PENTRYLO)(PDE_BASE |
            ((VIDEO_MEMORY_BASE >> (PDI_SHIFT - 2)) & 0xffc))) = HalpDisplayPte;

    if (HalpDisplayPCIDevice == TRUE){

        //
        // the display type is PCI
        // check physical address and reinitialize PTE
        // we assume that the device has no function
        //

        Slot.u.bits.FunctionNumber = 0;

        for (Slot.u.bits.DeviceNumber = 3; Slot.u.bits.DeviceNumber < 6; Slot.u.bits.DeviceNumber++){
            HalpReadPCIConfigUlongByOffset(Slot,&ReadValue,0);

            if (HalpDisplayControllerSetup == HalpDisplayTgaSetup && ReadValue == 0x00041011){

                //
                // DEC21030
                //

                HalpReadPCIConfigUlongByOffset(Slot,&ReadValue, 0x10);
                NowDisplayControlBase = (ReadValue & 0xfffffff0)+ TGA_REG_SPC_OFFSET;
                NowDisplayMemoryBase = 0;

            } else if (HalpDisplayControllerSetup == HalpDisplayGlintSetup && ReadValue == 0x00013d3d){

                //
                // GLINT 300SX
                //

                HalpReadPCIConfigUlongByOffset(Slot,&ReadValue, 0x10);
                NowDisplayControlBase = (ReadValue & 0xfffffff0);
                HalpReadPCIConfigUlongByOffset(Slot,&ReadValue, 0x18);
                NowDisplayMemoryBase = (ReadValue & 0xfffffff0);

            }
        }

        //
        // check to see if address has been changed
        //

        if (HalpDisplayControlBase != NowDisplayControlBase ||
            HalpDisplayMemoryBase != NowDisplayMemoryBase){

            // Called by OS, so reinitialize PTE
            HalpDisplayControlBase = NowDisplayControlBase;
            HalpDisplayMemoryBase = NowDisplayMemoryBase;

            Pte.G = 0;
            Pte.V = 1;
            Pte.D = 1;

#if defined(R3000)
            Pte.N = 1;
#endif
#if defined(R4000)
            Pte.C = UNCACHED_POLICY;
#endif

            //
            // Initialize the page table page.
            //

            PageFrame = (PENTRYLO)(PTE_BASE | (VIDEO_MEMORY_BASE >> (PDI_SHIFT - PTI_SHIFT)));

            if (HalpDisplayControllerSetup == HalpDisplayTgaSetup){
                HalpTgaPhigicalVideoCont.HighPart = 1;
                HalpTgaPhigicalVideoCont.LowPart = HalpDisplayControlBase - TGA_REG_SPC_OFFSET +  TGA_DSP_BUF_OFFSET;
                Pte.PFN = (HalpTgaPhigicalVideoCont.LowPart >> PAGE_SHIFT) &
                      (0x7fffffff >> PAGE_SHIFT-1) |
                           HalpTgaPhigicalVideoCont.HighPart << (32 - PAGE_SHIFT);

            } else if (HalpDisplayControllerSetup == HalpDisplayGlintSetup) { // M021

                HalpGlintPhygicalVideo.HighPart = 1;
                HalpGlintPhygicalVideo.LowPart = HalpDisplayMemoryBase;
                Pte.PFN = (HalpGlintPhygicalVideo.LowPart >> PAGE_SHIFT) &
                    (0x7fffffff >> PAGE_SHIFT-1) |
                        HalpGlintPhygicalVideo.HighPart << (32 - PAGE_SHIFT);
            }

            //
            // Page table entries of the video memory.
            //

            for (Index = 0; Index < ((PAGE_SIZE / sizeof(ENTRYLO)) - 1); Index += 1) {
                *PageFrame++ = Pte;
                Pte.PFN += 1;
            }

            if (HalpDisplayControllerSetup == HalpDisplayTgaSetup){
                HalpTgaPhigicalVideoCont.HighPart = 1;
                HalpTgaPhigicalVideoCont.LowPart = HalpDisplayControlBase;
                Pte.PFN = (HalpTgaPhigicalVideoCont.LowPart >> PAGE_SHIFT) &
                    (0x7fffffff >> PAGE_SHIFT-1) |
                        HalpTgaPhigicalVideoCont.HighPart << (32 - PAGE_SHIFT);
                *PageFrame = Pte;

            } else if (HalpDisplayControllerSetup == HalpDisplayGlintSetup) { // M021
                HalpGlintPhygicalVideoCont.HighPart = 1;
                HalpGlintPhygicalVideoCont.LowPart = HalpDisplayControlBase;

                //
                // IBM RGB525
                //

                Pte.PFN = ((HalpGlintPhygicalVideoCont.LowPart + 0x4000) >> PAGE_SHIFT) &
                    (0x7fffffff >> PAGE_SHIFT-1) |
                        HalpGlintPhygicalVideoCont.HighPart << (32 - PAGE_SHIFT);
                *(PageFrame - 2) = Pte;

                //
                // GLINT 300SX Internal Video Registers
                //

                Pte.PFN = ((HalpGlintPhygicalVideoCont.LowPart + 0x3000) >> PAGE_SHIFT) &
                    (0x7fffffff >> PAGE_SHIFT-1) |
                        HalpGlintPhygicalVideoCont.HighPart << (32 - PAGE_SHIFT);
                *(PageFrame - 1) = Pte;

                //
                // GLINT 300SX Control Status Registers
                //

                Pte.PFN = (HalpGlintPhygicalVideoCont.LowPart >> PAGE_SHIFT) &
                    (0x7fffffff >> PAGE_SHIFT-1) |
                        HalpGlintPhygicalVideoCont.HighPart << (32 - PAGE_SHIFT);
                *PageFrame = Pte;
            }
        }
    }

    //
    // If ownership of the display has been switched to the system display
    // driver, then reinitialize the display controller and revert ownership
    // to the HAL.
    //

    if (HalpDisplayOwnedByHal == FALSE) {
// M003 +++
	if (HalpResetDisplayParameters &&
	    HalpDisplayControllerSetup == HalpDisplayINT10Setup) {
        //
        // Video work-around.  The video driver has a reset function,
        // call it before resetting the system in case the bios doesn't
        // know how to reset the displays video mode.
        //

	    if (HalpResetDisplayParameters(80, 50)) {
	    }
// M003---
    }

    //
    // M010
    // for Software controlled power suply.
    //

#if defined(_MRCPOWER_)
	HalpMrcModeChange((UCHAR)0);        // M007
#endif // _MRCPOWER_

        HalpDisplayControllerSetup();
//        HalpResetX86DisplayAdapter();

	//
	// M004
	// re-initialize critical message
	//

	HalpInitDisplayStringIntoNvram();

    }

    //
    // M004
    // ESM critical error logging start (set colomn,row)
    //

    HalStringIntoBufferStart( HalpColumn, HalpRow );

    //
    // Display characters until a null byte is encountered.
    //

    while (*String != 0) {
        HalpDisplayCharacter(*String++);
    }

    // M004
    // ESM critical error logging(strings)
    //

    HalpStringBufferCopyToNvram();

    //
    // Restore the previous mapping for the current process, flush the TB,
    // release the display adapter spin lock, and lower IRQL to its previous
    // level.
    //

    KeFlushCurrentTb();
    *((PENTRYLO)(PDE_BASE | ((VIDEO_MEMORY_BASE >> (PDI_SHIFT - 2)) & 0xffc))) = SavedPte;

#if defined(_DUO_)

    KiReleaseSpinLock(&HalpDisplayAdapterLock);

#endif

    KeLowerIrql(OldIrql);
    return;
}

VOID
HalpDisplayCharacterOld (
    IN UCHAR Character
    )

/*++

Routine Description:

    This routine displays a character at the current x and y positions in
    the frame buffer. If a newline is encounter, then the frame buffer is
    scrolled. If characters extend below the end of line, then they are not
    displayed.

Arguments:

    Character - Supplies a character to be displayed.

Return Value:

    None.

--*/

{

    PUCHAR Destination;
    PUSHORT DestinationShort; // M021
    ULONG Index;

    //
    // If the character is a newline, then scroll the screen up, blank the
    // bottom line, and reset the x position.
    //

    if (Character == '\n') {
        HalpColumn = 0;
        if (HalpRow < (HalpDisplayText - 1)) {
            HalpRow += 1;

        } else {
            HalpMoveMemory32((PUCHAR)VIDEO_MEMORY_BASE,
                             (PUCHAR)(VIDEO_MEMORY_BASE + HalpScrollLine),
                             HalpScrollLength);

//              /* START M002 */
//              RtlMoveMemory((PUCHAR)VIDEO_MEMORY_BASE,   // S013
//                        (PUCHAR)(VIDEO_MEMORY_BASE + HalpScrollLine),
//                        HalpScrollLength);
//              /* END M002 */

            if (HalpDisplayType16BPP) {
                DestinationShort = (PUSHORT)(VIDEO_MEMORY_BASE + HalpScrollLength);

            } else {
                Destination = (PUCHAR)VIDEO_MEMORY_BASE + HalpScrollLength;
            }

            if (HalpDisplayType16BPP) {
                for (Index = 0; Index < HalpScrollLine/2; Index += 1) {
                    *DestinationShort++ = (USHORT)0x000f;
                }

            } else {
                for (Index = 0; Index < HalpScrollLine; Index += 1) {
                    *Destination++ = 1;
                }
            }

        }

	//
	// M006
	// ESM critical logging re-print(column, row)
	//

	HalpStringBufferCopyToNvram();
	HalStringIntoBufferStart( HalpColumn, HalpRow );
	
    } else if (Character == '\r') {
        HalpColumn = 0;

	//
	// M006
	// ESM critical logging re-print(column,row)
	//

	HalpStringBufferCopyToNvram();
	HalStringIntoBufferStart( HalpColumn, HalpRow );

    } else {

	//
	// M006
	// ESM critical logging put character.
	//

	HalStringIntoBuffer( Character );

        if ((Character < HalpFontHeader->FirstCharacter) ||
            (Character > HalpFontHeader->LastCharacter)) {
            Character = HalpFontHeader->DefaultCharacter;
        }

        Character -= HalpFontHeader->FirstCharacter;
        HalpOutputCharacterOld((PUCHAR)HalpFontHeader + HalpFontHeader->Map[Character].Offset);
    }

    return;
}

//351
VOID
HalpDisplayCharacterVGA (
    IN UCHAR Character
    )

/*++
Routine Description:

    This routine displays a character at the current x and y positions in
    the frame buffer. If a newline is encounter, then the frame buffer is
    scrolled. If characters extend below the end of line, then they are not
    displayed.

Arguments:

    Character - Supplies a character to be displayed.

Return Value:

    None.

--*/
{

    //
    // If the character is a newline, then scroll the screen up, blank the
    // bottom line, and reset the x position.
    //

    if (Character == '\n') {
      HalpColumn = 0;
      if (HalpRow < (HalpDisplayText - 1)) {
        HalpRow += 1;
      } else { // need to scroll up the screen
          HalpScrollINT10(1);
      }

      //
      // M006
      // ESM critical logging re-print(column, row)
      //

      HalpStringBufferCopyToNvram();
      HalStringIntoBufferStart( HalpColumn, HalpRow );

    }

    //=========================================================================
    //
    //  IBMBJB  added tab processing
    //

    else if( Character == '\t' )    // tab?
        {
        HalpColumn += TAB_SIZE;
        HalpColumn = (HalpColumn / TAB_SIZE) * TAB_SIZE;

        if( HalpColumn >= 80 )      // tab beyond end of screen?
            {
            HalpColumn = 0;         // next tab stop is 1st column of next line

            if( HalpRow >= (HalpDisplayText - 1) )
                HalpScrollINT10( 1 );     // scroll the screen up
            else
                ++HalpRow;
            }
        }

    //=========================================================================

    else if (Character == '\r') {

	//
	// M006
	// ESM critical logging re-print(column,row)
	//

	HalpStringBufferCopyToNvram();
	HalStringIntoBufferStart( HalpColumn, HalpRow );

        HalpColumn = 0;

    } else if (Character == 0x7f) { /* DEL character */
	if (HalpColumn != 0) {
	    HalpColumn -= 1;
	    HalpOutputCharacterINT10(0);
	    HalpColumn -= 1;
	} else /* do nothing */
	    ;
    } else if (Character >= 0x20) {
	//
	// M006
	// ESM critical logging put character.
	//

	HalStringIntoBuffer( Character );

	// Auto wrap for 80 columns per line
	if (HalpColumn >= 80) {
	    HalpColumn = 0;
	    if (HalpRow < (HalpDisplayText - 1)) {
                HalpRow += 1;
	    } else { // need to scroll up the screen
		HalpScrollINT10(1);
	    }
	}
	HalpOutputCharacterINT10(Character);
    } else /* skip the nonprintable character */
	;

    return;

} /* end of HalpDisplayCharacterVGA() */

VOID
HalQueryDisplayParameters (
    OUT PULONG WidthInCharacters,
    OUT PULONG HeightInLines,
    OUT PULONG CursorColumn,
    OUT PULONG CursorRow
    )

/*++

Routine Description:

    This routine return information about the display area and current
    cursor position.

Arguments:

    WidthInCharacter - Supplies a pointer to a varible that receives
        the width of the display area in characters.

    HeightInLines - Supplies a pointer to a variable that receives the
        height of the display area in lines.

    CursorColumn - Supplies a pointer to a variable that receives the
        current display column position.

    CursorRow - Supplies a pointer to a variable that receives the
        current display row position.

Return Value:

    None.

--*/

{

    //
    // Set the display parameter values and return.
    //

    *WidthInCharacters = HalpDisplayWidth;
    *HeightInLines = HalpDisplayText;
    *CursorColumn = HalpColumn;
    *CursorRow = HalpRow;
    return;
}

VOID
HalSetDisplayParameters (
    IN ULONG CursorColumn,
    IN ULONG CursorRow
    )

/*++

Routine Description:

    This routine set the current cursor position on the display area.

Arguments:

    CursorColumn - Supplies the new display column position.

    CursorRow - Supplies a the new display row position.

Return Value:

    None.

--*/

{

    //
    // Set the display parameter values and return.
    //

    if (CursorColumn > HalpDisplayWidth) {
        CursorColumn = HalpDisplayWidth;
    }

    if (CursorRow > HalpDisplayText) {
        CursorRow = HalpDisplayText;
    }

    HalpColumn = CursorColumn;
    HalpRow = CursorRow;
    return;
}

VOID
HalpOutputCharacterOld(
    IN PUCHAR Glyph
    )

/*++

Routine Description:

    This routine insert a set of pixels into the display at the current x
    cursor position. If the current x cursor position is at the end of the
    line, then a newline is displayed before the specified character.

Arguments:

    Character - Supplies a character to be displayed.

Return Value:

    None.

--*/

{

    PUCHAR Destination;
    PUSHORT DestinationShort; // M021
    ULONG FontValue;
    ULONG I;
    ULONG J;

    //
    // If the current x cursor position is at the end of the line, then
    // output a line feed before displaying the character.
    //

    if (HalpColumn == HalpDisplayWidth) {
        HalpDisplayCharacter('\n');
    }

    //
    // Output the specified character and update the x cursor position.
    //

    if (HalpDisplayType16BPP) {
        DestinationShort = (PUSHORT)(VIDEO_MEMORY_BASE +
                (HalpRow * HalpScrollLine) + (HalpColumn * HalpCharacterWidth * sizeof(USHORT)));

    } else {
        Destination = (PUCHAR)(VIDEO_MEMORY_BASE +
                (HalpRow * HalpScrollLine) + (HalpColumn * HalpCharacterWidth));
    }

    for (I = 0; I < HalpCharacterHeight; I += 1) {
        FontValue = 0;
        for (J = 0; J < HalpBytesPerRow; J += 1) {
            FontValue |= *(Glyph + (J * HalpCharacterHeight)) << (24 - (J * 8));
        }

        Glyph += 1;
        for (J = 0; J < HalpCharacterWidth ; J += 1) {

            if (HalpDisplayType16BPP) {
                if (FontValue >> 31)
                    *DestinationShort++ = (USHORT)0xffff;
                else
                    *DestinationShort++ = (USHORT)0x000f;

            }else{
                *Destination++ = (UCHAR) (FontValue >> 31) ^ 1; /* M008 */
            }

            FontValue <<= 1;
        }

        if(HalpDisplayControllerSetup == HalpDisplayCirrusSetup){
            Destination +=
                (1024 - HalpCharacterWidth);
        }
        else if (HalpDisplayType16BPP){
            DestinationShort +=
                (HalpMonitorConfigurationData.HorizontalResolution - HalpCharacterWidth);
        }
        else {
            Destination +=
                (HalpMonitorConfigurationData.HorizontalResolution - HalpCharacterWidth);
        }

    }
    HalpColumn += 1;
    return;
}

VOID
Write_Dbg_Uchar(
    PUCHAR Ioadress,
    UCHAR Moji
    )
{
#if defined (R96DBG)
        DbgPrint("Disply I/O Adress %x Char %x \n",Ioadress,Moji);
#endif
        WRITE_PORT_UCHAR(Ioadress,Moji);
}

VOID
HalpMoveMemory32 (
   PUCHAR Destination,
   PUCHAR Source,
   ULONG Length
   )

/*++
 Routine Description:

    This function moves blocks of memory.

 Arguments:

    Destination  - Supplies a pointer to the destination address of
       the move operation.

    Source  - Supplies a pointer to the source address of the move
       operation.

    Length  - Supplies the length, in bytes, of the memory to be moved.

 Return Value:

    None.

--*/

{
    UCHAR Remainder;
    PUCHAR Dstend;
    PUCHAR Srcend;

    if ( (Source == Destination) || (Length == 0) ) {
        return;
    }

    if ((Source < Destination)&((Source + Length) > Destination)) {
      if((Destination - Source)  >  4){
        Remainder = (UCHAR) Length &0x03;
        Length = Length / 4;
        Dstend = Destination + Length - 4 ;
        Srcend = Source + Length -4;

        for (; Length > 0; Length--) {
            *(PULONG)(Dstend) = *(PULONG)(Srcend);
            Dstend -= 4;
            Srcend -= 4;
        }
        for (; Remainder > 0; Remainder--) {
            *Dstend = *Srcend;
            Dstend--;
            Srcend--;
        }
        return;
       }
        for (; Length > 0; Length--) {
            *Dstend = *Srcend;
            Dstend--;
            Srcend--;
        }
        return;
    }

    else {
     if( (Source - Destination) > 4  ){
        Remainder = (UCHAR) Length &0x03;
        Length = Length / 4;
        for (; Length > 0; Length--) {
            *(PULONG)(Destination) = *(PULONG)(Source);
            Destination += 4;
            Source += 4;
        }
        for (; Remainder > 0; Remainder--) {
            *Destination = *Source;
            Destination++;
            Source++;
        }
        return;
      }

        for (; Length > 0; Length--) {
            *Destination = *Source;
            Destination++;
            Source++;
        }
    }
}

VOID
HalpOutputCharacterINT10 (
    IN UCHAR Character
    )
{
    ULONG Eax,Ebx,Ecx,Edx,Esi,Edi,Ebp;

    Eax = 2 << 8;               // AH = 2
    Ebx = 0;            // BH = page number
    Edx = (HalpRow << 8) + HalpColumn;
    HalCallBios(0x10, &Eax,&Ebx,&Ecx,&Edx,&Esi,&Edi,&Ebp);

    Eax = (0x0A << 8) + Character;  // AH = 0xA    AL = character
    Ebx = 0;
    Ecx = 1;
    HalCallBios(0x10, &Eax,&Ebx,&Ecx,&Edx,&Esi,&Edi,&Ebp);

    HalpColumn += 1;
}

VOID
HalpScrollINT10 (
    IN UCHAR LinesToScroll
    )
{
    ULONG Eax,Ebx,Ecx,Edx,Esi,Edi,Ebp;

    Eax = 6 << 8;               // AH = 6 (scroll up)
    Eax |= LinesToScroll; // AL = lines to scroll
    Ebx = TEXT_ATTR << 8;       // BH = attribute to fill blank line(s)
    Ecx = 0;            // CH,CL = upper left
    Edx = ((HalpDisplayText - 1) << 8)
          + (HalpDisplayWidth - 1);       // DH,DL = lower right // M001,S005
    HalCallBios(0x10, &Eax,&Ebx,&Ecx,&Edx,&Esi,&Edi,&Ebp);
}

VOID
HalpDisplayINT10Setup (
    VOID
    )
{
    ULONG Eax,Ebx,Ecx,Edx,Esi,Edi,Ebp;

    HalpColumn = 0;
    HalpRow = 0;
    HalpDisplayWidth = 80;
    HalpDisplayText = 50; // M001
    HalpScrollLine = 160;
    HalpScrollLength = HalpScrollLine * (HalpDisplayText - 1);

    HalpDisplayOwnedByHal = TRUE;

    HalpResetX86DisplayAdapter();  // for compaq q-vision reset

// M001 +++
    //
    // Load 8x8 font   80x50 on VGA
    //
    Eax = 0x1112;			// AH = 11 AL=12
    Ebx = 0;
    HalCallBios(0x10, &Eax,&Ebx,&Ecx,&Edx,&Esi,&Edi,&Ebp);
// M001 ---

    //
    // Set cursor to (0,0)
    //
    Eax = 0x02 << 8;                    // AH = 2
    Ebx = 0;                            // BH = page Number
    Edx = 0;                            // DH = row   DL = column
    HalCallBios(0x10, &Eax,&Ebx,&Ecx,&Edx,&Esi,&Edi,&Ebp);

    //
    // Make screen white on blue by scrolling entire screen
    //
    Eax = 0x06 << 8;                    // AH = 6   AL = 0
    Ebx = TEXT_ATTR << 8;               // BH = attribute
    Ecx = 0;                            // (x,y) upper left
    Edx = ((HalpDisplayText-1) << 8);   // (x,y) lower right
    Edx += HalpScrollLine/2;
    HalCallBios(0x10, &Eax,&Ebx,&Ecx,&Edx,&Esi,&Edi,&Ebp);

}

// start M014
VOID
HalpReadPCIConfigUlongByOffset (
    IN PCI_SLOT_NUMBER Slot,
    IN PULONG Buffer,
    IN ULONG Offset
   )

/*++
 Routine Description:

    This function returns PCI configuration data.

 Arguments:

    Slot - Supplies a PCI slot number and function number.
    Buffer - Supplies a pointer to a configuration data is returned.
    Offset - Offset in the PCI configuration header.

 Return Value:

    None.

--*/

{
    R94A_PCI_CONFIGURATION_ADDRESS_REG ConfigAddressValue;
    USHORT StatusValue;
    KIRQL OldIrql;

    //
    // check to see if specified slot is valid
    //

    if (!HalpCheckPCIDevice(Slot)) {

        //
        // Invalid SlotID return no data
        //

        *Buffer = 0xffffffff;
        return ;

    }

    *((PULONG) &ConfigAddressValue) = 0x0;

    ConfigAddressValue.ConfigEnable = 1;
    ConfigAddressValue.DeviceNumber = Slot.u.bits.DeviceNumber;
    ConfigAddressValue.FunctionNumber = Slot.u.bits.FunctionNumber;
    ConfigAddressValue.RegisterNumber = Offset >> 2;

    //
    // Synchronize with PCI configuration
    //

//  KeRaiseIrql(PROFILE_LEVEL, &OldIrql); // M017
//  KiAcquireSpinLock(&HalpPCIConfigLock);

    WRITE_REGISTER_ULONG(R94A_PCI_ADDR_REG, *((PULONG) &ConfigAddressValue));

    *Buffer = READ_REGISTER_ULONG(R94A_PCI_DATA_REG);

    //
    // Release spinlock
    //

//  KiReleaseSpinLock(&HalpPCIConfigLock);
//  KeLowerIrql(OldIrql);

    return;
}



BOOLEAN
HalpCheckPCIDevice (
    IN PCI_SLOT_NUMBER Slot
   )

/*++
 Routine Description:

    This function checks if spcified PCI slot is valid.

 Arguments:

    Slot - Supplies a PCI slot number and function number.

 Return Value:

    TRUE - specified slot is valid
    FALSE - specified slot is invalid

--*/

{
    R94A_PCI_CONFIGURATION_ADDRESS_REG ConfigAddressValue;
    BOOLEAN ReturnValue;
    ULONG OrigData;
    ULONG IdValue;
    KIRQL OldIrql;

    //
    // Disable PCI-MasterAbort interrupt during configration read.
    //

    OrigData = READ_REGISTER_ULONG(&((PDMA_REGISTERS)DMA_VIRTUAL_BASE)->PCIInterruptEnable);
    WRITE_REGISTER_ULONG(&((PDMA_REGISTERS)DMA_VIRTUAL_BASE)->PCIInterruptEnable, OrigData & 0xffffff7f);

    //
    // read VendorID and DeviceID of the specified slot
    //

    *((PULONG) &ConfigAddressValue) = 0x0;

    ConfigAddressValue.ConfigEnable = 1;
    ConfigAddressValue.DeviceNumber = Slot.u.bits.DeviceNumber;
    ConfigAddressValue.FunctionNumber = Slot.u.bits.FunctionNumber;

    //
    // Synchronize with PCI configuration
    //

//  KeRaiseIrql(PROFILE_LEVEL, &OldIrql); // M017
//  KiAcquireSpinLock(&HalpPCIConfigLock);

    WRITE_REGISTER_ULONG(R94A_PCI_ADDR_REG, *((PULONG) &ConfigAddressValue));
    IdValue = READ_REGISTER_ULONG(R94A_PCI_DATA_REG);

    //
    // Release spinlock
    //

//  KiReleaseSpinLock(&HalpPCIConfigLock);
//  KeLowerIrql(OldIrql);

    if ((USHORT)(IdValue & 0xffff) == 0xffff){

        //
        // waiting until ReceivedMasterAbort bit is set
        //

        while(!(READ_REGISTER_USHORT(&((PDMA_REGISTERS)DMA_VIRTUAL_BASE)->PCIStatus) & 0x2000))
            ;

        //
        // clear the ReceivedMasterAbort bit
        //

        WRITE_REGISTER_USHORT(&((PDMA_REGISTERS)DMA_VIRTUAL_BASE)->PCIStatus, 0x2000)

        //
        // M018
        // Clear memory address error registers.
        //

        {
            LARGE_INTEGER registerLarge;
            READ_REGISTER_DWORD((PVOID)&((PDMA_REGISTERS)DMA_VIRTUAL_BASE)->InvalidAddress, &registerLarge);
        }

        ReturnValue = FALSE;

    } else {

        ReturnValue = TRUE;

    }

    //
    // Restore the PCIInterruptEnable register.
    //

    WRITE_REGISTER_ULONG(&((PDMA_REGISTERS)DMA_VIRTUAL_BASE)->PCIInterruptEnable, OrigData);

    return ReturnValue;

}
// end M014

#if defined(_R94A_)
VOID
HalpDisplayGlintSetup(
    VOID
    )
/*++

Routine Description:

    This routine initializes the GLINT 300SX Graphics accelerator.

Arguments:

    None

Return Value:

    None

--*/

{
        ULONG       VerticalFrequency;
        ULONG       horizontalTotal;
        ULONG       verticalTotal;

        __VIDEO __Video;
        VIDEO Video = &__Video;
        long ClockDivider;

        // for initialize ramdac
        ULONG Dac = RGB525_REGISTER_BASE;
        UCHAR ByteVal;

        // for initialize timing
        ULONG Glint = GLINT_VIDEO_REGISTER_BASE - 0x3000;

        // for initialize control
        ULONG SerialClk;
        ULONG Temp;
        ULONG Data;
        ULONG Mask;

        // for initialize RampLut
        ULONG Index;

        // for clear the screen
        PULONG DestinationUlong;
        ULONG Length;

 	/* check revision id R01 or R02. assume 40MHz(R01) or 50MHz(R02) reference clock for now */

        if ( HalpGlintRevisionId == 0){
			Video->RefDivCount = RGB525_PLL_REFCLK_40_MHz;
				} else {
			Video->RefDivCount = RGB525_PLL_REFCLK_50_MHz;          // S006
				}

//
// Calculate vertical frequency.
//

#if defined(_GLINT60HZ_)

        VerticalFrequency = 60;

#else

        horizontalTotal = ( HalpMonitorConfigurationData.HorizontalDisplayTime
                       +HalpMonitorConfigurationData.HorizontalBackPorch
                       +HalpMonitorConfigurationData.HorizontalFrontPorch
                       +HalpMonitorConfigurationData.HorizontalSync );

        verticalTotal = ( HalpMonitorConfigurationData.VerticalResolution
                     +HalpMonitorConfigurationData.VerticalBackPorch
                     +HalpMonitorConfigurationData.VerticalFrontPorch
                     +HalpMonitorConfigurationData.VerticalSync );

        VerticalFrequency = 1000000000 / ( horizontalTotal * verticalTotal);

#endif // _GLINT60HZ_

        //
        // Initialize video data
        //

        /* get timing values for named resolution */

        if ( HalpMonitorConfigurationData.HorizontalResolution == 1280
                && HalpMonitorConfigurationData.VerticalResolution == 1024
                && VerticalFrequency == 75)
        {
                Video->ImageWidth       = 1280;
                Video->ImageHeight      = 1024;
                Video->HLimit           = 8 * 211;
                Video->HSyncStart       = 8 *   2;
                Video->HSyncEnd         = 8 *  20;
                Video->HBlankEnd        = 8 *  51;
                Video->HSyncPolarity    = GLINT_HSYNC_ACTIVE_LOW;
                Video->VLimit           = 1066;
                Video->VSyncStart       = 2;
                Video->VSyncEnd         = 5;
                Video->VBlankEnd        = 42;
                Video->VSyncPolarity    = GLINT_HSYNC_ACTIVE_LOW;
                /* 134 MHz */
                Video->PixelClock = RGB525_DF(3) | RGB525_VCO_DIV_COUNT(2);
        }
        else if ( HalpMonitorConfigurationData.HorizontalResolution == 1024
                && HalpMonitorConfigurationData.VerticalResolution == 768
                && VerticalFrequency == 75)
        {
                Video->ImageWidth       = 1024;
                Video->ImageHeight      = 768;
                Video->HLimit           = 8 * 164;
                Video->HSyncStart       = 8 *   2;
                Video->HSyncEnd         = 8 *  14;
                Video->HBlankEnd        = 8 *  36;
                Video->HSyncPolarity    = GLINT_HSYNC_ACTIVE_HIGH;
                Video->VLimit           = 800;
                Video->VSyncStart       = 2;
                Video->VSyncEnd         = 5;
                Video->VBlankEnd        = 32;
                Video->VSyncPolarity    = GLINT_HSYNC_ACTIVE_HIGH;
                /* 79 MHz */
                Video->PixelClock = RGB525_DF(2) | RGB525_VCO_DIV_COUNT(14);
        }
        else if ( HalpMonitorConfigurationData.HorizontalResolution == 1024
                && HalpMonitorConfigurationData.VerticalResolution == 768
                && VerticalFrequency == 60)
        {
                Video->ImageWidth       = 1024;
                Video->ImageHeight      = 768;
                Video->HLimit           = 8 * 168;
                Video->HSyncStart       = 8 *   3;
                Video->HSyncEnd         = 8 *  20;
                Video->HBlankEnd        = 8 *  40;
                Video->HSyncPolarity    = GLINT_HSYNC_ACTIVE_LOW;
                Video->VLimit           = 806;
                Video->VSyncStart       = 4;
                Video->VSyncEnd         = 10;
                Video->VBlankEnd        = 38;
                Video->VSyncPolarity    = GLINT_HSYNC_ACTIVE_LOW;
                /* 65 MHz */
                Video->PixelClock = RGB525_DF(2) | RGB525_VCO_DIV_COUNT(0);
        }
        else if ( HalpMonitorConfigurationData.HorizontalResolution == 800
                && HalpMonitorConfigurationData.VerticalResolution == 600
                && VerticalFrequency == 75)
        {
                Video->ImageWidth       = 800;
                Video->ImageHeight      = 600;
                Video->HLimit           = 8 * 132;
                Video->HSyncStart       = 8 *   2;
                Video->HSyncEnd         = 8 *  12;
                Video->HBlankEnd        = 8 *  32;
                Video->HSyncPolarity    = GLINT_HSYNC_ACTIVE_HIGH;
                Video->VLimit           = 625;
                Video->VSyncStart       = 2;
                Video->VSyncEnd         = 5;
                Video->VBlankEnd        = 25;
                Video->VSyncPolarity    = GLINT_VSYNC_ACTIVE_HIGH;
                /* 49.5 MHz */
                Video->PixelClock = RGB525_DF(1) | RGB525_VCO_DIV_COUNT(34);
        }
        else if ( HalpMonitorConfigurationData.HorizontalResolution == 800
                && HalpMonitorConfigurationData.VerticalResolution == 600
                && VerticalFrequency == 60)
        {
                Video->ImageWidth       = 800;
                Video->ImageHeight      = 600;
                Video->HLimit           = 8 * 132;
                Video->HSyncStart       = 8 *   5;
                Video->HSyncEnd         = 8 *  21;
                Video->HBlankEnd        = 8 *  32;
                Video->HSyncPolarity    = GLINT_HSYNC_ACTIVE_HIGH;
                Video->VLimit           = 628;
                Video->VSyncStart       = 2;
                Video->VSyncEnd         = 6;
                Video->VBlankEnd        = 28;
                Video->VSyncPolarity    = GLINT_VSYNC_ACTIVE_HIGH;
                /* 40 MHz */
                Video->PixelClock = RGB525_DF(1) | RGB525_VCO_DIV_COUNT(15);
        }
        else if ( HalpMonitorConfigurationData.HorizontalResolution == 640 // add 4/5/1995
                && HalpMonitorConfigurationData.VerticalResolution == 480
                && VerticalFrequency == 60)
        {
                Video->ImageWidth       = 640;
                Video->ImageHeight      = 480;
                Video->HLimit           = 8 * 100;
                Video->HSyncStart       = 8 *   2;
                Video->HSyncEnd         = 8 *  14;
                Video->HBlankEnd        = 8 *  20;
                Video->HSyncPolarity    = GLINT_HSYNC_ACTIVE_LOW;
                Video->VLimit           = 525;
                Video->VSyncStart       = 12;
                Video->VSyncEnd         = 13;
                Video->VBlankEnd        = 45;
                Video->VSyncPolarity    = GLINT_VSYNC_ACTIVE_LOW;
                /* 31.5 MHz */
                Video->PixelClock = RGB525_DF(0) | RGB525_VCO_DIV_COUNT(36);
        }
        else if ( HalpMonitorConfigurationData.HorizontalResolution == 640
                && HalpMonitorConfigurationData.VerticalResolution == 480
                && VerticalFrequency == 75)
        {
                Video->ImageWidth       = 640;
                Video->ImageHeight      = 480;
                Video->HLimit           = 8 * 105;
                Video->HSyncStart       = 8 *   2;
                Video->HSyncEnd         = 8 *  10;
                Video->HBlankEnd        = 8 *  25;
                Video->HSyncPolarity    = GLINT_HSYNC_ACTIVE_LOW;
                Video->VLimit           = 500;
                Video->VSyncStart       = 2;
                Video->VSyncEnd         = 5;
                Video->VBlankEnd        = 20;
                Video->VSyncPolarity    = GLINT_VSYNC_ACTIVE_LOW;
                /* 31.5 MHz */
                Video->PixelClock = RGB525_DF(0) | RGB525_VCO_DIV_COUNT(61);
        }
        else if ( HalpMonitorConfigurationData.HorizontalResolution == 1280
                && HalpMonitorConfigurationData.VerticalResolution == 1024
                && VerticalFrequency == 57)
        {
                Video->ImageWidth       = 1280;
                Video->ImageHeight      = 1024;
                Video->HLimit           = 8 * 211;
                Video->HSyncStart       = 8 *   2;
                Video->HSyncEnd         = 8 *  20;
                Video->HBlankEnd        = 8 *  51;
                Video->HSyncPolarity    = GLINT_HSYNC_ACTIVE_LOW;
                Video->VLimit           = 1066;
                Video->VSyncStart       = 2;
                Video->VSyncEnd         = 5;
                Video->VBlankEnd        = 42;
                Video->VSyncPolarity    = GLINT_HSYNC_ACTIVE_LOW;
                /* 103 MHz */
                Video->PixelClock = RGB525_DF(2) | RGB525_VCO_DIV_COUNT(38);
        }
        else {
                //
                // force to set the resolution. 1024x768(60MHz)
                //

                Video->ImageWidth       = 1024;
                Video->ImageHeight      = 768;
                Video->HLimit           = 8 * 168;
                Video->HSyncStart       = 8 *   3;
                Video->HSyncEnd         = 8 *  20;
                Video->HBlankEnd        = 8 *  40;
                Video->HSyncPolarity    = GLINT_HSYNC_ACTIVE_LOW;
                Video->VLimit           = 806;
                Video->VSyncStart       = 4;
                Video->VSyncEnd         = 10;
                Video->VBlankEnd        = 38;
                Video->VSyncPolarity    = GLINT_HSYNC_ACTIVE_LOW;
                /* 65 MHz */
                Video->PixelClock = RGB525_DF(2) | RGB525_VCO_DIV_COUNT(0);
#if DBG
                DbgBreakPoint();
#endif
        }

        /* record image depth */

        Video->ImageDepth = 16;

        /* determine video clock divider and pixel format */

        ClockDivider = 4;
        Video->PixelFormat = RGB525_PIXEL_FORMAT_16_BPP;

        /* adjust horizontal timings */

        Video->HLimit     /= ClockDivider;
        Video->HSyncStart /= ClockDivider;
        Video->HSyncEnd   /= ClockDivider;
        Video->HBlankEnd  /= ClockDivider;

        //
        // Initialize ramdac data
        //

        RGB525_WRITE(Dac, __RGB525_PixelMask, 0xff);

        /* set MiscControlOne register */
        ByteVal = RGB525_MISR_CNTL_OFF
                | RGB525_VMSK_CNTL_OFF
                | RGB525_PADR_RFMT_READ_ADDR
                | RGB525_SENS_DSAB_DISABLE
                | RGB525_VRAM_SIZE_64;
        RGB525_SET_REG(Dac, __RGB525_MiscControlOne, ByteVal);

        /* set MiscControlTwo register */
        ByteVal = RGB525_PCLK_SEL_PLL
                | RGB525_INTL_MODE_DISABLE
                | RGB525_BLANK_CNTL_NORMAL
                | RGB525_COL_RES_8_BIT
                | RGB525_PORT_SEL_VRAM;
        RGB525_SET_REG(Dac, __RGB525_MiscControlTwo, ByteVal);

        /* set MiscControlThree register */
        ByteVal = RGB525_SWAP_RB_DISABLE
                | RGB525_SWAP_WORD_31_00_FIRST
                | RGB525_SWAP_NIB_07_04_FIRST;
        RGB525_SET_REG(Dac, __RGB525_MiscControlThree, ByteVal);

        /* set MiscClockControl register */
        ByteVal = RGB525_DDOTCLK_DISABLE
                | RGB525_SCLK_ENABLE
                | RGB525_PLL_ENABLE;
        RGB525_SET_REG(Dac, __RGB525_MiscClockControl, ByteVal);

        /* set SyncControl register */
        ByteVal = RGB525_DLY_CNTL_ADD
                | RGB525_VSYN_INVT_DISABLE
                | RGB525_HSYN_INVT_DISABLE
                | RGB525_VSYN_CNTL_NORMAL
                | RGB525_HSYN_CNTL_NORMAL;
        RGB525_SET_REG(Dac, __RGB525_SyncControl, ByteVal);

        /* set HSyncControl register */
        RGB525_SET_REG(Dac, __RGB525_HSyncControl, RGB525_HSYN_POS(0));

        /* set PowerManagement register */
        ByteVal = RGB525_SCLK_PWR_NORMAL
                | RGB525_DDOT_PWR_DISABLE
                | RGB525_SYNC_PWR_NORMAL
                | RGB525_ICLK_PWR_NORMAL
                | RGB525_DAC_PWR_NORMAL;
        RGB525_SET_REG(Dac, __RGB525_PowerManagement, ByteVal);

        /* set DACOperation register */
        ByteVal = RGB525_SOG_DISABLE
                | RGB525_BRB_NORMAL
                | RGB525_DSR_FAST
                | RGB525_DPE_ENABLE;    /* disable? */
        RGB525_SET_REG(Dac, __RGB525_DACOperation, ByteVal);

        /* set PaletteControl register */
        ByteVal = RGB525_6BIT_LINEAR_ENABLE
                | RGB525_PALETTE_PARTITION(0);
        RGB525_SET_REG(Dac, __RGB525_PaletteControl, ByteVal);

        /* set PixelFormat register */
        RGB525_SET_REG(Dac, __RGB525_PixelFormat, Video->PixelFormat);

        /* set 8BitPixelControl register */
        RGB525_SET_REG(Dac, __RGB525_8BitPixelControl,
                                RGB525_B8_DCOL_INDIRECT);

        /* set 16BitPixelControl register */
        ByteVal = RGB525_B16_DCOL_INDIRECT
                | RGB525_B16_565
                | RGB525_B16_ZIB
                | RGB525_B16_SPARSE;

        RGB525_SET_REG(Dac, __RGB525_16BitPixelControl, ByteVal);

        /* set 32BitPixelControl register */
        RGB525_SET_REG(Dac, __RGB525_32BitPixelControl,
                                RGB525_B32_DCOL_INDIRECT);

        /* set PLLControlOne register */
        ByteVal = RGB525_REF_SRC_REFCLK
                | RGB525_PLL_INT_FS_DIRECT;
        RGB525_SET_REG(Dac, __RGB525_PLLControlOne, ByteVal);

        /* set PLLControlTwo register */
        RGB525_SET_REG(Dac, __RGB525_PLLControlTwo, RGB525_PLL_INT_FS(0));

        /* set PLLRefDivCount register */
        RGB525_SET_REG(Dac, __RGB525_PLLRefDivCount, Video->RefDivCount);

        /* set F0 register */
        RGB525_SET_REG(Dac, __RGB525_F0, Video->PixelClock);

        /* set CursorControl register */
        RGB525_SET_REG(Dac, __RGB525_CursorControl, RGB525_CURSOR_MODE_OFF);

        //
        // Initialize timing
        //

        /* horizontal video timing values */

        GLINT_WRITE(Glint, __GLINT_VTGHLimit, Video->HLimit);
        GLINT_WRITE(Glint, __GLINT_VTGHSyncStart, Video->HSyncStart);
        GLINT_WRITE(Glint, __GLINT_VTGHSyncEnd, Video->HSyncEnd);
        GLINT_WRITE(Glint, __GLINT_VTGHBlankEnd, Video->HBlankEnd);

        /* vertical video timing values */

        GLINT_WRITE(Glint, __GLINT_VTGVLimit, Video->VLimit);
        GLINT_WRITE(Glint, __GLINT_VTGVSyncStart, Video->VSyncStart);
        GLINT_WRITE(Glint, __GLINT_VTGVSyncEnd, Video->VSyncEnd);
        GLINT_WRITE(Glint, __GLINT_VTGVBlankEnd, Video->VBlankEnd);

        /* horizontal clock gate values */

        GLINT_WRITE(Glint, __GLINT_VTGHGateStart, Video->HBlankEnd - 2);
        GLINT_WRITE(Glint, __GLINT_VTGHGateEnd, Video->HLimit - 2);

        /* vertical clock gate values */

        GLINT_WRITE(Glint, __GLINT_VTGVGateStart, Video->VBlankEnd - 1)
        GLINT_WRITE(Glint, __GLINT_VTGVGateEnd, Video->VBlankEnd);

        //
        // Initialize control
        //

        /* serial clock control */

        SerialClk = GLINT_EXTERNAL_QSF
                        | GLINT_SPLIT_SIZE_128_WORD
                        | GLINT_SCLK_VCLK_DIV_2;

        GLINT_WRITE(Glint, __GLINT_VTGSerialClk, SerialClk);

        /* set sync polarities and unblank screen */

        //      UpdatePolarityRegister(Glint,
        //                      GLINT_CBLANK_ACTIVE_LOW
        //                              | Video->HSyncPolarity
        //                              | Video->VSyncPolarity,
        //                      GLINT_CBLANK_POLARITY_MASK
        //                              | GLINT_HSYNC_POLARITY_MASK
        //                              | GLINT_VSYNC_POLARITY_MASK);

        Data = GLINT_CBLANK_ACTIVE_LOW
                | Video->HSyncPolarity
                | Video->VSyncPolarity;
        Mask = GLINT_CBLANK_POLARITY_MASK
                | GLINT_HSYNC_POLARITY_MASK
                | GLINT_VSYNC_POLARITY_MASK;

        /* read video polarity control register */

        GLINT_READ(Glint, __GLINT_VTGPolarity, Temp);

        /* replace existing polarity field */

        Temp = (Temp & ~Mask) | (Data & Mask);

        /* write result back to polarity register */

        GLINT_WRITE(Glint, __GLINT_VTGPolarity, Temp);

        /* set FrameRowAddr */

        GLINT_WRITE(Glint, __GLINT_VTGFrameRowAddr, 0);

        //
        // Initialize RampLut
        //

        /* initialise palette address */

        RGB525_WRITE(Dac, __RGB525_PalAddrWrite, 0);

        /* ramp colour components using auto-increment */

        for (Index = 0; Index <= 0xff; Index++)
        {
                RGB525_WRITE(Dac, __RGB525_PaletteData, Index);
                RGB525_WRITE(Dac, __RGB525_PaletteData, Index);
                RGB525_WRITE(Dac, __RGB525_PaletteData, Index);
        }

        //
        // Clear the screen.
        //

        DestinationUlong = (PULONG)VIDEO_MEMORY_BASE;

        Length = (HalpMonitorConfigurationData.VerticalResolution *
                  HalpMonitorConfigurationData.HorizontalResolution -1) * sizeof(USHORT);

        for (Index = 0; Index < (Length / sizeof(ULONG)); Index++)
            *(DestinationUlong++) = (ULONG)0x000f000f;

        //
        // Initialize the current display column, row, and ownership values.
        //

        HalpColumn = 0;
        HalpRow = 0;
        HalpDisplayOwnedByHal = TRUE;
        return;

}
#endif // _R94A_

//
// M010
// for software controlled power supply.
//
#if defined(_MRCPOWER_)
/* Start M007 */
VOID
HalpMrcModeChange(
    UCHAR Mode
)
/*++

Routine Description:

    This routine is change Mode bit on MRC Controller.

Arguments:

    Mode - Parameter for setting Mode bit on MRC

Return Value:

    None

--*/
{

  PHYSICAL_ADDRESS physicalAddress;
  UCHAR ModeNow;
  KIRQL OldIrql;
  ENTRYLO Pte[2];

  //
  // MRC Controller Mapping, when first call
  //

  if (HalpMrcControlMapped == FALSE) {
      physicalAddress.HighPart = 0;
      physicalAddress.LowPart = MRC_TEMP_PHYSICAL_BASE;
      HalpMrcControlBase = MmMapIoSpace(physicalAddress,
					PAGE_SIZE,
					FALSE);
      if (HalpMrcControlBase != NULL) {
	  HalpMrcControlMapped = TRUE;
      }
  }

  if (HalpMrcControlMapped == TRUE){

    ModeNow = READ_REGISTER_UCHAR(
				  &((PMRC_REGISTERS)HalpMrcControlBase)->Mode
				  );

    //
    // Set MRC Mode bit
    //
    WRITE_REGISTER_UCHAR(
			 &((PMRC_REGISTERS)HalpMrcControlBase)->Mode,
			 ((ModeNow & 0x02) | (Mode << 7)),
			 );

  } else {

    KeRaiseIrql(HIGH_LEVEL, &OldIrql);

    Pte[0].PFN = MRC_TEMP_PHYSICAL_BASE >> PAGE_SHIFT;

    Pte[0].G = 1;
    Pte[0].V = 1;
    Pte[0].D = 1;

    //
    // set second page to global and not valid.
    //

    Pte[0].C = UNCACHED_POLICY;
    Pte[1].G = 1;
    Pte[1].V = 0;

    //
    // Map MRC using virtual address of DMA controller.
    //

    KeFillFixedEntryTb((PHARDWARE_PTE)&Pte[0],
		       (PVOID)DMA_VIRTUAL_BASE,
		       DMA_ENTRY);

    ModeNow = READ_REGISTER_UCHAR(
				  &MRC_CONTROL->Mode	// B028
				  );

    //
    // Set MRC Mode bit
    //
    WRITE_REGISTER_UCHAR(
			 &MRC_CONTROL->Mode,
			 ((ModeNow & 0x02) | (Mode << 7)),
			 );


    // Start M008
    //
    // Resume fixed TLB for DMA
    //

    Pte[0].PFN = DMA_PHYSICAL_BASE >> PAGE_SHIFT;
    Pte[0].G = 1;
    Pte[0].V = 1;
    Pte[0].D = 1;

    Pte[0].C = UNCACHED_POLICY;


    Pte[1].PFN = INTERRUPT_PHYSICAL_BASE >> PAGE_SHIFT;
    Pte[1].G = 1;
    Pte[1].V = 1;
    Pte[1].D = 1;

    Pte[1].C = UNCACHED_POLICY;

    // End M008

    KeFillFixedEntryTb((PHARDWARE_PTE)&Pte[0],
		       (PVOID)DMA_VIRTUAL_BASE,
		       DMA_ENTRY);

    KeLowerIrql(OldIrql);
  }
}
/* End M007 */
#endif // _MRCPOWER_

VOID
HalpWritePCIConfigUlongByOffset (
    IN PCI_SLOT_NUMBER Slot,
    IN PULONG Buffer,
    IN ULONG Offset
   )

/*++
 Routine Description:

    This function writes PCI configuration data.

 Arguments:

    Slot - Supplies a PCI slot number and function number.
    Buffer - Supplies a pointer to a configuration data to write.
    Offset - Offset in the PCI configuration header.

 Return Value:

    None.

--*/

{
    R94A_PCI_CONFIGURATION_ADDRESS_REG ConfigAddressValue;
    USHORT StatusValue;
    KIRQL OldIrql;

    //
    // check to see if specified slot is valid
    //

    if (!HalpCheckPCIDevice(Slot)) {

        //
        // Invalid SlotID return no data
        //

        *Buffer = 0xffffffff;
        return ;

    }

    *((PULONG) &ConfigAddressValue) = 0x0;

    ConfigAddressValue.ConfigEnable = 1;
    ConfigAddressValue.DeviceNumber = Slot.u.bits.DeviceNumber;
    ConfigAddressValue.FunctionNumber = Slot.u.bits.FunctionNumber;
    ConfigAddressValue.RegisterNumber = Offset >> 2;

    //
    // Synchronize with PCI configuration
    //

//  KeRaiseIrql(PROFILE_LEVEL, &OldIrql); // M017
//  KiAcquireSpinLock(&HalpPCIConfigLock);

    WRITE_REGISTER_ULONG(R94A_PCI_ADDR_REG, *((PULONG) &ConfigAddressValue));

//    *Buffer = READ_REGISTER_ULONG(R94A_PCI_DATA_REG);
    WRITE_REGISTER_ULONG(R94A_PCI_DATA_REG, *Buffer);

    //
    // Release spinlock
    //

//  KiReleaseSpinLock(&HalpPCIConfigLock);
//  KeLowerIrql(OldIrql);

    return;
}
VOID
HalpReadPCIConfigUshortByOffset (
    IN PCI_SLOT_NUMBER Slot,
    IN PUSHORT Buffer,
    IN ULONG Offset
   )

/*++
 Routine Description:

    This function returns PCI configuration data.

 Arguments:

    Slot - Supplies a PCI slot number and function number.
    Buffer - Supplies a pointer to a configuration data is returned.
    Offset - Offset in the PCI configuration header.

 Return Value:

    None.

--*/

{
    R94A_PCI_CONFIGURATION_ADDRESS_REG ConfigAddressValue;
    USHORT StatusValue;
    KIRQL OldIrql;

    //
    // check to see if specified slot is valid
    //

    if (!HalpCheckPCIDevice(Slot)) {

        //
        // Invalid SlotID return no data
        //

        *Buffer = 0xffff;
        return ;

    }

    *((PULONG) &ConfigAddressValue) = 0x0;

    ConfigAddressValue.ConfigEnable = 1;
    ConfigAddressValue.DeviceNumber = Slot.u.bits.DeviceNumber;
    ConfigAddressValue.FunctionNumber = Slot.u.bits.FunctionNumber;
    ConfigAddressValue.RegisterNumber = Offset >> 2;

    //
    // Synchronize with PCI configuration
    //

//  KeRaiseIrql(PROFILE_LEVEL, &OldIrql); // M017
//  KiAcquireSpinLock(&HalpPCIConfigLock);

    WRITE_REGISTER_ULONG(R94A_PCI_ADDR_REG, *((PULONG) &ConfigAddressValue));

    *Buffer = READ_REGISTER_USHORT((PUCHAR)R94A_PCI_DATA_REG + (Offset % sizeof(ULONG)));

    //
    // Release spinlock
    //

//  KiReleaseSpinLock(&HalpPCIConfigLock);
//  KeLowerIrql(OldIrql);

    return;
}

VOID
HalpWritePCIConfigUshortByOffset (
    IN PCI_SLOT_NUMBER Slot,
    IN PUSHORT Buffer,
    IN ULONG Offset
   )

/*++
 Routine Description:

    This function returns PCI configuration data.

 Arguments:

    Slot - Supplies a PCI slot number and function number.
    Buffer - Supplies a pointer to a configuration data is returned.
    Offset - Offset in the PCI configuration header.

 Return Value:

    None.

--*/

{
    R94A_PCI_CONFIGURATION_ADDRESS_REG ConfigAddressValue;
    USHORT StatusValue;
    KIRQL OldIrql;

    //
    // check to see if specified slot is valid
    //

    if (!HalpCheckPCIDevice(Slot)) {

        //
        // Invalid SlotID return no data
        //

        *Buffer = 0xffff;
        return ;

    }

    *((PULONG) &ConfigAddressValue) = 0x0;

    ConfigAddressValue.ConfigEnable = 1;
    ConfigAddressValue.DeviceNumber = Slot.u.bits.DeviceNumber;
    ConfigAddressValue.FunctionNumber = Slot.u.bits.FunctionNumber;
    ConfigAddressValue.RegisterNumber = Offset >> 2;

    //
    // Synchronize with PCI configuration
    //

//  KeRaiseIrql(PROFILE_LEVEL, &OldIrql); // M017
//  KiAcquireSpinLock(&HalpPCIConfigLock);

    WRITE_REGISTER_ULONG(R94A_PCI_ADDR_REG, *((PULONG) &ConfigAddressValue));

//    *Buffer = READ_REGISTER_ULONG(R94A_PCI_DATA_REG);
    WRITE_REGISTER_USHORT((PUCHAR)R94A_PCI_DATA_REG + (Offset % sizeof(ULONG)),*Buffer);

    //
    // Release spinlock
    //

//  KiReleaseSpinLock(&HalpPCIConfigLock);
//  KeLowerIrql(OldIrql);

    return;
}

VOID
HalpReadPCIConfigUcharByOffset (
    IN PCI_SLOT_NUMBER Slot,
    IN PUCHAR Buffer,
    IN ULONG Offset
   )

/*++
 Routine Description:

    This function returns PCI configuration data.

 Arguments:

    Slot - Supplies a PCI slot number and function number.
    Buffer - Supplies a pointer to a configuration data is returned.
    Offset - Offset in the PCI configuration header.

 Return Value:

    None.

--*/

{
    R94A_PCI_CONFIGURATION_ADDRESS_REG ConfigAddressValue;
    USHORT StatusValue;
    KIRQL OldIrql;

    //
    // check to see if specified slot is valid
    //

    if (!HalpCheckPCIDevice(Slot)) {

        //
        // Invalid SlotID return no data
        //

        *Buffer = 0xff;
        return ;

    }

    *((PULONG) &ConfigAddressValue) = 0x0;

    ConfigAddressValue.ConfigEnable = 1;
    ConfigAddressValue.DeviceNumber = Slot.u.bits.DeviceNumber;
    ConfigAddressValue.FunctionNumber = Slot.u.bits.FunctionNumber;
    ConfigAddressValue.RegisterNumber = Offset >> 2;

    //
    // Synchronize with PCI configuration
    //

//  KeRaiseIrql(PROFILE_LEVEL, &OldIrql); // M017
//  KiAcquireSpinLock(&HalpPCIConfigLock);

    WRITE_REGISTER_ULONG(R94A_PCI_ADDR_REG, *((PULONG) &ConfigAddressValue));

    *Buffer = READ_REGISTER_UCHAR((PUCHAR)R94A_PCI_DATA_REG + (Offset % sizeof(ULONG)));

    //
    // Release spinlock
    //

//  KiReleaseSpinLock(&HalpPCIConfigLock);
//  KeLowerIrql(OldIrql);

    return;
}

VOID
HalpWritePCIConfigUcharByOffset (
    IN PCI_SLOT_NUMBER Slot,
    IN PUCHAR Buffer,
    IN ULONG Offset
   )

/*++
 Routine Description:

    This function returns PCI configuration data.

 Arguments:

    Slot - Supplies a PCI slot number and function number.
    Buffer - Supplies a pointer to a configuration data is returned.
    Offset - Offset in the PCI configuration header.

 Return Value:

    None.

--*/

{
    R94A_PCI_CONFIGURATION_ADDRESS_REG ConfigAddressValue;
    USHORT StatusValue;
    KIRQL OldIrql;

    //
    // check to see if specified slot is valid
    //

    if (!HalpCheckPCIDevice(Slot)) {

        //
        // Invalid SlotID return no data
        //

        *Buffer = 0xff;
        return ;

    }

    *((PULONG) &ConfigAddressValue) = 0x0;

    ConfigAddressValue.ConfigEnable = 1;
    ConfigAddressValue.DeviceNumber = Slot.u.bits.DeviceNumber;
    ConfigAddressValue.FunctionNumber = Slot.u.bits.FunctionNumber;
    ConfigAddressValue.RegisterNumber = Offset >> 2;

    //
    // Synchronize with PCI configuration
    //

//  KeRaiseIrql(PROFILE_LEVEL, &OldIrql); // M017
//  KiAcquireSpinLock(&HalpPCIConfigLock);

    WRITE_REGISTER_ULONG(R94A_PCI_ADDR_REG, *((PULONG) &ConfigAddressValue));

//    *Buffer = READ_REGISTER_ULONG(R94A_PCI_DATA_REG);
    WRITE_REGISTER_UCHAR((PUCHAR)R94A_PCI_DATA_REG + (Offset % sizeof(ULONG)),*Buffer);

    //
    // Release spinlock
    //

//  KiReleaseSpinLock(&HalpPCIConfigLock);
//  KeLowerIrql(OldIrql);

    return;
}