summaryrefslogtreecommitdiffstats
path: root/private/mvdm/softpc.new/base/video/ega_vide.c
blob: 28f73127ef4f9a7610ddcf973a0701b5ffc2ecfa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
#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 		: EGA BIOS

RELATED DOCS		: IBM EGA Technical reference.

DESIGNER		: William Gulland

REVISION HISTORY	:
First version		: 17/8/88 William

SUBMODULE NAME		: ega_video

PURPOSE			:  Emulate IBM EGA BIOS.


SccsID[]="@(#)ega_video.c	1.70 07/04/95 Copyright Insignia Solutions Ltd.";


[1.INTERMODULE INTERFACE SPECIFICATION]


[1.1    INTERMODULE EXPORTS]

	PROCEDURES() :	give procedure type,name, and argument types
			void ega_video_init()
			void ega_video_io()

	DATA 	     :	give type and name

-------------------------------------------------------------------------
[1.2 DATATYPES FOR [1.1] (if not basic C types)]

	STRUCTURES/TYPEDEFS/ENUMS: 

-------------------------------------------------------------------------
[1.3 INTERMODULE IMPORTS]
     (not o/s objects or standard libs)

	PROCEDURES() : 	give name, and source module name

	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)

[1.4.2 EXPORTED OBJECTS]
=========================================================================
PROCEDURE	  : 	ega_video_init()

PURPOSE		  : 	Initialize EGA-specific bits of the video BIOS.

PARAMETERS	   None

ACCESS		  :	called from video_init if EGA installed.

DESCRIPTION	  : 	describe what (not how) function does

			Initializes ega_info & ega_info3.

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

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

[3.1 INTERMODULE IMPORTS]						*/

/* [3.1.1 #INCLUDES]                                                    */


#ifdef EGG
#include <stdio.h>
#include TypesH
#include FCntlH

#include "xt.h"
#include CpuH
#include "sas.h"
#include "ios.h"
#include "gmi.h"
#include "gvi.h"
#include "bios.h"
#include "error.h"
#include "config.h"
#include "equip.h"
#include "egacpu.h"
#include "egaports.h"
#include "gfx_upd.h"
#include "egagraph.h"
#include "egaread.h"
#include "video.h"
#include "egavideo.h"
#include "vgaports.h"
#include "debug.h"
#include "timer.h"
#include "host_gfx.h"
#include "idetect.h"
#ifndef PROD
#include "trace.h"
#endif
#include "host.h"

#ifdef  GISP_SVGA 
#include HostHwVgaH
#include "hwvga.h"
#endif		/* GISP_SVGA */

/* [3.1.2 DECLARATIONS]                                                 */

GLOBAL IU8 Video_mode;	/* Shadow copy of BIOS video mode */
GLOBAL IU8 Currently_emulated_video_mode = 0;	/* Holds last video mode 
						 * set through bios */

#if defined(NTVDM) && defined(X86GFX)
/* Loads font from PC's BIOS into video memory */
IMPORT void loadNativeBIOSfont IPT1( int, lines );
#endif

#ifdef NTVDM
IMPORT int soft_reset;
IMPORT BOOL VDMForWOW;
IMPORT BOOL WowModeInitialized;
#ifndef X86GFX
IMPORT void mouse_video_mode_changed(int new_video_mode);
#endif
#endif	/* NTVDM */

#ifdef CPU_40_STYLE
GLOBAL IBOOL forceVideoRmSemantics = FALSE;
#endif

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

[5.1 LOCAL DECLARATIONS]						*/

#ifdef ANSI
GLOBAL void ega_set_mode(void),ega_char_gen(void);
static void ega_set_palette(void),ega_alt_sel(void);
GLOBAL void ega_set_cursor_mode(void);
static void ega_emul_set_palette(void);
#else
GLOBAL void ega_set_mode(),ega_char_gen();
static void ega_set_palette(),ega_alt_sel();
GLOBAL void ega_set_cursor_mode();
static void ega_emul_set_palette();
#endif /* ANSI */
static void (*ega_video_func[]) () = {
				ega_set_mode,
				ega_set_cursor_mode, 
				vd_set_cursor_position, 
				vd_get_cursor_position,
		 		vd_get_light_pen,
				vd_set_active_page,
				vd_scroll_up,
				vd_scroll_down,
				vd_read_attrib_char,
				vd_write_char_attrib,
				vd_write_char,
				ega_emul_set_palette,
				vd_write_dot,
				vd_read_dot,
				vd_write_teletype,
				vd_get_mode,
				ega_set_palette,
				ega_char_gen,
				ega_alt_sel,
				vd_write_string,
#ifdef VGG
				not_imp,
				not_imp,
				not_imp,
				not_imp,
				not_imp,
				not_imp,
				vga_disp_comb,	/* Function 1A */
				vga_disp_func,
				vga_int_1C,	/* Save/Restore Video State */
#endif
			   };

static int v7_mode_64_munge[4] ={0, 3, 12, 15};
IMPORT half_word bg_col_mask;

#ifdef	VGG
/*
 * Define arrays for mapping the Video BIOS call start and end
 * cursor scanline to their corresponding VGA/EGA register values.
 * There are seperate arrays for cursor start and end and for
 * 8x8 and 8x16 char cell sizes.
 */

UTINY	vga_cursor8_start[17][17] = {
	/*00*/	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 
	/*01*/	0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 
	/*02*/	0x00, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 
	/*03*/	0x00, 0x01, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 
	/*04*/	0x00, 0x01, 0x05, 0x06, 0x07, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 
	/*05*/	0x00, 0x01, 0x02, 0x05, 0x06, 0x07, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 
	/*06*/	0x00, 0x01, 0x02, 0x04, 0x05, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 
	/*07*/	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*08*/	0x00, 0x01, 0x02, 0x04, 0x04, 0x04, 0x05, 0x06, 0x07, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 
	/*09*/	0x00, 0x01, 0x02, 0x04, 0x04, 0x04, 0x04, 0x05, 0x06, 0x07, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 
	/*10*/	0x00, 0x01, 0x02, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x06, 0x07, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 
	/*11*/	0x00, 0x01, 0x02, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x06, 0x07, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 
	/*12*/	0x00, 0x01, 0x02, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x06, 0x07, 0x0c, 0x0c, 0x0c, 0x0c, 
	/*13*/	0x00, 0x01, 0x02, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x06, 0x07, 0x0d, 0x0d, 0x0d, 
	/*14*/	0x00, 0x01, 0x02, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x06, 0x07, 0x0e, 0x0e, 
	/*15*/	0x00, 0x01, 0x02, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x06, 0x07, 0x0f, 
	/*16*/	0x00, 0x01, 0x02, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x06, 0x07, 
	};


UTINY	vga_cursor16_start[17][17] = {
	/*00*/	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 
	/*01*/	0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 
	/*02*/	0x00, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 
	/*03*/	0x00, 0x01, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 
	/*04*/	0x00, 0x01, 0x0c, 0x0d, 0x0e, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 
	/*05*/	0x00, 0x01, 0x02, 0x0c, 0x0d, 0x0e, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 
	/*06*/	0x00, 0x01, 0x02, 0x08, 0x0c, 0x0d, 0x0e, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 
	/*07*/	0x00, 0x01, 0x02, 0x08, 0x08, 0x0c, 0x0d, 0x0e, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*08*/	0x00, 0x01, 0x02, 0x08, 0x08, 0x08, 0x0c, 0x0d, 0x0e, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 
	/*09*/	0x00, 0x01, 0x02, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x0d, 0x0e, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 
	/*10*/	0x00, 0x01, 0x02, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x0d, 0x0e, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 
	/*11*/	0x00, 0x01, 0x02, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x0d, 0x0e, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 
	/*12*/	0x00, 0x01, 0x02, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x0d, 0x0e, 0x0c, 0x0c, 0x0c, 0x0c, 
	/*13*/	0x00, 0x01, 0x02, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x0d, 0x0e, 0x0d, 0x0d, 0x0d, 
	/*14*/	0x00, 0x01, 0x02, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x0d, 0x0e, 0x0e, 0x0e, 
	/*15*/	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x0f, 
	/*16*/	0x00, 0x01, 0x02, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x0d, 0x0e, 
	};

#ifdef	USE_CURSOR_END_TABLES

UTINY	vga_cursor8_end[17][17] = {
	/*00*/	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	/*01*/	0x01, 0x01, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*02*/	0x02, 0x02, 0x02, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*03*/	0x03, 0x03, 0x03, 0x03, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*04*/	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*05*/	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*06*/	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*07*/	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*08*/	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*09*/	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*10*/	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*11*/	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*12*/	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*13*/	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*14*/	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*15*/	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	/*16*/	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 
	};

UTINY	vga_cursor16_end[17][17] = {
	/*00*/	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	/*01*/	0x01, 0x01, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	/*02*/	0x02, 0x02, 0x02, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	/*03*/	0x03, 0x03, 0x03, 0x03, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	/*04*/	0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	/*05*/	0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	/*06*/	0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	/*07*/	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	/*08*/	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	/*09*/	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	/*10*/	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	/*11*/	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	/*12*/	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f, 0x0f, 
	/*13*/	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f, 
	/*14*/	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 
	/*15*/	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	/*16*/	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 
	};
#endif	/* USE_CURSOR_END_TABLES */
#endif	/* VGG */

/* [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 "VIDEO_BIOS_EGA.seg"
#endif

/* [5.1.2 TYPEDEF, STRUCTURE, ENUM DECLARATIONS]			*/


/* [5.1.3 PROCEDURE() DECLARATIONS]					*/

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

   [5.2.1 INTERNAL DATA DEFINITIONS 					*/

/* [5.2.2 INTERNAL PROCEDURE DEFINITIONS]				*/


/*
==========================================================================
FUNCTION	:	do_outb
PURPOSE		:	handy utility to output a value to an EGA chip register.
INPUT  PARAMS	:	index port, register, value to write
RETURN PARAMS   :	None
==========================================================================
FUNCTION	:	follow_ptr
PURPOSE		:	handy utility to follow a 'long' intel pointer.
INPUT  PARAMS	:	Address in M of the pointer
RETURN PARAMS   :	Address in M of the pointed-to byte.
==========================================================================
FUNCTION	:	low_set_mode
PURPOSE		:	Does low-level mode change.
EXTERNAL OBJECTS:	list any used, and state changes incurred
RETURN VALUE	:	
INPUT  PARAMS	:	mode: screen mode to change to.
RETURN PARAMS   :
==========================================================================
FUNCTION	:	load_font
PURPOSE		:	load part of a font into EGA font memory.
EXTERNAL OBJECTS:	list any used, and state changes incurred
RETURN VALUE	:	
INPUT  PARAMS	:	sys_addr table	Address in M of the character bitmaps
			int count	number of characters to redefine
			int char_off	first character to redefine
			int font_no	font to change
			int nbytes	Number of bytes per character
RETURN PARAMS   :
==========================================================================
PROCEDURE	  : 	ega_set_mode()
PURPOSE		  : 	Switch screen mode.
PARAMETERS	  :	 AL = mode.

GLOBALS		  :	describe what exported data objects are
			accessed and how. Likewise for imported
			data objects.

ACCESS		  :	via ega_video_func[] jump table.

RETURNED VALUE	  : 	None.

DESCRIPTION	  : 	
==========================================================================
PROCEDURE	  : 	ega_alt_sel()
PURPOSE		  : 	Get EGA info
PARAMETERS	  :	BL = function
GLOBALS		  :	
ACCESS		  :	via ega_video_func[] jump table.
RETURNED VALUE	  : 	None.
DESCRIPTION	  : 	
==========================================================================
FUNCTION	:	ega_set_palette
PURPOSE		:	brief description
EXTERNAL OBJECTS:	list any used, and state changes incurred
RETURN VALUE	:	
INPUT  PARAMS	:
RETURN PARAMS   :
==========================================================================
FUNCTION	:	ega_emul_set_palette
PURPOSE		:	brief description
EXTERNAL OBJECTS:	list any used, and state changes incurred
RETURN VALUE	:	
INPUT  PARAMS	:
RETURN PARAMS   :
==========================================================================
FUNCTION	:	ega_char_gen
PURPOSE		:	brief description
EXTERNAL OBJECTS:	list any used, and state changes incurred
RETURN VALUE	:	
INPUT  PARAMS	:
RETURN PARAMS   :
==========================================================================
FUNCTION	:	write_ch_set/xor()
PURPOSE		:	Output character to screen in EGA graphics modes.
EXTERNAL OBJECTS:	list any used, and state changes incurred
RETURN VALUE	:	
INPUT  PARAMS	:
RETURN PARAMS   :
==========================================================================
FUNCTION	:	name
PURPOSE		:	brief description
EXTERNAL OBJECTS:	list any used, and state changes incurred
RETURN VALUE	:	
INPUT  PARAMS	:
RETURN PARAMS   :
==========================================================================
*/
#ifdef VGG
/* Called for not implemented functions */
void not_imp IFN0()
{
	setAL(0);
}
#endif

static void do_outb IFN3(int, index,int, ega_reg, byte, value)
{
	outb(index,ega_reg);
	outb(index+1,value);
}

sys_addr video_effective_addr IFN2(IU16, seg, IU16, offset)
{
#ifdef CPU_40_STYLE
	if (forceVideoRmSemantics)
	{
		/* can't call effective_addr, as the segment is almost
		** certainly bogus in prot mode. This mode of operation
		** should ONLY be used when we are bypassing going to v86
		** mode to do a video bios operation (see WinVDD.c)
		*/
		return((sys_addr)((((IU32)seg)<<4) + offset));
	}
	else
#endif
	{
		return effective_addr(seg, offset);
	}
}

sys_addr follow_ptr IFN1(sys_addr, addr)
{
	return video_effective_addr(sas_w_at_no_check(addr+2),
		sas_w_at_no_check(addr));
}

void low_set_mode IFN1(int, mode)
{
	int i;
	sys_addr save_addr,params_addr,palette_addr;
	word temp_word;
	half_word start, end, video_mode;


	params_addr = find_mode_table(mode,&save_addr);

/* setup Sequencer */
#ifndef REAL_VGA
	do_outb(EGA_SEQ_INDEX,0,1);	/* Synchronous reset - turn off Sequencer */
#else
	do_outb(EGA_SEQ_INDEX,0,0);	/* Reset - turn off Sequencer */
#endif
	do_outb(EGA_CRTC_INDEX,0x11,0);
	for(i=0;i<EGA_PARMS_SEQ_SIZE;i++)
	{
		do_outb(EGA_SEQ_INDEX,i+1,sas_hw_at_no_check(params_addr+EGA_PARMS_SEQ+i));
	}
	do_outb(EGA_SEQ_INDEX,0,3);	/* Turn Sequencer back on */
/* setup Miscellaneous register */
	outb(EGA_MISC_REG,sas_hw_at_no_check(params_addr+EGA_PARMS_MISC));
/* setup CRTC */
	for(i=0;i<EGA_PARMS_CRTC_SIZE;i++)
	{
		do_outb(EGA_CRTC_INDEX,i,sas_hw_at_no_check(params_addr+EGA_PARMS_CRTC+i));
	}
	if (video_adapter == EGA) {
	    if( (get_EGA_switches() & 1) && mode < 4)
	    {
		/* For some reason, the CRTC parameter table for 'enhanced' text has
		 * the same cursor start and end as for 'unenhanced' text.
		 * So fix the cursor start & end values to sensible things.
		 * This is not the case for the VGA BIOS mode table.
	 	 */
    		do_outb(EGA_CRTC_INDEX, R10_CURS_START, 11);
    		do_outb(EGA_CRTC_INDEX, R11_CURS_END, 12);
	    }
	}
/* setup attribute chip - NB need to do an inb() to clear the address */
	inb(EGA_IPSTAT1_REG, (half_word *)&temp_word);
	for(i=0;i<EGA_PARMS_ATTR_SIZE;i++)
	{
		outb(EGA_AC_INDEX_DATA,i);
		outb(EGA_AC_INDEX_DATA,sas_hw_at_no_check(params_addr+EGA_PARMS_ATTR+i));
	}
/* setup graphics chips */
	for(i=0;i<EGA_PARMS_GRAPH_SIZE;i++)
	{
		do_outb(EGA_GC_INDEX,i,sas_hw_at_no_check(params_addr+EGA_PARMS_GRAPH+i));
	}

#ifdef V7VGA
/* setup extensions registers */
#ifndef GISP_SVGA	/* Don't want the V7 stuff for GISP
			   builds that still use our
			   video ROMS */

	if (video_adapter == VGA)
	{
		/* turn on extension registers */
		do_outb(EGA_SEQ_INDEX, 6, 0xea);

		if (mode < 0x46)
		{
			do_outb(EGA_SEQ_INDEX, 0xfd, 0x22);
			do_outb(EGA_SEQ_INDEX, 0xa4, 0x00);
			do_outb(EGA_SEQ_INDEX, 0xfc, 0x08);
			do_outb(EGA_SEQ_INDEX, 0xf6, 0x00);
			do_outb(EGA_SEQ_INDEX, 0xf8, 0x00);
			do_outb(EGA_SEQ_INDEX, 0xff, 0x00);
		}
		else
		{
			if (mode < 0x62)
				do_outb(EGA_SEQ_INDEX, 0xfd, 0x00);
			else if (mode == 0x62)
				do_outb(EGA_SEQ_INDEX, 0xfd, 0x90);
			else
				do_outb(EGA_SEQ_INDEX, 0xfd, 0xa0);

			if (mode == 0x60)
				do_outb(EGA_SEQ_INDEX, 0xa4, 0x00);
			else
				do_outb(EGA_SEQ_INDEX, 0xa4, 0x10);

			if (mode < 0x66)
				if ((mode == 0x63) || (mode == 0x64))
					do_outb(EGA_SEQ_INDEX, 0xfc, 0x18);
				else
					do_outb(EGA_SEQ_INDEX, 0xfc, 0x08);
			else
				do_outb(EGA_SEQ_INDEX, 0xfc, 0x6c);

			if ((mode < 0x65) || (mode == 0x66))
			{
				do_outb(EGA_SEQ_INDEX, 0xf6, 0x00);
				do_outb(EGA_SEQ_INDEX, 0xff, 0x00);
			}
			else
			{
				do_outb(EGA_SEQ_INDEX, 0xf6, 0xc0);
				do_outb(EGA_SEQ_INDEX, 0xff, 0x10);
			}

			if (mode == 0x62)
				do_outb(EGA_SEQ_INDEX, 0xf8, 0x10);
			else
				do_outb(EGA_SEQ_INDEX, 0xf8, 0x00);
		}

		/* turn off extension registers */
		do_outb(EGA_SEQ_INDEX, 6, 0xae);
	}
#endif		/* GISP_SVGA */

	/***
		Update Extended BIOS data stuff ?
	***/
#endif

    /*
     * Update BIOS data variables
     */

    sas_storew_no_check(VID_COLS,sas_hw_at_no_check(params_addr+EGA_PARMS_COLS)); /* byte in ROM, word in BIOS var! */
    sas_store_no_check(vd_rows_on_screen, sas_hw_at_no_check(params_addr+EGA_PARMS_ROWS));
    sas_store_no_check(ega_char_height, sas_hw_at_no_check(params_addr+EGA_PARMS_HEIGHT));
    sas_storew_no_check(VID_LEN,sas_w_at_no_check(params_addr+EGA_PARMS_LENGTH));

/* save cursor mode: BIOS data area has end byte at the low address,
   so the bytes must be swapped over from the CRTC register sense */
    start = sas_hw_at_no_check(params_addr+EGA_PARMS_CURSOR);
    sas_store_no_check(VID_CURMOD+1, start);
    end = sas_hw_at_no_check(params_addr+EGA_PARMS_CURSOR+1);
    sas_store_no_check(VID_CURMOD, end);
    sure_sub_note_trace2(CURSOR_VERBOSE,"changing mode, setting cursor bios vbls to start=%d, end=%d",start,end);
    sure_sub_note_trace2(CURSOR_VERBOSE,"changing mode, mode=%#x, params_addr=%#x",mode,params_addr);

/* save Palette registers if necessary */
	palette_addr = follow_ptr(save_addr+PALETTE_OFFSET);
    if(palette_addr)
    {
	for(i=0;i<16;i++)
		sas_store_no_check(palette_addr+i, sas_hw_at_no_check(params_addr+EGA_PARMS_ATTR+i));
	sas_store_no_check(palette_addr+16, sas_hw_at_no_check(params_addr+EGA_PARMS_ATTR+17));
    }

/* Get the video_.. variables from the mode table */
	video_mode = sas_hw_at_no_check(vd_video_mode);
#ifdef V7VGA
	if (video_adapter == VGA)
	{
		if (video_mode > 0x13)
			video_mode += 0x4c;
		else if ((video_mode == 1) && extensions_controller.foreground_latch_1)
			video_mode = extensions_controller.foreground_latch_1;
	}

	if (video_mode >= 0x60)
	{
    	video_pc_low_regen = vd_ext_graph_table[video_mode-0x60].start_addr;
    	video_pc_high_regen = vd_ext_graph_table[video_mode-0x60].end_addr;
	}
	else if (video_mode >= 0x40)
	{
    	video_pc_low_regen = vd_ext_text_table[video_mode-0x40].start_addr;
    	video_pc_high_regen = vd_ext_text_table[video_mode-0x40].end_addr;
	}
	else
	{
    	video_pc_low_regen = vd_mode_table[video_mode].start_addr;
    	video_pc_high_regen = vd_mode_table[video_mode].end_addr;
	}
#else
    video_pc_low_regen = vd_mode_table[video_mode].start_addr;
    video_pc_high_regen = vd_mode_table[video_mode].end_addr;
#endif /* V7VGA */

#ifdef VGG
    if (video_adapter == VGA) {
	i = get_scanlines();	/* WARNING - needs the BIOS variables! */	
	if(mode == 0x13 || mode > 0x65)
	{
	    init_vga_dac(2);  /* 256 colour DAC table */
	}
	else if(i == RS200 || mode == 0x63 || mode == 0x64)
	{
	    init_vga_dac(1);  /* DACs to emulate CGA palette - RGB + Intensity*/
	}
	else
	{
	    init_vga_dac(0);  /* DACs to emulate EGA palette - RGB + rgb */
	}
	outb(VGA_DAC_MASK,0xff);
	/* Initialize the fancy VGA palette stuff to look like an EGA */
	inb(EGA_IPSTAT1_REG, (half_word *)&temp_word);
	outb(EGA_AC_INDEX_DATA, 20); /* Pixel padding register */
	outb(EGA_AC_INDEX_DATA, 0);  /* Use first block of 64 in DACs */
    }
#endif
}

/* Load part of a font into EGA font memory. */
void load_font IFN5
   (
   sys_addr, table,	/* Address in M of the character bitmaps */
   int, count,		/* number of characters to redefine */
   int, char_off,	/* first character to redefine */
   int, font_no,	/* font to change */
   int, nbytes		/* Number of bytes per character */
   )
{
#if !(defined(NTVDM) && defined(X86GFX))
	int i,j;
	sys_addr font_addr;
	sys_addr data_addr;
#endif /* !(NTVDM && X86GFX) */
	half_word temp_word;
	half_word video_mode;
	static word font_off[] = { 0, 0x4000, 0x8000, 0xc000, 0x2000, 0x6000, 0xa000, 0xe000 };

/* First switch to font loading mode */
	low_set_mode(FONT_LOAD_MODE);


#if defined(NTVDM) && defined(X86GFX)
	loadNativeBIOSfont( 25 );

#else
#ifdef GISP_SVGA
	if( hostIsFullScreen( ) )
	{
		loadFontToVGA( table , count , char_off , font_no , nbytes );
	}
	else
	{
		loadFontToEmulation( table , count , char_off , font_no , nbytes );
	}
#else /* GISP_SVGA */


	/* Work out where to put the font. */
	font_addr = 0xA0000 + font_off[font_no] + FONT_MAX_HEIGHT*char_off;
	data_addr = table;

	for(i=0;i<count;i++)   /* for each character */
	{
		for(j=0;j<nbytes;j++)   /* for each byte of character */
		{
			sas_store(font_addr, sas_hw_at_no_check(data_addr));
			font_addr++;
			data_addr++;
		}

		font_addr += (FONT_MAX_HEIGHT - nbytes);
	}
#endif	/* GISP_SVGA */
#endif	/* NTVDM && X86GFX */

/* Finally switch back to the BIOS mode */
	video_mode = sas_hw_at_no_check(vd_video_mode);
#ifdef V7VGA
	if (video_adapter == VGA)
		if (video_mode > 0x13)
			video_mode += 0x4c;
		else if ((video_mode == 1) && extensions_controller.foreground_latch_1)
			video_mode = extensions_controller.foreground_latch_1;
#endif /* V7VGA */

	low_set_mode(video_mode);
	inb(EGA_IPSTAT1_REG,&temp_word);
	outb(EGA_AC_INDEX_DATA, EGA_PALETTE_ENABLE);	/* re-enable video */
}

void recalc_text IFN1(int, height)
{
	int scan_lines;
	half_word video_mode;
	word screen_height;
	half_word oflo;
	half_word protect;
#ifdef NTVDM
    MAX_SCAN_LINE   crtc_reg9;
#endif

	video_mode = sas_hw_at_no_check(vd_video_mode);
#ifdef V7VGA
	if (video_adapter == VGA)
		if (video_mode > 0x13)
			video_mode += 0x4c;
		else if ((video_mode == 1) && extensions_controller.foreground_latch_1)
			video_mode = extensions_controller.foreground_latch_1;
#endif /* V7VGA */

	if(video_adapter == EGA && !(get_EGA_switches() & 1) && (video_mode < 4))
		scan_lines = 200; /* Low res text mode */
	else
		scan_lines = get_screen_height() + 1;

	sas_store_no_check(ega_char_height, height);
	sas_store_no_check(vd_rows_on_screen, scan_lines/height - 1);
	if ( video_mode < 4 &&  scan_lines/height == 25 )
		sas_storew_no_check(VID_LEN, video_mode<2 ? 0x800 : 0x1000);
	else
		sas_storew_no_check(VID_LEN,(sas_hw_at_no_check(vd_rows_on_screen)+1)*sas_w_at_no_check(VID_COLS)*2);
#ifdef NTVDM
    /* preserve other bits in register 9 for VGA */
    if (video_adapter == VGA) {
        outb(EGA_CRTC_INDEX, 9);
        inb(EGA_CRTC_DATA, (half_word *) &crtc_reg9);
        crtc_reg9.as_bfld.maximum_scan_line = height -1;
        outb(EGA_CRTC_DATA, crtc_reg9.as.abyte);
    }
    else
        do_outb(EGA_CRTC_INDEX,9,height-1); /* Character height */
#else
    do_outb(EGA_CRTC_INDEX,9,height-1); /* Character height */
#endif
	do_outb(EGA_CRTC_INDEX,0xA,height-1);	/* Cursor start */
	do_outb(EGA_CRTC_INDEX,0xB,0);		/* Cursor end */

	/* 
	* VGA adapter height setting occupies Vertical Display End register
	* plus 2 bits in the overflow register. The overflow register may also
	* be write protected.
	*/
	if (video_adapter == VGA)
	{
#ifdef NTVDM
	/* Some globals that the mouse driver needs to have available */
	/* when an application (such as any CW based apps.) makes a   */
	/* call to int 33h AX = 26h.				      */
      
	IMPORT word VirtualX, VirtualY;
#endif /* NTVDM */

		screen_height = (sas_hw_at_no_check(vd_rows_on_screen)+1)*height-1;
  
#ifdef NTVDM
	    /* Create the virtual screen size maximums for the text modes */
	    /* This is needed here for CW applications.  	         */

		VirtualX = 640;		/* This is always this value */
 		if(scan_lines == 401)
 		    VirtualY = 400;		/* 50 text row mode - 400 scanlines */
 		else if(scan_lines == 351)
 		    VirtualY = 344;		/* 43 text row mode - 350 scanlines */
 		else
 		    VirtualY = 200;		/* Failsafe - 25 row mode or rest!  */

#endif /* NTVDM */

		outb(EGA_CRTC_INDEX, 7);	/* overflow register */
		inb(EGA_CRTC_DATA, &oflo);
		outb(EGA_CRTC_INDEX, 0x11);	/* vert sync contains protect bit */
		inb(EGA_CRTC_DATA, &protect);
		
		if (screen_height & 0x100)
			oflo |= 2;   /* bit 8 of height -> bit 1 of overflow register */
		else
			oflo &= ~2;
		if (screen_height & 0x200)
			oflo |= 0x40;   /* bit 9 of height -> bit 6 of overflow register */
		else
			oflo &= ~0x40;
		if ((protect & 0x80) == 0x80)    /* overflow reg protected */
		{
			do_outb(EGA_CRTC_INDEX, 0x11, protect & 0x7f); /* enable writes */
			do_outb(EGA_CRTC_INDEX, 7, oflo);	/* overflow reg */
			do_outb(EGA_CRTC_INDEX, 0x11, protect); /* put back old value */
		}
		else
			do_outb(EGA_CRTC_INDEX, 7, oflo);	/* overflow reg */
		
		do_outb(EGA_CRTC_INDEX,0x12, screen_height & 0xff); /* Vertical display end = scan lines */
	}
	else
	if (video_adapter == EGA)
	{
		screen_height = (sas_hw_at_no_check(vd_rows_on_screen)+1)*height-1;
		outb(EGA_CRTC_INDEX, 7);	/* overflow register */
		inb(EGA_CRTC_DATA, &oflo);
		if (screen_height & 0x100)
			oflo |= 2;   /* bit 8 of height -> bit 1 of overflow reg */
		else
			oflo &= ~2;
		do_outb(EGA_CRTC_INDEX, 7, oflo);	/* overflow reg */
		do_outb(EGA_CRTC_INDEX, 0x12, screen_height & 0xff); /* Vertical display end = scan lines */
	}
	else
	{
		assert1(NO, "Bad video adapter (%d) in recalc_text", video_adapter);
	}

	do_outb(EGA_CRTC_INDEX,0x14,height); /* Underline scan line - ie no underline */
}

static void set_graph_font IFN1(int, height)
{
	switch (getBL())
	{
		case 0:
			sas_store_no_check(vd_rows_on_screen, getDL()-1);
			break;
		case 1:
			sas_store_no_check(vd_rows_on_screen, 13);
			break;
		case 2:
			sas_store_no_check(vd_rows_on_screen, 24);
			break;
		case 3:
			sas_store_no_check(vd_rows_on_screen, 42);
			break;
		default:
			assert2(FALSE,"Illegal char gen sub-function %#x:%#x",getAL(),getBL());
	}
	sas_store_no_check(ega_char_height, height);
}

LOCAL VOID
write_ch_set IFN5(sys_addr, char_addr, int, screen_off,
	int, colour, int, nchs, int, scan_length)
{
	unsigned int i, j, colourmask, data, temp, char_height;
	unsigned int *screen;
	register sys_addr font;

#ifndef	REAL_VGA

	/*
	 * video mode 11 (VGA 640x480 2 colour mode) is a special case as
	 * it does not have a 'no display' attribute.
	 */

	if( sas_hw_at_no_check(vd_video_mode) == 0x11 )
		colourmask = ~0;
	else
		colourmask = sr_lookup[colour & 0xf];

	font = char_addr;

	screen = (unsigned int *) &EGA_planes[screen_off << 2];
	char_height = sas_hw_at_no_check(ega_char_height);

	if( nchs == 1 )
	{
		for( i = char_height; i > 0; i-- )
		{
			data = sas_hw_at_no_check(font);
			font++;
			temp = data << 8;
			data |= temp;
			temp = data << 16;
			data |= temp;

			*screen = data & colourmask;
			screen += scan_length;
		}
	}
	else
	{
		scan_length -= nchs;

		for( i = char_height; i > 0; i-- )
		{
			data = sas_hw_at_no_check(font);
			font++;
			temp = data << 8;
			data |= temp;
			temp = data << 16;
			data |= temp;

			data &= colourmask;

			for( j = nchs; j > 0; j-- )
			{
				*screen++ = data;
			}

			screen += scan_length;
		}
	}
#else
	vga_card_w_ch_set(char_addr, screen_off, colour, nchs, scan_length, char_height);
#endif
}

void write_ch_xor IFN5(sys_addr, char_addr, int, screen_off,
	int, colour, int, nchs, int, scan_length)
{
	unsigned int i, j, colourmask, data, temp, char_height;
	unsigned int *screen;
	register sys_addr font;

#ifndef	REAL_VGA
	/*
	 * video mode 11 (VGA 640x480 2 colour mode) is a special case as
	 * it does not have a 'no display' attribute.
	 */
	if(sas_hw_at_no_check(vd_video_mode) == 0x11)
		colourmask = ~0;
	else
		colourmask = sr_lookup[colour & 0xf];

	font = char_addr;
	char_height = sas_hw_at_no_check(ega_char_height);

	screen = (unsigned int *) &EGA_planes[screen_off << 2];

	if( nchs == 1 )
	{
		for( i = char_height; i > 0; i-- )
		{
			data = sas_hw_at_no_check(font);
			font++;
			temp = data << 8;
			data |= temp;
			temp = data << 16;
			data |= temp;

			*screen ^= data & colourmask;
			screen += scan_length;
		}
	}
	else
	{
		scan_length -= nchs;

		for( i = char_height; i > 0; i-- )
		{
			data = sas_hw_at_no_check(font);
			font++;
			temp = data << 8;
			data |= temp;
			temp = data << 16;
			data |= temp;

			data &= colourmask;

			for( j = nchs; j > 0; j-- )
			{
				*screen++ ^= data;
			}

			screen += scan_length;
		}
	}
#else
	vga_card_w_ch_xor(char_addr, screen_off, colour, nchs, scan_length, char_height);
#endif
}

GLOBAL void ega_set_mode IFN0()
{
	int pag;
	sys_addr save_addr,font_addr;
	int font_offset;
	half_word temp_word;
	byte mode_byte;
	byte video_mode;
#ifdef V7VGA
	byte saveBL;
#endif /* V7VGA */

#ifndef PROD
	trace("setting video mode", DUMP_REG);
#endif

#ifdef GISP_SVGA
	/* Try and catch mode changes early */

	/* Are we in the ROMS at the BOP 10 ? */
	if( getCS( ) == EgaROMSegment )
	{
		if( videoModeIs( getAL( ) , GRAPH ) )
		{
			/* Seem to have got a video mode int 10 */
			videoInfo.modeType = GRAPH;
			if( !hostEasyMode( ) )
			{
				videoInfo.forcedFullScreen = TRUE;

				/* point IP at the JMP to host roms */
				setIP( 0x820 );

				/* and return, to let the host bios do the change */
				return;
			}


		}

		/* Not in the vga roms so carry on */
	}
#endif		/* GISP_SVGA */

#ifdef V7VGA
	/*
	   Real video-7 maps mode 7 and mode f to mode 0.
	*/

	if (video_adapter==VGA) {
		video_mode=(getAL()&0x7F);
		if (video_mode==7||video_mode==0xF) {
			setAL(getAL()&0x80);
			always_trace1("V7 doesn't support mode %02x, using mode 0\n",video_mode);
		}
	}
#endif

	if (is_bad_vid_mode(getAL()))
	{
#ifdef V7VGA
		if ((video_adapter == VGA) && is_v7vga_mode(getAL() + 0x4c))
		{
			saveBL = getBL();
			/* Put the mode value where the V7 BIOS expects it */
			setBL(getAL() + 0x4c);
			v7vga_extended_set_mode();
			setBL(saveBL);
		}
		else
#endif /* V7VGA */
			always_trace1("Bad video mode - %d.\n", getAL());
		return;
	}

	video_mode=(getAL()&0x7F);

#ifdef V7VGA
	/*
	 * The real V7 VGA does not change into 40 col mode while
	 * in any proprietary text mode. (A bug ?!)
	 * Emulate this behaviour !
	 */
	if ( video_adapter == VGA && video_mode == 1
	      && is_v7vga_mode(extensions_controller.foreground_latch_1) ) {
		saveBL = getBL();
		/*
		 * This is all backwards - we make the v7vga extended mode setup
		 * believe the new mode is the old one. Probably the real card's BIOS
		 * is just as confused as this code.
		 * Put the mode value where the V7 BIOS expects it.
		 */
		setBL(extensions_controller.foreground_latch_1);
		v7vga_extended_set_mode();
		setBL(saveBL);
		return;
	}
	
	/*
	 * Don't confuse the tricky V7 extended mode setting, as
	 * implemented in v7_video.c, v7vga_extended_set_mode().
	 * low_set_mode() looks at it. Zero it.
	 */
	extensions_controller.foreground_latch_1 = 0;
#endif  /* V7VGA */

/*
 * Only update the global video mode if we're in the system virtual machine.
 * The global mode should then be valid for use in timer interrupts.
 */

	if (sas_hw_at_no_check(BIOS_VIRTUALISING_BYTE) == 0)
		Video_mode = video_mode;

	sas_store_no_check(vd_video_mode, getAL() & 0x7F); /* get rid of top bit - indicates clear or not */
 	sas_store_no_check(ega_info, (sas_hw_at_no_check(ega_info) & 0x7F ) | (getAL() & 0x80)); /* update screen clear flag in ega_info */ 

#ifdef CPU_40_STYLE
	if (forceVideoRmSemantics && (!get_EGA_no_clear()))
	{
	    /* empty the planes... */
	    memset(&EGA_planes[0], 0, 64*1024*4);
	}
#endif

#ifdef MSWDVR
	/*
	 * If the video mode has actually changed, then call
	 * host_mswin_disable().
	 */
	if (Currently_emulated_video_mode != video_mode)
	{
#ifdef CPU_40_STYLE
		if (!getPE())
		{
			host_mswin_disable();
		}
#else
		host_mswin_disable();
#endif /* CPU_40_STYLE */
	}
#endif /* MSWDVR */

	Currently_emulated_video_mode = video_mode;

#if defined(NTVDM) && defined(X86GFX)
	/*
	** Tim August 92. MicroSoft.
	** Give host a chance to do a zany non-standard mode change.
	** For Microsoft NT this is a transition to full-screen ie. the
	** real PC's video BIOS and graphics board.
	**
	** Return value of TRUE means host has done the mode change for
	** us, so no need to continue.
	*/
	{
		extern BOOL hostModeChange IPT0();

		if( hostModeChange() )
			return;
	}
#endif  /* NTVDM & X86GFX */

	save_addr = follow_ptr(EGA_SAVEPTR);
	if(alpha_num_mode())
	{
#ifdef VGG
		/* load_font will do the mode change for us */
		if (video_adapter == VGA)
		{
#ifdef NTVDM
		/* Some globals that the mouse driver needs to have available */
		/* when an application (such as any CW based apps.) makes a   */
		/* call to int 33h AX = 26h.				      */

		IMPORT word VirtualX, VirtualY;
#endif /* NTVDM */

		    switch (get_VGA_lines())
		    {
			case S350:
				load_font(EGA_CGMN,256,0,0,14);
#ifdef NTVDM
				VirtualX = 640;
				VirtualY = 344;
#endif /* NTVDM */
				break;
			case S400:
				load_font(EGA_HIFONT,256,0,0,16);
#ifdef NTVDM
			/* This one gets hit the most by C.W. applications. */
			/* Actually, the other cases never seem to get hit  */
			/* but are there JUST IN CASE! The 43 and 50 row    */
			/* modes in recalc_text(). 			    */

				VirtualX = 640;
				VirtualY = 200;
#endif /* NTVDM */
				break;
			default:
				load_font(EGA_CGDDOT,256,0,0,8);
#ifdef NTVDM
				VirtualX = 640;
				VirtualY = 400;
#endif /* NTVDM */
		    }
		}
		else
#endif	/* VGG */
		{
		    if(get_EGA_switches() & 1)
			load_font(EGA_CGMN,256,0,0,14);
		    else
			load_font(EGA_CGDDOT,256,0,0,8);
		}
		/* Now see if we have a nasty font to load */
		font_addr = follow_ptr(save_addr+ALPHA_FONT_OFFSET);
		if(font_addr != 0)
		{
		/* See if it applies to us */
			font_offset = 11;
			do
		   {
				mode_byte = sas_hw_at_no_check(font_addr + font_offset);
				if(mode_byte == video_mode)
			{
					load_font(follow_ptr(font_addr+6),sas_w_at_no_check(font_addr+2),sas_w_at_no_check(font_addr+4), sas_hw_at_no_check(font_addr+1), sas_hw_at_no_check(font_addr));
					recalc_text(sas_hw_at_no_check(font_addr));
					if(sas_hw_at_no_check(font_addr+10) != 0xff)
						sas_store_no_check(vd_rows_on_screen, sas_hw_at_no_check(font_addr+10)-1);
			   break;
			}
				font_offset++;
			} while(mode_byte != 0xff);
		   }
		}
	else
	{
		/* graphics mode. No font load, so do mode change ourselves */
		low_set_mode(video_mode);
		/* Set up default graphics font */
		sas_storew_no_check(EGA_FONT_INT*4+2,EGA_SEG);
		if(video_mode == 16)
			sas_storew_no_check(EGA_FONT_INT*4,EGA_CGMN_OFF);
		else
#ifdef VGG 
			if (video_mode == 17 || video_mode == 18)
			sas_storew_no_check(EGA_FONT_INT*4,EGA_HIFONT_OFF);
		    else
#endif
			sas_storew_no_check(EGA_FONT_INT*4,EGA_CGDDOT_OFF);
		/* Now see if we have a nasty font to load */
		font_addr = follow_ptr(save_addr+GRAPH_FONT_OFFSET);
		if(font_addr != 0)
		{
		/* See if it applies to us */
			font_offset = 7;
			do
		   {
				mode_byte = sas_hw_at_no_check(font_addr + font_offset);
				if(mode_byte == video_mode)
			{
					sas_store_no_check(vd_rows_on_screen, sas_hw_at_no_check(font_addr)-1);
					sas_store_no_check(ega_char_height, sas_hw_at_no_check(font_addr+1));
					sas_move_bytes_forward(font_addr+3, 4*EGA_FONT_INT, 4);
			   break;
			}
				font_offset++;
			} while(mode_byte != 0xff);
		   }
		}

    sas_store_no_check(vd_current_page, 0);
    sas_storew_no_check((sys_addr)VID_ADDR, 0);
    sas_storew_no_check((sys_addr)VID_INDEX, EGA_CRTC_INDEX);
/*
 * CGA bios fills this entry in 'vd_mode_table' with 'this is a bad mode'
 * value, so make one up for VGA - used in VGA bios disp_func
 */
    if(video_mode < 8)
		sas_store_no_check(vd_crt_mode, vd_mode_table[video_mode].mode_control_val);
    else if(video_mode < 0x10)
		sas_store_no_check(vd_crt_mode, 0x29);
	else
		sas_store_no_check(vd_crt_mode, 0x1e);
	if(video_mode == 6)
		sas_store_no_check(vd_crt_palette, 0x3f);
	else
		sas_store_no_check(vd_crt_palette, 0x30);

    for(pag=0; pag<8; pag++)
		sas_storew_no_check(VID_CURPOS + 2*pag, 0);

#ifdef V7VGA
	set_host_pix_height(1);
	set_banking( 0, 0 );
#endif

#ifdef NTVDM
    /* Don't want to clear screen on startup if integrated with the console. */
    if (soft_reset)
#endif /* NTVDM */
    {
	/* Clear screen */
	if(!get_EGA_no_clear())
	{
#ifdef REAL_VGA
    	    sas_fillsw_16(video_pc_low_regen, vd_mode_table[video_mode].clear_char,
				 (video_pc_high_regen - video_pc_low_regen)/ 2 + 1);
#else /* REAL_VGA */
    	    sas_fillsw(video_pc_low_regen, vd_mode_table[video_mode].clear_char,
				 (video_pc_high_regen - video_pc_low_regen)/ 2 + 1);
#ifdef NTVDM
	    /*
	     * Need to call host clear screen on NT because text windows don't
	     * resize and we need to clear portion not being written to.
	     */
	    host_clear_screen();
	    host_mark_screen_refresh();
#endif /* NTVDM */
#endif /* REAL_VGA */
	}
    }
    inb(EGA_IPSTAT1_REG,&temp_word);
    outb(EGA_AC_INDEX_DATA, EGA_PALETTE_ENABLE);	/* re-enable video */
#if defined(NTVDM) && !defined(X86GFX)
    /*  tell mouse that video mode is changed so it can update its own
     *  EGA registers(for EGA.SYS interface). Only do this on RISC machine.
     *  On X86 machines, ntio.sys int10 handler redirects set mode call
     *  to mouse first which then goes to ega_video_io.
     */
    mouse_video_mode_changed(video_mode);
#endif
#ifndef PROD
    trace("end of video set mode", DUMP_NONE);
#endif
}

/*
 * Set the cursor start and end positions. A bit strange, in that it assumes
 * the caller thinks the cursor is in an 8*8 character cell ... but this
 * should be a copy of the IBM EGA BIOS routine ... what more can we do?
 */
#define	CGA_CURSOR_OFF_MASK	0x60
#define	CGA_CURSOR_OFF_VALUE	0x20
#define	EGA_CURSOR_OFF_START	0x1e
#define	EGA_CURSOR_OFF_END	0x00

GLOBAL void ega_set_cursor_mode IFN0()  
{ 
    /*
     * Set cursor mode
     * Parameters:
     *  CX - cursor value (CH - start scanline, CL - stop scanline)
     */
    int start,end,char_height;

    /* get cursor start and end scan lines */
    start = getCH();
    end = getCL(); 

    /* The following check is done to see if the application is trying
       to turn the cursor off using a technique that worked on the CGA.
       If the application wants to turn the cursor off, it is faked
       up using suitable EGA start and end values */
    if ((start & CGA_CURSOR_OFF_MASK) == CGA_CURSOR_OFF_VALUE)
    {
	sure_sub_note_trace0(CURSOR_VERBOSE,"ega curs - application req curs off??");
	start = EGA_CURSOR_OFF_START;
	end = EGA_CURSOR_OFF_END;
    }
    /* If the application has enabled cursor emulation, try to fake
       up the same cursor appearance on the EGA 14 scan line character
       matrix as you would get on the CGA 8 scan line matrix. */
    else if(!get_EGA_cursor_no_emulate())
    {
	sure_sub_note_trace2(CURSOR_VERBOSE,"emulate CGA cursor using EGA cursor, CGA vals; start=%d, end = %d",start,end);

	char_height = sas_hw_at_no_check(ega_char_height);

#ifdef	VGG
	if (video_adapter == VGA) {
	   UTINY saved_start;

	   if ( start > 0x10 )
	   	start = 0x10;
	   if ( end > 0x10 )
	   	end = 0x10;

	   /*
	    * No more guessing, take the exact values from a real VGA:
	    */

	   saved_start = start;

	   if ( char_height >= 16 ) {
	   	start = vga_cursor16_start[end][start];
#ifdef	USE_CURSOR_END_TABLES
	   	end   = vga_cursor16_end[end][saved_start]];
#else
	   	if ( end && (end > 3 || saved_start > end) )
	   		if ( end != 0xF && end >= saved_start
	   				&& end <= saved_start + 2 )
	   			end = 0xE;
	   		else
	   			end = 0xF;
#endif
	   } else {
	   	start = vga_cursor8_start[end][start];
#ifdef	USE_CURSOR_END_TABLES
	   	end   = vga_cursor8_end[end][saved_start]];
#else
	   	if ( end && (end > 3 || saved_start > end)
	   		 && !(saved_start==6 && end==6) )
	   		end = 7;
#endif
	   }
	} else {
#endif	/* VGG */
	   /* EGA does not allow for character height & does this. */
	   if(start > 4)start += 5;
	   if(end > 4)end += 5;

	/* adjust end scan line because the last line is specified by
	   the cursor end register MINUS 1 on the EGA ... */
	end++;

	/* on the EGA, cursors extending to the bottom of the character
	   matrix are achieved by setting the end register to 0 ... */

	if (start != 0 && end >= char_height)
	    end = 0;

	/* this last bit defies any explanation, but it is what the
	   IBM BIOS does ... */
	if ((end - start) == 0x10)
	    end++;
#ifdef VGG
    }
#endif
    }

    /* actually set the EGA registers */
    sure_sub_note_trace2(CURSOR_VERBOSE,"ega_cur mode start %d end %d", start,end);
    do_outb(EGA_CRTC_INDEX, R10_CURS_START, start);
    do_outb(EGA_CRTC_INDEX, R11_CURS_END, end);

    /*
     * Update BIOS data variables
     */

    sas_storew_no_check((sys_addr)VID_CURMOD, getCX());
    setAH(0);
}

/* This routine is an approximate conversion of the corresponding IBM BIOS routine.
 * I don't think the IBM version works either.
 */
static void ega_emul_set_palette IFN0()
{
	sys_addr save_table;
	half_word work_BL;
	byte temp;

	save_table = follow_ptr( follow_ptr(EGA_SAVEPTR)+PALETTE_OFFSET);
/* setup attribute chip - NB need to do an inb() to clear the address */
	inb(EGA_IPSTAT1_REG,&temp);
	work_BL = getBL();
	if(getBH() == 0)
	{
		sas_store_no_check(vd_crt_palette, (sas_hw_at_no_check(vd_crt_palette) & 0xe0) | (work_BL & 0x1f));
	   work_BL = (work_BL & 7) | ((work_BL<<1) & 0x10);
	   if(!alpha_num_mode())
	   {
		/* set Palette 0 (the background) */
		outb(EGA_AC_INDEX_DATA,0);
		outb(EGA_AC_INDEX_DATA,work_BL);
			if(save_table)
				sas_store_no_check(save_table, work_BL);
	   }
	/* set the overscan register (the border) */
	   outb(EGA_AC_INDEX_DATA,17);
	   outb(EGA_AC_INDEX_DATA,work_BL);
		if(save_table)
			sas_store_no_check(save_table+16, work_BL);

	/* Now set BL as if we came in with BH = 1 */
		work_BL = (sas_hw_at_no_check(vd_crt_palette) & 0x20)>>5;
	}

/* Now do BH = 1 stuff. */
	if(!alpha_num_mode())
	{
		sas_store_no_check(vd_crt_palette, (sas_hw_at_no_check(vd_crt_palette) & 0xdf) | ((work_BL<<5) & 0x20));
		work_BL = work_BL | (sas_hw_at_no_check(vd_crt_palette) & 0x10) | 2;
	   outb(EGA_AC_INDEX_DATA,1);
	   outb(EGA_AC_INDEX_DATA,work_BL);
		if(save_table)
			sas_store_no_check(save_table+16, work_BL);
	   work_BL += 2;
	   outb(EGA_AC_INDEX_DATA,2);
	   outb(EGA_AC_INDEX_DATA,work_BL);
		if(save_table)
			sas_store_no_check(save_table+16, work_BL);
	   work_BL += 2;
	   outb(EGA_AC_INDEX_DATA,3);
	   outb(EGA_AC_INDEX_DATA,work_BL);
		if(save_table)
			sas_store_no_check(save_table+16, work_BL);
	}
	outb(EGA_AC_INDEX_DATA, EGA_PALETTE_ENABLE);	/* re-enable video */
}

static void ega_set_palette IFN0()
{
	int i;
	byte temp;
	sys_addr save_table, palette_table;
	half_word old_mask;

	save_table = follow_ptr( follow_ptr(EGA_SAVEPTR)+PALETTE_OFFSET);
/* setup attribute chip - NB need to do an inb() to clear the address */
	inb(EGA_IPSTAT1_REG,&temp);
	switch (getAL())
	{
		case 0:
			outb(EGA_AC_INDEX_DATA,getBL());
			outb(EGA_AC_INDEX_DATA,getBH());
			outb(EGA_AC_INDEX_DATA, EGA_PALETTE_ENABLE);
			if(save_table)
				sas_store_no_check(save_table + getBL(), getBH());
			break;
		case 1:
			outb(EGA_AC_INDEX_DATA,17);	/* the border colour register */
			outb(EGA_AC_INDEX_DATA,getBH());
			outb(EGA_AC_INDEX_DATA, EGA_PALETTE_ENABLE);
			if(save_table)
				sas_store_no_check(save_table + 16, getBH());
			break;
		case 2:
			palette_table = video_effective_addr(getES(),
				getDX());
			for(i=0;i<16;i++)
			{
				outb(EGA_AC_INDEX_DATA,i);
				outb(EGA_AC_INDEX_DATA,sas_hw_at_no_check(palette_table+i));
			}
			outb(EGA_AC_INDEX_DATA,17);
			outb(EGA_AC_INDEX_DATA,sas_hw_at_no_check(palette_table+16));
			outb(EGA_AC_INDEX_DATA, EGA_PALETTE_ENABLE);
			if(save_table)
				for(i=0;i<17;i++)
					sas_store_no_check(save_table + i, sas_hw_at_no_check(palette_table+i));
			break;
		case 3:
/*<REAL_VGA>*/
		/* Select blinking or intensity - bit3 of AR10 */
			/*inb(EGA_IPSTAT1_REG,&temp);*/
			outb(EGA_AC_INDEX_DATA,16); /* mode control index */
			inb(EGA_AC_SECRET,&temp);  /* Old value */
			outb(EGA_AC_INDEX_DATA,
				(temp & 0xf7) | ((getBL() & 1)<<3));
			inb(EGA_IPSTAT1_REG,&temp);
			outb(EGA_AC_INDEX_DATA, EGA_PALETTE_ENABLE);
/*<REAL_VGA>*/
			old_mask = bg_col_mask;
			if (getBL())
			{
				bg_col_mask = 0x70;
				assert0(FALSE,"Blinking not supported");
				sas_store_no_check(vd_crt_mode, 0x29);
			}
			else
			{
				bg_col_mask = 0xf0; /* Intensity bit set */
				sas_store_no_check(vd_crt_mode, 0x09);
			}

            if ( bg_col_mask != old_mask )
                screen_refresh_required();

			break;
		default:
#ifdef VGG
			if (video_adapter == VGA)
				vga_set_palette();	/* VGA has many more subfuncs */
			else
			{
				assert1(FALSE,"Bad set palette submode %#x",getAL());
				not_imp();
			}
#else
			assert1(FALSE,"Bad set palette submode %#x",getAL());
			setAL(0);
#endif
			break;
	}
}

GLOBAL void ega_char_gen IFN0()
{
	switch (getAL())
	{
		case 3:
			do_outb(EGA_SEQ_INDEX,3,getBL());
			break;
		case 0:
		case 0x10:
			load_font(video_effective_addr(getES(),getBP()),getCX(),getDX(),getBL(),getBH());
			if(getAL()==0x10)
				recalc_text(getBH());

#if defined(NTVDM) && defined(X86GFX)
			if( getBH()==0x16 )
				loadNativeBIOSfont( 25 );
			else if( getBH()==0x14 )
				loadNativeBIOSfont( 28 );
			else
				loadNativeBIOSfont( 50 );
#endif /* NTVDM && X86GFX */
			break;
		case 1:
		case 0x11:
			load_font(EGA_CGMN,256,0,getBL(),14);
			if(getAL()==0x11)
				recalc_text(14);

#if defined(NTVDM) && defined(X86GFX)
			loadNativeBIOSfont( 28 );
#endif	/* NTVDM & X86GFX */
			break;
		case 2:
		case 0x12:
			load_font(EGA_CGDDOT,256,0,getBL(),8);
			if(getAL()==0x12)
				recalc_text(8);

#if defined(NTVDM) && defined(X86GFX)
			loadNativeBIOSfont( 50 );
#endif	/* NTVDM & X86GFX */
			break;
#ifdef VGG
		case 4:
		case 0x14:
			load_font(EGA_HIFONT,256,0,getBL(),16);
			if(getAL()==0x14)
				recalc_text(16);

#if defined(NTVDM) && defined(X86GFX)
			loadNativeBIOSfont( 25 );
#endif	/* NTVDM & X86GFX */
			break;
#endif	/* VGG */
		case 0x20:
			sas_storew_no_check(BIOS_EXTEND_CHAR*4,getBP());
			sas_storew_no_check(BIOS_EXTEND_CHAR*4+2,getES());
			break;
		case 0x21:
			sas_storew_no_check(EGA_FONT_INT*4,getBP());
			sas_storew_no_check(EGA_FONT_INT*4+2,getES());
			set_graph_font(getCX());
			break;
		case 0x22:
#if defined(NTVDM) && defined(X86GFX)
			sas_storew_no_check(EGA_FONT_INT*4,nativeFontAddresses[F8x14].off);
			sas_storew_no_check(EGA_FONT_INT*4+2,nativeFontAddresses[F8x14].seg);
#else
			sas_storew_no_check(EGA_FONT_INT*4,EGA_CGMN_OFF);
			sas_storew_no_check(EGA_FONT_INT*4+2,EGA_SEG);
#endif	/* NTVDM & X86GFX */
			set_graph_font(14);
			break;
		case 0x23:
#if defined(NTVDM) && defined(X86GFX)
			sas_storew_no_check(EGA_FONT_INT*4,nativeFontAddresses[F8x8pt1].off);
			sas_storew_no_check(EGA_FONT_INT*4+2,nativeFontAddresses[F8x8pt1].seg);
#else
			sas_storew_no_check(EGA_FONT_INT*4,EGA_CGDDOT_OFF);
			sas_storew_no_check(EGA_FONT_INT*4+2,EGA_SEG);
#endif	/* NTVDM & X86GFX */
			set_graph_font(8);
			break;
#ifdef VGG
		case 0x24:
#if defined(NTVDM) && defined(X86GFX)
			sas_storew_no_check(EGA_FONT_INT*4,nativeFontAddresses[F8x16].off);
			sas_storew_no_check(EGA_FONT_INT*4+2,nativeFontAddresses[F8x16].seg);
#else
			sas_storew_no_check(EGA_FONT_INT*4,EGA_HIFONT_OFF);
			sas_storew_no_check(EGA_FONT_INT*4+2,EGA_SEG);
#endif	/* NTVDM & X86GFX */
			set_graph_font(16);
			break;
#endif
		case 0x30:
			setCX(sas_hw_at_no_check(ega_char_height));
			setDL(sas_hw_at_no_check(vd_rows_on_screen));
			switch (getBH())
			{
				case 0:
					setBP(sas_w_at_no_check(BIOS_EXTEND_CHAR*4));
					setES(sas_w_at_no_check(BIOS_EXTEND_CHAR*4+2));
					break;
				case 1:
					setBP(sas_w_at_no_check(EGA_FONT_INT*4));
					setES(sas_w_at_no_check(EGA_FONT_INT*4+2));
					break;

#if defined(NTVDM) && defined(X86GFX)

/* ntdetect.com gets the font info from real card on NT boot. VDM reads it into
 * array 'nativeFontAddresses'. Return these fonts as Insignia ROM not loaded.
 */
				case 2:
					setBP(nativeFontAddresses[F8x14].off);
					setES(nativeFontAddresses[F8x14].seg);
					break;
				case 3:
					setBP(nativeFontAddresses[F8x8pt1].off);
					setES(nativeFontAddresses[F8x8pt1].seg);
					break;
				case 4:
					setBP(nativeFontAddresses[F8x8pt2].off);
					setES(nativeFontAddresses[F8x8pt2].seg);
					break;
				case 5:
					setBP(nativeFontAddresses[F9x14].off);
					setES(nativeFontAddresses[F9x14].seg);
					break;
				case 6:
					setBP(nativeFontAddresses[F8x16].off);
					setES(nativeFontAddresses[F8x16].seg);
					break;
				case 7:
					setBP(nativeFontAddresses[F9x16].off);
					setES(nativeFontAddresses[F9x16].seg);
					break;

#else	/* NTVDM & X86GFX */

				case 2:
					setBP(EGA_CGMN_OFF);
					setES(EGA_SEG);
					break;
				case 3:
					setBP(EGA_CGDDOT_OFF);
					setES(EGA_SEG);
					break;
				case 4:
					setBP(EGA_INT1F_OFF);
					setES(EGA_SEG);
					break;
				case 5:
					setBP(EGA_CGMN_FDG_OFF);
					setES(EGA_SEG);
					break;

#ifdef VGG
				case 6:
				case 7:
					setBP(EGA_HIFONT_OFF);
					setES(EGA_SEG);
					break;
#endif	/* VGG */
#endif	/* NTVDM & X86GFX */
				default:
					assert2(FALSE,"Illegal char_gen subfunction %#x %#x",getAL(),getBH());
			}
			break;
		default:
			assert1(FALSE,"Illegal char_gen %#x",getAL());
	}
}

static void ega_alt_sel IFN0()
{
	switch (getBL())
	{
	   case 0x10:
		setBH( get_EGA_disp() );
		setBL( get_EGA_mem() );
		setCH( get_EGA_feature() );
		setCL( get_EGA_switches() );
		break;
	   case 0x20:
	/* Was "enable Print Screen that can do variables lines on screen."
	 * This PC/XT bug fix function is redundant on PC/AT's and
	 * is removed by BCN3330 -- it has been broken since BCN101.
	 */
		assert1(FALSE,"Illegal alt_sel %#x",getBL());
		setAL(0);	/* A function we don't support */
		break;
	   default:
#ifdef VGG
		if (video_adapter == VGA)
		    vga_func_12();	/* Try extra VGA stuff */
		else
#endif
		{
		    setAL(0);	/* A function we don't support */
		    assert1(FALSE,"Illegal alt_sel %#x",getBL());
		}
	}
}


/*
7.INTERMODULE INTERFACE IMPLEMENTATION :

/*
[7.1 INTERMODULE DATA DEFINITIONS]				*/
/*
[7.2 INTERMODULE PROCEDURE DEFINITIONS]				*/

void ega_video_init IFN0()
{
	EQUIPMENT_WORD equip_flag;

	/*
	 * ESTABLISH EQUIPMENT WORD INITIAL VIDEO MODE FIELD.
	 *
	 * This field will already have been initialised by this stage
	 * to 00(binary) from the corresponding field of the CMOS equipment
	 * byte; in that context 00(binary) meant 'primary display has its
	 * own BIOS'.
	 *
	 * However, 00(binary) is not meaningful as the initial mode field
	 * and must be updated at this point to 10(binary) for 80X25 colour.
	 */
	equip_flag.all = sas_w_at_no_check(EQUIP_FLAG);
	equip_flag.bits.video_mode = VIDEO_MODE_80X25_COLOUR;
	sas_storew_no_check(EQUIP_FLAG, equip_flag.all);

#if !defined(NTVDM) || ( defined(NTVDM) && !defined(X86GFX) )
/* Initialize the INTs */
	sas_storew_no_check(BIOS_EXTEND_CHAR*4, EGA_INT1F_OFF);
	sas_storew_no_check(BIOS_EXTEND_CHAR*4+2, EGA_SEG);
	sas_move_bytes_forward(BIOS_VIDEO_IO*4, 0x42*4, 4); /* save old INT 10 as INT 42 */
	sas_storew_no_check(BIOS_VIDEO_IO*4, EGA_ENTRY_OFF);
	sas_storew_no_check(BIOS_VIDEO_IO*4+2, EGA_SEG);

/* Now set up the EGA BIOS variables */
	if (video_adapter == VGA)
	    sas_storew_no_check(EGA_SAVEPTR,VGA_PARMS_OFFSET);
	else 
	    sas_storew_no_check(EGA_SAVEPTR,EGA_PARMS_OFFSET);
	sas_storew_no_check(EGA_SAVEPTR+2,EGA_SEG);
#endif	/* !NTVDM | (NTVDM & !X86GFX) */
#if defined(NTVDM) && defined(X86GFX)
    sas_store_no_check(ega_info,0x60); /* Clear on mode change, 256K, EGA active, emulate cursor */
#else
#ifdef V7VGA
	if ( video_adapter == VGA )
		sas_store_no_check(ega_info, 0x70);   /* Clear on mode change, 256K, Extensions allowed, EGA active, emulate cursor */
	else
		sas_store_no_check(ega_info, 0x60);   /* Clear on mode change, 256K, EGA active, emulate cursor */
#else	/* V7VGA  -- Macs don't have V7 */
	sas_store_no_check(ega_info, 0x60);   /* Clear on mode change, 256K, EGA active, emulate cursor */
#endif /* V7VGA */

#endif /* NTVDM & X86GFX */
#if !(defined(NTVDM) && defined(X86GFX))
	/* Some VGA cards eg ET4000, store info here needed for sync.
	 * Inherit that info from page 0 copy.
	 */
	sas_store_no_check(ega_info3, 0xf9);  /* feature bits = 0xF, EGA installed, use 8*14 font */
#endif


#ifdef VGG
	set_VGA_flags(S400 | VGA_ACTIVE);
#endif

/* Set the default mode */
	ega_set_mode();
}

void ega_video_io IFN0()
{


#if defined(NTVDM) && !defined(X86GFX)
    if (stream_io_enabled && getAH()!= 0x0E &&  getAX() != 0x13FF)
        disable_stream_io();
#endif


    /*
     * The type of operation is coded into the AH register.  Some PC code
     * calls AH functions that are for other even more advanced cards - so we
     * ignore these.
     */

#ifdef V7VGA
#define check_video_func(AH)	((AH >= 0 && AH < EGA_FUNC_SIZE) || (AH == 0x6f && video_adapter == VGA))
#else
#define check_video_func(AH)	(AH >= 0 && AH < EGA_FUNC_SIZE)
#endif

    if (getAH() != 0xff)
	assert1(check_video_func(getAH()),"Illegal EGA VIO:%#x",getAH());
    if (check_video_func(getAH()))
    {
		IDLE_video();	/* add video anti-idle indicator */
#ifdef V7VGA
		if (getAH() == 0x6f)
			v7vga_func_6f();
		else
#endif /* V7VGA */
			(*ega_video_func[getAH()])();
		setCF(0);
    }
    else
        setCF(1);
}

/***** Routines to handle the EGA graphics modes,called from video.c **********/
void ega_graphics_write_char IFN6(int, col, int, row, int, ch,
	int, colour, int, page, int, nchs)
{
	sys_addr char_addr;
	register int i;
	int screen_off;
	byte char_height;
	register int scan_length = sas_w_at_no_check(VID_COLS);

	char_height = sas_hw_at_no_check(ega_char_height);
	char_addr = follow_ptr(EGA_FONT_INT*4)+char_height*ch;
	screen_off = page*sas_w_at_no_check(VID_LEN)+row*scan_length*char_height+col;
#ifdef V7VGA
	if ( video_adapter == VGA )
		if (sas_hw_at_no_check(vd_video_mode) == 0x18)
			colour = v7_mode_64_munge[colour&3];
#endif /* V7VGA */
	if(colour & 0x80)
		write_ch_xor(char_addr,screen_off,colour,nchs,scan_length);
	else
		write_ch_set(char_addr,screen_off,colour,nchs,scan_length);

#ifndef REAL_VGA
	nchs--;

	if( nchs )
	{
		for(i=char_height;i>0;i--)
		{
			(*update_alg.mark_fill)( screen_off, screen_off + nchs );
			screen_off += scan_length;
		}
	}
	else	
	{
		for(i=char_height;i>0;i--)
		{
			(*update_alg.mark_byte)(screen_off);
			screen_off += scan_length;
		}
	}
#endif
}

void ega_write_dot IFN4(int, colour, int, page, int, pixcol, int, row)
{
	register int screen_off,pixmask,setmask,colourmask,temp;

	screen_off = page*sas_w_at_no_check(VID_LEN)+(row*sas_w_at_no_check(VID_COLS)&0xFFFF)+pixcol/8;
	pixmask = 0x80 >>  (pixcol&7);

#ifndef REAL_VGA

	temp = pixmask << 8;
	pixmask |= temp;
	temp = pixmask << 16;
	pixmask |= temp;

#ifdef V7VGA
	if ( video_adapter == VGA )
		if (sas_hw_at_no_check(vd_video_mode) == 0x18)
			colour = v7_mode_64_munge[colour&3];
#endif /* V7VGA */

	colourmask = sr_lookup[colour & 0xf];

	setmask = pixmask & colourmask;

	if( colour & 0x80 )
	{
		/* XOR pixel */

		temp = *( (unsigned int *) EGA_planes + screen_off );
		*( (unsigned int *) EGA_planes + screen_off ) = temp ^ setmask;
	}
	else
	{
		/* set/clear pixel */

		temp = *( (unsigned int *) EGA_planes + screen_off );
		temp &= ~pixmask;
		*( (unsigned int *) EGA_planes + screen_off ) = ( temp | setmask );
	}

	/* Get the screen updated */

	(*update_alg.mark_byte)(screen_off);
#else
	vga_card_w_dot(screen_off, pixmask, colour);
#endif
}

void ega_sensible_graph_scroll_up IFN6(int, row,
	int, col, int, rowsdiff, int, colsdiff, int, lines, int, attr)
{
	register int col_incr = sas_w_at_no_check(VID_COLS);
	register int i,source,dest;
	byte char_height;
	boolean screen_updated;

	char_height = sas_hw_at_no_check(ega_char_height);
	dest = sas_w_at_no_check(VID_ADDR)+row*col_incr*char_height+col;
	rowsdiff *= char_height;
	lines *= char_height;
	source = dest+lines*col_incr;
#ifdef REAL_VGA
	vga_card_scroll_up(source, dest, rowsdiff, colsdiff, lines, attr, col_incr);
#else
	screen_updated = (col+colsdiff) <= col_incr;  /* Check for silly scroll */

	if(screen_updated)
		screen_updated = (*update_alg.scroll_up)(dest,colsdiff,rowsdiff,attr,lines,0);

	for(i=0;i<rowsdiff-lines;i++)
	{
		memcpy(&EGA_planes[dest<<2],&EGA_planes[source<<2],colsdiff<<2);

		if(!screen_updated)
			(*update_alg.mark_string)(dest, dest+colsdiff-1);
		source += col_incr;
		dest += col_incr;
	}

	attr = sr_lookup[attr & 0xf];

	while(lines--)
	{
		memset4( attr, (ULONG *)&EGA_planes[dest<<2], colsdiff );

		if(!screen_updated)
			(*update_alg.mark_fill)(dest, dest+colsdiff-1);

		dest += col_incr;
	}
#endif
}

void ega_sensible_graph_scroll_down IFN6(int, row,
	int, col, int, rowsdiff, int, colsdiff, int, lines, int, attr)
{
	register int col_incr = sas_w_at_no_check(VID_COLS);
	register int i,source,dest;
	byte char_height;
	boolean screen_updated;

	char_height = sas_hw_at_no_check(ega_char_height);
	dest = sas_w_at_no_check(VID_ADDR)+row*col_incr*char_height+col;
	rowsdiff *= char_height;
	lines *= char_height;
#ifdef REAL_VGA
	dest += rowsdiff*col_incr-1; /* Last byte in destination */
	source = dest-lines*col_incr;
	vga_card_scroll_down(source, dest, rowsdiff, colsdiff, lines, attr, col_incr);
#else
	screen_updated = (col+colsdiff) <= col_incr;  /* Check for silly scroll */
	if(screen_updated)
		screen_updated = (*update_alg.scroll_down)(dest,colsdiff,rowsdiff,attr,lines,0);
	dest += (rowsdiff-1)*col_incr; /* First byte in last row of dest */
	source = dest-lines*col_incr;

	for(i=0;i<rowsdiff-lines;i++)
	{
		memcpy(&EGA_planes[dest<<2],&EGA_planes[source<<2],colsdiff<<2);

		if(!screen_updated)
			(*update_alg.mark_string)(dest, dest+colsdiff-1);
		source -= col_incr;
		dest -= col_incr;
	}

	attr = sr_lookup[attr & 0xf];

	while(lines--)
	{
		memset4( attr, (ULONG *)&EGA_planes[dest<<2], colsdiff );

		if(!screen_updated)
			(*update_alg.mark_fill)(dest, dest+colsdiff-1);

		dest -= col_incr;
	}
#endif
}

/* This is called from vga_video.c as well. */
void search_font IFN2(char *, the_char,int, height)
{
	register int i;
	register host_addr scratch_addr;
	register sys_addr font_addr;

	font_addr = follow_ptr(4*EGA_FONT_INT);
	scratch_addr = sas_scratch_address(height);
	for(i=0;i<256;i++)
	{
		sas_loads(font_addr, scratch_addr, height);
		if(memcmp(scratch_addr,the_char,height) == 0)
			break;
		font_addr += height;
	}
	if(i<256)
		setAL(i);
	else
		setAL(0); /* Didn't find a character */
}

void ega_read_attrib_char IFN3(int, col, int, row, int, page)
{
	byte the_char[256], char_height;
	int screen_off;
	int i, data;

	char_height = sas_hw_at_no_check(ega_char_height);
	screen_off = page*sas_w_at_no_check(VID_LEN)+row*sas_w_at_no_check(VID_COLS)*char_height+col;
	/* 
	 * Load up the screen character into the_char.
	 * We are looking for non-zero pixels, so OR all the planes together
	 */
#ifndef REAL_VGA
	for(i=0;i<char_height;i++)
	{
		data = *( (unsigned int *) EGA_planes + screen_off );
		data = ( data >> 16 ) | data;
		the_char[i] = ( data >> 8 ) | data;
		screen_off += sas_w_at_no_check(VID_COLS);
	}
#else
	vga_card_read_ch(screen_off, sas_w_at_no_check(VID_COLS), char_height, the_char);
#endif
	/* Now search the font */
	search_font((char *)the_char,(int)char_height);
}
void ega_read_dot IFN3(int, page, int, col, int, row)
{
	int screen_off;
	int shift;
	unsigned int data;
	byte val;
	byte mask;
#ifdef	REAL_VGA
	extern half_word vga_card_read_dot();
#endif


	/*
	 * The following fixes a bug in print screen from DOS shell.
	 * There is a bug in DOS shell that results in -1 and -2 being
	 * passed through for the row.  Ignoring these values stops
	 * SoftPC falling over.
	 */
	 
	if (row & 0x8000)
		return;

	screen_off = page*sas_w_at_no_check(VID_LEN)+row*sas_w_at_no_check(VID_COLS)+(col/8);
	/*
	 * The value to return is calculated as:
	 * val = plane0 | plane1*2**1 | plane2*2**2 | plane3*2**3
	 * The masked-out bit from each plane must therefore be
	 * shifted right to bit 0 (note it may already be there)
	 * and then shifted up again by the appropriate amount for
	 * each plane.
	 */

	mask = 0x80 >> (col & 7);
	shift = 7 - (col & 7);

#ifndef REAL_VGA

	data = *((unsigned int *) EGA_planes + screen_off );

	val = ((data >> 24) & mask) >> shift;
	val |= (((data >> 16) & mask) >> shift) << 1;
	val |= (((data >> 8) & mask) >> shift) << 2;
	val |= ((data & mask) >> shift) << 3;

#else
	val = vga_card_read_dot(screen_off, shift);
#endif
	setAL(val);
}

/*
 * Routine to grovel around with the fancy EGA mode tables to find the register parameters.
 * This is also called by the mouse driver, because it needs to know where the default 
 * EGA register table for the current mode is stored.
 */
sys_addr find_mode_table IFN2(int, mode, sys_addr *, save_addr)
{
	sys_addr params_addr;
/*  get address of the SAVEPTR table, and hence the video params table. */
	*save_addr = follow_ptr(EGA_SAVEPTR);
	params_addr = follow_ptr(*save_addr) + mode*EGA_PARMS_SIZE;
/*  If we are modes F or 10, adjust to pick up the 256K EGA parameters */

#ifdef NTVDM
    /* only take real mode number */
    mode &= 0x7F;
#endif
	if(mode == 0xF || mode == 0x10)
		params_addr += 2*EGA_PARMS_SIZE;
#ifdef VGG
	if (video_adapter == VGA) 
	{
#ifdef V7VGA
	    /* If mode is 0x60+, pick up parameters from 0x1d onwards */
	    if (mode >= 0x60)
		params_addr -= 67*EGA_PARMS_SIZE;
	    else
	    if (mode >= 0x40)
		params_addr -= 25*EGA_PARMS_SIZE;
	    else
#endif /* V7VGA */
/*  If we are modes 0x11 - 0x13, pick up parameters from entry 0x1a onwards */
	    if(mode == 0x11 || mode == 0x12 || mode == 0x13)
		params_addr += 9*EGA_PARMS_SIZE;
	    else if(mode < 4 || mode == 7) /* Alphanumeric mode */
	    {
		switch(get_VGA_lines())
		{
		  case S350:	/* EGA-type 350 scanlines */
			params_addr += 19*EGA_PARMS_SIZE;
			break;
		  case S400:	/* Real VGA text mode */
			switch(mode)
			{
			   case 0:
			   case 1:
				params_addr += (0x17-mode)*EGA_PARMS_SIZE;
				break;
			   case 2:
			   case 3:
				params_addr += (0x18-mode)*EGA_PARMS_SIZE;
				break;
			   case 7:
				params_addr += (0x19-mode)*EGA_PARMS_SIZE;
			}
		  default:	/* 200 scanlines - OK as is. */
			break;
		}
	    }
	}
	else
#endif	/* VGG */
	{		/* EGA */
/*  If modes 0-3, activate enhancement if switches say so */
	    if( (get_EGA_switches() & 1) && mode < 4)
			params_addr += 19*EGA_PARMS_SIZE;
	}

#if defined(NTVDM) && defined(X86GFX)
	/*
	* Tim August 92, Microsoft.
	* Make text modes (0-3) use our mode parameters in KEYBOARD.SYS
	* Three entries in table: 40x25, 80x25 & 80x25 mono
	* Make that 4 - add font load mode B. We have to be defensive in
	* case of dubious values from cards or m/c. (Pro II/EISA, Olivetti MP)
	* Table order: 40x25, 80x25, mono, font
	*/
	{
		extern word babyModeTable;
		extern UTINY tempbabymode[];

		if(babyModeTable == 0)    /* ntio not loaded - use temp table */
		{
		    if (!soft_reset)       /* be absolutely sure about this */
		    {
			/* magic location:good until 16 bit code is running */
			sas_stores(0x41000, tempbabymode, 2 * EGA_PARMS_SIZE);
			if (mode == 0xb)
			    params_addr = 0x41000 + EGA_PARMS_SIZE;
			else
			    params_addr = 0x41000;  /* if not mode 3 tough */
			return params_addr;
		    }
#ifndef PROD
		    else
			printf("NTVDM:video window parm table not loaded but system initialised\n");
#endif
		}
		if(babyModeTable > 0)
		{
		    if (get_VGA_lines() == S350 && mode < 4)
		    {
			if (mode < 2)
			    params_addr = babyModeTable + 4*EGA_PARMS_SIZE;
			else
			    params_addr = babyModeTable + 5*EGA_PARMS_SIZE;
		    }
		    else
		    {
			if (mode < 4)
			{
			    mode = mode/2;
				params_addr = babyModeTable + mode*EGA_PARMS_SIZE;
			}
			else
			{
			    if (mode == 0xb)
				params_addr = babyModeTable + 3 * EGA_PARMS_SIZE;
			    else if (mode == 7)	/* skip first 2 table entries */
				params_addr = babyModeTable + 2 * EGA_PARMS_SIZE;
			}
		    }
		}
	}
#endif	/* NTVDM & X86GFX */

	return params_addr;
}

/*
 * Calculate how many scanlines are currently displayed, and return a code:
 * RS200: 200 scanlines
 * RS350: 350 scanlines
 * RS400: 400 scanlines
 * RS480: 480 scanlines
 *
 * Different numbers of scanlines are returned as the code corresonding
 * to the nearest kosher scanline number.
 */

int get_scanlines IFN0()
{
	int scanlines,res;

	scanlines = sas_hw_at_no_check(ega_char_height) * sas_hw_at_no_check(vd_rows_on_screen);

	if(scanlines <= 275)
		res = RS200;
	else if(scanlines <=375)
		res = RS350;
	else if(scanlines <= 440)
		res = RS400;
	else 
		res = RS480;

	return (res);
}

#endif /* EGG */