summaryrefslogblamecommitdiffstats
path: root/private/mvdm/softpc.new/base/video/ega_prts.c
blob: b30fc8b5eb2b6dd1689e9adf5d3d58e092fba3ad (plain) (tree)
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


































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                               
#include "insignia.h"
#include "host_def.h"
/*			INSIGNIA (SUB)MODULE SPECIFICATION
			-----------------------------


	THIS PROGRAM SOURCE FILE  IS  SUPPLIED IN CONFIDENCE TO THE
	CUSTOMER, THE CONTENTS  OR  DETAILS  OF  ITS OPERATION MUST
	NOT BE DISCLOSED TO ANY  OTHER PARTIES  WITHOUT THE EXPRESS
	AUTHORISATION FROM THE DIRECTORS OF INSIGNIA SOLUTIONS LTD.

DOCUMENT 		: name and number

RELATED DOCS		: include all relevant references

DESIGNER		: 

REVISION HISTORY	:
First version		: 13 July 1988, J.Roper

SUBMODULE NAME		: ega		

SOURCE FILE NAME	: ega_ports.c

PURPOSE			: emulation of EGA registers (ports).
			  Calls lower levels of the EGA emulation to do the real work.

static char SccsID[]="@(#)ega_ports.c	1.54 07/18/94 Copyright Insignia Solutions Ltd.";


[1.INTERMODULE INTERFACE SPECIFICATION]

[1.0 INCLUDE FILE NEEDED TO ACCESS THIS INTERFACE FROM OTHER SUBMODULES]

	INCLUDE FILE : ega_ports.gi

[1.1    INTERMODULE EXPORTS]

	PROCEDURES() :
			void ega_init()
			void ega_term()
			int ega_get_line_compare()	(* hunter only *)
			int ega_get_max_scan_lines()	(* hunter only *)

-------------------------------------------------------------------------
[1.2 DATATYPES FOR [1.1] (if not basic C types)]
-------------------------------------------------------------------------
[1.3 INTERMODULE IMPORTS]
     (not o/s objects or standard libs)

	PROCEDURES() : 
			io_define_inb
			io_define_outb
			io_connect_port
			io_disconnect_port

	DATA 	     : 	give name, and source module name

-------------------------------------------------------------------------

[1.4 DESCRIPTION OF INTERMODULE INTERFACE]

[1.4.1 IMPORTED OBJECTS]

DATA OBJECTS	  :	specify in following procedure descriptions
			how these are accessed (read/modified)

FILES ACCESSED    :	NONE

DEVICES ACCESSED  :	NONE

SIGNALS CAUGHT	  :	NONE

SIGNALS ISSUED	  :	NONE


[1.4.2 EXPORTED OBJECTS]
=========================================================================
PROCEDURE	  : 	ega_init

PURPOSE		  : 	initialize EGA.

PARAMETERS	  :	none

GLOBALS		  :	none

DESCRIPTION	  : 	establish ega ports. 
			initialize ega code to sensible state.

ERROR INDICATIONS :	none.

ERROR RECOVERY	  :	none.
=========================================================================

=========================================================================
PROCEDURE	  : 	ega_term

PURPOSE		  : 	terminate EGA.

PARAMETERS	  :	none

GLOBALS		  :	none

DESCRIPTION	  : 	remove ega ports. 
			free up allocated memory etc.

ERROR INDICATIONS :	none.

ERROR RECOVERY	  :	none.
=========================================================================

=========================================================================
PROCEDURE	  : 	ega_seq_outb((io_addr) port, (half_word) value)

PURPOSE		  : 	deal with bytes written to the sequencer chip's ports, and pass
			appropriate info to ega sub-modules.

PARAMETERS
	port	  :	port address written to.
	value	  :	the byte written to the port.

GLOBALS		  :	none

DESCRIPTION	  : 	

ERROR INDICATIONS :	none.

ERROR RECOVERY	  :	none.
=========================================================================

=========================================================================
PROCEDURE	  : 	ega_crtc_outb((io_addr) port, (half_word) value)

PURPOSE		  : 	deal with bytes written to the sequencer chip's ports, and pass
			appropriate info to ega sub-modules.

PARAMETERS
	port	  :	port address written to.
	value	  :	the byte written to the port.

GLOBALS		  :	none

DESCRIPTION	  : 	

ERROR INDICATIONS :	none.

ERROR RECOVERY	  :	none.
=========================================================================

=========================================================================
PROCEDURE	  : 	ega_crtc_inb((io_addr) port, (half_word) *value)

PURPOSE		  : 	deal with an attempt to read a byte from one of the crtc's register ports,
			and gets info from appropriate ega sub-modules.

PARAMETERS
	port	  :	port address written to.
	value	  :	pointer to memory byte where value read from port should go.

GLOBALS		  :	none

DESCRIPTION	  : 	

ERROR INDICATIONS :	none.

ERROR RECOVERY	  :	none.
=========================================================================

=========================================================================
PROCEDURE	  : 	ega_gc_outb((io_addr) port, (half_word) value)

PURPOSE		  : 	deal with bytes written to the graphics controller chip's ports,
			and pass appropriate info to ega sub-modules.

PARAMETERS
	port	  :	port address written to.
	value	  :	the byte written to the port.

GLOBALS		  :	none

DESCRIPTION	  : 	

ERROR INDICATIONS :	none.

ERROR RECOVERY	  :	none.
=========================================================================

=========================================================================
PROCEDURE	  : 	ega_ac_outb((io_addr) port, (half_word) value)

PURPOSE		  : 	deal with bytes written to the attribute controller chip's ports, and pass
			appropriate info to ega sub-modules.

PARAMETERS
	port	  :	port address written to.
	value	  :	the byte written to the port.

GLOBALS		  :	none

DESCRIPTION	  : 	

ERROR INDICATIONS :	none.

ERROR RECOVERY	  :	none.
=========================================================================

=========================================================================
PROCEDURE	  : 	ega_misc_outb((io_addr) port, (half_word) value)

PURPOSE		  : 	deal with bytes written to the miscellaneous register's port, and pass
			appropriate info to ega sub-modules.

PARAMETERS
	port	  :	port address written to.
	value	  :	the byte written to the port.

GLOBALS		  :	none

DESCRIPTION	  : 	

ERROR INDICATIONS :	none.

ERROR RECOVERY	  :	none.
=========================================================================

=========================================================================
PROCEDURE	  : 	ega_feat_outb((io_addr) port, (half_word) value)

PURPOSE		  : 	deal with bytes written to the Feature Control register's port, and pass
			appropriate info to ega sub-modules.

PARAMETERS
	port	  :	port address written to.
	value	  :	the byte written to the port.

GLOBALS		  :	none

DESCRIPTION	  : 	

ERROR INDICATIONS :	none.

ERROR RECOVERY	  :	none.
=========================================================================

=========================================================================
PROCEDURE	  : 	ega_ipstat0_inb((io_addr) port, (half_word) *value)

PURPOSE		  : 	deal with an attempt to read a byte from the input status register 0 port,
			and gets info from appropriate ega sub-modules.

PARAMETERS
	port	  :	port address written to.
	value	  :	pointer to memory byte where value read from port should go.

GLOBALS		  :	none

DESCRIPTION	  : 	

ERROR INDICATIONS :	none.

ERROR RECOVERY	  :	none.
=========================================================================

=========================================================================
PROCEDURE	  : 	ega_ipstat1_inb((io_addr) port, (half_word) *value)

PURPOSE		  : 	deal with an attempt to read a byte from the input status register 1 port,
			and gets info from appropriate ega sub-modules.

PARAMETERS
	port	  :	port address written to.
	value	  :	pointer to memory byte where value read from port should go.

GLOBALS		  :	none

DESCRIPTION	  : 	

ERROR INDICATIONS :	none.

ERROR RECOVERY	  :	none.
=========================================================================

=========================================================================
PROCEDURE	  : 	int ega_get_line_compare()

PURPOSE		  : 	Hunter only - returns the line compare value
			from the crtc registers structure

PARAMETERS	  :	none

GLOBALS		  :	none

DESCRIPTION	  : 	Obtains the line compare value from bit 4 of the
			overflow register (0x7) and the line compare
			register (0x18).

ERROR INDICATIONS :	none.

ERROR RECOVERY	  :	none.
=========================================================================

=========================================================================

PROCEDURE	  : 	int ega_get_max_scan_lines()

PURPOSE		  : 	Hunter only - returns the maximum scan lines value
			from the crtc registers structure

PARAMETERS	  :	none

GLOBALS		  :	none

DESCRIPTION	  : 	Obtains the max scan lines value from the max scan
			lines register (0x9).

ERROR INDICATIONS :	none.

ERROR RECOVERY	  :	none.
=========================================================================


/*=======================================================================
[3.INTERMODULE INTERFACE DECLARATIONS]
=========================================================================

[3.1 INTERMODULE IMPORTS]						*/

/* [3.1.1 #INCLUDES]                                                    */


#ifndef REAL_VGA	/* ega port handling moved to host for REAL_VGA */
#ifdef EGG

#include	"xt.h"
#include	CpuH
#include	"debug.h"
#include        "timer.h"
#include	"sas.h"
#include	"gmi.h"
#include	"gvi.h"
#include	"ios.h"
#include        "ica.h"
#include        "gfx_upd.h"
#include	"egacpu.h"
#include	"egagraph.h"
#include	"egaread.h"
#include	"egamode.h"
#include	"error.h"
#include	"config.h"

#include	"host_gfx.h"
#include	"egaports.h"

#ifdef GORE
#include  "gore.h"
#endif /* GORE */

#include  "ga_mark.h"
#include  "ga_defs.h"

/* [3.2 INTERMODULE EXPORTS]						*/ 


/* [3.1.2 DECLARATIONS]                                                 */

IMPORT	void	ega_mode_init IPT0();
IMPORT	int	get_ega_switch_setting IPT0();
IMPORT void v7_get_banks IPT2(UTINY *, rd_bank, UTINY *, wrt_bank );
#ifndef cursor_changed
IMPORT void cursor_changed IPT2(int, x, int, y);
#endif /* cursor_changed */
IMPORT void update_shift_count IPT0();

/*
5.MODULE INTERNALS   :   (not visible externally, global internally)]     

[5.1 LOCAL DECLARATIONS]						*/

LOCAL	void	vote_ega_mode IPT0();
LOCAL void	ega_seq_outb_index IPT2(io_addr, port, half_word, value);
LOCAL void	ega_crtc_outb IPT2(io_addr, port, half_word, value);
LOCAL void	ega_crtc_inb IPT2(io_addr, port, half_word *, value);
LOCAL void	ega_ac_outb IPT2(io_addr, port, half_word, value);
LOCAL void	ega_misc_outb IPT2(io_addr, port, half_word, value);
LOCAL void	ega_feat_outb IPT2(io_addr, port, half_word, value);
LOCAL void	ega_ipstat0_inb IPT2(io_addr, port, half_word *, value);
LOCAL void	ega_ipstat1_inb IPT2(io_addr, port, half_word *, value);

/* [5.1.1 #DEFINES]							*/
#ifdef SEGMENTATION
/*
 * The following #include specifies the code segment into which this
 * module will by placed by the MPW C compiler on the Mac II running
 * MultiFinder.
 */
#include "SOFTPC_EGA.seg"
#endif

GLOBAL VOID ega_gc_outw IPT2(io_addr, port, word, outval);

/*
 * EGA_PLANE_DISP_SIZE is already declared in egaports.h. However if V7VGA is
 * defined using this definition will cause problems. See BCN 1486 for details.
 */
#define EGA_PLANE_SZ	0x10000

/* [5.1.2 TYPEDEF, STRUCTURE, ENUM DECLARATIONS]			*/
#ifdef BIT_ORDER1
typedef	union
{
	struct {
		unsigned abyte : 8;
    	} as;
    	struct {
	    unsigned hardware_reset		: 1,	/* NO		*/
	    	     word_or_byte_mode		: 1,	/* YES 		*/
	    	     address_wrap		: 1,	/* NO 		*/
	    	     output_control		: 1,	/* YES - screen goes black		*/
	    	     count_by_two		: 1,	/* NO		*/
	    	     horizontal_retrace_select	: 1,	/* NO		*/
	    	     select_row_scan_counter	: 1,	/* NO		*/
		     compatibility_mode_support	: 1;	/* YES - CGA graphics banks		*/
	} as_bfld;
} MODE_CONTROL;

typedef	union
{
	struct {
		unsigned abyte : 8;
	} as;
    	struct {
	    unsigned not_used				: 3,
	    	     line_compare_bit_8			: 1,	/* YES	*/
	    	     start_vertical_blank_bit_8		: 1,	/* NO	*/
	    	     vertical_retrace_start_bit_8	: 1,	/* NO	*/
	    	     vertical_display_enab_end_bit_8	: 1,	/* YES	*/
		     vertical_total_bit_8		: 1;	/* NO	*/
	} as_bfld;
} CRTC_OVERFLOW;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 3,
		     maximum_scan_line			: 5;	/* YES					*/
	} as_bfld;
} MAX_SCAN_LINE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 3,
		     cursor_start			: 5;	/* YES					*/
	} as_bfld;
} CURSOR_START;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 1,
		     cursor_skew_control		: 2,	/* NO					*/
		     cursor_end				: 5;	/* YES					*/
	} as_bfld;
} CURSOR_END;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used			: 6,
	    	     synchronous_reset		: 1,		/* Ditto (could implement as enable_ram)*/
		     asynchronous_reset		: 1;		/* NO - damages video and font RAM	*/
	} as_bfld;
} SEQ_RESET;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used			: 4,
	    	     dot_clock			: 1,		/* YES - distinguishes 40 or 80 chars	*/
	    	     shift_load			: 1,		/* NO					*/
	    	     bandwidth			: 1,		/* NO					*/
		     eight_or_nine_dot_clocks	: 1;		/* NO - only for mono display		*/
	} as_bfld;
} CLOCKING_MODE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used			: 4,
		     all_planes			: 4;		/* YES					*/
	} as_bfld;
} MAP_MASK;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used			: 4,
	    	     character_map_select_b	: 2,		/* YES					*/
		     character_map_select_a	: 2;		/* YES					*/
    	} as_bfld;
	struct {
	    unsigned not_used			: 4,
	    	     map_selects		: 4;		/* YES					*/
    	} character;
} CHAR_MAP_SELECT;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned
	    	     not_used			: 5,		/* If above 2 not both 1, bank 0 set 2	*/
	    	     not_odd_or_even		: 1,		/* YES (check consistency)		*/
	    	     extended_memory		: 1,		/* NO - assume full 256K on board	*/
		     alpha_mode			: 1;		/* YES (check consistency)		*/
	} as_bfld;
} MEMORY_MODE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 4,
		     set_or_reset			: 4;	/* YES - write mode 0 only		*/
	} as_bfld;
} SET_OR_RESET;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 4,
		     enable_set_or_reset		: 4;	/* YES - write mode 0 only		*/
	} as_bfld;
} ENABLE_SET_OR_RESET;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 4,
		     color_compare			: 4;	/* YES - read mode 1 only		*/
	} as_bfld;
} COLOR_COMPARE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 3,
	    	     function_select			: 2,	/* YES - write mode 0 only		*/
		     rotate_count			: 3;	/* YES - write mode 0 only		*/
	} as_bfld;
} DATA_ROTATE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 5,
		 map_select				: 3;	/* YES  				*/
	} as_bfld;
} READ_MAP_SELECT;

typedef	union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 2,
	    	     shift_register_mode		: 1,	/* YES - CGA colour graphics		*/
	    	     odd_or_even			: 1,	/* YES (check for consistency)		*/
	    	     read_mode				: 1,	/* YES					*/
	    	     test_condition			: 1,	/* NO					*/
		     write_mode				: 2;	/* YES					*/
	} as_bfld;
} MODE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 4,
	    	     memory_map				: 2,	/* YES - location of EGA in M		*/
	    	     odd_or_even			: 1,	/* YES (check consistency)		*/
		     graphics_mode			: 1;	/* YES					*/
	} as_bfld;
} MISC_REG;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 4,
		     color_dont_care			: 4;	/* YES - read mode 1 only		*/
	} as_bfld;
} COLOR_DONT_CARE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned
	    	     not_used				: 4,
	    	     background_intensity_or_blink	: 1,	/* NO - never blink			*/
	    	     enable_line_graphics_char_codes	: 1,	/* NO mono display only			*/
	    	     display_type			: 1,	/* NO - always colour display		*/
		     graphics_mode			: 1;	/* YES - with Sequencer Mode reg	*/
	} as_bfld;
} AC_MODE_CONTROL;

typedef union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	unsigned vertical_retrace_polarity	: 1,		/* YES - switch between 200/350 lines	*/
    		 horizontal_retrace_polarity	: 1,		/* NO - probably destroys display!	*/
    		 page_bit_odd_even		: 1,		/* NO - selects 32k page in odd/even?	*/
    		 disable_internal_video_drivers	: 1,		/* NO - like switching PC off		*/
    		 clock_select			: 2,		/* YES - only for switch address	*/
    		 enable_ram			: 1,		/* YES - writes to display mem ignored	*/
		 io_address_select		: 1;		/* NO - only used for mono screens	*/
	} as_bfld;
} MISC_OUTPUT_REG;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	unsigned not_used			: 4,
    		 reserved			: 2,		/* YES - ignore				*/
		 feature_control		: 2;		/* NO - device not supported		*/
	} as_bfld;
} FEAT_CONT_REG;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	unsigned crt_interrupt			: 1,		/* YES - sequence if not timing		*/
    		 reserved			: 2,		/* YES - all bits 1			*/
    		 switch_sense			: 1,		/* YES - switch selected by clock sel.	*/
		 not_used			: 4;		/* YES - all bits 1			*/
	} as_bfld;
} INPUT_STAT_REG0;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
    	struct {
	    unsigned not_used				: 2,
		     video_status_mux			: 2,	/* NO					*/
		     color_plane_enable			: 4;	/* YES  NB. affects attrs in text mode	*/
	} as_bfld;
} COLOR_PLANE_ENABLE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	unsigned not_used			: 1,		/* YES - set to 1			*/
    		 diagnostic_0			: 1,		/* NO - set to 0			*/
    		 diagnostic_1			: 1,		/* NO - set to 0			*/
    		 vertical_retrace		: 1,		/* YES - sequence only			*/
    		 light_pen_switch		: 1,		/* YES - set to 0			*/
    		 light_pen_strobe		: 1,		/* YES - set to 1			*/
		 display_enable			: 1;		/* YES - sequence only			*/
	} as_bfld;
} INPUT_STAT_REG1;
#endif
#ifdef BIT_ORDER2
typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned compatibility_mode_support		: 1,	/* YES - CGA graphics banks		*/
	    	     select_row_scan_counter		: 1,	/* NO					*/
	    	     horizontal_retrace_select		: 1,	/* NO					*/
	    	     count_by_two			: 1,	/* NO					*/
	    	     output_control			: 1,	/* YES - screen goes black		*/
	    	     address_wrap			: 1,	/* NO 					*/
	    	     word_or_byte_mode			: 1,	/* YES 					*/
	    	     hardware_reset			: 1;	/* NO					*/
	} as_bfld;
} MODE_CONTROL;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned vertical_total_bit_8		: 1,	/* NO					*/
	    	     vertical_display_enab_end_bit_8	: 1,	/* YES					*/
	    	     vertical_retrace_start_bit_8	: 1,	/* NO					*/
	    	     start_vertical_blank_bit_8		: 1,	/* NO					*/
	    	     line_compare_bit_8			: 1,	/* YES					*/
	    	     not_used				: 3;
	} as_bfld;
} CRTC_OVERFLOW;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned maximum_scan_line			: 5,	/* YES					*/
	    	     not_used				: 3;
	} as_bfld;
} MAX_SCAN_LINE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned cursor_start			: 5,	/* YES					*/
	    	     not_used				: 3;
	} as_bfld;
} CURSOR_START;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned cursor_end				: 5,	/* YES					*/
		     cursor_skew_control		: 2,	/* NO					*/
	    	     not_used				: 1;
	} as_bfld;
} CURSOR_END;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned asynchronous_reset		: 1,		/* NO - damages video and font RAM	*/
	    	     synchronous_reset		: 1,		/* Ditto (could implement as enable_ram)*/
	    	     not_used			: 6;
	} as_bfld;
} SEQ_RESET;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned eight_or_nine_dot_clocks	: 1,		/* NO - only for mono display		*/
	    	     bandwidth			: 1,		/* NO					*/
	    	     shift_load			: 1,		/* NO					*/
	    	     dot_clock			: 1,		/* YES - distinguishes 40 or 80 chars	*/
	    	     not_used			: 4;
	} as_bfld;
} CLOCKING_MODE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned all_planes			: 4,		/* YES					*/
		     not_used			: 4;
	} as_bfld;
} MAP_MASK;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned character_map_select_a	: 2,		/* YES					*/
	    	     character_map_select_b	: 2,		/* YES					*/
	    	     not_used			: 4;
    	} as_bfld;
	struct {
	    unsigned map_selects		: 4,		/* YES					*/
	    	     not_used			: 4;
    	} character;
} CHAR_MAP_SELECT;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned alpha_mode			: 1,		/* YES (check consistency)		*/
	    	     extended_memory		: 1,		/* NO - assume full 256K on board	*/
	    	     not_odd_or_even		: 1,		/* YES (check consistency)		*/
	    	     not_used			: 5;		/* If above 2 not both 1, bank 0 set 2	*/
	} as_bfld;
} MEMORY_MODE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned set_or_reset			: 4,	/* YES - write mode 0 only		*/
	    	     not_used				: 4;
	} as_bfld;
} SET_OR_RESET;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned enable_set_or_reset		: 4,	/* YES - write mode 0 only		*/
	    	     not_used				: 4;
	} as_bfld;
} ENABLE_SET_OR_RESET;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned color_compare			: 4,	/* YES - read mode 1 only		*/
	    	     not_used				: 4;
	} as_bfld;
} COLOR_COMPARE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned rotate_count			: 3,	/* YES - write mode 0 only		*/
	    	     function_select			: 2,	/* YES - write mode 0 only		*/
	    	     not_used				: 3;
	} as_bfld;
} DATA_ROTATE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned map_select				: 3,	/* YES - read mode 0 only		*/
	         not_used				: 5;
	} as_bfld;
} READ_MAP_SELECT;

typedef	union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned write_mode				: 2,	/* YES					*/
	    	     test_condition			: 1,	/* NO					*/
	    	     read_mode				: 1,	/* YES					*/
	    	     odd_or_even			: 1,	/* YES (check for consistency)		*/
	    	     shift_register_mode		: 1,	/* YES - CGA colour graphics		*/
	    	     not_used				: 2;
	} as_bfld;
} MODE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned graphics_mode			: 1,	/* YES					*/
	    	     odd_or_even			: 1,	/* YES (check consistency)		*/
	    	     memory_map				: 2,	/* YES - location of EGA in M		*/
	    	     not_used				: 4;
	} as_bfld;
} MISC_REG;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned color_dont_care			: 4,	/* YES - read mode 1 only		*/
	    	     not_used				: 4;
	} as_bfld;
} COLOR_DONT_CARE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned graphics_mode			: 1,	/* YES - with Sequencer Mode reg	*/
	    	     display_type			: 1,	/* NO - always colour display		*/
	    	     enable_line_graphics_char_codes	: 1,	/* NO mono display only			*/
	    	     background_intensity_or_blink	: 1,	/* NO - never blink			*/
	    	     not_used				: 4;
	} as_bfld;
} AC_MODE_CONTROL;

typedef union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	unsigned io_address_select		: 1,		/* NO - only used for mono screens	*/
    		 enable_ram			: 1,		/* YES - writes to display mem ignored	*/
    		 clock_select			: 2,		/* YES - only for switch address	*/
    		 disable_internal_video_drivers	: 1,		/* NO - like switching PC off		*/
    		 page_bit_odd_even		: 1,		/* NO - selects 32k page in odd/even?	*/
    		 horizontal_retrace_polarity	: 1,		/* NO - probably destroys display!	*/
    		 vertical_retrace_polarity	: 1;		/* YES - switch between 200/350 lines	*/
	} as_bfld;
} MISC_OUTPUT_REG;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	unsigned feature_control		: 2,		/* NO - device not supported		*/
    		 reserved			: 2,		/* YES - ignore				*/
    		 not_used			: 4;
	} as_bfld;
} FEAT_CONT_REG;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	unsigned not_used			: 4,		/* YES - all bits 1			*/
    		 switch_sense			: 1,		/* YES - switch selected by clock sel.	*/
    		 reserved			: 2,		/* YES - all bits 1			*/
    		 crt_interrupt			: 1;		/* YES - sequence if not timing		*/
	} as_bfld;
} INPUT_STAT_REG0;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
    	struct {
	    unsigned color_plane_enable			: 4,	/* YES  NB. affects attrs in text mode	*/
		     video_status_mux			: 2,	/* NO					*/
	    	     not_used				: 2;
	} as_bfld;
} COLOR_PLANE_ENABLE;

typedef	union
{
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	unsigned display_enable			: 1,		/* YES - sequence only			*/
    		 light_pen_strobe		: 1,		/* YES - set to 1			*/
    		 light_pen_switch		: 1,		/* YES - set to 0			*/
    		 vertical_retrace		: 1,		/* YES - sequence only			*/
    		 diagnostic_1			: 1,		/* NO - set to 0			*/
    		 diagnostic_0			: 1,		/* NO - set to 0			*/
    		 not_used			: 1;		/* YES - set to 1			*/
	} as_bfld;
} INPUT_STAT_REG1;
#endif



/* [5.1.3 PROCEDURE() DECLARATIONS]					*/


/* -----------------------------------------------------------------------
[5.2 LOCAL DEFINITIONS]

   [5.2.1 INTERNAL DATA DEFINITIONS 					*/

/*

/* EGA REGISTERS */
/* Comments after bitfields indicate whether change of value affects emulated screen display or memory interface */

/* Registers not contained in an LSI device */

static	MISC_OUTPUT_REG	miscellaneous_output_register;

static	FEAT_CONT_REG	feature_control_register;

static	INPUT_STAT_REG0	input_status_register_zero;

static	INPUT_STAT_REG1	input_status_register_one;

/* The Sequencer Registers */
#ifdef BIT_ORDER1
static struct
{
    union
    {
	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	    unsigned not_used			: 5,
		     index			: 3;
    	} as_bfld;
    } address;

    SEQ_RESET		reset;
    CLOCKING_MODE	clocking_mode;
    MAP_MASK		map_mask;
    CHAR_MAP_SELECT	character_map_select;
    MEMORY_MODE		memory_mode;
} sequencer;


/* The CRT Controller Registers */

static struct
{
    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	    unsigned not_used				: 3,
		     index				: 5;
    	} as_bfld;
    } address;

    byte horizontal_total;					/* NO - screen trash if wrong value	*/
    byte horizontal_display_end;				/* YES - defines line length!!		*/
    byte start_horizontal_blanking;				/* NO					*/

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 1,
	    	     display_enable_skew_control	: 2,	/* NO					*/
		     end_blanking			: 5;	/* NO					*/
	} as_bfld;
    } end_horizontal_blanking;

    byte start_horizontal_retrace;				/* NO					*/

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 1,
	    	     horizontal_retrace_delay		: 2,	/* NO					*/
		     end_horizontal_retrace		: 5;	/* NO					*/
	} as_bfld;
    } end_horizontal_retrace;

    byte vertical_total;					/* NO					*/
    CRTC_OVERFLOW	crtc_overflow;

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 3,
		     preset_row_scan			: 5;	/* NO					*/
	} as_bfld;
    } preset_row_scan;

    MAX_SCAN_LINE	maximum_scan_line;
    CURSOR_START	cursor_start;
    CURSOR_END		cursor_end;
    byte start_address_high;					/* YES					*/
    byte start_address_low;					/* YES					*/
    byte cursor_location_high;					/* YES					*/
    byte cursor_location_low;					/* YES					*/
    byte vertical_retrace_start;				/* NO					*/
    byte light_pen_high;					/* NO					*/

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned 
	    	     not_used				: 2,
	    	     enable_vertical_interrupt		: 1,	/* YES - ditto				*/
	    	     clear_vertical_interrupt		: 1,	/* YES - needs investigation		*/
		     vertical_retrace_end		: 4;	/* NO					*/
	} as_bfld;
    } vertical_retrace_end;

    byte light_pen_low;						/* NO					*/
    byte vertical_display_enable_end;				/* YES - defines screen height		*/
    byte offset;						/* YES (maybe!)	???????			*/

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 3,
		     underline_location			: 5;	/* NO (mono display only)		*/
	} as_bfld;
    } underline_location;

    byte start_vertical_blanking;				/* NO					*/
    byte end_vertical_blanking;					/* NO					*/
    MODE_CONTROL	mode_control;
    byte line_compare;						/* YES					*/

} crt_controller;


/* The Graphics Controller Registers */

static struct
{
    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	    unsigned not_used				: 4,
		     index				: 4;
    	} as_bfld;
    } address;

    SET_OR_RESET	set_or_reset;
    ENABLE_SET_OR_RESET	enable_set_or_reset;
    COLOR_COMPARE	color_compare;
    DATA_ROTATE		data_rotate;
    READ_MAP_SELECT	read_map_select;
    MODE		mode;
    MISC_REG		miscellaneous;
    COLOR_DONT_CARE	color_dont_care;
    byte bit_mask_register;					/* YES - write modes 0 & 2		*/
    byte graphics_1_position;					/* NO					*/
    byte graphics_2_position;					/* NO					*/
} graphics_controller;


/* The Attribute Controller Registers */

static struct
{
    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	    unsigned not_used				: 3,
		     index				: 5;
    	} as_bfld;
    } address;

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned
		     not_used				: 2,	/* YES					*/
		     secondary_red			: 1,	/* YES					*/
		     secondary_green			: 1,	/* YES					*/
		     secondary_blue			: 1,	/* YES					*/
		     red				: 1,	/* YES					*/
		     green				: 1,	/* YES					*/
		     blue				: 1;	/* YES					*/
	} as_bfld;
    } palette[EGA_PALETTE_SIZE];

    AC_MODE_CONTROL	mode_control;

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 2,
		     secondary_red_border		: 1,	/* YES					*/
		     secondary_green_border		: 1,	/* YES					*/
		     secondary_blue_border		: 1,	/* YES					*/
		     red_border				: 1,	/* YES					*/
		     green_border			: 1,	/* YES					*/
		     blue_border			: 1;	/* YES - real thing isn't good at this	*/
	} as_bfld;
    } overscan_color;

    COLOR_PLANE_ENABLE	color_plane_enable;

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned not_used				: 4,
		     horizontal_pel_panning		: 4;	/* NO					*/
	} as_bfld;
    } horizontal_pel_panning;
} attribute_controller;
#endif

#ifdef BIT_ORDER2
static struct
{
    union
    {
	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	    unsigned index				: 3,
		     not_used				: 5;
    	} as_bfld;
    } address;

    SEQ_RESET		reset;
    CLOCKING_MODE	clocking_mode;
    MAP_MASK		map_mask;
    CHAR_MAP_SELECT	character_map_select;
    MEMORY_MODE		memory_mode;
} sequencer;


/* The CRT Controller Registers */

static struct
{
    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	    unsigned index				: 5,
		     not_used				: 3;
    	} as_bfld;
    } address;

    byte horizontal_total;					/* NO - screen trash if wrong value	*/
    byte horizontal_display_end;				/* YES - defines line length!!		*/
    byte start_horizontal_blanking;				/* NO					*/

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned end_blanking			: 5,	/* NO					*/
	    	     display_enable_skew_control	: 2,	/* NO					*/
	    	     not_used				: 1;
	} as_bfld;
    } end_horizontal_blanking;

    byte start_horizontal_retrace;				/* NO					*/

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned end_horizontal_retrace		: 5,	/* NO					*/
	    	     horizontal_retrace_delay		: 2,	/* NO					*/
	    	     not_used				: 1;
	} as_bfld;
    } end_horizontal_retrace;

    byte vertical_total;					/* NO					*/
    CRTC_OVERFLOW	crtc_overflow;

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned preset_row_scan			: 5,	/* NO					*/
	    	     not_used				: 3;
	} as_bfld;
    } preset_row_scan;

    MAX_SCAN_LINE	maximum_scan_line;
    CURSOR_START	cursor_start;
    CURSOR_END		cursor_end;
    byte start_address_high;					/* YES					*/
    byte start_address_low;					/* YES					*/
    byte cursor_location_high;					/* YES					*/
    byte cursor_location_low;					/* YES					*/
    byte vertical_retrace_start;				/* NO					*/
    byte light_pen_high;					/* NO					*/

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned vertical_retrace_end		: 4,	/* NO					*/
	    	     clear_vertical_interrupt		: 1,	/* YES - needs investigation		*/
	    	     enable_vertical_interrupt		: 1,	/* YES - ditto				*/
	    	     not_used				: 2;
	} as_bfld;
    } vertical_retrace_end;

    byte light_pen_low;						/* NO					*/
    byte vertical_display_enable_end;				/* YES - defines screen height		*/
    byte offset;						/* YES (maybe!)	???????			*/

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned underline_location			: 5,	/* NO (mono display only)		*/
	    	     not_used				: 3;
	} as_bfld;
    } underline_location;

    byte start_vertical_blanking;				/* NO					*/
    byte end_vertical_blanking;					/* NO					*/
    MODE_CONTROL	mode_control;
    byte line_compare;						/* YES					*/

} crt_controller;


/* The Graphics Controller Registers */

static struct
{
    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	    unsigned index				: 4,
    	    	     not_used				: 4;
    	} as_bfld;
    } address;

    SET_OR_RESET	set_or_reset;
    ENABLE_SET_OR_RESET	enable_set_or_reset;
    COLOR_COMPARE	color_compare;
    DATA_ROTATE		data_rotate;
    READ_MAP_SELECT	read_map_select;
    MODE		mode;
    MISC_REG		miscellaneous;
    COLOR_DONT_CARE	color_dont_care;
    byte bit_mask_register;					/* YES - write modes 0 & 2		*/
    byte graphics_1_position;					/* NO					*/
    byte graphics_2_position;					/* NO					*/
} graphics_controller;


/* The Attribute Controller Registers */

static struct
{
    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
    	    unsigned index				: 5,
    	    	     not_used				: 3;
    	} as_bfld;
    } address;

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned blue				: 1,	/* YES					*/
		     green				: 1,	/* YES					*/
		     red				: 1,	/* YES					*/
		     secondary_blue			: 1,	/* YES					*/
		     secondary_green			: 1,	/* YES					*/
		     secondary_red			: 1,	/* YES					*/
		     not_used				: 2;	/* YES					*/
	} as_bfld;
    } palette[EGA_PALETTE_SIZE];

    AC_MODE_CONTROL	mode_control;

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned blue_border			: 1,	/* YES - real thing isn't good at this	*/
		     green_border			: 1,	/* YES					*/
		     red_border				: 1,	/* YES					*/
		     secondary_blue_border		: 1,	/* YES					*/
		     secondary_green_border		: 1,	/* YES					*/
		     secondary_red_border		: 1,	/* YES					*/
		     not_used				: 2;
	} as_bfld;
    } overscan_color;

    COLOR_PLANE_ENABLE	color_plane_enable;

    union
    {
    	struct {
		unsigned abyte : 8;
	} as;
	struct {
	    unsigned horizontal_pel_panning		: 4,	/* NO					*/
	    	     not_used				: 4;
	} as_bfld;
    } horizontal_pel_panning;
} attribute_controller;
#endif

static	boolean	ac_index_state = NO;
extern half_word bg_col_mask; /* Used to work out the background colour */

IMPORT VOID _ega_gc_outb_index IPT2(io_addr,port,half_word,value);
IMPORT VOID _ega_gc_outb_mask IPT2(io_addr,port,half_word,value);
IMPORT VOID _ega_gc_outb_mask_ff IPT2(io_addr,port,half_word,value);

/* Declarations for new multi-routine graphics controller */
void	ega_gc_set_reset IPT2(io_addr, port, half_word, value);
void	ega_gc_enable_set IPT2(io_addr, port, half_word, value);
void	ega_gc_compare IPT2(io_addr, port, half_word, value);
void	ega_gc_rotate IPT2(io_addr, port, half_word, value);
void	ega_gc_read_map IPT2(io_addr, port, half_word, value);
void	ega_gc_mode IPT2(io_addr, port, half_word, value);
void	ega_gc_misc IPT2(io_addr, port, half_word, value);
void	ega_gc_dont_care IPT2(io_addr, port, half_word, value);
LOCAL void	ega_gc_mask IPT2(io_addr, port, half_word, value);
void	ega_gc_mask_ff IPT2(io_addr, port, half_word, value);
LOCAL void	ega_index_invalid IPT2(io_addr, port, half_word, value);

void (*ega_gc_regs[]) IPT2(io_addr, port, half_word, value) = {
      ega_gc_set_reset,
      ega_gc_enable_set,
      ega_gc_compare,
      ega_gc_rotate,
      ega_gc_read_map,
      ega_gc_mode,
      ega_gc_misc,
      ega_gc_dont_care,
      ega_gc_mask,
      ega_index_invalid,
      ega_index_invalid,
      ega_index_invalid,
      ega_index_invalid,
      ega_index_invalid,
      ega_index_invalid,
      ega_index_invalid,
};

#ifndef	A2CPU
void (*ega_gc_regs_cpu[]) IPT2(io_addr,port,half_word,value) = {
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
};
#endif	/* A2CPU */

/* Declarations for new seqencer code */
void	ega_seq_reset IPT2(io_addr, port, half_word, value);
void	ega_seq_clock IPT2(io_addr, port, half_word, value);
void	ega_seq_map_mask IPT2(io_addr, port, half_word, value);
void	ega_seq_char_map IPT2(io_addr, port, half_word, value);
void	ega_seq_mem_mode IPT2(io_addr, port, half_word, value);

void (*ega_seq_regs[]) IPT2(io_addr, port, half_word, value) =
{
      ega_seq_reset,
      ega_seq_clock,
      ega_seq_map_mask,
      ega_seq_char_map,
      ega_seq_mem_mode,
      ega_index_invalid,
      ega_index_invalid,
      ega_index_invalid,
};


/* [5.2.2 INTERNAL PROCEDURE DEFINITIONS]				*/

/*
==========================================================================
FUNCTION	:	set_index_state()
PURPOSE		:	Set the attribute controller to use the next value
			written to its port as the index value.
EXTERNAL OBJECTS:	
RETURN VALUE	:	None
INPUT  PARAMS	:	None
RETURN PARAMS   :	None
==========================================================================
*/

void	set_index_state IFN0()
{
	/*
	 * Seems strange, but in_index_state changes the state & returns the result
	 * so we set state to NO, so that next call of in_index_state will return YES
	 */
	ac_index_state = NO;
}

/*
==========================================================================
FUNCTION	:	in_index_state()
PURPOSE		:	To determine if the value written to the attribute
			controller is destined for the index register, or
			another register specified by the current index
			value.
EXTERNAL OBJECTS:	
RETURN VALUE	:	Boolean
INPUT  PARAMS	:	None
RETURN PARAMS   :	None
==========================================================================
*/

boolean	in_index_state IFN0()
{
	ac_index_state = ! ac_index_state;
	return(ac_index_state);
}

/*
==========================================================================
FUNCTION	:	do_new_cursor()
PURPOSE		:	deals with the shape of the cursor according to 
			char_height, cursor_start and cursor_end. See Tech
			Memo 88.6.1 for details.
EXTERNAL OBJECTS:	EGA_GRAPH.cursor_start,EGA_GRAPH.cursor_height,EGA_cursor_start1,
			EGA_GRAPH.cursor_height1,host_cursor_has_changed().
RETURN VALUE	:	None
INPUT  PARAMS	:	None
RETURN PARAMS   :	None
==========================================================================
*/

LOCAL void	do_new_cursor IFN0()
{

	note_entrance0("do_new_cursor()");

#ifdef VGG
    if ( video_adapter == VGA ) {
		note_entrance0("VGA cursor");
		set_cursor_start(crt_controller.cursor_start.as_bfld.cursor_start);
		set_cursor_height(crt_controller.cursor_end.as_bfld.cursor_end - crt_controller.cursor_start.as_bfld.cursor_start);
		set_cursor_start1(0);	/* cursor never splits */
		set_cursor_height1(0);
		set_cursor_visible(TRUE);
		host_cursor_size_changed(crt_controller.cursor_start.as_bfld.cursor_start,
					crt_controller.cursor_end.as_bfld.cursor_end);
    } else {
#endif
	if (crt_controller.cursor_start.as_bfld.cursor_start >= get_char_height() ) {
		note_entrance0("No cursor");
		set_cursor_visible(FALSE);
		host_cursor_size_changed(crt_controller.cursor_start.as_bfld.cursor_start | 0x20,
					crt_controller.cursor_end.as_bfld.cursor_end);
	}
	else if (crt_controller.cursor_end.as_bfld.cursor_end == 0) {
		note_entrance0("cursor from start to bum");
		set_cursor_start1(0);
		set_cursor_height1(0);
		set_cursor_start(crt_controller.cursor_start.as_bfld.cursor_start);
		set_cursor_height(get_char_height() - get_cursor_start());
		set_cursor_visible(TRUE);
		host_cursor_size_changed(crt_controller.cursor_start.as_bfld.cursor_start,
					crt_controller.cursor_end.as_bfld.cursor_end);
	}
	else if (crt_controller.cursor_end.as_bfld.cursor_end < crt_controller.cursor_start.as_bfld.cursor_start) {
		note_entrance0("2 cursors");
		set_cursor_start1(0);
		set_cursor_height1(crt_controller.cursor_end.as_bfld.cursor_end);
		set_cursor_start(crt_controller.cursor_start.as_bfld.cursor_start);
		set_cursor_height(get_char_height() - get_cursor_start());
		set_cursor_visible(TRUE);
		host_cursor_size_changed(crt_controller.cursor_start.as_bfld.cursor_start,
					crt_controller.cursor_end.as_bfld.cursor_end);
	}
	else if (crt_controller.cursor_end.as_bfld.cursor_end == crt_controller.cursor_start.as_bfld.cursor_start) {
		note_entrance0("One line cursor");
		set_cursor_start(crt_controller.cursor_start.as_bfld.cursor_start);
		set_cursor_height(1);
		set_cursor_start1(0);
		set_cursor_height1(0);
		set_cursor_visible(TRUE);
		host_cursor_size_changed(crt_controller.cursor_start.as_bfld.cursor_start,
					crt_controller.cursor_end.as_bfld.cursor_end);
	}
	else if (crt_controller.cursor_end.as_bfld.cursor_end - 1 >= get_char_height()) {
		note_entrance0("block cursor");
		set_cursor_start(0);
		set_cursor_height(get_char_height());
		set_cursor_start1(0);
		set_cursor_height1(0);
		set_cursor_visible(TRUE);
		host_cursor_size_changed(crt_controller.cursor_start.as_bfld.cursor_start,
					crt_controller.cursor_end.as_bfld.cursor_end);
	}
	else {
		assert2(((crt_controller.cursor_end.as_bfld.cursor_end - 1) >= crt_controller.cursor_start.as_bfld.cursor_start),
				"cursor values do not match default set Start %d, End %d",
				crt_controller.cursor_end.as_bfld.cursor_end,
				crt_controller.cursor_start.as_bfld.cursor_start);
		note_entrance0("normal cursor");
		set_cursor_start(crt_controller.cursor_start.as_bfld.cursor_start);
		set_cursor_height(crt_controller.cursor_end.as_bfld.cursor_end - crt_controller.cursor_start.as_bfld.cursor_start);
		set_cursor_start1(0);
		set_cursor_height1(0);
		set_cursor_visible(TRUE);
		host_cursor_size_changed(crt_controller.cursor_start.as_bfld.cursor_start,
					crt_controller.cursor_end.as_bfld.cursor_end);
	}
#ifdef VGG
    }
#endif

	if(( get_cur_y() < 0 ) ||
			((( get_cur_y() + 1 ) * get_char_height()) > get_screen_height() ))
	{
		set_cursor_visible( FALSE );
	}

	base_cursor_shape_changed();
}

/*
==========================================================================
FUNCTION	:	do_chain_majority_decision()
PURPOSE		:	deals with any contention regarding whether the 
			ega registers indicate that the addressing of the
			planes is in chained mode or not. If the result
			of the election is a new addressing mode, then
			the video routines, read mode and paint modules
			are informed of the change.
EXTERNAL OBJECTS:	uses local ega register data to count votes.
RETURN VALUE	:	None
INPUT  PARAMS	:	None
RETURN PARAMS   :	None
==========================================================================
*/


LOCAL void	do_chain_majority_decision IFN0()
{
	static	int	current_votes=0;
	int		new_votes;

	new_votes = sequencer.memory_mode.as_bfld.not_odd_or_even ? 0 : 1 ;	/* 0 - chained */
	new_votes += graphics_controller.mode.as_bfld.odd_or_even ;	/* 1 - chained */
	new_votes += graphics_controller.miscellaneous.as_bfld.odd_or_even ;	/* 1 - chained */

	if( new_votes == 1 && current_votes > 1 )
	{
		/*
		 * Transition from chained to unchained
		 */

		EGA_CPU.chain  = UNCHAINED;
		setVideochain(EGA_CPU.chain);
		ega_read_routines_update();
		ega_write_routines_update(CHAINED);
		set_memory_chained(NO);
		flag_mode_change_required();
	}
	else
		if( new_votes > 1 && current_votes == 1 )
		{
			/*
			 * Transition from unchained to chained
			 */

			EGA_CPU.chain = CHAIN2;
			setVideochain(EGA_CPU.chain);
			ega_read_routines_update();
			ega_write_routines_update(CHAINED);
			set_memory_chained(YES);
			flag_mode_change_required();
		}

	current_votes = new_votes;
}


/*
7.INTERMODULE INTERFACE IMPLEMENTATION :

/*
[7.1 INTERMODULE DATA DEFINITIONS]				*/

/*
 * This structure should contain all the global definitions used by EGA
 */

struct	EGA_GLOBALS	EGA_GRAPH;
struct	EGA_CPU_GLOBALS	EGA_CPU;

byte 	*EGA_planes;

int ega_int_enable;

GLOBAL UTINY *ega_gc_outb_index_addr;


/*
[7.2 INTERMODULE PROCEDURE DEFINITIONS]				*/

GLOBAL void
set_banking IFN2(UTINY, rd_bank, UTINY, wrt_bank)
{
	ULONG roffs, woffs;
#ifdef PIG
	IMPORT ULONG pig_vid_bank;
#endif

#ifdef V7VGA
	if( get_seq_chain4_mode() && get_chain4_mode() )
	{
		roffs = (ULONG)rd_bank << 16;
		woffs = (ULONG)wrt_bank << 16;
	}
	else
	{
		roffs = (ULONG)rd_bank << 18;
		woffs = (ULONG)wrt_bank << 18;
	}
#else
		UNUSED(rd_bank);
		UNUSED(wrt_bank);
		roffs = 0;
		woffs = 0;
#endif

#ifdef PIG
	pig_vid_bank = woffs;
#endif
	setVideorplane(EGA_planes + roffs);
	setVideowplane(EGA_planes + woffs);

#ifdef	VGG
	if( get_256_colour_mode() )
		setVideov7_bank_vid_copy_off(woffs >> 2);
	else
#endif	/* VGG */
		setVideov7_bank_vid_copy_off(woffs >> 4);

#ifdef GORE
	gd.max_vis_addr = get_screen_length() - 1 + woffs;
#endif /* GORE */
}

GLOBAL void
update_banking IFN0()
{
	UTINY rd_bank, wrt_bank;

#ifdef V7VGA
	v7_get_banks( &rd_bank, &wrt_bank );
#else
	rd_bank = wrt_bank = 0;
#endif

	set_banking( rd_bank, wrt_bank );
}

VOID
init_vga_globals IFN0()
{
	setVideov7_bank_vid_copy_off(0);
	setVideosr_lookup(sr_lookup);
	setVideovideo_copy(&video_copy[0]);
	setVideoscratch(sas_scratch_address(0x10000));
	setVideoscreen_ptr(EGA_planes);
	setVideorotate(0);
#if !defined(NTVDM) || (defined(NTVDM) && !defined(X86GFX))
#ifndef CPU_40_STYLE	/* EVID */
	setVideomark_byte(_simple_mark_sml);
	setVideomark_word(_simple_mark_sml);
	setVideomark_string(_simple_mark_lge);
#else
	SetMarkPointers(0);
#endif	/* CPU_40_STYLE - EVID */
#endif


	update_banking();
}

void	ega_init IFN0()
{
	note_entrance0("ega_init");
	/*
	 * Define sequencer's ports
	 */

	io_define_outb(EGA_SEQ_ADAP_INDEX,ega_seq_outb_index);
	io_define_outb(EGA_SEQ_ADAP_DATA,ega_seq_reset);
	io_connect_port(EGA_SEQ_INDEX,EGA_SEQ_ADAP_INDEX,IO_WRITE);
	io_connect_port(EGA_SEQ_DATA,EGA_SEQ_ADAP_DATA,IO_WRITE);

	/*
	 * Define CRTC's ports
	 */

	io_define_outb(EGA_CRTC_ADAPTOR,ega_crtc_outb);
	io_define_inb(EGA_CRTC_ADAPTOR,ega_crtc_inb);
	io_connect_port(EGA_CRTC_INDEX,EGA_CRTC_ADAPTOR,IO_WRITE);
	io_connect_port(EGA_CRTC_DATA,EGA_CRTC_ADAPTOR,IO_READ_WRITE);

	/*
	 * Define Graphics Controller's ports
	 */

	ega_gc_outb_index_addr = (UTINY *) &graphics_controller.address;

	/*io_define_outb(EGA_GC_ADAP_INDEX,ega_gc_outb_index);*/
	io_define_out_routines(EGA_GC_ADAP_INDEX, ega_gc_outb_index, ega_gc_outw, NULL, NULL);

#ifndef CPU_40_STYLE	/* TEMPORARY */
	Cpu_define_outb(EGA_GC_ADAP_INDEX,_ega_gc_outb_index);
#endif

	io_define_outb(EGA_GC_ADAP_DATA,ega_gc_set_reset);
	Cpu_define_outb(EGA_GC_ADAP_DATA,NULL);
#ifndef A2CPU
	ega_gc_regs_cpu[8] = NULL;
#endif

	io_connect_port(EGA_GC_INDEX,EGA_GC_ADAP_INDEX,IO_WRITE);
	io_connect_port(EGA_GC_DATA,EGA_GC_ADAP_DATA,IO_WRITE);

	/*
	 * Define Attribute controller's ports
	 */

	io_define_outb(EGA_AC_ADAPTOR,ega_ac_outb);
	io_connect_port(EGA_AC_INDEX_DATA,EGA_AC_ADAPTOR,IO_WRITE);
	io_connect_port(EGA_AC_SECRET,EGA_AC_ADAPTOR,IO_WRITE);

	/*
	 * Define Miscellaneous register's port
	 */

	io_define_outb(EGA_MISC_ADAPTOR,ega_misc_outb);
	io_connect_port(EGA_MISC_REG,EGA_MISC_ADAPTOR,IO_WRITE);

	/*
	 * Define Feature controller's port
	 */

	io_define_outb(EGA_FEAT_ADAPTOR,ega_feat_outb);
	io_connect_port(EGA_FEAT_REG,EGA_FEAT_ADAPTOR,IO_WRITE);

	/*
	 * Define Input Status Register 0 port
	 */

	io_define_inb(EGA_IPSTAT0_ADAPTOR,ega_ipstat0_inb);
	io_connect_port(EGA_IPSTAT0_REG,EGA_IPSTAT0_ADAPTOR,IO_READ);

	/*
	 * Define Input Status Register 1 port
	 */

	io_define_inb(EGA_IPSTAT1_ADAPTOR,ega_ipstat1_inb);
	io_connect_port(EGA_IPSTAT1_REG,EGA_IPSTAT1_ADAPTOR,IO_READ);

	/*
	 * Initialise internals of EGA
	 * +++++++++++++++++++++++++++
	 */

	/* hardware reset sets Misc reg to 0, so.. */
	/* Perhaps this should be in 'ega_reset()'? */

	miscellaneous_output_register.as.abyte = 0;

	set_pc_pix_height(1); /* set by bit 7 of the misc reg */
	set_host_pix_height(1);

	/* Initialize address map */

	graphics_controller.miscellaneous.as.abyte = 0;
	graphics_controller.read_map_select.as_bfld.map_select = 0;

	/* Looking for bright white */

	graphics_controller.color_compare.as_bfld.color_compare = 0xf;

	/* All planes significant */

	graphics_controller.color_dont_care.as_bfld.color_dont_care = 0xf;

	/* Initialise crtc screen height fields and set screen height to be consistent */

	crt_controller.vertical_display_enable_end = 0;
	crt_controller.crtc_overflow.as_bfld.vertical_display_enab_end_bit_8 = 0;

	set_screen_height(0);

	init_vga_globals();

	EGA_CPU.fun_or_protection = 1;	/* assume complicated until we know it's easy */

	setVideobit_prot_mask(0xffffffff);

	ega_write_init();
	ega_read_init();
	ega_mode_init();	/* sets a flag in ega_mode.c to allow optimisation of mode changes without falling over */

	/*
	 * Some parts of input status register always return 1, so set fields accordingly
	 */
	input_status_register_zero.as.abyte = 0x7f ;

	/*
	 * set up some variables to get us going
	 * (They may have to be changed in the fullness of time)
	 */

	gvi_pc_low_regen  = CGA_REGEN_START;
	gvi_pc_high_regen = CGA_REGEN_END;

	choose_display_mode = choose_ega_display_mode;

	set_pix_width(1);
	set_pix_char_width(8);
	set_display_disabled(FALSE);

	set_char_height(8);
	set_screen_limit(0x8000);
	set_screen_start(0);
	set_word_addressing(YES);
	set_actual_offset_per_line(80);
	set_offset_per_line(160);	/* chained */
	set_horiz_total(80);	/* calc screen params from this and prev 3 */
	set_screen_split(511);	/* make sure there is no split screen to start with ! */

	set_prim_font_index(0);
	set_sec_font_index(0);

	set_regen_ptr(0,EGA_planes);

	/* prevent copyright message mysteriously disappearing */
	timer_video_enabled = TRUE;

}

void	ega_term IFN0()
{

int	index;

	note_entrance0("ega_term");

	/*
	 * Disconnect sequencer's ports
	 */

	io_disconnect_port(EGA_SEQ_INDEX,EGA_SEQ_ADAP_INDEX);
	io_disconnect_port(EGA_SEQ_DATA,EGA_SEQ_ADAP_DATA);

	/*
	 * Disconnect CRTC's ports
	 */

	io_disconnect_port(EGA_CRTC_INDEX,EGA_CRTC_ADAPTOR);
	io_disconnect_port(EGA_CRTC_DATA,EGA_CRTC_ADAPTOR);

	/*
	 * Disconnect Graphics Controller's ports
	 */

	io_disconnect_port(EGA_GC_INDEX,EGA_GC_ADAP_INDEX);
	io_disconnect_port(EGA_GC_DATA,EGA_GC_ADAP_DATA);

	/*
	 * Disconnect Attribute controller's ports
	 */

	io_disconnect_port(EGA_AC_INDEX_DATA,EGA_AC_ADAPTOR);
	io_disconnect_port(EGA_AC_SECRET,EGA_AC_ADAPTOR);

	/*
	 * Disconnect Miscellaneous register's port
	 */

	io_disconnect_port(EGA_MISC_REG,EGA_MISC_ADAPTOR);

	/*
	 * Disconnect Feature controller's port
	 */

	io_disconnect_port(EGA_FEAT_REG,EGA_FEAT_ADAPTOR);

	/*
	 * Disconnect Input Status Register 0 port
	 */

	io_disconnect_port(EGA_IPSTAT0_REG,EGA_IPSTAT0_ADAPTOR);

	/*
	 * Disconnect Input Status Register 1 port
	 */

	io_disconnect_port(EGA_IPSTAT1_REG,EGA_IPSTAT1_ADAPTOR);

	/*
	 * Free internals of EGA
	 */

	/* free the font files */
	for (index = 0; index < 4; index++)
		host_free_font(index);

	/* Disable CPU read processing */
	ega_read_term();
	ega_write_term();
}

LOCAL void	ega_seq_outb_index IFN2(io_addr, port, half_word, value)
{
#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_seq_outb_index(%x,%x)", port, value);
	assert1(value<5,"Bad seq index %d",value);
	NON_PROD(sequencer.address.as.abyte = value);
	io_redefine_outb(EGA_SEQ_ADAP_DATA,ega_seq_regs[value & 7]);
}

void	ega_seq_reset IFN2(io_addr, port, half_word, value)
{
#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_seq_reset(%x,%x)", port, value);
	/* change reset register */
	note_entrance0("reset register");
	sequencer.reset.as.abyte = value ;
	if (sequencer.reset.as_bfld.asynchronous_reset==0)
		set_bit_display_disabled(ASYNC_RESET); 
	else
		clear_bit_display_disabled(ASYNC_RESET);
	if (sequencer.reset.as_bfld.synchronous_reset==0)
		 set_bit_display_disabled(SYNC_RESET);
	else
		 clear_bit_display_disabled(SYNC_RESET);
}

void	ega_seq_clock IFN2(io_addr, port, half_word, value)
{
register int dot_clock;

#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_seq_clock(%x,%x)", port, value);
	/* clock mode register */
	dot_clock = sequencer.clocking_mode.as_bfld.dot_clock;
	sequencer.clocking_mode.as.abyte = value;
	if (sequencer.clocking_mode.as_bfld.dot_clock != dot_clock) {
		/*
		** Switch to/from double width pixels
		*/
		if (sequencer.clocking_mode.as_bfld.dot_clock==1) {
			set_pix_width(2);
			set_double_pix_wid(YES);
			set_pix_char_width(16);
		} else {
			set_pix_width(1);
			set_double_pix_wid(NO);
			set_pix_char_width(8);
		}
		flag_mode_change_required();
	}
}

void	ega_seq_map_mask IFN2(io_addr, port, half_word, value)
{
#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_seq_map_mask(%x,%x)", port, value);

	/* map mask register */

	/*
	 * Different display plane(s) have been enabled. Update the video
	 * routines to deal with this
	 */

	setVideoplane_enable(value & 0xf);
	setVideoplane_enable_mask(sr_lookup[value & 0xf]);
	write_state.pe = ((value & 0xf) == 0xf) ? 1 : 0;
	setVideowrstate(EGA_CPU.ega_state.mode_0.lookup);

	ega_write_routines_update(PLANES_ENABLED);
}

void	ega_seq_char_map IFN2(io_addr, port, half_word, value)
{
register int map_selects;

#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_seq_char_map(%x,%x)", port, value);
	/* char map select reg */
	map_selects = sequencer.character_map_select.character.map_selects;
	sequencer.character_map_select.as.abyte = value;
	if (sequencer.character_map_select.character.map_selects != map_selects)
	{
		/*
		** character mapping attributes have changed.
		**
		** If fonts selected are different bit 3 of attribute byte in alpha mode
		** selects which of the two fonts to use (giving 512 chars).
		*/

		EGA_GRAPH.attrib_font_select = (sequencer.character_map_select.as_bfld.character_map_select_a != 
						sequencer.character_map_select.as_bfld.character_map_select_b );
		set_prim_font_index(sequencer.character_map_select.as_bfld.character_map_select_a);
		set_sec_font_index(sequencer.character_map_select.as_bfld.character_map_select_b);

		host_select_fonts(get_prim_font_index(), get_sec_font_index());
		flag_mode_change_required();
	}
}

void	ega_seq_mem_mode IFN2(io_addr, port, half_word, value)
{
#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_seq_mem_mode(%x,%x)", port, value);

	/* mem mode register */

	sequencer.memory_mode.as.abyte = value ;

	/*
	** Decide alpha/graphics mode by voting
	 */
	vote_ega_mode();

	/*
	 * See if this causes a by-election for plane addressing
	 */

	do_chain_majority_decision();

	assert1(sequencer.memory_mode.as_bfld.extended_memory == 1,"Someone is trying to set extended memory to 0 (reg=%x)",value);
}

LOCAL void	ega_crtc_outb IFN2(io_addr, port, half_word, value)
{
	SHORT offset;
	struct {	/* avoid alignment problems with casts */
		unsigned value : 8;
	} new;
	static int old_underline_start;


	note_entrance2("ega_crtc_outb(%x,%x)", port, value);
	new.value = value;
	switch (port) {
		case 0x3d4:
			note_entrance1("New crtc index %d",value);
			crt_controller.address.as.abyte = value;
			break;
		case 0x3d5:
			note_entrance1( "Index %d", crt_controller.address.as_bfld.index );
			switch (crt_controller.address.as_bfld.index) {
				case 0:
					note_entrance0("horiz total");
					NON_PROD(crt_controller.horizontal_total = value);
					break;
				case 1:
					note_entrance0("horiz display end");
					crt_controller.horizontal_display_end = value+1;
					set_horiz_total(crt_controller.horizontal_display_end);
					break;
				case 2:
					note_entrance0("start horiz blank");
					NON_PROD(crt_controller.start_horizontal_blanking = value);
					break;
				case 3:
					note_entrance0("end horiz blank");
					NON_PROD(crt_controller.end_horizontal_blanking.as.abyte = value);
					break;
				case 4:
					note_entrance0("start horiz retrace");
					NON_PROD(crt_controller.start_horizontal_retrace = value);
					break;
				case 5:
					note_entrance0("end horiz retrace");
					NON_PROD(crt_controller.end_horizontal_retrace.as.abyte = value);
					break;
				case 6:
					note_entrance0("vert tot");
					NON_PROD(crt_controller.vertical_total = value);
					break;
				case 7:
					note_entrance0("overflow");
					if (crt_controller.crtc_overflow.as_bfld.vertical_display_enab_end_bit_8 !=
							((CRTC_OVERFLOW*)&new)->as_bfld.vertical_display_enab_end_bit_8)
					{
						/*
						 * Screen height changed
						 */

#ifdef VGG
					/*
					 * if VGG is set then the screen height
					 * definition is extended from 9 bits to
					 * 10. Thus the 9th bit is now a 'med'
					 * bit and not a 'hi' bit.
					 */
						set_screen_height_med_recal(
							((CRTC_OVERFLOW*)&new)->as_bfld.vertical_display_enab_end_bit_8 );
#else
						set_screen_height_hi_recal(
							((CRTC_OVERFLOW*)&new)->as_bfld.vertical_display_enab_end_bit_8 );
#endif
						flag_mode_change_required();
					}
					if (crt_controller.crtc_overflow.as_bfld.line_compare_bit_8 != 
						((CRTC_OVERFLOW*)&new)->as_bfld.line_compare_bit_8)
					{
						/*
						 * split screen height changed
						 */

						EGA_GRAPH.screen_split.as_bfld.top_bit = 
								((CRTC_OVERFLOW*)&new)->as_bfld.line_compare_bit_8;

						if( !get_split_screen_used() )
							flag_mode_change_required();

						screen_refresh_required();
					}
					crt_controller.crtc_overflow.as.abyte = value;
					break;
				case 8:
					note_entrance0("preset row scan");
					NON_PROD(crt_controller.preset_row_scan.as.abyte = value);
					break;
				case 9:
					note_entrance0("max scan line");
					if (crt_controller.maximum_scan_line.as_bfld.maximum_scan_line != 
						((MAX_SCAN_LINE*)&new)->as_bfld.maximum_scan_line)
					{
						set_char_height_recal(
							(((MAX_SCAN_LINE*)&new)->as_bfld.maximum_scan_line)+1);
						do_new_cursor();
					}
					crt_controller.maximum_scan_line.as.abyte = value;
					break;
				case 10:
					note_entrance0("cursor start");
					if (crt_controller.cursor_start.as_bfld.cursor_start != 
						((CURSOR_START*)&new)->as_bfld.cursor_start)
					{
						crt_controller.cursor_start.as.abyte = value;
						do_new_cursor();
					}
					break;
				case 11:
					note_entrance0("cursor end");
					if (crt_controller.cursor_end.as_bfld.cursor_end !=
						((CURSOR_END*)&new)->as_bfld.cursor_end)
					{
						crt_controller.cursor_end.as.abyte = value;
						assert0(crt_controller.cursor_end.as_bfld.cursor_skew_control == 0,
								"Someone is trying to use cursor skew");
						do_new_cursor();
					}
					break;
				case 12:
					note_entrance0("start address high");
					if (crt_controller.start_address_high != value)
					{
						set_screen_start((value << 8) + crt_controller.start_address_low);
						host_screen_address_changed(crt_controller.start_address_high,
									crt_controller.start_address_low);
						/* check if it wraps now */
						if ( get_memory_chained() ) {
							if( (get_screen_start()<<1) + get_screen_length() > 2*EGA_PLANE_SZ )
								choose_ega_display_mode();
						}
						else {
							if( get_screen_start() + get_screen_length() > EGA_PLANE_SZ )
								choose_ega_display_mode();
						}
						screen_refresh_required();
					}
					crt_controller.start_address_high = value;
					break;
				case 13:
					note_entrance0("start address low");
					if (crt_controller.start_address_low != value)
					{
						set_screen_start((crt_controller.start_address_high << 8) + value);
						host_screen_address_changed(crt_controller.start_address_high,
									crt_controller.start_address_low);
						/* check if it wraps now */
						if ( get_memory_chained() ) {
							if( (get_screen_start()<<1) + get_screen_length() > 2*EGA_PLANE_SZ )
								choose_ega_display_mode();
						}
						else {
							if( get_screen_start() + get_screen_length() > EGA_PLANE_SZ )
								choose_ega_display_mode();
						}
						screen_refresh_required();
					}
					crt_controller.start_address_low = value;
					break;
				case 14:
					note_entrance0("cursor loc high");
					if (crt_controller.cursor_location_high != value)
					{
						crt_controller.cursor_location_high = value;

						offset = (value<<8) | crt_controller.cursor_location_low;
						offset -= get_screen_start();

						set_cur_x(offset % crt_controller.horizontal_display_end);
						set_cur_y(offset / crt_controller.horizontal_display_end);

						do_new_cursor();

						if(!get_mode_change_required() && is_it_text())
							cursor_changed( get_cur_x(), get_cur_y());
					}
					break;
				case 15:
					note_entrance0("cursor loc lo");
					if (crt_controller.cursor_location_low != value)
					{
						crt_controller.cursor_location_low = value;

						offset = value | (crt_controller.cursor_location_high<<8);
						offset -= get_screen_start();

						set_cur_x(offset % crt_controller.horizontal_display_end);
						set_cur_y(offset / crt_controller.horizontal_display_end);

						do_new_cursor();

						if(!get_mode_change_required() && is_it_text())
							cursor_changed( get_cur_x(), get_cur_y());
					}
					break;
				case 16:
					note_entrance0("vert retrace start");
					NON_PROD(crt_controller.vertical_retrace_start = value);
					break;
				case 17:
					note_entrance0("vert retrace end");
					crt_controller.vertical_retrace_end.as.abyte = value;
                                        if ((value & 32) == 32)
                                           ega_int_enable = 0;
                                        else
                                           ega_int_enable = 1;
                                        if ((value & 16) != 16)
                                        {
                                           ica_clear_int(AT_EGA_VTRACE_ADAPTER,AT_EGA_VTRACE_INT);
                                           /*
                                            * clear status latch
                                            */
                                           input_status_register_zero.as_bfld.crt_interrupt = 0;        /* = !VS */
                                        }
			/* ??? */
					break;
				case 18:
					note_entrance0("vert disp enable end");
					if (crt_controller.vertical_display_enable_end != value)
					{
						crt_controller.vertical_display_enable_end = value;
						set_screen_height_lo_recal(value);
					}
					break;
				case 19:
					note_entrance0("offset");
					if (crt_controller.offset != value)
					{
						crt_controller.offset = value;
						set_actual_offset_per_line(value<<1);	/* actual offset into plane in bytes */
						flag_mode_change_required();
					}
					break;
				case 20:
					note_entrance0("underline loc");
					crt_controller.underline_location.as.abyte = value;
					if( value != old_underline_start )
					{
						old_underline_start = value;
						set_underline_start(
				crt_controller.underline_location.as_bfld.underline_location);
						screen_refresh_required();
					}
					break;
				case 21:
					note_entrance0("start vert blank");
					NON_PROD(crt_controller.start_vertical_blanking = value);
					break;
				case 22:
					note_entrance0("end vert blank");
					NON_PROD(crt_controller.end_vertical_blanking = value);
					break;
				case 23:
					note_entrance0("mode control");
					if (crt_controller.mode_control.as_bfld.compatibility_mode_support != 
						((MODE_CONTROL*)&new)->as_bfld.compatibility_mode_support)
					{
						if ( (((MODE_CONTROL*)&new)->as_bfld.compatibility_mode_support) == 0)
							set_cga_mem_bank(YES);
						else	set_cga_mem_bank(NO);
						flag_mode_change_required();
					}
					if (crt_controller.mode_control.as_bfld.word_or_byte_mode !=
						((MODE_CONTROL*)&new)->as_bfld.word_or_byte_mode)
					{
						set_word_addressing_recal(
							(((MODE_CONTROL*)&new)->as_bfld.word_or_byte_mode) == 0 );
					}
					crt_controller.mode_control.as.abyte = value;
					assert0(crt_controller.mode_control.as_bfld.select_row_scan_counter == 1,"Row scan 0");
					assert0(crt_controller.mode_control.as_bfld.horizontal_retrace_select == 0,
														"retrace select 1");
					assert0(crt_controller.mode_control.as_bfld.output_control == 0,"output control set");
					assert0(crt_controller.mode_control.as_bfld.hardware_reset == 1,"hardware reset cleared");
					break;
				case 24:
					note_entrance0("line compare reg");
					if (crt_controller.line_compare != value)
					{
						crt_controller.line_compare = value;
						EGA_GRAPH.screen_split.as_bfld.low_byte = value;

						if( !get_split_screen_used() )
							flag_mode_change_required();

						screen_refresh_required();

					}
					break;
				default:
					assert1(NO,"Bad crtc index %d",crt_controller.address.as_bfld.index);
					break;
			}
			break;
		default:
			assert1(NO,"Bad port passed %x", port );
			break;
	}
}

LOCAL void	ega_crtc_inb IFN2(io_addr, port, half_word *, value)
{
#ifdef PROD
	UNUSED(port);
#endif
	note_entrance3("ega_crtc_inb(%x,%x) index %d", port, value, crt_controller.address.as_bfld.index);
	switch(crt_controller.address.as_bfld.index) {
		case	10:
			*value = crt_controller.cursor_start.as.abyte ;
			note_entrance1("cursor start %d",*value);
			break;
		case	11:
			*value = crt_controller.cursor_end.as.abyte ;
			note_entrance1("cursor end %d",*value);
			break;
		case	12:
			*value = crt_controller.start_address_high ;
			note_entrance1("start address high %x",*value);
			break;
		case	13:
			*value = crt_controller.start_address_low ;
			note_entrance1("start address low %x",*value);
			break;
		case	14:
			*value = crt_controller.cursor_location_high ;
			note_entrance1("cursor location high %x",*value);
			break;
		case	15:
			*value = crt_controller.cursor_location_low ;
			note_entrance1("cursor location low %x",*value);
			break;
		case	16:
			*value = 0;	/* light pen high */
			note_entrance1("light pen high %x",*value);
			break;
		case	17:
			*value = 0;	/* light pen low */
			note_entrance1("light pen low %x",*value);
			break;
		default:
			assert1(crt_controller.address.as_bfld.index>24,"inb from bad crtc index %d",crt_controller.address.as_bfld.index);
			*value = IO_EMPTY_PORT_BYTE_VALUE;
			break;
	}
}


void	ega_gc_outb_index IFN2(io_addr, port, half_word, value)
{
#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_gc_outb_index(%x,%x)", port, value);
	NON_PROD(graphics_controller.address.as.abyte = value);
	assert1(value<9,"Bad gc index %d",value);

	io_redefine_outb(EGA_GC_ADAP_DATA,ega_gc_regs[value & 15]);
	Cpu_define_outb(EGA_GC_ADAP_DATA,ega_gc_regs_cpu[value & 15]);
}


/**/


/*( ega_gc_outw
**	Most PC programs do an "OUT DX, AX" which sets up the GC index
**	register with the AL and the GC data register with AH.
**	Avoid going through generic_outw() by doing it all here!
)*/
GLOBAL VOID ega_gc_outw IFN2(io_addr, port, word, outval)
{
	reg     temp;
	INT		value;

	temp.X = outval;
	value = temp.byte.low;

	NON_PROD(graphics_controller.address.as.abyte = value);

	assert1(value<9,"Bad gc index %#x", value);

	value &= 15;

	io_redefine_outb(EGA_GC_ADAP_DATA,ega_gc_regs[value]);
	Cpu_define_outb(EGA_GC_ADAP_DATA,ega_gc_regs_cpu[value]);

	(*(ega_gc_regs[value]))(port+1, temp.byte.high);
}


/**/


void	ega_gc_set_reset IFN2(io_addr, port, half_word, value)
{
register int set_reset;

#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_gc_set_reset(%x,%x)", port, value);

	set_reset = graphics_controller.set_or_reset.as_bfld.set_or_reset;
	graphics_controller.set_or_reset.as.abyte = value;

	if (graphics_controller.set_or_reset.as_bfld.set_or_reset != set_reset)
	{
		EGA_CPU.set_reset = graphics_controller.set_or_reset.as_bfld.set_or_reset;
		ega_write_routines_update(SET_RESET);
	}
}

void	ega_gc_enable_set IFN2(io_addr, port, half_word, value)
{
register int en_set_reset;

#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_gc_enable_set(%x,%x)", port, value);

	en_set_reset = graphics_controller.enable_set_or_reset.as_bfld.enable_set_or_reset;
	graphics_controller.enable_set_or_reset.as.abyte = value;

	if (graphics_controller.enable_set_or_reset.as_bfld.enable_set_or_reset != en_set_reset)
	{
		EGA_CPU.sr_enable = graphics_controller.enable_set_or_reset.as_bfld.enable_set_or_reset;
		write_state.sr = graphics_controller.enable_set_or_reset.as_bfld.enable_set_or_reset==0?0:1;
		setVideowrstate(EGA_CPU.ega_state.mode_0.lookup);
		ega_write_routines_update(ENABLE_SET_RESET);
	}
}

void	ega_gc_compare IFN2(io_addr, port, half_word, value)
{
register int colour_compare;

#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_gc_compare(%x,%x)", port, value);
	colour_compare = graphics_controller.color_compare.as_bfld.color_compare;
	graphics_controller.color_compare.as.abyte = value;
	if (graphics_controller.color_compare.as_bfld.color_compare != colour_compare)
	{
		read_state.colour_compare = graphics_controller.color_compare.as_bfld.color_compare;
		if (read_state.mode == 1) ega_read_routines_update();
	}
}

void	ega_gc_rotate IFN2(io_addr, port, half_word, value)
{
	struct {
		unsigned value : 8;
	} new;

#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_gc_rotate(%x,%x)", port, value);
	note_entrance0("data rotate");
	new.value = value;
	if (graphics_controller.data_rotate.as_bfld.rotate_count != ((DATA_ROTATE*)&new)->as_bfld.rotate_count )
	{
		setVideorotate(((DATA_ROTATE*)&new)->as_bfld.rotate_count);
		ega_write_routines_update(ROTATION);
	}

	if (graphics_controller.data_rotate.as_bfld.function_select != ((DATA_ROTATE*)&new)->as_bfld.function_select)
	{
		write_state.func = ((DATA_ROTATE*)&new)->as_bfld.function_select;
		setVideowrstate(EGA_CPU.ega_state.mode_0.lookup);
		ega_write_routines_update(FUNCTION);
	}
	EGA_CPU.fun_or_protection = (value != 0) || write_state.bp;
	graphics_controller.data_rotate.as.abyte = value;
}

void	ega_gc_read_map IFN2(io_addr, port, half_word, value)
{
#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_gc_read_map(%x,%x)", port, value);

	setVideoread_mapped_plane(value & 3);

	update_shift_count();
}

void	ega_gc_mode IFN2(io_addr, port, half_word, value)
{
MODE new_mode;

#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_gc_set_reset(%x,%x)", port, value);
	new_mode.as.abyte = value;
	if (graphics_controller.mode.as_bfld.write_mode != new_mode.as_bfld.write_mode)
	{
		/*
		 * write mode change
		 */

		EGA_CPU.write_mode = new_mode.as_bfld.write_mode;
		setVideowrmode(EGA_CPU.write_mode);
		ega_write_routines_update(WRITE_MODE);
	}

	if (graphics_controller.mode.as_bfld.read_mode != new_mode.as_bfld.read_mode)
	{
		/*
		 * read mode change 
		 */
		read_state.mode = new_mode.as_bfld.read_mode;
		ega_read_routines_update();
	}

	if (graphics_controller.mode.as_bfld.shift_register_mode != new_mode.as_bfld.shift_register_mode)
	{
		/*
		 * going to/from one cga graphics mode to another
		 */
		set_graph_shift_reg(new_mode.as_bfld.shift_register_mode);
		flag_mode_change_required();
	}

	graphics_controller.mode.as.abyte = new_mode.as.abyte;

	/*
	 * Check for any change to chained mode rule by having an election
	 * (Note: EGA registers must be updated before calling election)
	 */

	do_chain_majority_decision();

	assert0(graphics_controller.mode.as_bfld.test_condition == 0,"Test conditon set");
}

void	ega_gc_misc IFN2(io_addr, port, half_word, value)
{
register int memory_map;

#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_gc_misc(%x,%x)", port, value);
	memory_map = graphics_controller.miscellaneous.as_bfld.memory_map;
	graphics_controller.miscellaneous.as.abyte = value;
	if (graphics_controller.miscellaneous.as_bfld.memory_map != memory_map)
	{
		/*
		 * Where EGA appears in PC memory space changed.
		*/
		if (miscellaneous_output_register.as_bfld.enable_ram)
			sas_disconnect_memory(gvi_pc_low_regen,gvi_pc_high_regen);

		switch (graphics_controller.miscellaneous.as_bfld.memory_map)
		{
			case 0:
				gvi_pc_low_regen = 0xA0000;
				gvi_pc_high_regen = 0xBFFFF;
				break;
			case 1:
				gvi_pc_low_regen = 0xA0000;
				gvi_pc_high_regen = 0xAFFFF;
				break;
			case 2:
				gvi_pc_low_regen = 0xB0000;
				gvi_pc_high_regen = 0xB7FFF;
				break;
			case 3:
				gvi_pc_low_regen = 0xB8000;
				gvi_pc_high_regen = 0xBFFFF;
				break;
		}

		if (miscellaneous_output_register.as_bfld.enable_ram)
			sas_connect_memory(gvi_pc_low_regen,gvi_pc_high_regen,(half_word)SAS_VIDEO);

		/*
		 * Tell cpu associated modules that regen area has moved
		 */

		ega_read_routines_update();
		ega_write_routines_update(RAM_MOVED);
	}

	/*
 	** Vote on alpha/graphics mode.
	 */
	vote_ega_mode();

	/*
	 * Check for any change to chained mode rule by having an election
	 * (Note: EGA registers must be updated before calling election)
	 */

	do_chain_majority_decision();
}

void	ega_gc_dont_care IFN2(io_addr, port, half_word, value)
{
register int colour_dont_care;

#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_gc_dont_care(%x,%x)", port, value);
	colour_dont_care = graphics_controller.color_dont_care.as_bfld.color_dont_care;
	graphics_controller.color_dont_care.as.abyte = value;
	if (graphics_controller.color_dont_care.as_bfld.color_dont_care != colour_dont_care)
	{
		read_state.colour_dont_care = graphics_controller.color_dont_care.as_bfld.color_dont_care;
		if (read_state.mode == 1) ega_read_routines_update();
	}
}

/*
 * The EGA mask register is written to more times than all other ports added together! 
 * To help make this register fast, we have two different routines to handle it:
 * ega_gc_mask for when the register's current value is not 0xFF, ie. masking is active
 * ega_gc_mask_ff for when the mask register = 0xFF, so masking is disabled.
 */

/*(
** ega_mask_register_changed
**	This gets called whenever the mask register gets changed, and
**	updates the internals appropriately. Since the mask registers
**	are hit more than any other registers, this should do the job!
**
**	Rather than calling the monster ega_write_routines_update() (in "ega_write.c"),
**	we do as little as we possibly can here!
**	In particular, all we do is set the video write pointer handlers
**	to the appropriate one and update the internal EGA_CPU state...
**
**	We DON'T do anything about altering the marking funcs, etc.
**
**	See also "vga_mask_register_changed" in "vga_ports.c"
**
**	NB: GLOBAL for JOKER.
**
)*/
#include	"cpu_vid.h"

GLOBAL VOID ega_mask_register_changed IFN1(BOOL, gotBitProtection)
{
	ULONG				state;
	SAVED IU8			masks[] = {0x1f, 0x01, 0x0f, 0x0f};
	IMPORT WRT_POINTERS	*mode_chain_handler_table[];
#ifdef V7VGA
	IMPORT	UTINY		Last_v7_fg_bg, fg_bg_control;
#endif

	write_state.bp = gotBitProtection;
	setVideowrstate(EGA_CPU.ega_state.mode_0.lookup);
	EGA_CPU.fun_or_protection = (gotBitProtection || (graphics_controller.data_rotate.as.abyte != 0));

	/* Check that we're not trying to handle any pathological cases here...
	** This means we chicken out for Chain2 and V7VGA dithering.
	*/

	if ((EGA_CPU.chain == CHAIN2)
#ifdef V7VGA
		|| ( Last_v7_fg_bg != fg_bg_control)
#endif /* V7VGA */
		)
	{
		ega_write_routines_update(BIT_PROT);

		return;
	}

	/* the "mode_0" union variant has the largest "lookup" field (5 bits.) */

	state = EGA_CPU.ega_state.mode_0.lookup & masks[EGA_CPU.write_mode];

#ifdef A3CPU
#ifdef C_VID
	Glue_set_vid_wrt_ptrs(&mode_chain_handler_table[EGA_CPU.saved_mode_chain][state]);
#else
	Cpu_set_vid_wrt_ptrs(&mode_chain_handler_table[EGA_CPU.saved_mode_chain][state]);	
#endif /* C_VID */
#else
#if !(defined(NTVDM) && defined(MONITOR))
	Glue_set_vid_wrt_ptrs(&mode_chain_handler_table[EGA_CPU.saved_mode_chain][state]);
#endif /* !(NTVDM && MONITOR) */
#endif /* A3CPU */

	EGA_CPU.saved_state = state;
}


/**/


/* ega_gc_mask is the one that is usually called */

LOCAL void	ega_gc_mask IFN2(io_addr, port, half_word, value)
{
	register unsigned int mask;

#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_gc_mask(%x,%x)", port, value);

	/*
	 * Update video routine according to new bit protection
	 */

	mask = value | (((USHORT)value) << 8);
	mask |= (mask << 16);	/* replicate the mask into 4 bytes */
	setVideobit_prot_mask(mask);
	setVideodata_xor_mask(~(EGA_CPU.calc_data_xor & mask));
	setVideolatch_xor_mask(EGA_CPU.calc_latch_xor & mask);
	if( value == 0xff )
	{
#ifndef	USE_OLD_MASK_CODE
		ega_mask_register_changed(/*bit protection :=*/0);
#else
		write_state.bp = 0;
		setVideowrstate(EGA_CPU.ega_state.mode_0.lookup);
		EGA_CPU.fun_or_protection = (graphics_controller.data_rotate.as.abyte != 0);
		ega_write_routines_update(BIT_PROT);
#endif	/* USE_OLD_MASK_CODE */

		/* Alter the function table used by ega_gc_index */
		ega_gc_regs[8] = ega_gc_mask_ff;

#ifndef CPU_40_STYLE	/* TEMPORARY */
#ifndef A2CPU
		/* Alter the function table used by assembler ega_gc_index */
		ega_gc_regs_cpu[8] = _ega_gc_outb_mask_ff;
#endif
#endif

		io_redefine_outb(EGA_GC_ADAP_DATA,ega_gc_mask_ff);

#ifndef CPU_40_STYLE	/* TEMPORARY */
		Cpu_define_outb(EGA_GC_ADAP_DATA,_ega_gc_outb_mask_ff);
#endif
	}
}

/* This version isn't called so often */
void	ega_gc_mask_ff IFN2(io_addr, port, half_word, value)
{
	register unsigned int mask;

#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_gc_mask(%x,%x)", port, value);

	/*
	 * Update video routine according to new bit protection
	 */

	if(value != 0xff)
	{
		mask = value | (((USHORT)value) << 8);
		mask |= (mask << 16);	/* replicate the mask into 4 bytes */
		setVideobit_prot_mask(mask);
		setVideodata_xor_mask(~(EGA_CPU.calc_data_xor & mask));
		setVideolatch_xor_mask(EGA_CPU.calc_latch_xor & mask);
#ifndef	USE_OLD_MASK_CODE
		ega_mask_register_changed(/*bit protection :=*/1);
#else
		write_state.bp = 1;
		setVideowrstate(EGA_CPU.ega_state.mode_0.lookup);
		EGA_CPU.fun_or_protection = TRUE;
		ega_write_routines_update(BIT_PROT);
#endif	/* USE_OLD_MASK_CODE*/

		/* Alter the function table used by ega_gc_index */
		ega_gc_regs[8] = ega_gc_mask;

#ifndef CPU_40_STYLE	/* TEMPORARY */
#ifndef A2CPU
		/* Alter the function table used by assembler ega_gc_index */
		ega_gc_regs_cpu[8] = _ega_gc_outb_mask;
#endif
#endif

		io_redefine_outb(EGA_GC_ADAP_DATA,ega_gc_mask);

#ifndef CPU_40_STYLE	/* TEMPORARY */
		Cpu_define_outb(EGA_GC_ADAP_DATA,_ega_gc_outb_mask);
#endif
	}
}

LOCAL void	ega_index_invalid IFN2(io_addr, port, half_word, value)
{
#ifdef PROD
	UNUSED(port);
	UNUSED(value);
#endif
	note_entrance2("ega_index_invalid(%x,%x)", port, value);
	assert1(NO,"Invalid index %d",graphics_controller.address.as_bfld.index);
}

LOCAL void	ega_ac_outb IFN2(io_addr, port, half_word, value)
{
	struct {
		unsigned value : 8;
	} new;

#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_ac_outb(%x,%x)", port, value);
	assert1( port == EGA_AC_INDEX_DATA || port == EGA_AC_SECRET, "Bad port %x", port);
	new.value = value;
	if ( in_index_state() ) {
		note_entrance1("Setting index to %d", value);
		attribute_controller.address.as.abyte = value;
	} else {
		switch (attribute_controller.address.as_bfld.index) {
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
		case 11:
		case 12:
		case 13:
		case 14:
		case 15:
/*
 *		A real EGA monitor behaves in a strange way:
 *		When it is in 200 scan line mode (vertical_retrace_polarity = 0)
 *		it emulates a CGA monitor - not just in screen resolution, but also
 *		in the way it inteprets the colour signals:
 *		Instead of having 6 colour signals: RGBrgb,
 *		it has 4, RGBI. The Intensity signal is on the same input pin as the secondary green signal.
 */
			note_entrance1("Change palette %d",attribute_controller.address.as_bfld.index);
			attribute_controller.palette[attribute_controller.address.as_bfld.index].as.abyte = value;
			if(miscellaneous_output_register.as_bfld.vertical_retrace_polarity)
			{
				EGA_GRAPH.palette[attribute_controller.address.as_bfld.index].red = 
										get_palette_color(red,secondary_red);
				EGA_GRAPH.palette[attribute_controller.address.as_bfld.index].green = 
										get_palette_color(green,secondary_green);
				EGA_GRAPH.palette[attribute_controller.address.as_bfld.index].blue = 
										get_palette_color(blue,secondary_blue);
			}
			else
			{
				/* Interpret secondary_green as intensity */
				EGA_GRAPH.palette[attribute_controller.address.as_bfld.index].red = 
										get_palette_color(red,secondary_green);
				EGA_GRAPH.palette[attribute_controller.address.as_bfld.index].green = 
										get_palette_color(green,secondary_green);
				EGA_GRAPH.palette[attribute_controller.address.as_bfld.index].blue = 
										get_palette_color(blue,secondary_green);
			}
			host_set_palette(EGA_GRAPH.palette,EGA_PALETTE_SIZE);
			break;
		case 16:
			note_entrance0("mode control reg");
			if (attribute_controller.mode_control.as_bfld.background_intensity_or_blink !=
				((AC_MODE_CONTROL*)&new)->as_bfld.background_intensity_or_blink)
			{
				set_intensity( ((AC_MODE_CONTROL*)&new)->as_bfld.background_intensity_or_blink );
			}

			attribute_controller.mode_control.as.abyte = value;

			if (attribute_controller.mode_control.as_bfld.background_intensity_or_blink)
				/* blinking - not supported */
				bg_col_mask = 0x70;
			else
				/* using blink bit to provide 16 background colours */
				bg_col_mask = 0xf0;

			/*
			 ** Vote on alpha/graphics mode
			 */
			 vote_ega_mode();
			assert0(attribute_controller.mode_control.as_bfld.display_type == 0, "Mono display selected");
			assert0(attribute_controller.mode_control.as_bfld.enable_line_graphics_char_codes == 0, 
											"line graphics enabled");
			break;
		case 17:
			note_entrance0("set border");
			attribute_controller.overscan_color.as.abyte = value;
			EGA_GRAPH.border[RED] = get_border_color(red_border,secondary_red_border);
			EGA_GRAPH.border[GREEN] = get_border_color(green_border,secondary_green_border);
			EGA_GRAPH.border[BLUE] = get_border_color(blue_border,secondary_blue_border);
			host_set_border_colour(value);
			break;
		case 18:
			note_entrance1("color plane enable %x",value);
			if ( attribute_controller.color_plane_enable.as_bfld.color_plane_enable !=
					((COLOR_PLANE_ENABLE*)&new)->as_bfld.color_plane_enable ) {
				set_plane_mask(((COLOR_PLANE_ENABLE*)&new)->as_bfld.color_plane_enable);
				host_change_plane_mask(get_plane_mask());	/* Update Host palette */
			}
			attribute_controller.color_plane_enable.as.abyte = value;
			break;
		case 19:
			note_entrance0("horiz pel panning");
			NON_PROD(attribute_controller.horizontal_pel_panning.as.abyte = value);
			break;
		default:
			assert1(NO,"Bad ac index %d", attribute_controller.address.as_bfld.index);
			break;
		}
	}
}

LOCAL void	ega_misc_outb IFN2(io_addr, port, half_word, value)
{
MISC_OUTPUT_REG new;

#ifdef PROD
	UNUSED(port);
#endif
	note_entrance2("ega_misc_outb(%x,%x)", port, value);
	assert1(port==EGA_MISC_REG,"Bad port %x",port);
	new.as.abyte = value;
	if (miscellaneous_output_register.as_bfld.enable_ram != new.as_bfld.enable_ram)
	{
		/*
		 * writes to plane memory en/disabled
		 */

		note_entrance0("Ram enabled");
		if(new.as_bfld.enable_ram)
			sas_connect_memory(gvi_pc_low_regen,gvi_pc_high_regen,(half_word)SAS_VIDEO);
		else
			sas_disconnect_memory(gvi_pc_low_regen,gvi_pc_high_regen);

		EGA_CPU.ram_enabled = new.as_bfld.enable_ram;
		ega_read_routines_update();
		ega_write_routines_update(RAM_ENABLED);
	}

	if (miscellaneous_output_register.as_bfld.vertical_retrace_polarity != 
		new.as_bfld.vertical_retrace_polarity)
	{
		/*
		 * Going to/from CGA monitor compatibility mode
		 * if this bit is set, it means that the pixels are 'stretched' vertically.
		 */

		set_pc_pix_height( new.as_bfld.vertical_retrace_polarity ? 1 : 2);
		flag_mode_change_required();
	}

	miscellaneous_output_register.as.abyte = new.as.abyte;

	set_bit_display_disabled(miscellaneous_output_register.as_bfld.disable_internal_video_drivers ? VIDEO_DRIVERS_DISABLED : 0);

	/*
	 * register value used by ipsr0 to find out the index into the switches
	 * so that correct switch setting can be returned.
	 */
}

LOCAL void	ega_feat_outb IFN2(io_addr, port, half_word, value)
{
#ifdef PROD
	UNUSED(port);
	UNUSED(value);
#endif
	note_entrance2("ega_feat_outb(%x,%x)", port, value);
	NON_PROD(feature_control_register.as.abyte = value);
}

LOCAL void	ega_ipstat0_inb IFN2(io_addr, port, half_word *, value)
{
#ifdef PROD
	UNUSED(port);
#endif
	note_entrance1("ega_ipstat0_inb(%x)", port);
	input_status_register_zero.as_bfld.switch_sense = 
	
	/* The following function call used to pass the argument
	** miscellaneous_output_register.as_bfld.clock_select
	** but the function expects no argument so it was removed.
	*/
	get_ega_switch_setting();
	*value = input_status_register_zero.as.abyte;
	note_entrance1("returning %x",*value);
}

LOCAL void	ega_ipstat1_inb IFN2(io_addr, port, half_word *, value)
{

	/*
	 * The whole of this routine has been nicked from the cga without modification
	 * The s_lengths array should probably be altered for the ega timings, and somewhere
	 * an interrupt should be fired off.
	 */

	static int ega_state = 0;	/* current ega status state */
	static int state_count = 1;     /* position in that state */
	static int sub_state = 0;	/* sub state for ega state 2 */

     static unsigned long gmfudge = 17; /* Random number seed for pseudo-random
                                           bitstream generator to give the state                                           lengths below that 'genuine' feel to
                                           progs that require it! */
     register unsigned long h;

    /*
     * relative 'lengths' of each state. State 2 is *3 as it has 3 sub states
     */

	static int s_lengths[] = { 8, 18, 8, 6 };

    /*
     * Status register, simulated adapter has
     *
     *	bit			setting
     *	---			-------
     *	Display enable		   1/0 Toggling each inb
     *	Light Pen		   0
     *	Light Pen		   0
     * 	Vertical Sync		   1/0 Toggling each inb
     *	4-7 Unused		   0,0,0,0
     *
     * The upper nibble of the byte is always set.
     * Some programs synchronise with the display by waiting for the
     * next vertical retrace.
     *
     * We attempt to follow the following waveform
     *
     *    --                                                     ----------
     * VS  |_____________________________________________________|        |____
     *
     *
     *    -------------  -   -                           ------------------
     * DE             |__||__||__ ... about 180         _|
     *
     *State|--- 0 ----|-------------- 1 -----------------|-- 3 --|-- 4 --|
     *
     * We do this with a 4 state machine. Each state has a count associated
     * with it to represent the relative time spent in each state. When this
     * count is exhausted the machine moves into the next state. One Inb 
     * equals 1 count. The states are as follows:
     *     0: VS low, DE high.
     *     1: VS low, DE toggles. This works via an internal state.
     *     3: VS low, DE high.
     *     4: VS high,DE high.
     *
     */

#ifdef PROD
	UNUSED(port);
#endif
	note_entrance1("ega_ipstat1_inb(%x)", port);
    note_entrance2("ega_ipstat1_inb(%x,%x)", port, value);

    set_index_state();	/* Initialize the Attribute register flip-flop (EGA tech ref, p 56) */

    state_count --;	/* attempt relative 'timings' */
    switch (ega_state) {

    case 0:
	if (state_count == 0) {	/* change to next state ? */
            h = gmfudge << 1;
            gmfudge = (h&0x80000000) ^ (gmfudge & 0x80000000)? h|1 : h;
	    state_count = s_lengths[1] + (gmfudge & 3);
	    ega_state = 1;
	}
	input_status_register_zero.as_bfld.crt_interrupt = 1;	/* = !VS */
	*value = 0xf1;
	break;

    case 1:
	if (state_count == 0) {	/* change to next state ? */
            h = gmfudge << 1;
            gmfudge = (h&0x80000000) ^ (gmfudge & 0x80000000)? h|1 : h;
	    state_count = s_lengths[2] + (gmfudge & 3);
	    ega_state = 2;
	    sub_state = 2;
	}
	switch (sub_state) {	/* cycle through 0,0,1 sequence */
	case 0:		/* to represent DE toggling */
	    *value = 0xf0;
	    sub_state = 1;
	    break;
	case 1:
	    *value = 0xf0;
	    sub_state = 2;
	    break;
	case 2:
	    *value = 0xf1;
	    sub_state = 0;
	    break;
        }
	input_status_register_zero.as_bfld.crt_interrupt = 1;	/* = !VS */
	break;

    case 2:
	if (state_count == 0) {	/* change to next state ? */
            h = gmfudge << 1;
            gmfudge = (h&0x80000000) ^ (gmfudge & 0x80000000)? h|1 : h;
	    state_count = s_lengths[3] + (gmfudge & 3);
	    ega_state = 3;
	}
	*value = 0xf1;
	input_status_register_zero.as_bfld.crt_interrupt = 1;	/* = !VS */
	break;

    case 3:
	if (state_count == 0) {	/* wrap back to first state */
            h = gmfudge << 1;
            gmfudge = (h&0x80000000) ^ (gmfudge & 0x80000000)? h|1 : h;
	    state_count = s_lengths[0] + (gmfudge & 3);
	    ega_state = 0;
	}
	input_status_register_zero.as_bfld.crt_interrupt = 0;	/* = !VS */
	*value = 0xf9;
	break;
    }
    note_entrance1("returning %x",*value);
}

LOCAL	void	vote_ega_mode IFN0()
{
	static	int	old_votes = 3;
	int	votes;

	votes = sequencer.memory_mode.as_bfld.alpha_mode ? 0 : 1;
	votes += graphics_controller.miscellaneous.as_bfld.graphics_mode;
	votes += attribute_controller.mode_control.as_bfld.graphics_mode; 
	assert1( votes == 3 || votes == 0, "Headline: Mode government returned with small majority %d", votes);
	if ((old_votes < 2) && (votes >= 2))
	{
		/* change to graphics mode */
		set_text_mode(NO);
		flag_mode_change_required();
	}
	else if ((old_votes >= 2) && (votes < 2))
	{
		/* change to text mode */
		set_text_mode(YES);
		flag_mode_change_required();
	}
	old_votes = votes;
}

#ifdef HUNTER

/* Get line compare value */

int ega_get_line_compare IFN0()

    {
    int		return_value;

    return_value = crt_controller.line_compare;
    if (crt_controller.crtc_overflow.as_bfld.line_compare_bit_8 != 0)
	return_value += 0x100;
    return (return_value);
    }			/* ega_get_line_compare */

/* Get maximum scan lines value */

int ega_get_max_scan_lines IFN0()

    {
    return (crt_controller.maximum_scan_line.as_bfld.maximum_scan_line);
    }			/* ega_get_max_scan_lines */

/* Set line compare value */

void ega_set_line_compare IFN1(int, lcomp_val)
    {
    CRTC_OVERFLOW	new_overflow;

    new_overflow.as.abyte = crt_controller.crtc_overflow.as.abyte;
    if (lcomp_val >= 0x100)
	new_overflow.as_bfld.line_compare_bit_8 = 1;
    else
	new_overflow.as_bfld.line_compare_bit_8 = 0;

    outb(EGA_CRTC_INDEX, 7);
    outb(EGA_CRTC_DATA, new_overflow.as.abyte);
    outb(EGA_CRTC_INDEX, 24);
    outb(EGA_CRTC_DATA, lcomp_val & 0xff);
    }

#endif /* HUNTER */
#endif /* EGG */
#endif /* REAL_VGA */