File: vgabios.c

package info (click to toggle)
vgabios 0.8a%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 628 kB
  • sloc: ansic: 12,621; makefile: 99; sh: 18
file content (4784 lines) | stat: -rw-r--r-- 95,358 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
/*
 * $Id: vgabios.c 288 2021-05-28 19:05:28Z vruppert $
 */
// ============================================================================================
//
//  Copyright (C) 2001-2021 The LGPL VGABios developers Team
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
//
// ============================================================================================
//
//  This VGA Bios is specific to the Bochs Emulated VGA card.
//  You can NOT drive any physical vga card with it.
//
// ============================================================================================
//
//  This file contains code ripped from :
//   - rombios.c of plex86
//
//  This VGA Bios contains fonts from :
//   - fntcol16.zip (c) by Joseph Gil avalable at :
//      ftp://ftp.simtel.net/pub/simtelnet/msdos/screen/fntcol16.zip
//     These fonts are public domain
//
//  This VGA Bios is based on information taken from :
//   - Kevin Lawton's vga card emulation for bochs/plex86
//   - Ralf Brown's interrupts list available at http://www.cs.cmu.edu/afs/cs/user/ralf/pub/WWW/files.html
//   - Finn Thogersons' VGADOC4b available at http://home.worldonline.dk/~finth/
//   - Michael Abrash's Graphics Programming Black Book
//   - Francois Gervais' book "programmation des cartes graphiques cga-ega-vga" edited by sybex
//   - DOSEMU 1.0.1 source code for several tables values and formulas
//
// Thanks for patches, comments and ideas to :
//   - techt@pikeonline.net
//
// ============================================================================================

#include "vgabios.h"

#ifdef VBE
#include "vbe.h"
#define VGAEXT
#define VGAEXT_is_8bpp_mode    vbe_is_8bpp_mode
#define VGAEXT_8bpp_write_char vbe_8bpp_write_char
#define VGAEXT_8bpp_copy       vbe_8bpp_copy
#define VGAEXT_8bpp_fill       vbe_8bpp_fill
#endif
#ifdef CIRRUS
#define VGAEXT
#define VGAEXT_is_8bpp_mode    cirrus_is_8bpp_mode
#define VGAEXT_8bpp_write_char cirrus_bitblt_write_char
#define VGAEXT_8bpp_copy       cirrus_bitblt_copy
#define VGAEXT_8bpp_fill       cirrus_bitblt_fill
#endif
#ifdef BANSHEE
#define VGAEXT
#define VGAEXT_is_8bpp_mode    banshee_is_8bpp_mode
#define VGAEXT_8bpp_write_char banshee_8bpp_write_char
#define VGAEXT_8bpp_copy       banshee_8bpp_copy
#define VGAEXT_8bpp_fill       banshee_8bpp_fill
#endif

#define USE_BX_INFO

/* Declares */
static Bit8u          read_byte();
static Bit8u          read_bda_byte();
static Bit16u         read_word();
static Bit16u         read_bda_word();
static void           write_byte();
static void           write_bda_byte();
static void           write_word();
static void           write_bda_word();
static Bit8u          inb();
static Bit16u         inw();
static void           outb();
static void           outw();

static Bit16u         get_SS();

// Output
static void           printf();
static void           unimplemented();
static void           unknown();

static Bit8u find_vga_entry();
static void load_dac_palette();
static void set_cursor_pos();

static void memsetb();
static void memsetw();
static void memcpyb();
static void memcpyw();

static void biosfn_set_video_mode();
static void biosfn_set_active_page();
static void biosfn_scroll();
static void biosfn_read_char_attr();
static void biosfn_write_char_attr();
static void biosfn_write_char_only();
static void biosfn_write_pixel();
static void biosfn_read_pixel();
static void biosfn_write_teletype();
static void biosfn_load_text_user_pat();
static void biosfn_load_text_8_14_pat();
static void biosfn_load_text_8_8_pat();
static void biosfn_load_text_8_16_pat();
static void biosfn_load_gfx_8_8_chars();
static void biosfn_load_gfx_user_chars();
static void biosfn_load_gfx_8_14_chars();
static void biosfn_load_gfx_8_8_dd_chars();
static void biosfn_load_gfx_8_16_chars();
static void biosfn_alternate_prtsc();
static void biosfn_switch_video_interface();
static void biosfn_enable_video_refresh_control();
static void biosfn_write_string();
extern Bit8u video_save_pointer_table[];

// This is for compiling with gcc2 and gcc3
#define ASM_START #asm
#define ASM_END   #endasm

ASM_START

MACRO SET_INT_VECTOR
  push ds
  xor ax, ax
  mov ds, ax
  mov ax, ?3
  mov ?1*4, ax
  mov ax, ?2
  mov ?1*4+2, ax
  pop ds
MEND

ASM_END

ASM_START
.text
.rom

#ifdef BANSHEE
.org 0x7ff8
.byte 0x1a, 0x12, 0x04, 0x00
#endif

.org 0

use16 386

vgabios_start:
.byte 0x55, 0xaa /* BIOS signature, required for BIOS extensions */

.byte 0x40       /* BIOS extension length in units of 512 bytes */


vgabios_entry_point:

  jmp vgabios_init_func

#ifdef PCIBIOS
.org 0x18
.word vgabios_pci_data
#endif

// Info from Bart Oldeman
.org 0x1e
.ascii  "IBM"
.byte   0x00

vgabios_name:
.ascii	"Bochs VGABios"
#ifdef PCIBIOS
.ascii	" (PCI)"
#endif
.ascii	" "
.byte	0x00

vgabios_version:
#ifndef VGABIOS_VERS
.ascii	"current-svn"
#else
.ascii VGABIOS_VERS
#endif
.ascii	" "

vgabios_date:
.ascii  VGABIOS_DATE
.byte   0x0a,0x0d
.byte	0x00

vgabios_copyright:
.ascii	"(C) 2002-2021 the LGPL VGABios developers Team"
.byte	0x0a,0x0d
.byte	0x00

vgabios_license:
.ascii	"This VGA/VBE Bios is released under the GNU LGPL"
.byte	0x0a,0x0d
.byte	0x0a,0x0d
.byte	0x00

vgabios_website:
.ascii	"Please visit :"
.byte	0x0a,0x0d
.ascii	" . http://bochs.sourceforge.net"
.byte	0x0a,0x0d
.ascii	" . http://www.nongnu.org/vgabios"
.byte	0x0a,0x0d
.byte	0x0a,0x0d
.byte	0x00

#ifdef PCIBIOS
.align 4 // DWORD alignment required by PCI Firmware Specification
vgabios_pci_data:
.ascii "PCIR"
#ifdef CIRRUS
.word 0x1013
.word 0x00b8 // CLGD5446
#elif defined(PCI_VID) && defined(PCI_DID)
.word PCI_VID
.word PCI_DID
#elif defined(VBE)
.word 0x1234
.word 0x1111 // Bochs VBE support
#elif defined(BANSHEE)
.word 0x121a
.word 0x0003 // Banshee PCI
#else
#error "Unknown PCI vendor and device id"
#endif
.word 0 // reserved
.word 0x18 // dlen
.byte 0 // revision
.byte 0x0 // class,hi: vga display
.word 0x300 // class,lo: vga display
.word 0x40 // bios size
.word 1 // revision
.byte 0 // intel x86 data
.byte 0x80 // last image
.word 0 // reserved
#endif


;; ============================================================================================
;;
;; Init Entry point
;;
;; ============================================================================================
vgabios_init_func:

;; init vga card
  call init_vga_card

;; init basic bios vars
  call init_bios_area

#ifdef VBE
;; init vbe functions
  call vbe_init
#endif

;; set int10 vect
  SET_INT_VECTOR(0x10, #0xC000, #vgabios_int10_handler)

#ifdef CIRRUS
  call cirrus_init
#endif

#ifdef BANSHEE
  call banshee_init
#endif

;; display splash screen
  call _display_splash_screen

;; init video mode and clear the screen
  mov ax,#0x0003
  int #0x10

;; show info
  call _display_info

#ifdef VBE
;; show vbe info
  call vbe_display_info
#endif

#ifdef CIRRUS
;; show cirrus info
  call cirrus_display_info
#endif

#ifdef BANSHEE
;; show 3dfx Banshee info
  call banshee_display_info
#endif

  retf
ASM_END

/*
 *  int10 handled here
 */
ASM_START
vgabios_int10_handler:
  pushf
#ifdef DEBUG
  push es
  push ds
  pusha
  mov   bx, #0xc000
  mov   ds, bx
  call _int10_debugmsg
  popa
  pop ds
  pop es
#endif
#ifdef VBE
  cmp   ah, #0x4f
  jne   int10_no_vbefn
  jmp   vbe_main_handler
#endif
int10_no_vbefn:
  push  #int10_end
  cmp   ah, #0x01
  jne   int10_test_02
  jmp   biosfn_set_cursor_shape
int10_test_02:
  cmp   ah, #0x02
  jne   int10_test_03
  jmp   biosfn_set_cursor_pos
int10_test_03:
  cmp   ah, #0x03
  jne   int10_test_04
  jmp   biosfn_get_cursor_pos
int10_test_04:
  cmp   ah, #0x04
  jne   int10_test_0F
  jmp   biosfn_read_light_pen_pos
int10_test_0F:
  cmp   ah, #0x0f
  jne   int10_test_1A
  jmp   biosfn_get_video_mode
int10_test_1A:
  cmp   ah, #0x1a
  jne   int10_test_0B
  jmp   biosfn_group_1A
int10_test_0B:
  cmp   ah, #0x0b
  jne   int10_test_1103
  jmp   biosfn_group_0B
int10_test_1103:
  cmp   ax, #0x1103
  jne   int10_test_1130
  jmp   biosfn_set_text_block_specifier
int10_test_1130:
  cmp   ax, #0x1130
  jne   int10_test_12
  jmp   biosfn_get_font_info
int10_test_12:
  cmp   ah, #0x12
  jne   int10_test_10
  cmp   bl, #0x10
  jne   int10_test_BL30
  jmp   biosfn_get_ega_info
int10_test_BL30:
  cmp   bl, #0x30
  jne   int10_test_BL31
  jmp   biosfn_select_vert_res
int10_test_BL31:
  cmp   bl, #0x31
  jne   int10_test_BL32
  jmp   biosfn_enable_default_palette_loading
int10_test_BL32:
  cmp   bl, #0x32
  jne   int10_test_BL33
  jmp   biosfn_enable_video_addressing
int10_test_BL33:
  cmp   bl, #0x33
  jne   int10_test_BL34
  jmp   biosfn_enable_grayscale_summing
int10_test_BL34:
  cmp   bl, #0x34
  jne   int10_normal
  jmp   biosfn_enable_cursor_emulation
int10_test_10:
  cmp   ah, #0x10
  jne   int10_test_1B
  jmp   biosfn_group_10
int10_test_1B:
  cmp   ah, #0x1b
  jne   int10_test_1C
  jmp   biosfn_read_state_info
int10_test_1C:
  cmp   ah, #0x1c
  jne   int10_normal
  cmp   al, #0x02
  je    int10_1C02
  ja    int10_normal
  cmp   al, #0x01
  je    int10_1C01
  jmp   biosfn_read_video_state_size
int10_1C01:
  jmp   biosfn_save_video_state
int10_1C02:
  jmp   biosfn_restore_video_state
int10_normal:
  push es
  push ds
  pusha
  mov   bx, #0xc000
  mov   ds, bx
  call _int10_func
  popa
  pop ds
  pop es
  ret

int10_end:
  popf
  iret
ASM_END

#include "vgatables.h"
#include "vgafonts.h"

/*
 * Boot time harware inits
 */
ASM_START
init_vga_card:
;; switch to color mode and enable CPU access 480 lines
  mov dx, #0x3C2
  mov al, #0xC3
  outb dx,al

;; more than 64k 3C4/04
  mov dx, #0x3C4
  mov al, #0x04
  outb dx,al
  mov dx, #0x3C5
  mov al, #0x02
  outb dx,al

#if defined(USE_BX_INFO) || defined(DEBUG)
  mov  bx, #msg_vga_init
  push bx
  call _printf
  inc  sp
  inc  sp
#endif
  ret

#if defined(USE_BX_INFO) || defined(DEBUG)
msg_vga_init:
.ascii "VGABios $Id: vgabios.c 288 2021-05-28 19:05:28Z vruppert $"
.byte 0x0d,0x0a,0x00
#endif
ASM_END

// --------------------------------------------------------------------------------------------
/*
 *  Boot time bios area inits
 */
ASM_START
init_bios_area:
  push  ds
  mov   ax, # BIOSMEM_SEG
  mov   ds, ax

;; init detected hardware BIOS Area
  mov   bx, # BIOSMEM_INITIAL_MODE
  mov   ax, [bx]
  and   ax, #0xffcf
;; set 80x25 color (not clear from RBIL but usual)
  or    ax, #0x0020
  mov   [bx], ax

;; Just for the first int10 find its children

;; the default char height
  mov   bx, # BIOSMEM_CHAR_HEIGHT
  mov   al, #0x10
  mov   [bx], al

;; Clear the screen
  mov   bx, # BIOSMEM_VIDEO_CTL
  mov   al, #0x60
  mov   [bx], al

;; Set the basic screen we have
  mov   bx, # BIOSMEM_SWITCHES
  mov   al, #0xf9
  mov   [bx], al

;; Set the basic modeset options
  mov   bx, # BIOSMEM_MODESET_CTL
  mov   al, #0x51
  mov   [bx], al

;; Set the  default MSR
  mov   bx, # BIOSMEM_CURRENT_MSR
  mov   al, #0x09
  mov   [bx], al

  pop ds
  ret

_video_save_pointer_table:
  .word _video_param_table
  .word 0xc000

  .word 0 /* XXX: fill it */
  .word 0

  .word 0 /* XXX: fill it */
  .word 0

  .word 0 /* XXX: fill it */
  .word 0

  .word 0 /* XXX: fill it */
  .word 0

  .word 0 /* XXX: fill it */
  .word 0

  .word 0 /* XXX: fill it */
  .word 0

ASM_END

// --------------------------------------------------------------------------------------------
/*
 *  Boot time Splash screen
 */
static void display_splash_screen()
{
}

// --------------------------------------------------------------------------------------------
/*
 *  Tell who we are
 */

static void display_info()
{
ASM_START
 mov ax,#0xc000
 mov ds,ax
 mov si,#vgabios_name
 call _display_string
 mov si,#vgabios_version
 call _display_string

 ;;mov si,#vgabios_copyright
 ;;call _display_string
 ;;mov si,#crlf
 ;;call _display_string

 mov si,#vgabios_license
 call _display_string
 mov si,#vgabios_website
 call _display_string
ASM_END
}

static void display_string()
{
 // Get length of string
ASM_START
 mov ax,ds
 mov es,ax
 mov di,si
 xor cx,cx
 not cx
 xor al,al
 cld
 repne
  scasb
 not cx
 dec cx
 push cx

 mov ax,#0x0300
 mov bx,#0x0000
 int #0x10

 pop cx
 mov ax,#0x1301
 mov bx,#0x000b
 mov bp,si
 int #0x10
ASM_END
}

// --------------------------------------------------------------------------------------------
#ifdef DEBUG
static void int10_debugmsg(DI, SI, BP, SP, BX, DX, CX, AX, DS, ES, FLAGS)
  Bit16u DI, SI, BP, SP, BX, DX, CX, AX, ES, DS, FLAGS;
{
 // 0E is write char...
 if(GET_AH()!=0x0E)
  printf("vgabios call ah%02x al%02x bx%04x cx%04x dx%04x\n",GET_AH(),GET_AL(),BX,CX,DX);
}
#endif

// --------------------------------------------------------------------------------------------
/*
 * int10 main dispatcher
 */
static void int10_func(DI, SI, BP, SP, BX, DX, CX, AX, DS, ES, FLAGS)
  Bit16u DI, SI, BP, SP, BX, DX, CX, AX, ES, DS, FLAGS;
{

 // BIOS functions
 switch(GET_AH())
  {
   case 0x00:
     biosfn_set_video_mode(GET_AL());
     switch(GET_AL()&0x7F)
      {case 6:
        SET_AL(0x3F);
        break;
       case 0:
       case 1:
       case 2:
       case 3:
       case 4:
       case 5:
       case 7:
        SET_AL(0x30);
        break;
      default:
        SET_AL(0x20);
      }
     break;
   case 0x05:
     biosfn_set_active_page(GET_AL());
     break;
   case 0x06:
     biosfn_scroll(GET_AL(),GET_BH(),GET_CH(),GET_CL(),GET_DH(),GET_DL(),0xFF,SCROLL_UP);
     break;
   case 0x07:
     biosfn_scroll(GET_AL(),GET_BH(),GET_CH(),GET_CL(),GET_DH(),GET_DL(),0xFF,SCROLL_DOWN);
     break;
   case 0x08:
     biosfn_read_char_attr(GET_BH(),&AX);
     break;
   case 0x09:
     biosfn_write_char_attr(GET_AL(),GET_BH(),GET_BL(),CX);
     break;
   case 0x0A:
     biosfn_write_char_only(GET_AL(),GET_BH(),GET_BL(),CX);
     break;
   case 0x0C:
     biosfn_write_pixel(GET_BH(),GET_AL(),CX,DX);
     break;
   case 0x0D:
     biosfn_read_pixel(GET_BH(),CX,DX,&AX);
     break;
   case 0x0E:
     // Ralf Brown Interrupt list is WRONG on bh(page)
     // We do output only on the current page !
     biosfn_write_teletype(GET_AL(),0xff,GET_BL(),NO_ATTR);
     break;
   case 0x11:
     switch(GET_AL())
      {
       case 0x00:
       case 0x10:
        biosfn_load_text_user_pat(GET_AL(),ES,BP,CX,DX,GET_BL(),GET_BH());
        break;
       case 0x01:
       case 0x11:
        biosfn_load_text_8_14_pat(GET_AL(),GET_BL());
        break;
       case 0x02:
       case 0x12:
        biosfn_load_text_8_8_pat(GET_AL(),GET_BL());
        break;
       case 0x04:
       case 0x14:
        biosfn_load_text_8_16_pat(GET_AL(),GET_BL());
        break;
       case 0x20:
        biosfn_load_gfx_8_8_chars(ES,BP);
        break;
       case 0x21:
        biosfn_load_gfx_user_chars(ES,BP,CX,GET_BL(),GET_DL());
        break;
       case 0x22:
        biosfn_load_gfx_8_14_chars(GET_BL());
        break;
       case 0x23:
        biosfn_load_gfx_8_8_dd_chars(GET_BL());
        break;
       case 0x24:
        biosfn_load_gfx_8_16_chars(GET_BL());
        break;
#ifdef DEBUG
       default:
        unknown();
#endif
      }

     break;
   case 0x12:
     switch(GET_BL())
      {
       case 0x20:
        biosfn_alternate_prtsc();
        break;
       case 0x35:
        biosfn_switch_video_interface(GET_AL(),ES,DX);
        SET_AL(0x12);
        break;
       case 0x36:
        biosfn_enable_video_refresh_control(GET_AL());
        SET_AL(0x12);
        break;
#ifdef DEBUG
       default:
        unknown();
#endif
      }
     break;
   case 0x13:
     biosfn_write_string(GET_AL(),GET_BH(),GET_BL(),CX,GET_DH(),GET_DL(),ES,BP);
     break;
#ifdef VBE
   case 0x4f:
     if (vbe_has_vbe_display()) {
       switch(GET_AL()) {
         case 0x00:
          vbe_biosfn_return_controller_information(&AX,ES,DI);
          break;
         case 0x01:
          vbe_biosfn_return_mode_information(&AX,CX,ES,DI);
          break;
         case 0x02:
          vbe_biosfn_set_mode(&AX,BX);
          break;
       }
     } else {
       // No VBE display
       AX=0x0100;
     }
     break;
#endif

#ifdef DEBUG
   default:
     unknown();
#endif
  }
}

ASM_START
dac_palette_table:
.word _palette0
.word _palette1
.word _palette2
.word _palette3

_load_dac_palette:
  push  bp
  mov   bp, sp
  push  ax
  push  bx
  push  cx
  push  dx
  push  si
  push  ds
  mov   ax, #0xc000
  mov   ds, ax
  mov   al, 4[bp] ; palette num
  xor   ah, ah
  push  ax
  shl   ax, #1
  mov   bx, #dac_palette_table
  add   bx, ax
  mov   si, [bx]
  pop   ax
  mov   bx, #_dac_regs
  add   bx, ax
  mov   al, [bx]
  mov   bl, #0x03
  mul   bl
  add   ax, #0x03
  mov   cx, ax
  mov   bx, #0x0300
  mov   al, #0x00
  mov   dx, #VGAREG_DAC_WRITE_ADDRESS
  out   dx, al
  cld
  mov   dx, #VGAREG_DAC_DATA
dac_load_loop:
  lodsb
  out   dx, al
  dec   bx
  loop  dac_load_loop
  or    bx, bx
  jz    no_fill_dac
  mov   cx, bx
  xor   al, al
dac_fill_loop:
  out   dx, al
  loop  dac_fill_loop
no_fill_dac:
  pop   ds
  pop   si
  pop   dx
  pop   cx
  pop   bx
  pop   ax
  pop   bp
  ret
ASM_END

// ============================================================================================
//
// BIOS functions
//
// ============================================================================================

static void biosfn_set_video_mode(mode) Bit8u mode;
{// mode: Bit 7 is 1 if no clear screen

  // Should we clear the screen ?
  Bit8u noclearmem=mode&0x80;
  Bit8u line,mmask,*palette,vpti;
  Bit16u i,twidth,theightm1,cheight;
  Bit8u modeset_ctl,video_ctl,vga_switches;
  Bit16u crtc_addr;

#ifdef VBE
  if (vbe_has_vbe_display()) {
    dispi_set_enable(VBE_DISPI_DISABLED);
  }
#endif // def VBE

  // The real mode
  mode=mode&0x7f;

  // find the entry in the video modes
  line=find_vga_entry(mode);

#ifdef DEBUG
  printf("mode search %02x found line %02x\n",mode,line);
#endif

  if(line==0xFF)
    return;

  vpti=line_to_vpti[line];
  twidth=video_param_table[vpti].twidth;
  theightm1=video_param_table[vpti].theightm1;
  cheight=video_param_table[vpti].cheight;

  // Read the bios vga control
  video_ctl=read_bda_byte(BIOSMEM_VIDEO_CTL);

  // Read the bios vga switches
  vga_switches=read_bda_byte(BIOSMEM_SWITCHES);

  // Read the bios mode set control
  modeset_ctl=read_bda_byte(BIOSMEM_MODESET_CTL);

  // Then we know the number of lines
  // FIXME

  // if palette loading (bit 3 of modeset ctl = 0)
  if((modeset_ctl&0x08)==0)
  {
    // Set the PEL mask
    outb(VGAREG_PEL_MASK,vga_modes[line].pelmask);

    load_dac_palette(vga_modes[line].dacmodel);

    if((modeset_ctl&0x02)==0x02)
    {
ASM_START
      xor  bx, bx
      mov  cx, #0x0100
      call biosfn_perform_gray_scale_summing
ASM_END
    }
  }

  // Reset Attribute Ctl flip-flop
  inb(VGAREG_ACTL_RESET);

 // Set Attribute Ctl
 for(i=0;i<=0x13;i++)
  {outb(VGAREG_ACTL_ADDRESS,i);
   outb(VGAREG_ACTL_WRITE_DATA,video_param_table[vpti].actl_regs[i]);
  }
 outb(VGAREG_ACTL_ADDRESS,0x14);
 outb(VGAREG_ACTL_WRITE_DATA,0x00);

 // Set Sequencer Ctl
 outb(VGAREG_SEQU_ADDRESS,0);
 outb(VGAREG_SEQU_DATA,0x03);
 for(i=1;i<=4;i++)
  {outb(VGAREG_SEQU_ADDRESS,i);
   outb(VGAREG_SEQU_DATA,video_param_table[vpti].sequ_regs[i - 1]);
  }

 // Set Grafx Ctl
 for(i=0;i<=8;i++)
  {outb(VGAREG_GRDC_ADDRESS,i);
   outb(VGAREG_GRDC_DATA,video_param_table[vpti].grdc_regs[i]);
  }

 // Set CRTC address VGA or MDA
 crtc_addr=vga_modes[line].memmodel==MTEXT?VGAREG_MDA_CRTC_ADDRESS:VGAREG_VGA_CRTC_ADDRESS;

 // Disable CRTC write protection
 outw(crtc_addr,0x0011);
 // Set CRTC regs
 for(i=0;i<=0x18;i++)
  {outb(crtc_addr,i);
   outb(crtc_addr+1,video_param_table[vpti].crtc_regs[i]);
  }

 // Set the misc register
 outb(VGAREG_WRITE_MISC_OUTPUT,video_param_table[vpti].miscreg);

 // Enable video
 outb(VGAREG_ACTL_ADDRESS,0x20);
 inb(VGAREG_ACTL_RESET);

#ifdef BANSHEE
ASM_START
  call banshee_set_vga_mode
ASM_END
#endif

 if(noclearmem==0x00)
  {
   if(vga_modes[line].class==TEXT)
    {
     memsetw(vga_modes[line].sstart,0,0x0720,0x4000); // 32k
    }
   else
    {
     if(mode<0x0d)
      {
       memsetw(vga_modes[line].sstart,0,0x0000,0x4000); // 32k
      }
     else
      {
       outb( VGAREG_SEQU_ADDRESS, 0x02 );
       mmask = inb( VGAREG_SEQU_DATA );
       outb( VGAREG_SEQU_DATA, 0x0f ); // all planes
       memsetw(vga_modes[line].sstart,0,0x0000,0x8000); // 64k
       outb( VGAREG_SEQU_DATA, mmask );
      }
    }
  }

 // Set the BIOS mem
 write_bda_byte(BIOSMEM_CURRENT_MODE,mode);
 write_bda_word(BIOSMEM_NB_COLS,twidth);
 write_bda_word(BIOSMEM_PAGE_SIZE,*(Bit16u *)&video_param_table[vpti].slength_l);
 write_bda_word(BIOSMEM_CRTC_ADDRESS,crtc_addr);
 write_bda_byte(BIOSMEM_NB_ROWS,theightm1);
 write_bda_word(BIOSMEM_CHAR_HEIGHT,cheight);
 write_bda_byte(BIOSMEM_VIDEO_CTL,(0x60|noclearmem));
 write_bda_byte(BIOSMEM_SWITCHES,0xF9);
 write_bda_byte(BIOSMEM_MODESET_CTL,read_bda_byte(BIOSMEM_MODESET_CTL)&0x7f);

 // FIXME We nearly have the good tables. to be reworked
 write_bda_byte(BIOSMEM_DCC_INDEX,0x08);    // 8 is VGA should be ok for now
 write_bda_word(BIOSMEM_VS_POINTER, video_save_pointer_table);
 write_bda_word(BIOSMEM_VS_POINTER+2, 0xc000);

 // FIXME
 write_bda_byte(BIOSMEM_CURRENT_MSR,0x00); // Unavailable on vanilla vga, but...
 write_bda_byte(BIOSMEM_CURRENT_PAL,0x00); // Unavailable on vanilla vga, but...

 // Set cursor shape
 if(vga_modes[line].class==TEXT)
  {
ASM_START
  mov  cx, #0x0607
  call biosfn_set_cursor_shape
ASM_END
  }

 // Set cursor pos for page 0..7
 for(i=0;i<8;i++)
  set_cursor_pos(i,0x0000);

 // Set active page 0
 biosfn_set_active_page(0x00);

 // Write the fonts in memory
 if(vga_modes[line].class==TEXT)
  {
ASM_START
  ;; copy and activate 8x16 font
  mov ax, #0x1104
  mov bl, #0x00
  int #0x10
  mov ax, #0x1103
  mov bl, #0x00
  int #0x10
ASM_END
  }

 // Set the ints 0x1F and 0x43
ASM_START
 SET_INT_VECTOR(0x1f, #0xC000, #_vgafont8+128*8)
ASM_END

  switch(cheight)
   {case 8:
ASM_START
     SET_INT_VECTOR(0x43, #0xC000, #_vgafont8)
ASM_END
     break;
    case 14:
ASM_START
     SET_INT_VECTOR(0x43, #0xC000, #_vgafont14)
ASM_END
     break;
    case 16:
ASM_START
     SET_INT_VECTOR(0x43, #0xC000, #_vgafont16)
ASM_END
     break;
   }
}

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_set_cursor_shape:
  push  ds
  push  ax
  push  bx
  push  dx
  mov   ax, # BIOSMEM_SEG
  mov   ds, ax
  and   cx, #0x3f1f
  mov   bx, # BIOSMEM_CURSOR_TYPE
  mov   [bx], cx
  mov   bx, # BIOSMEM_MODESET_CTL
  mov   al, [bx]
  and   al, #0x01
  jz    no_resize
  mov   bx, # BIOSMEM_CHAR_HEIGHT
  mov   dl, [bx]
  cmp   dl, #0x08
  jb    no_resize
  cmp   cl, #0x08
  jnb   no_resize
  cmp   ch, #0x20
  jnb   no_resize
  mov   al, ch
  inc   al
  cmp   cl, al
  jz    line_cursor
  mul   al, dl
  shr   ax, #3
  jmp   setup_ch
line_cursor:
  mov   al, cl
  inc   al
  mul   al, dl
  shr   ax, #3
  dec   al
setup_ch:
  dec   al
  mov   ch, al
recalc_cl:
  mov   al, cl
  inc   al
  mul   al, dl
  shr   ax, #3
  dec   al
  mov   cl, al
no_resize:
  mov   bx, # BIOSMEM_CRTC_ADDRESS
  mov   dx, [bx]
  mov   al, #0x0a
  mov   ah, ch
  out   dx, ax
  inc   al
  mov   ah, cl
  out   dx, ax
  pop   dx
  pop   bx
  pop   ax
  pop   ds
  ret

; --------------------------------------------------------------------------------------------
biosfn_set_cursor_pos:
  push  ds
  push  ax
  push  dx
  cmp   bh, #0x07
  ja    invalid_page_1
  mov   ax, # BIOSMEM_SEG
  mov   ds, ax
  push  cx
  push  bx
  mov   cl, bh
  mov   al, bh
  xor   ah, ah
  mov   bx, # BIOSMEM_CURSOR_POS
  shl   ax, #1
  add   bx, ax
  mov   [bx], dx
  mov   bx, # BIOSMEM_CURRENT_PAGE
  cmp   cl, [bx]
  jnz   not_set_hw_cursor
  mov   bx, # BIOSMEM_CURRENT_START
  mov   ax, [bx]
  shr   ax, #1
  push  ax
  mov   bx, # BIOSMEM_NB_COLS
  mov   ah, [bx]
  mov   al, dh
  mul   al, ah
  xor   dh, dh
  add   ax, dx
  pop   bx
  add   ax, bx
  push  ax
  mov   bx, # BIOSMEM_CRTC_ADDRESS
  mov   dx, [bx]
  mov   al, #0x0e
  out   dx, ax
  pop   ax
  mov   ah, al
  mov   al, #0x0f
  out   dx, ax
not_set_hw_cursor:
  pop   bx
  pop   cx
invalid_page_1:
  pop   dx
  pop   ax
  pop   ds
  ret

; this function is called from C code
_set_cursor_pos:
  push bp
  mov  bp, sp
  mov  ax, 4[bp]
  mov  bh, al
  mov  dx, 6[bp]
  call biosfn_set_cursor_pos
  pop  bp
  ret

;--------------------------------------------------------------------------------------------
biosfn_get_cursor_pos:
  push  ds
  push  ax
  cmp   bh, #0x07
  ja    invalid_page_2
  mov   ax, # BIOSMEM_SEG
  mov   ds, ax
  xor   ax, ax
  mov   al, bh
  push  bx
  mov   bx, # BIOSMEM_CURSOR_TYPE
  mov   cx, [bx]
  mov   bx, # BIOSMEM_CURSOR_POS
  shl   ax, #1
  add   bx, ax
  mov   dx, [bx]
  pop   bx
invalid_page_2:
  pop   ax
  pop   ds
  ret

; this function is called from C code
_get_cursor_pos:
  push  bp
  mov   bp, sp
  push  ds
  mov   ax, # BIOSMEM_SEG
  mov   ds, ax
  mov   ax, 4[bp]
  cmp   al, #0x07
  jbe   page_ok
  xor   ax, ax
  jz    page_ret
page_ok:
  xor   ah, ah
  shl   ax, #1
  mov   bx, # BIOSMEM_CURSOR_POS
  add   bx, ax
  mov   ax, [bx]
page_ret:
  pop   ds
  pop   bp
  ret

;--------------------------------------------------------------------------------------------
biosfn_read_light_pen_pos:
;; unsupported on VGA
#ifdef DEBUG
  call _unimplemented
#endif
  xor   ax, ax
  xor   bx, bx
  xor   cx, cx
  xor   dx, dx
  ret
ASM_END

// --------------------------------------------------------------------------------------------
static void biosfn_set_active_page (page)
Bit8u page;
{
 Bit16u cursor,crtc_addr;
 Bit16u pgsize,address;
 Bit8u mode,line;

 if(page>7)return;

 // Get the mode
 mode=read_bda_byte(BIOSMEM_CURRENT_MODE);
 line=find_vga_entry(mode);
 if(line==0xFF)return;

 // Get pos curs pos for the right page
 cursor=get_cursor_pos(page);

 if(vga_modes[line].class==TEXT)
  {
   // Get the page size
   pgsize=read_bda_word(BIOSMEM_PAGE_SIZE);

   // Calculate the mem address
   address=pgsize*page;
   write_bda_word(BIOSMEM_CURRENT_START,address);

   // Now the CRTC start address
   address>>=1;
  }
 else
  {
   address = page * (*(Bit16u *)&video_param_table[line_to_vpti[line]].slength_l);
  }

 // CRTC regs 0x0c and 0x0d
 crtc_addr=read_bda_word(BIOSMEM_CRTC_ADDRESS);
 outb(crtc_addr,0x0c);
 outb(crtc_addr+1,(address&0xff00)>>8);
 outb(crtc_addr,0x0d);
 outb(crtc_addr+1,address&0x00ff);

 // And change the BIOS page
 write_bda_byte(BIOSMEM_CURRENT_PAGE,page);

#ifdef DEBUG
 printf("Set active page %02x address %04x\n",page,address);
#endif

 // Display the cursor, now the page is active
 set_cursor_pos(page,cursor);
}

// --------------------------------------------------------------------------------------------
static void vgamem_copy_pl4(xstart,ysrc,ydest,cols,nbcols,cheight)
Bit8u xstart;Bit8u ysrc;Bit8u ydest;Bit8u cols;Bit8u nbcols;Bit8u cheight;
{
 Bit16u src,dest;
 Bit8u i;

 src=ysrc*cheight*nbcols+xstart;
 dest=ydest*cheight*nbcols+xstart;
 outw(VGAREG_GRDC_ADDRESS, 0x0105);
 for(i=0;i<cheight;i++)
  {
   memcpyb(0xa000,dest+i*nbcols,0xa000,src+i*nbcols,cols);
  }
 outw(VGAREG_GRDC_ADDRESS, 0x0005);
}

// --------------------------------------------------------------------------------------------
static void vgamem_fill_pl4(xstart,ystart,cols,nbcols,cheight,attr)
Bit8u xstart;Bit8u ystart;Bit8u cols;Bit8u nbcols;Bit8u cheight;Bit8u attr;
{
 Bit16u dest;
 Bit8u i;

 dest=ystart*cheight*nbcols+xstart;
 outw(VGAREG_GRDC_ADDRESS, 0x0205);
 for(i=0;i<cheight;i++)
  {
   memsetb(0xa000,dest+i*nbcols,attr,cols);
  }
 outw(VGAREG_GRDC_ADDRESS, 0x0005);
}

// --------------------------------------------------------------------------------------------
static void vgamem_copy_cga(xstart,ysrc,ydest,cols,nbcols,cheight)
Bit8u xstart;Bit8u ysrc;Bit8u ydest;Bit8u cols;Bit8u nbcols;Bit8u cheight;
{
 Bit16u src,dest;
 Bit8u i;

 src=((ysrc*cheight*nbcols)>>1)+xstart;
 dest=((ydest*cheight*nbcols)>>1)+xstart;
 for(i=0;i<cheight;i++)
  {
   if (i & 1)
     memcpyb(0xb800,0x2000+dest+(i>>1)*nbcols,0xb800,0x2000+src+(i>>1)*nbcols,cols);
   else
     memcpyb(0xb800,dest+(i>>1)*nbcols,0xb800,src+(i>>1)*nbcols,cols);
  }
}

// --------------------------------------------------------------------------------------------
static void vgamem_fill_cga(xstart,ystart,cols,nbcols,cheight,attr)
Bit8u xstart;Bit8u ystart;Bit8u cols;Bit8u nbcols;Bit8u cheight;Bit8u attr;
{
 Bit16u dest;
 Bit8u i;

 dest=((ystart*cheight*nbcols)>>1)+xstart;
 for(i=0;i<cheight;i++)
  {
   if (i & 1)
     memsetb(0xb800,0x2000+dest+(i>>1)*nbcols,attr,cols);
   else
     memsetb(0xb800,dest+(i>>1)*nbcols,attr,cols);
  }
}

// --------------------------------------------------------------------------------------------
ASM_START
; arguments: xstart,ysrc,ydest,cols,nbcols,cheight
_vgamem_copy_lin:
  push bp
  mov  bp, sp
#ifdef VGAEXT
  call VGAEXT_is_8bpp_mode
  or   ax, ax
  jz   lin_copy_vga
  call VGAEXT_8bpp_copy
  pop  bp
  ret
lin_copy_vga:
#endif
  push ax
  push bx
  push cx
  push dx
  push ds
  push es
  push si
  push di
  mov  ax, 4[bp] ; xstart
  shl  ax, #3
  push ax
  mov  al, 12[bp] ; nbcols
  mov  bl, 14[bp] ; cheight
  mul  bl
  shl  ax, #3
  mov  cx, ax
  xor  ax, ax
  mov  dx, 6[bp] ; ysrc
  or   dx, dx
  jz   lin_copy_set_start1
  mov  ax, cx
  mov  bx, dx
  mul  bx
lin_copy_set_start1:
  pop  bx
  push bx
  add  ax, bx
  mov  si, ax
  xor  ax, ax
  mov  dx, 8[bp] ; ydest
  or   dx, dx
  jz   lin_copy_set_start2
  mov  ax, cx
  mov  bx, dx
  mul  bx
lin_copy_set_start2:
  pop  bx
  add  ax, bx
  mov  di, ax
  mov  ax, #0xa000
  mov  ds, ax
  mov  es, ax
  mov  bl, 14[bp] ; cheight
  mov  cx, 10[bp] ; cols
  shl  cx, #2
  mov  dx, 12[bp] ; nbcols
  shl  dx, #3
lin_copy_loop:
  push cx
  push si
  push di
  cld
  rep
    movsw
  pop  di
  pop  si
  pop  cx
  dec  bl
  jz   lin_copy_end
  add  si, dx
  add  di, dx
  jmp  lin_copy_loop
lin_copy_end:
  pop  di
  pop  si
  pop  es
  pop  ds
  pop  dx
  pop  cx
  pop  bx
  pop  ax
  pop  bp
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
; arguments: xstart,ystart,cols,rows,nbcols,cheight,attr
_vgamem_fill_lin:
  push bp
  mov  bp, sp
#ifdef VGAEXT
  call VGAEXT_is_8bpp_mode
  or   ax, ax
  jz   lin_fill_vga
  call VGAEXT_8bpp_fill
  pop  bp
  ret
lin_fill_vga:
#endif
  push ax
  push bx
  push cx
  push dx
  push es
  push di
  mov  ax, 4[bp] ; xstart
  shl  ax, #3
  push ax
  xor  ax, ax
  mov  dx, 6[bp] ; ystart
  or   dx, dx
  jz   lin_fill_set_start
  mov  al, 12[bp] ; nbcols
  mov  bl, 14[bp] ; cheight
  mul  bl
  shl  ax, #3
  mov  bx, dx
  mul  bx
lin_fill_set_start:
  pop  bx
  add  ax, bx
  mov  di, ax
  mov  ax, #0xa000
  mov  es, ax
  mov  bl, 14[bp] ; cheight
  mov  al, 10[bp] ; rows
  mul  bl
  mov  bx, ax
  mov  al, 16[bp] ; attr
  mov  ah, al
  mov  cx, 8[bp] ; cols
  shl  cx, #2
  mov  dx, 12[bp] ; nbcols
  shl  dx, #3
lin_fill_loop:
  push cx
  push di
  cld
  rep
    stosw
  pop  di
  pop  cx
  dec  bx
  jz   lin_fill_end
  add  di, dx
  jmp  lin_fill_loop
lin_fill_end:
  pop  di
  pop  es
  pop  dx
  pop  cx
  pop  bx
  pop  ax
  pop  bp
  ret
ASM_END

// --------------------------------------------------------------------------------------------
static void biosfn_scroll (nblines,attr,rul,cul,rlr,clr,page,dir)
Bit8u nblines;Bit8u attr;Bit8u rul;Bit8u cul;Bit8u rlr;Bit8u clr;Bit8u page;Bit8u dir;
{
 // page == 0xFF if current

 Bit8u mode,line,cheight,bpp,cols;
 Bit16u nbcols,nbrows,i;
 Bit16u address,pgsize;

 if(rul>rlr)return;
 if(cul>clr)return;

 // Get the mode
 mode=read_bda_byte(BIOSMEM_CURRENT_MODE);
 line=find_vga_entry(mode);
#ifdef VGAEXT
 if (VGAEXT_is_8bpp_mode())
  line = 14;
#endif
 if(line==0xFF)return;

 // Get the dimensions
 nbrows=read_bda_byte(BIOSMEM_NB_ROWS)+1;
 nbcols=read_bda_word(BIOSMEM_NB_COLS);

 // Get the current page
 if(page==0xFF)
  page=read_bda_byte(BIOSMEM_CURRENT_PAGE);

 if(rlr>=nbrows)rlr=nbrows-1;
 if(clr>=nbcols)clr=nbcols-1;
 if(nblines>nbrows)nblines=0;
 cols=clr-cul+1;

 if(vga_modes[line].class==TEXT)
  {
   // Get the page size
   pgsize=read_bda_word(BIOSMEM_PAGE_SIZE);
   // Compute the address
   address=pgsize*page;
#ifdef DEBUG
   printf("Scroll, address %04x (%04x %04x %02x)\n",address,nbrows,nbcols,page);
#endif

   if(nblines==0&&rul==0&&cul==0&&rlr==nbrows-1&&clr==nbcols-1)
    {
     memsetw(vga_modes[line].sstart,address,(Bit16u)attr*0x100+' ',nbrows*nbcols);
    }
   else
    {// if Scroll up
     if(dir==SCROLL_UP)
      {for(i=rul;i<=rlr;i++)
        {
         if((i+nblines>rlr)||(nblines==0))
          memsetw(vga_modes[line].sstart,address+(i*nbcols+cul)*2,(Bit16u)attr*0x100+' ',cols);
         else
          memcpyw(vga_modes[line].sstart,address+(i*nbcols+cul)*2,vga_modes[line].sstart,((i+nblines)*nbcols+cul)*2,cols);
        }
      }
     else
      {for(i=rlr;i>=rul;i--)
        {
         if((i<rul+nblines)||(nblines==0))
          memsetw(vga_modes[line].sstart,address+(i*nbcols+cul)*2,(Bit16u)attr*0x100+' ',cols);
         else
          memcpyw(vga_modes[line].sstart,address+(i*nbcols+cul)*2,vga_modes[line].sstart,((i-nblines)*nbcols+cul)*2,cols);
         if (i>rlr) break;
        }
      }
    }
  }
 else
  {
   // FIXME gfx modes > 8 bpp not supported
   cheight=read_bda_byte(BIOSMEM_CHAR_HEIGHT);
   if(nblines==0&&rul==0&&cul==0&&rlr==nbrows-1&&clr==nbcols-1&&vga_modes[line].memmodel!=LINEAR8)
    {
     switch(vga_modes[line].memmodel)
      {
       case PLANAR4:
       case PLANAR1:
         outw(VGAREG_GRDC_ADDRESS, 0x0205);
         memsetb(vga_modes[line].sstart,0,attr,nbrows*nbcols*cheight);
         outw(VGAREG_GRDC_ADDRESS, 0x0005);
         break;
       case CGA:
         bpp=vga_modes[line].pixbits;
         memsetb(vga_modes[line].sstart,0,attr,nbrows*nbcols*cheight*bpp);
         break;
      }
    }
   else
    {
     switch(vga_modes[line].memmodel)
      {
       case PLANAR4:
       case PLANAR1:
         if(dir==SCROLL_UP)
          {for(i=rul;i<=rlr;i++)
            {
             if((i+nblines>rlr)||(nblines==0))
              vgamem_fill_pl4(cul,i,cols,nbcols,cheight,attr);
             else
              vgamem_copy_pl4(cul,i+nblines,i,cols,nbcols,cheight);
            }
          }
         else
          {for(i=rlr;i>=rul;i--)
            {
             if((i<rul+nblines)||(nblines==0))
              vgamem_fill_pl4(cul,i,cols,nbcols,cheight,attr);
             else
              vgamem_copy_pl4(cul,i-nblines,i,cols,nbcols,cheight);
            }
          }
         break;
       case CGA:
         bpp=vga_modes[line].pixbits;
         if(bpp==2)
          {
           cul<<=1;
           cols<<=1;
           nbcols<<=1;
          }
         // if Scroll up
         if(dir==SCROLL_UP)
          {for(i=rul;i<=rlr;i++)
            {
             if((i+nblines>rlr)||(nblines==0))
              vgamem_fill_cga(cul,i,cols,nbcols,cheight,attr);
             else
              vgamem_copy_cga(cul,i+nblines,i,cols,nbcols,cheight);
            }
          }
         else
          {for(i=rlr;i>=rul;i--)
            {
             if((i<rul+nblines)||(nblines==0))
              vgamem_fill_cga(cul,i,cols,nbcols,cheight,attr);
             else
              vgamem_copy_cga(cul,i-nblines,i,cols,nbcols,cheight);
            }
          }
         break;
       case LINEAR8:
         if(nblines==0)
          vgamem_fill_lin(cul,rul,cols,rlr-rul+1,nbcols,cheight,attr);
         else if(dir==SCROLL_UP)
          {for(i=rul;i<=rlr;i++)
            {
             if(i+nblines>rlr)
              vgamem_fill_lin(cul,i,cols,1,nbcols,cheight,attr);
             else
              vgamem_copy_lin(cul,i+nblines,i,cols,nbcols,cheight);
            }
          }
         else
          {for(i=rlr;i>=rul;i--)
            {
             if(i<rul+nblines)
              vgamem_fill_lin(cul,i,cols,1,nbcols,cheight,attr);
             else
              vgamem_copy_lin(cul,i-nblines,i,cols,nbcols,cheight);
            }
          }
         break;
#ifdef DEBUG
       default:
         printf("Scroll in graphics mode ");
         unimplemented();
#endif
      }
    }
  }
}

// --------------------------------------------------------------------------------------------
static void biosfn_read_char_attr (page,car)
Bit8u page;Bit16u *car;
{Bit16u ss=get_SS();
 Bit8u xcurs,ycurs,mode,line;
 Bit16u nbcols,pgsize,address;
 Bit16u cursor;

 // Get the mode
 mode=read_bda_byte(BIOSMEM_CURRENT_MODE);
 line=find_vga_entry(mode);
 if(line==0xFF)return;

 // Get the cursor pos for the page
 cursor=get_cursor_pos(page);
 xcurs=cursor&0x00ff;ycurs=(cursor&0xff00)>>8;

 // Get the dimensions
 nbcols=read_bda_word(BIOSMEM_NB_COLS);

 if(vga_modes[line].class==TEXT)
  {
   // Get the page size
   pgsize=read_bda_word(BIOSMEM_PAGE_SIZE);
   // Compute the address
   address=pgsize*page+(xcurs+ycurs*nbcols)*2;

   write_word(ss,car,read_word(vga_modes[line].sstart,address));
  }
 else
  {
   // FIXME gfx mode
#ifdef DEBUG
   unimplemented();
#endif
  }
}

// --------------------------------------------------------------------------------------------
static void write_gfx_char_pl4(car,attr,xcurs,ycurs,nbcols,cheight)
Bit8u car;Bit8u attr;Bit8u xcurs;Bit8u ycurs;Bit8u nbcols;Bit8u cheight;
{
 Bit8u i,j,mask;
 Bit8u fdata;
 Bit16u fseg,foffs;
 Bit16u addr,dest,src;

 addr=xcurs+ycurs*cheight*nbcols;
 foffs = read_word(0x0, 0x43*4);
 fseg = read_word(0x0, 0x43*4+2);
 src = foffs + car * cheight;
ASM_START
  push bp
  mov  bp, sp
  push ax
  push bx
  push dx
  mov  dx, # VGAREG_SEQU_ADDRESS
  mov  ax, #0x0f02
  out  dx, ax
  mov  dx, # VGAREG_GRDC_ADDRESS
  mov  ax, #0x0205
  out  dx, ax
  mov  ax, #0x0003
  mov  bx, 22[bp] ; attr
  test bl, #0x80
  jz   no_xor
  mov  ah, #0x18
no_xor:
  out  dx, ax
  pop  dx
  pop  bx
  pop  ax
  pop  bp
ASM_END
 for(i=0;i<cheight;i++)
  {
   fdata = read_byte(fseg, src + i);
   dest=addr+i*nbcols;
   for(j=0;j<8;j++)
    {
     mask=0x80>>j;
     outw(VGAREG_GRDC_ADDRESS, (mask << 8) | 0x08);
     read_byte(0xa000,dest);
     if(fdata&mask)
      {
       write_byte(0xa000,dest,attr&0x0f);
      }
     else
      {
       write_byte(0xa000,dest,0x00);
      }
    }
  }
ASM_START
  mov dx, # VGAREG_GRDC_ADDRESS
  mov ax, #0xff08
  out dx, ax
  mov ax, #0x0005
  out dx, ax
  mov ax, #0x0003
  out dx, ax
ASM_END
}

// --------------------------------------------------------------------------------------------
static void write_gfx_char_cga(car,attr,xcurs,ycurs,nbcols,bpp)
Bit8u car;Bit8u attr;Bit8u xcurs;Bit8u ycurs;Bit8u nbcols;Bit8u bpp;
{
 Bit8u i,j,mask,data;
 Bit8u *fdata;
 Bit16u addr,dest,src;

 fdata = &vgafont8;
 addr=(xcurs*bpp)+ycurs*320;
 src = car * 8;
 for(i=0;i<8;i++)
  {
   dest=addr+(i>>1)*80;
   if (i & 1) dest += 0x2000;
   mask = 0x80;
   if (bpp == 1)
    {
     if (attr & 0x80)
      {
       data = read_byte(0xb800,dest);
      }
     else
      {
       data = 0x00;
      }
     for(j=0;j<8;j++)
      {
       if (fdata[src+i] & mask)
        {
         if (attr & 0x80)
          {
           data ^= (attr & 0x01) << (7-j);
          }
         else
          {
           data |= (attr & 0x01) << (7-j);
          }
        }
       mask >>= 1;
      }
     write_byte(0xb800,dest,data);
    }
   else
    {
     while (mask > 0)
      {
       if (attr & 0x80)
        {
         data = read_byte(0xb800,dest);
        }
       else
        {
         data = 0x00;
        }
       for(j=0;j<4;j++)
        {
         if (fdata[src+i] & mask)
          {
           if (attr & 0x80)
            {
             data ^= (attr & 0x03) << ((3-j)*2);
            }
           else
            {
             data |= (attr & 0x03) << ((3-j)*2);
            }
          }
         mask >>= 1;
        }
       write_byte(0xb800,dest,data);
       dest += 1;
      }
    }
  }
}

// --------------------------------------------------------------------------------------------
ASM_START
; arguments: car,attr,xcurs,ycurs,nbcols,cheight
_write_gfx_char_lin:
  push bp
  mov  bp, sp
#ifdef VGAEXT
  call VGAEXT_is_8bpp_mode
  or   ax, ax
  jz   lin_wc_vga
  call VGAEXT_8bpp_write_char
  pop  bp
  ret
lin_wc_vga:
#endif
  push ax
  push bx
  push cx
  push dx
  push ds
  push es
  push si
  push di
  xor  ax, ax
  mov  ds, ax
  mov  bx, #0x10c ; INT 0x43
  mov  si, [bx]
  mov  ax, [bx+2]
  mov  ds, ax
  mov  bx, 14[bp] ; cheight
  mov  al, 4[bp]  ; character
  mul  bl
  add  si, ax
  mov  ax, 8[bp] ; xcurs
  shl  ax, #3
  push ax
  mov  dx, 10[bp] ; ycurs
  or   dx, dx
  jz   lin_wc_set_start
  mov  al, 12[bp] ; nbcols
  mov  bl, 14[bp] ; cheight
  mul  bl
  shl  ax, #3
  mov  bx, dx
  mul  bx
lin_wc_set_start:
  pop  bx
  add  ax, bx
  mov  di, ax
  mov  ax, #0xa000
  mov  es, ax
  mov  bl, 14[bp] ; cheight
  mov  dx, 12[bp] ; nbcols
  shl  dx, #3
lin_wc_loop1:
  push di
  lodsb
  mov  cx, #0x08
  cld
lin_wc_loop2:
  shl  al, #1
  push ax
  jnc  lin_wc_set_bkgnd
  mov  al, 6[bp] ; attr
  db    0xa9 ; skip next opcode (TEST AX, #0xC030)
lin_wc_set_bkgnd:
  xor  al, al
  stosb
  pop  ax
  loop lin_wc_loop2
  pop  di
  dec  bl
  jz   lin_wc_end
  add  di, dx
  jmp  lin_wc_loop1
lin_wc_end:
  pop  di
  pop  si
  pop  es
  pop  ds
  pop  dx
  pop  cx
  pop  bx
  pop  ax
  pop  bp
  ret
ASM_END

// --------------------------------------------------------------------------------------------
static void biosfn_write_char_attr (car,page,attr,count)
Bit8u car;Bit8u page;Bit8u attr;Bit16u count;
{
 Bit8u cheight,xcurs,ycurs,mode,line,bpp;
 Bit16u nbcols,pgsize,address;
 Bit16u cursor,carattr;

 // Get the mode
 mode=read_bda_byte(BIOSMEM_CURRENT_MODE);
 line=find_vga_entry(mode);
#ifdef VGAEXT
 if (VGAEXT_is_8bpp_mode())
  line = 14;
#endif
 if(line==0xFF)return;

 // Get the cursor pos for the page
 cursor=get_cursor_pos(page);
 xcurs=cursor&0x00ff;ycurs=(cursor&0xff00)>>8;

 // Get the dimensions
 nbcols=read_bda_word(BIOSMEM_NB_COLS);

 if(vga_modes[line].class==TEXT)
  {
   // Get the page size
   pgsize=read_bda_word(BIOSMEM_PAGE_SIZE);
   // Compute the address
   address=pgsize*page+(xcurs+ycurs*nbcols)*2;

   carattr=((Bit16u)attr<<8)+car;
   memsetw(vga_modes[line].sstart,address,carattr,count);
  }
 else
  {
   // FIXME gfx modes > 8 bpp not supported
   cheight=read_bda_byte(BIOSMEM_CHAR_HEIGHT);
   bpp=vga_modes[line].pixbits;
   while((count-->0) && (xcurs<nbcols))
    {
     switch(vga_modes[line].memmodel)
      {
       case PLANAR4:
       case PLANAR1:
         write_gfx_char_pl4(car,attr,xcurs,ycurs,nbcols,cheight);
         break;
       case CGA:
         write_gfx_char_cga(car,attr,xcurs,ycurs,nbcols,bpp);
         break;
       case LINEAR8:
         write_gfx_char_lin(car,attr,xcurs,ycurs,nbcols,cheight);
         break;
#ifdef DEBUG
       default:
         unimplemented();
#endif
      }
     xcurs++;
    }
  }
}

// --------------------------------------------------------------------------------------------
static void biosfn_write_char_only (car,page,attr,count)
Bit8u car;Bit8u page;Bit8u attr;Bit16u count;
{
 Bit8u cheight,xcurs,ycurs,mode,line,bpp;
 Bit16u nbcols,pgsize,address;
 Bit16u cursor;

 // Get the mode
 mode=read_bda_byte(BIOSMEM_CURRENT_MODE);
 line=find_vga_entry(mode);
#ifdef VGAEXT
 if (VGAEXT_is_8bpp_mode())
  line = 14;
#endif
 if(line==0xFF)return;

 // Get the cursor pos for the page
 cursor=get_cursor_pos(page);
 xcurs=cursor&0x00ff;ycurs=(cursor&0xff00)>>8;

 // Get the dimensions
 nbcols=read_bda_word(BIOSMEM_NB_COLS);

 if(vga_modes[line].class==TEXT)
  {
   // Get the page size
   pgsize=read_bda_word(BIOSMEM_PAGE_SIZE);
   // Compute the address
   address=pgsize*page+(xcurs+ycurs*nbcols)*2;

   while(count-->0)
    {write_byte(vga_modes[line].sstart,address,car);
     address+=2;
    }
  }
 else
  {
   // FIXME gfx modes > 8 bpp not supported
   cheight=read_bda_byte(BIOSMEM_CHAR_HEIGHT);
   bpp=vga_modes[line].pixbits;
   while((count-->0) && (xcurs<nbcols))
    {
     switch(vga_modes[line].memmodel)
      {
       case PLANAR4:
       case PLANAR1:
         write_gfx_char_pl4(car,attr,xcurs,ycurs,nbcols,cheight);
         break;
       case CGA:
         write_gfx_char_cga(car,attr,xcurs,ycurs,nbcols,bpp);
         break;
       case LINEAR8:
         write_gfx_char_lin(car,attr,xcurs,ycurs,nbcols,cheight);
         break;
#ifdef DEBUG
       default:
         unimplemented();
#endif
      }
     xcurs++;
    }
  }
}

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_group_0B:
  cmp   bh, #0x00
  je    biosfn_set_border_color
  cmp   bh, #0x01
  je    biosfn_set_palette
#ifdef DEBUG
  call  _unknown
#endif
  ret
biosfn_set_border_color:
  push  ax
  push  bx
  push  cx
  push  dx
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, # VGAREG_ACTL_ADDRESS
  mov   al, #0x00
  out   dx, al
  mov   al, bl
  and   al, #0x0f
  test  al, #0x08
  jz    set_low_border
  add   al, #0x08
set_low_border:
  out   dx, al
  mov   cl, #0x01
  and   bl, #0x10
set_intensity_loop:
  mov   dx, # VGAREG_ACTL_ADDRESS
  mov   al, cl
  out   dx, al
  mov   dx, # VGAREG_ACTL_READ_DATA
  in    al, dx
  and   al, #0xef
  or    al, bl
  mov   dx, # VGAREG_ACTL_ADDRESS
  out   dx, al
  inc   cl
  cmp   cl, #0x04
  jne   set_intensity_loop
  mov   al, #0x20
  out   dx, al
  pop   dx
  pop   cx
  pop   bx
  pop   ax
  ret
biosfn_set_palette:
  push  ax
  push  bx
  push  cx
  push  dx
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   cl, #0x01
  and   bl, #0x01
set_cga_palette_loop:
  mov   dx, # VGAREG_ACTL_ADDRESS
  mov   al, cl
  out   dx, al
  mov   dx, # VGAREG_ACTL_READ_DATA
  in    al, dx
  and   al, #0xfe
  or    al, bl
  mov   dx, # VGAREG_ACTL_ADDRESS
  out   dx, al
  inc   cl
  cmp   cl, #0x04
  jne   set_cga_palette_loop
  mov   al, #0x20
  out   dx, al
  pop   dx
  pop   cx
  pop   bx
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
static void biosfn_write_pixel (BH,AL,CX,DX) Bit8u BH;Bit8u AL;Bit16u CX;Bit16u DX;
{
 Bit8u mode,line,mask,attr,data;
 Bit16u addr;

 // Get the mode
 mode=read_bda_byte(BIOSMEM_CURRENT_MODE);
 line=find_vga_entry(mode);
 if(line==0xFF)return;
 if(vga_modes[line].class==TEXT)return;

 switch(vga_modes[line].memmodel)
  {
   case PLANAR4:
   case PLANAR1:
     addr = CX/8+DX*read_bda_word(BIOSMEM_NB_COLS);
     mask = 0x80 >> (CX & 0x07);
     outw(VGAREG_GRDC_ADDRESS, (mask << 8) | 0x08);
     outw(VGAREG_GRDC_ADDRESS, 0x0205);
     data = read_byte(0xa000,addr);
     if (AL & 0x80)
      {
       outw(VGAREG_GRDC_ADDRESS, 0x1803);
      }
     write_byte(0xa000,addr,AL);
ASM_START
     mov dx, # VGAREG_GRDC_ADDRESS
     mov ax, #0xff08
     out dx, ax
     mov ax, #0x0005
     out dx, ax
     mov ax, #0x0003
     out dx, ax
ASM_END
     break;
   case CGA:
     if(vga_modes[line].pixbits==2)
      {
       addr=(CX>>2)+(DX>>1)*80;
      }
     else
      {
       addr=(CX>>3)+(DX>>1)*80;
      }
     if (DX & 1) addr += 0x2000;
     data = read_byte(0xb800,addr);
     if(vga_modes[line].pixbits==2)
      {
       attr = (AL & 0x03) << ((3 - (CX & 0x03)) * 2);
       mask = 0x03 << ((3 - (CX & 0x03)) * 2);
      }
     else
      {
       attr = (AL & 0x01) << (7 - (CX & 0x07));
       mask = 0x01 << (7 - (CX & 0x07));
      }
     if (AL & 0x80)
      {
       data ^= attr;
      }
     else
      {
       data &= ~mask;
       data |= attr;
      }
     write_byte(0xb800,addr,data);
     break;
   case LINEAR8:
     addr=CX+DX*(read_bda_word(BIOSMEM_NB_COLS)*8);
     write_byte(0xa000,addr,AL);
     break;
#ifdef DEBUG
   default:
     unimplemented();
#endif
  }
}

// --------------------------------------------------------------------------------------------
static void biosfn_read_pixel (BH,CX,DX,AX) Bit8u BH;Bit16u CX;Bit16u DX;Bit16u *AX;
{
 Bit8u mode,line,mask,attr,data,i;
 Bit16u addr;
 Bit16u ss=get_SS();

 // Get the mode
 mode=read_bda_byte(BIOSMEM_CURRENT_MODE);
 line=find_vga_entry(mode);
 if(line==0xFF)return;
 if(vga_modes[line].class==TEXT)return;

 switch(vga_modes[line].memmodel)
  {
   case PLANAR4:
   case PLANAR1:
     addr = CX/8+DX*read_bda_word(BIOSMEM_NB_COLS);
     mask = 0x80 >> (CX & 0x07);
     attr = 0x00;
     for(i=0;i<4;i++)
      {
       outw(VGAREG_GRDC_ADDRESS, (i << 8) | 0x04);
       data = read_byte(0xa000,addr) & mask;
       if (data > 0) attr |= (0x01 << i);
      }
     break;
   case CGA:
     addr=(CX>>2)+(DX>>1)*80;
     if (DX & 1) addr += 0x2000;
     data = read_byte(0xb800,addr);
     if(vga_modes[line].pixbits==2)
      {
       attr = (data >> ((3 - (CX & 0x03)) * 2)) & 0x03;
      }
     else
      {
       attr = (data >> (7 - (CX & 0x07))) & 0x01;
      }
     break;
   case LINEAR8:
     addr=CX+DX*(read_bda_word(BIOSMEM_NB_COLS)*8);
     attr=read_byte(0xa000,addr);
     break;
   default:
#ifdef DEBUG
     unimplemented();
#endif
     attr = 0;
  }
 write_word(ss,AX,(read_word(ss,AX) & 0xff00) | attr);
}

// --------------------------------------------------------------------------------------------
static void biosfn_write_teletype (car, page, attr, flag)
Bit8u car;Bit8u page;Bit8u attr;Bit8u flag;
{// flag = WITH_ATTR / NO_ATTR

 Bit8u cheight,xcurs,ycurs,mode,line,bpp;
 Bit16u nbcols,nbrows,pgsize,address;
 Bit16u cursor;

 // special case if page is 0xff, use current page
 if(page==0xff)
  page=read_bda_byte(BIOSMEM_CURRENT_PAGE);

 // Get the mode
 mode=read_bda_byte(BIOSMEM_CURRENT_MODE);
 line=find_vga_entry(mode);
#ifdef VGAEXT
 if (VGAEXT_is_8bpp_mode())
  line = 14;
#endif
 if(line==0xFF)return;

 // Get the cursor pos for the page
 cursor=get_cursor_pos(page);
 xcurs=cursor&0x00ff;ycurs=(cursor&0xff00)>>8;

 // Get the dimensions
 nbrows=read_bda_byte(BIOSMEM_NB_ROWS)+1;
 nbcols=read_bda_word(BIOSMEM_NB_COLS);

 switch(car)
  {
   case 7:
    //FIXME should beep
    break;

   case 8:
    if(xcurs>0)xcurs--;
    break;

   case '\r':
    xcurs=0;
    break;

   case '\n':
    ycurs++;
    break;

   case '\t':
    do
     {
      biosfn_write_teletype(' ',page,attr,flag);
      cursor=get_cursor_pos(page);
      xcurs=cursor&0x00ff;ycurs=(cursor&0xff00)>>8;
     }while(xcurs%8==0);
    break;

   default:

    if(vga_modes[line].class==TEXT)
     {
      // Get the page size
      pgsize=read_bda_word(BIOSMEM_PAGE_SIZE);
      // Compute the address
      address=pgsize*page+(xcurs+ycurs*nbcols)*2;

      // Write the char
      write_byte(vga_modes[line].sstart,address,car);

      if(flag==WITH_ATTR)
       write_byte(vga_modes[line].sstart,address+1,attr);
     }
    else
     {
      // FIXME gfx modes > 8 bpp not supported
      cheight=read_bda_byte(BIOSMEM_CHAR_HEIGHT);
      bpp=vga_modes[line].pixbits;
      switch(vga_modes[line].memmodel)
       {
        case PLANAR4:
        case PLANAR1:
          write_gfx_char_pl4(car,attr,xcurs,ycurs,nbcols,cheight);
          break;
        case CGA:
          write_gfx_char_cga(car,attr,xcurs,ycurs,nbcols,bpp);
          break;
        case LINEAR8:
          write_gfx_char_lin(car,attr,xcurs,ycurs,nbcols,cheight);
          break;
#ifdef DEBUG
        default:
          unimplemented();
#endif
       }
     }
    xcurs++;
  }

 // Do we need to wrap ?
 if(xcurs==nbcols)
  {xcurs=0;
   ycurs++;
  }

 // Do we need to scroll ?
 if(ycurs==nbrows)
  {
   if(vga_modes[line].class==TEXT)
    {
     address=pgsize*page+(xcurs+(ycurs-1)*nbcols)*2;
     attr=read_byte(vga_modes[line].sstart,address+1);
     biosfn_scroll(0x01,attr,0,0,nbrows-1,nbcols-1,page,SCROLL_UP);
    }
   else
    {
     biosfn_scroll(0x01,0x00,0,0,nbrows-1,nbcols-1,page,SCROLL_UP);
    }
   ycurs-=1;
  }

 // Set the cursor for the page
 cursor=ycurs; cursor<<=8; cursor+=xcurs;
 set_cursor_pos(page,cursor);
}

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_get_video_mode:
  push  ds
  mov   ax, # BIOSMEM_SEG
  mov   ds, ax
  push  bx
  mov   bx, # BIOSMEM_CURRENT_PAGE
  mov   al, [bx]
  pop   bx
  mov   bh, al
  push  bx
  mov   bx, # BIOSMEM_VIDEO_CTL
  mov   ah, [bx]
  and   ah, #0x80
  mov   bx, # BIOSMEM_CURRENT_MODE
  mov   al, [bx]
  or    al, ah
  mov   bx, # BIOSMEM_NB_COLS
  mov   ah, [bx]
  pop   bx
  pop   ds
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_group_10:
  cmp   al, #0x00
  jne   int10_test_1001
  jmp   biosfn_set_single_palette_reg
int10_test_1001:
  cmp   al, #0x01
  jne   int10_test_1002
  jmp   biosfn_set_overscan_border_color
int10_test_1002:
  cmp   al, #0x02
  jne   int10_test_1003
  jmp   biosfn_set_all_palette_reg
int10_test_1003:
  cmp   al, #0x03
  jne   int10_test_1007
  jmp   biosfn_toggle_intensity
int10_test_1007:
  cmp   al, #0x07
  jne   int10_test_1008
  jmp   biosfn_get_single_palette_reg
int10_test_1008:
  cmp   al, #0x08
  jne   int10_test_1009
  jmp   biosfn_read_overscan_border_color
int10_test_1009:
  cmp   al, #0x09
  jne   int10_test_1010
  jmp   biosfn_get_all_palette_reg
int10_test_1010:
  cmp   al, #0x10
  jne   int10_test_1012
  jmp  biosfn_set_single_dac_reg
int10_test_1012:
  cmp   al, #0x12
  jne   int10_test_1013
  jmp   biosfn_set_all_dac_reg
int10_test_1013:
  cmp   al, #0x13
  jne   int10_test_1015
  jmp   biosfn_select_video_dac_color_page
int10_test_1015:
  cmp   al, #0x15
  jne   int10_test_1017
  jmp   biosfn_read_single_dac_reg
int10_test_1017:
  cmp   al, #0x17
  jne   int10_test_1018
  jmp   biosfn_read_all_dac_reg
int10_test_1018:
  cmp   al, #0x18
  jne   int10_test_1019
  jmp   biosfn_set_pel_mask
int10_test_1019:
  cmp   al, #0x19
  jne   int10_test_101A
  jmp   biosfn_read_pel_mask
int10_test_101A:
  cmp   al, #0x1a
  jne   int10_test_101B
  jmp   biosfn_read_video_dac_state
int10_test_101B:
  cmp   al, #0x1b
  jne   int10_group_10_unknown
  jmp   biosfn_perform_gray_scale_summing
int10_group_10_unknown:
#ifdef DEBUG
  call  _unknown
#endif
  ret

biosfn_set_single_palette_reg:
  cmp   bl, #0x14
  ja    no_actl_reg1
  push  ax
  push  dx
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, # VGAREG_ACTL_ADDRESS
  in    al, dx
  and   al, #0x20
  or    al, bl
  out   dx, al
  mov   al, bh
  out   dx, al
  mov   al, #0x20
  out   dx, al
  pop   dx
  pop   ax
no_actl_reg1:
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_set_overscan_border_color:
  push  bx
  mov   bl, #0x11
  call  biosfn_set_single_palette_reg
  pop   bx
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_set_all_palette_reg:
  push  ax
  push  bx
  push  cx
  push  dx
  mov   bx, dx
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   cl, #0x00
  mov   dx, # VGAREG_ACTL_ADDRESS
  in    al, dx
  and   al, #0x20
  mov   ch, al
set_palette_loop:
  mov   al, cl
  or    al, ch
  out   dx, al
  seg   es
  mov   al, [bx]
  out   dx, al
  inc   bx
  inc   cl
  cmp   cl, #0x10
  jne   set_palette_loop
  mov   al, #0x11
  or    al, ch
  out   dx, al
  seg   es
  mov   al, [bx]
  out   dx, al
  mov   al, #0x20
  out   dx, al
  pop   dx
  pop   cx
  pop   bx
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_toggle_intensity:
  push  ax
  push  bx
  push  dx
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, # VGAREG_ACTL_ADDRESS
  in    al, dx
  and   al, #0x20
  or    al, #0x10
  out   dx, al
  mov   dx, # VGAREG_ACTL_READ_DATA
  in    al, dx
  and   al, #0xf7
  and   bl, #0x01
  shl   bl, 3
  or    al, bl
  mov   dx, # VGAREG_ACTL_ADDRESS
  out   dx, al
  mov   al, #0x20
  out   dx, al
  pop   dx
  pop   bx
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_get_single_palette_reg:
  cmp   bl, #0x14
  ja    no_actl_reg2
  push  ax
  push  dx
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, # VGAREG_ACTL_ADDRESS
  in    al, dx
  and   al, #0x20
  or    al, bl
  out   dx, al
  mov   dx, # VGAREG_ACTL_READ_DATA
  in    al, dx
  mov   bh, al
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, # VGAREG_ACTL_ADDRESS
  mov   al, #0x20
  out   dx, al
  pop   dx
  pop   ax
no_actl_reg2:
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_read_overscan_border_color:
  push  ax
  push  bx
  mov   bl, #0x11
  call  biosfn_get_single_palette_reg
  mov   al, bh
  pop   bx
  mov   bh, al
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_get_all_palette_reg:
  push  ax
  push  bx
  push  cx
  push  dx
  mov   bx, dx
  mov   cl, #0x00
get_palette_loop:
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, # VGAREG_ACTL_ADDRESS
  in    al, dx
  and   al, #0x20
  or    al, cl
  out   dx, al
  mov   dx, # VGAREG_ACTL_READ_DATA
  in    al, dx
  seg   es
  mov   [bx], al
  inc   bx
  inc   cl
  cmp   cl, #0x10
  jne   get_palette_loop
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, # VGAREG_ACTL_ADDRESS
  in    al, dx
  and   al, #0x20
  or    al, #0x11
  out   dx, al
  mov   dx, # VGAREG_ACTL_READ_DATA
  in    al, dx
  seg   es
  mov   [bx], al
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, # VGAREG_ACTL_ADDRESS
  mov   al, #0x20
  out   dx, al
  pop   dx
  pop   cx
  pop   bx
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_set_single_dac_reg:
  push  ax
  push  dx
  mov   ah, dh
  mov   dx, # VGAREG_DAC_WRITE_ADDRESS
  mov   al, bl
  out   dx, al
  mov   dx, # VGAREG_DAC_DATA
  mov   al, ah
  out   dx, al
  mov   al, ch
  out   dx, al
  mov   al, cl
  out   dx, al
  pop   dx
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_set_all_dac_reg:
  push  ax
  push  cx
  push  dx
  push  si
  push  ds
  mov   si, dx
  mov   ax, es
  mov   ds, ax
  mov   dx, # VGAREG_DAC_WRITE_ADDRESS
  mov   al, bl
  out   dx, al
  mov   dx, # VGAREG_DAC_DATA
  cld
set_dac_loop:
  lodsb
  out   dx, al
  lodsb
  out   dx, al
  lodsb
  out   dx, al
  loop  set_dac_loop
  pop   ds
  pop   si
  pop   dx
  pop   cx
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_select_video_dac_color_page:
  push  ax
  push  bx
  push  dx
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, # VGAREG_ACTL_ADDRESS
  mov   al, #0x10
  out   dx, al
  mov   dx, # VGAREG_ACTL_READ_DATA
  in    al, dx
  and   bl, #0x01
  jnz   set_dac_page
  and   al, #0x7f
  shl   bh, 7
  or    al, bh
  mov   dx, # VGAREG_ACTL_ADDRESS
  out   dx, al
  jmp   set_actl_normal
set_dac_page:
  push  ax
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, # VGAREG_ACTL_ADDRESS
  mov   al, #0x14
  out   dx, al
  pop   ax
  and   al, #0x80
  jnz   set_dac_16_page
  shl   bh, 2
set_dac_16_page:
  and   bh, #0x0f
  mov   al, bh
  out   dx, al
set_actl_normal:
  mov   al, #0x20
  out   dx, al
  pop   dx
  pop   bx
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_read_single_dac_reg:
  push  ax
  push  dx
  mov   dx, # VGAREG_DAC_READ_ADDRESS
  mov   al, bl
  out   dx, al
  mov   dx, # VGAREG_DAC_DATA
  in    al, dx
  mov   ah, al
  in    al, dx
  mov   ch, al
  in    al, dx
  mov   cl, al
  pop   dx
  mov   dh, ah
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_read_all_dac_reg:
  push  ax
  push  cx
  push  dx
  push  di
  mov   di, dx
  mov   dx, # VGAREG_DAC_READ_ADDRESS
  mov   al, bl
  out   dx, al
  mov   dx, # VGAREG_DAC_DATA
  cld
read_dac_loop:
  in    al, dx
  stosb
  in    al, dx
  stosb
  in    al, dx
  stosb
  loop  read_dac_loop
  pop   di
  pop   dx
  pop   cx
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_set_pel_mask:
  push  ax
  push  dx
  mov   dx, # VGAREG_PEL_MASK
  mov   al, bl
  out   dx, al
  pop   dx
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_read_pel_mask:
  push  ax
  push  dx
  mov   dx, # VGAREG_PEL_MASK
  in    al, dx
  mov   bl, al
  pop   dx
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_read_video_dac_state:
  push  ax
  push  dx
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, # VGAREG_ACTL_ADDRESS
  mov   al, #0x10
  out   dx, al
  mov   dx, # VGAREG_ACTL_READ_DATA
  in    al, dx
  mov   bl, al
  shr   bl, 7
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, # VGAREG_ACTL_ADDRESS
  mov   al, #0x14
  out   dx, al
  mov   dx, # VGAREG_ACTL_READ_DATA
  in    al, dx
  mov   bh, al
  and   bh, #0x0f
  test  bl, #0x01
  jnz   get_dac_16_page
  shr   bh, 2
get_dac_16_page:
  mov   dx, # VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, # VGAREG_ACTL_ADDRESS
  mov   al, #0x20
  out   dx, al
  pop   dx
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_perform_gray_scale_summing:
  push  ax
  push  bx
  push  cx
  push  dx
  mov   dx, #VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, #VGAREG_ACTL_ADDRESS
  mov   al, #0x00
  out   dx, al
dac_gss_loop:
  mov   al, bl
  mov   dx, #VGAREG_DAC_READ_ADDRESS
  push  bx
  out   dx, al
  mov   dx, #VGAREG_DAC_DATA
  in    al, dx
  mov   bl, #0x4d
  mul   al, bl
  push  ax
  in    al, dx
  mov   bl, #0x97
  mul   al, bl
  push  ax
  in    al, dx
  mov   bl, #0x1c
  mul   al, bl
  mov   dx, ax
  pop   ax
  add   ax, dx
  mov   dx, ax
  pop   ax
  add   ax, dx
  add   ax, #0x80
  shr   ax, #8
  cmp   al, #0x3f
  jna   dac_no_clip
  mov   al, #0x3f
dac_no_clip:
  pop   bx
  push  ax
  mov   al, bl
  mov   dx, #VGAREG_DAC_WRITE_ADDRESS
  out   dx, al
  pop   ax
  mov   dx, #VGAREG_DAC_DATA
  out   dx, al
  out   dx, al
  out   dx, al
  inc   bl
  dec   cx
  jnz   dac_gss_loop
  mov   dx, #VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, #VGAREG_ACTL_ADDRESS
  mov   al, #0x20
  out   dx, al
  pop   dx
  pop   cx
  pop   bx
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
static void get_font_access()
{
ASM_START
 mov dx, # VGAREG_SEQU_ADDRESS
 mov ax, #0x0100
 out dx, ax
 mov ax, #0x0402
 out dx, ax
 mov ax, #0x0704
 out dx, ax
 mov ax, #0x0300
 out dx, ax
 mov dx, # VGAREG_GRDC_ADDRESS
 mov ax, #0x0204
 out dx, ax
 mov ax, #0x0005
 out dx, ax
 mov ax, #0x0406
 out dx, ax
ASM_END
}

static void release_font_access()
{
ASM_START
 mov dx, # VGAREG_SEQU_ADDRESS
 mov ax, #0x0100
 out dx, ax
 mov ax, #0x0302
 out dx, ax
 mov ax, #0x0304
 out dx, ax
 mov ax, #0x0300
 out dx, ax
 mov dx, # VGAREG_READ_MISC_OUTPUT
 in  al, dx
 and al, #0x01
 shl al, 2
 or  al, #0x0a
 mov ah, al
 mov al, #0x06
 mov dx, # VGAREG_GRDC_ADDRESS
 out dx, ax
 mov ax, #0x0004
 out dx, ax
 mov ax, #0x1005
 out dx, ax
ASM_END
}

ASM_START
idiv_u:
  xor dx,dx
  div bx
  ret
ASM_END

static void set_scan_lines(lines) Bit8u lines;
{
 Bit16u crtc_addr,cols,page,vde;
 Bit8u crtc_r9,ovl,rows;

 crtc_addr = read_bda_word(BIOSMEM_CRTC_ADDRESS);
 outb(crtc_addr, 0x09);
 crtc_r9 = inb(crtc_addr+1);
 crtc_r9 = (crtc_r9 & 0xe0) | (lines - 1);
 outb(crtc_addr+1, crtc_r9);
 if(lines==8)
  {
ASM_START
  mov  cx, #0x0607
  call biosfn_set_cursor_shape
ASM_END
  }
 else
  {
ASM_START
  push bp
  mov  bp, sp
  mov  ch, _set_scan_lines.lines + 2[bp]
  sub  ch, #0x04 ; CH = lines-4
  mov  cl, ch
  inc  cl        ; CL = lines-3
  call biosfn_set_cursor_shape
  pop  bp
ASM_END
  }
 write_bda_word(BIOSMEM_CHAR_HEIGHT, lines);
 outb(crtc_addr, 0x12);
 vde = inb(crtc_addr+1);
 outb(crtc_addr, 0x07);
 ovl = inb(crtc_addr+1);
 vde += (((ovl & 0x02) << 7) + ((ovl & 0x40) << 3) + 1);
 rows = vde / lines;
 write_bda_byte(BIOSMEM_NB_ROWS, rows-1);
 cols = read_bda_word(BIOSMEM_NB_COLS);
 write_bda_word(BIOSMEM_PAGE_SIZE, SCREEN_SIZE(rows, cols));
}

static void biosfn_load_text_user_pat (AL,ES,BP,CX,DX,BL,BH) Bit8u AL;Bit16u ES;Bit16u BP;Bit16u CX;Bit16u DX;Bit8u BL;Bit8u BH;
{
 Bit16u blockaddr,dest,i,src;

 get_font_access();
 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
 for(i=0;i<CX;i++)
  {
   src = BP + i * BH;
   dest = blockaddr + (DX + i) * 32;
   memcpyb(0xA000, dest, ES, src, BH);
  }
 release_font_access();
 if(AL>=0x10)
  {
   set_scan_lines(BH);
  }
}

static void biosfn_load_text_8_14_pat (AL,BL) Bit8u AL;Bit8u BL;
{
 Bit16u blockaddr,dest,i,src;

 get_font_access();
 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
 for(i=0;i<0x100;i++)
  {
   src = i * 14;
   dest = blockaddr + i * 32;
   memcpyb(0xA000, dest, 0xC000, vgafont14+src, 14);
  }
 release_font_access();
 if(AL>=0x10)
  {
   set_scan_lines(14);
  }
}

static void biosfn_load_text_8_8_pat (AL,BL) Bit8u AL;Bit8u BL;
{
 Bit16u blockaddr,dest,i,src;

 get_font_access();
 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
 for(i=0;i<0x100;i++)
  {
   src = i * 8;
   dest = blockaddr + i * 32;
   memcpyb(0xA000, dest, 0xC000, vgafont8+src, 8);
  }
 release_font_access();
 if(AL>=0x10)
  {
   set_scan_lines(8);
  }
}

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_set_text_block_specifier:
  push  ax
  push  dx
  mov   dx, # VGAREG_SEQU_ADDRESS
  mov   ah, bl
  mov   al, #0x03
  out   dx, ax
  pop   dx
  pop   ax
  ret
ASM_END

// --------------------------------------------------------------------------------------------
static void biosfn_load_text_8_16_pat (AL,BL) Bit8u AL;Bit8u BL;
{
 Bit16u blockaddr,dest,i,src;

 get_font_access();
 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
 for(i=0;i<0x100;i++)
  {
   src = i * 16;
   dest = blockaddr + i * 32;
   memcpyb(0xA000, dest, 0xC000, vgafont16+src, 16);
  }
 release_font_access();
 if(AL>=0x10)
  {
   set_scan_lines(16);
  }
}

static void biosfn_load_gfx_8_8_chars (ES,BP) Bit16u ES;Bit16u BP;
{
ASM_START
  push bp
  mov  bp, sp
  push ax
  push bx
  push ds
  xor  ax, ax
  mov  ds, ax
  mov  bx, #0x007c ;; INT 0x1F
  mov  ax, 4[bp]
  mov  [bx+2], ax
  mov  ax, 6[bp]
  mov  [bx], ax
  mov  ax, #BIOSMEM_SEG
  mov  ds, ax
  mov  bx, #BIOSMEM_CHAR_HEIGHT
  mov  al, #0x08
  mov  [bx], al
  pop  ds
  pop  bx
  pop  ax
  pop  bp
ASM_END
}

static void biosfn_load_gfx_user_chars (ES,BP,CX,BL,DL) Bit16u ES;Bit16u BP;Bit16u CX;Bit8u BL;Bit8u DL;
{
    Bit8u mode; Bit8u line;

    /* set 0x43 INT pointer */
    write_word(0x0, 0x43*4, BP);
    write_word(0x0, 0x43*4+2, ES);

    switch (BL) {
     case 0:
      write_bda_byte(BIOSMEM_NB_ROWS, DL-1);
      break;
     case 1:
      write_bda_byte(BIOSMEM_NB_ROWS, 13);
      break;
     case 3:
      write_bda_byte(BIOSMEM_NB_ROWS, 42);
      break;
     case 2:
     default:
      write_bda_byte(BIOSMEM_NB_ROWS, 24);
      break;
    }
    write_bda_byte( BIOSMEM_CHAR_HEIGHT, CX);
}

static void biosfn_load_gfx_8_14_chars (BL) Bit8u BL;
{
    /* set 0x43 INT pointer */
ASM_START
    SET_INT_VECTOR(0x43, #0xC000, #_vgafont14)
ASM_END

    switch (BL) {
     case 1:
      write_bda_byte(BIOSMEM_NB_ROWS, 13);
      break;
     case 3:
      write_bda_byte(BIOSMEM_NB_ROWS, 42);
      break;
     case 2:
     default:
      write_bda_byte(BIOSMEM_NB_ROWS, 24);
      break;
    }
    write_bda_byte( BIOSMEM_CHAR_HEIGHT, 14);
}

static void biosfn_load_gfx_8_8_dd_chars (BL) Bit8u BL;
{
    /* set 0x43 INT pointer */
ASM_START
    SET_INT_VECTOR(0x43, #0xC000, #_vgafont8)
ASM_END

    switch (BL) {
     case 1:
      write_bda_byte(BIOSMEM_NB_ROWS, 13);
      break;
     case 3:
      write_bda_byte(BIOSMEM_NB_ROWS, 42);
      break;
     case 2:
     default:
      write_bda_byte(BIOSMEM_NB_ROWS, 24);
      break;
    }
    write_bda_byte( BIOSMEM_CHAR_HEIGHT, 8);
}

static void biosfn_load_gfx_8_16_chars (BL) Bit8u BL;
{
    /* set 0x43 INT pointer */
ASM_START
    SET_INT_VECTOR(0x43, #0xC000, #_vgafont16)
ASM_END

    switch (BL) {
     case 1:
      write_bda_byte(BIOSMEM_NB_ROWS, 13);
      break;
     case 3:
      write_bda_byte(BIOSMEM_NB_ROWS, 42);
      break;
     case 2:
     default:
      write_bda_byte(BIOSMEM_NB_ROWS, 24);
      break;
    }
    write_bda_byte( BIOSMEM_CHAR_HEIGHT, 16);
}

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_get_font_info:
  cmp  bh, #0x07
  ja   get_font_info_unknown
  push ax
  push bx
  push ds
  or   bh, bh
  jnz  get_font_info_1
  xor  ax, ax
  mov  ds, ax
  mov  bx, #0x007c ;; INT 0x1F
  mov  bp, [bx]
  mov  ax, [bx+2]
  mov  es, ax
  jmp  get_bda_data
get_font_info_1:
  cmp  bh, #0x01
  jne  get_font_info_2
  xor  ax, ax
  mov  ds, ax
  mov  bx, #0x010c ;; INT 0x43
  mov  bp, [bx]
  mov  ax, [bx+2]
  mov  es, ax
  jmp  get_bda_data
get_font_info_2:
  mov  ax, #0xc000
  mov  es, ax
  cmp  bh, #0x02
  jne  get_font_info_3
  mov  bp, #_vgafont14
  jmp  get_bda_data
get_font_info_3:
  cmp  bh, #0x03
  jne  get_font_info_4
  mov  bp, #_vgafont8
  jmp  get_bda_data
get_font_info_4:
  cmp  bh, #0x04
  jne  get_font_info_5
  mov  bp, #_vgafont8
  add  bp, #0x0400
  jmp  get_bda_data
get_font_info_5:
  cmp  bh, #0x05
  jne  get_font_info_6
  mov  bp, #_vgafont14alt
  jmp  get_bda_data
get_font_info_6:
  cmp  bh, #0x06
  jne  get_font_info_7
  mov  bp, #_vgafont16
  jmp  get_bda_data
get_font_info_7:
  mov  bp, #_vgafont16alt
get_bda_data:
  mov  ax, #BIOSMEM_SEG
  mov  ds, ax
  mov  cl, BIOSMEM_CHAR_HEIGHT
  xor  ch, ch
  mov  dl, BIOSMEM_NB_ROWS
  xor  dh, dh
  pop  ds
  pop  bx
  pop  ax
get_font_info_unknown:
  ret
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_get_ega_info:
  push  ds
  push  ax
  mov   ax, # BIOSMEM_SEG
  mov   ds, ax
  xor   ch, ch
  mov   bx, # BIOSMEM_SWITCHES
  mov   cl, [bx]
  and   cl, #0x0f
  mov   bx, # BIOSMEM_CRTC_ADDRESS
  mov   ax, [bx]
  mov   bx, #0x0003
  cmp   ax, # VGAREG_MDA_CRTC_ADDRESS
  jne   mode_ega_color
  mov   bh, #0x01
mode_ega_color:
  pop   ax
  pop   ds
  ret
ASM_END

// --------------------------------------------------------------------------------------------
static void biosfn_alternate_prtsc()
{
#ifdef DEBUG
 unimplemented();
#endif
}

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_select_vert_res:

; res : 00 200 lines, 01 350 lines, 02 400 lines

  push  ds
  push  bx
  push  dx
  mov   dl, al
  mov   ax, # BIOSMEM_SEG
  mov   ds, ax
  mov   bx, # BIOSMEM_MODESET_CTL
  mov   al, [bx]
  mov   bx, # BIOSMEM_SWITCHES
  mov   ah, [bx]
  cmp   dl, #0x01
  je    vert_res_350
  jb    vert_res_200
  cmp   dl, #0x02
  je    vert_res_400
#ifdef DEBUG
  mov   al, dl
  xor   ah, ah
  push  ax
  mov   bx, #msg_vert_res
  push  bx
  call  _printf
  add   sp, #4
#endif
  jmp   set_retcode
vert_res_400:

  ; reset modeset ctl bit 7 and set bit 4
  ; set switches bit 3-0 to 0x09

  and   al, #0x7f
  or    al, #0x10
  and   ah, #0xf0
  or    ah, #0x09
  jnz   set_vert_res
vert_res_350:

  ; reset modeset ctl bit 7 and bit 4
  ; set switches bit 3-0 to 0x09

  and   al, #0x6f
  and   ah, #0xf0
  or    ah, #0x09
  jnz   set_vert_res
vert_res_200:

  ; set modeset ctl bit 7 and reset bit 4
  ; set switches bit 3-0 to 0x08

  and   al, #0xef
  or    al, #0x80
  and   ah, #0xf0
  or    ah, #0x08
set_vert_res:
  mov   bx, # BIOSMEM_MODESET_CTL
  mov   [bx], al
  mov   bx, # BIOSMEM_SWITCHES
  mov   [bx], ah
set_retcode:
  mov   ax, #0x1212
  pop   dx
  pop   bx
  pop   ds
  ret

#ifdef DEBUG
msg_vert_res:
.ascii "Select vert res (%02x) was discarded"
.byte 0x0d,0x0a,0x00
#endif


biosfn_enable_default_palette_loading:
  push  ds
  push  bx
  push  dx
  mov   dl, al
  and   dl, #0x01
  shl   dl, 3
  mov   ax, # BIOSMEM_SEG
  mov   ds, ax
  mov   bx, # BIOSMEM_MODESET_CTL
  mov   al, [bx]
  and   al, #0xf7
  or    al, dl
  mov   [bx], al
  mov   ax, #0x1212
  pop   dx
  pop   bx
  pop   ds
  ret


biosfn_enable_video_addressing:
  push  bx
  push  dx
  mov   bl, al
  and   bl, #0x01
  xor   bl, #0x01
  shl   bl, 1
  mov   dx, # VGAREG_READ_MISC_OUTPUT
  in    al, dx
  and   al, #0xfd
  or    al, bl
  mov   dx, # VGAREG_WRITE_MISC_OUTPUT
  out   dx, al
  mov   ax, #0x1212
  pop   dx
  pop   bx
  ret


biosfn_enable_grayscale_summing:
  push  ds
  push  bx
  push  dx
  mov   dl, al
  and   dl, #0x01
  xor   dl, #0x01
  shl   dl, 1
  mov   ax, # BIOSMEM_SEG
  mov   ds, ax
  mov   bx, # BIOSMEM_MODESET_CTL
  mov   al, [bx]
  and   al, #0xfd
  or    al, dl
  mov   [bx], al
  mov   ax, #0x1212
  pop   dx
  pop   bx
  pop   ds
  ret


biosfn_enable_cursor_emulation:
  push  ds
  push  bx
  push  dx
  mov   dl, al
  and   dl, #0x01
  xor   dl, #0x01
  mov   ax, # BIOSMEM_SEG
  mov   ds, ax
  mov   bx, # BIOSMEM_MODESET_CTL
  mov   al, [bx]
  and   al, #0xfe
  or    al, dl
  mov   [bx], al
  mov   ax, #0x1212
  pop   dx
  pop   bx
  pop   ds
  ret
ASM_END

// --------------------------------------------------------------------------------------------
static void biosfn_switch_video_interface (AL,ES,DX) Bit8u AL;Bit16u ES;Bit16u DX;
{
#ifdef DEBUG
 unimplemented();
#endif
}
static void biosfn_enable_video_refresh_control (AL) Bit8u AL;
{
#ifdef DEBUG
 unimplemented();
#endif
}

// --------------------------------------------------------------------------------------------
static void biosfn_write_string (flag,page,attr,count,row,col,seg,offset)
Bit8u flag;Bit8u page;Bit8u attr;Bit16u count;Bit8u row;Bit8u col;Bit16u seg;Bit16u offset;
{
 Bit16u newcurs,oldcurs;
 Bit8u car;

 // Read curs info for the page
 oldcurs=get_cursor_pos(page);

 // if row=0xff special case : use current cursor position
 if(row==0xff)
  {col=oldcurs&0x00ff;
   row=(oldcurs&0xff00)>>8;
  }

 newcurs=row; newcurs<<=8; newcurs+=col;
 set_cursor_pos(page,newcurs);

 while(count--!=0)
  {
   car=read_byte(seg,offset++);
   if((flag&0x02)!=0)
    attr=read_byte(seg,offset++);

   biosfn_write_teletype(car,page,attr,WITH_ATTR);
  }

 // Set back curs pos
 if((flag&0x01)==0)
  set_cursor_pos(page,oldcurs);
}

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_group_1A:
  cmp   al, #0x00
  je    biosfn_read_display_code
  cmp   al, #0x01
  je    biosfn_set_display_code
#ifdef DEBUG
  call  _unknown
#endif
  ret
biosfn_read_display_code:
  push  ds
  push  ax
  mov   ax, # BIOSMEM_SEG
  mov   ds, ax
  mov   bx, # BIOSMEM_DCC_INDEX
  mov   al, [bx]
  mov   bl, al
  xor   bh, bh
  pop   ax
  mov   al, ah
  pop   ds
  ret
biosfn_set_display_code:
  push  ds
  push  ax
  push  bx
  mov   ax, # BIOSMEM_SEG
  mov   ds, ax
  mov   ax, bx
  mov   bx, # BIOSMEM_DCC_INDEX
  mov   [bx], al
#ifdef DEBUG
  mov   al, ah
  xor   ah, ah
  push  ax
  mov   bx, #msg_alt_dcc
  push  bx
  call  _printf
  add   sp, #4
#endif
  pop   bx
  pop   ax
  mov   al, ah
  pop   ds
  ret

#ifdef DEBUG
msg_alt_dcc:
.ascii "Alternate Display code (%02x) was discarded"
.byte 0x0d,0x0a,0x00
#endif
ASM_END

// --------------------------------------------------------------------------------------------
ASM_START
biosfn_read_state_info:
  and   bx, bx
  jnz   unsup_1B
  push  ds
  push  si
  push  di
  push  cx
  mov   ax, #BIOSMEM_SEG
  mov   ds, ax
  mov   ax, #_static_functionality
  stosw
  mov   ax, #0xc000
  stosw
  mov   si, #BIOSMEM_CURRENT_MODE
  mov   cx, #0x1e
  cld
  rep
     movsb
  mov   al, BIOSMEM_NB_ROWS
  stosb
  mov   ax, BIOSMEM_CHAR_HEIGHT
  stosw
  mov   al, BIOSMEM_DCC_INDEX
  stosb
  xor   al, al
  stosb
  mov   al, #0x10
  stosb
  xor   al, al
  stosb
  mov   al, #0x08
  stosb
  mov   al, #0x02
  stosb
  xor   ax, ax
  stosw
  mov   al, #0x03
  stosb
  xor   al, al
  stosb
  mov   cx, #0x0d
  rep
    stosb
  pop   cx
  pop   di
  pop   si
  pop   ds
  mov   ax, #0x1b1b
unsup_1B:
  ret
ASM_END

// -----------------------------------------------------------------------------
ASM_START
; called from VBE code
read_vga_state_size:
  xor   bx, bx
  test  cx, #0x01
  jz    no_hw_state
  add   bx, #0x46
no_hw_state:
  test  cx, #0x02
  jz    no_bda_state
  add   bx, #0x2d
no_bda_state:
  test  cx, #0x04
  jz    no_dac_state
  add   bx, #0x0304
no_dac_state:
  ret

biosfn_read_video_state_size:
  call  read_vga_state_size
  add   bx, #0x3f
  shr   bx, #6
  mov   ax, #0x001c
  ret

save_vga_state:
  push  dx
  push  di
  test  cx, #0x01
  jz    no_save_hw_state
  mov   di, bx
  mov   dx, #VGAREG_SEQU_ADDRESS
  in    al, dx
  stosb
  push  ds
  mov   ax, #BIOSMEM_SEG
  mov   ds, ax
  mov   dx, BIOSMEM_CRTC_ADDRESS
  in    al, dx
  stosb
  pop   ds
  push  dx
  mov   dx, #VGAREG_GRDC_ADDRESS
  in    al, dx
  stosb
  mov   dx, #VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, #VGAREG_ACTL_ADDRESS
  in    al, dx
  stosb
  and   al, #0x20
  mov   bh, al
  mov   dx, #VGAREG_READ_FEATURE_CTL
  in    al, dx
  stosb
  mov   bl, #0x01 ;; VGA sequencer controller registers
save_seqc_loop:
  mov   dx, #VGAREG_SEQU_ADDRESS
  mov   al, bl
  out   dx, al
  mov   dx, #VGAREG_SEQU_DATA
  in    al, dx
  stosb
  inc   bl
  cmp   bl, #0x04
  jbe   save_seqc_loop
  mov   dx, #VGAREG_SEQU_ADDRESS
  xor   al, al
  out   dx, al
  mov   dx, #VGAREG_SEQU_DATA
  in    al, dx
  stosb
  xor   bl, bl ;; VGA CRTC registers
save_crtc_loop:
  pop   dx
  mov   al, bl
  out   dx, al
  push  dx
  inc   dx
  in    al, dx
  stosb
  inc   bl
  cmp   bl, #0x18
  jbe   save_crtc_loop
  xor   bl, bl ;; VGA attribute controller registers
save_actl_loop:
  mov   dx, #VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, #VGAREG_ACTL_ADDRESS
  mov   al, bl
  or    al, bh
  out   dx, al
  mov   dx, #VGAREG_ACTL_READ_DATA
  in    al, dx
  stosb
  inc   bl
  cmp   bl, #0x13
  jbe   save_actl_loop
  mov   dx, #VGAREG_ACTL_RESET
  in    al, dx
  xor   bl, bl ;; VGA graphics controller registers
save_grdc_loop:
  mov   dx, #VGAREG_GRDC_ADDRESS
  mov   al, bl
  out   dx, al
  mov   dx, #VGAREG_GRDC_DATA
  in    al, dx
  stosb
  inc   bl
  cmp   bl, #0x08
  jbe   save_grdc_loop
  pop   ax ;; CRTC address
  stosw
  xor   bl, bl ;; VGA latches
  mov   dx, ax
  mov   al, #0x22
  out   dx, al
save_latches_loop:
  push  dx
  mov   dx, #VGAREG_GRDC_ADDRESS
  mov   al, #0x04
  mov   ah, bl
  out   dx, ax
  pop   dx
  inc   dx
  in    al, dx
  stosb
  dec   dx
  inc   bl
  cmp   bl, #0x04
  jb    save_latches_loop
  mov   dx, #VGAREG_GRDC_ADDRESS
  mov   ax, #0x0004
  out   dx, ax
  mov   bx, di
no_save_hw_state:
  test  cx, #0x02
  jz    no_save_bda_state
  push  ds
  push  cx
  push  si
  mov   ax, #BIOSMEM_SEG
  mov   ds, ax
  mov   si, #BIOSMEM_CURRENT_MODE
  mov   di, bx
  mov   cx, #0x1e
  cld
  rep
     movsb
  mov   si, #BIOSMEM_NB_ROWS
  mov   cx, #0x07
  cld
  rep
     movsb
  xor   ax, ax
  mov   ds, ax
  mov   eax, 0x007c ;; INT 0x1F
  stosd
  mov   eax, 0x010c ;; INT 0x43
  stosd
  mov   bx, di
  pop   si
  pop   cx
  pop   ds
no_save_bda_state:
  test  cx, #0x04
  jz    no_save_dac_state
  push  cx
  mov   di, bx
  mov   dx, #VGAREG_DAC_STATE
  in    al, dx
  stosb
  mov   dx, #VGAREG_DAC_WRITE_ADDRESS
  in    al, dx
  stosb
  mov   dx, #VGAREG_PEL_MASK
  in    al, dx
  stosb
  mov   dx, #VGAREG_DAC_READ_ADDRESS
  xor   al, al
  out   dx, al
  mov   cx, #0x0300
  cld
  mov   dx, #VGAREG_DAC_DATA
dac_read_loop:
  in    al, dx
  stosb
  loop  dac_read_loop
  mov   bl, #0x14
  call  biosfn_get_single_palette_reg
  mov   al, bh
  stosb
  mov   bx, di
  pop   cx
no_save_dac_state:
  pop   di
  pop   dx
  ret

biosfn_save_video_state:
  push  bx
  call  save_vga_state
  pop   bx
  mov   ax, #0x001c
  ret
ASM_END

// -----------------------------------------------------------------------------
ASM_START
restore_vga_state:
  test  cx, #0x01
  jz    no_rest_hw_state
  push  cx
  push  dx
  push  bx
  add   bx, #5
  mov   cl, #0x01 ;; VGA sequencer controller registers
  mov   dx, #VGAREG_SEQU_ADDRESS
rest_seqc_loop:
  mov   al, cl
  seg   es
  mov   ah, [bx]
  out   dx, ax
  inc   bx
  inc   cl
  cmp   cl, #0x04
  jbe   rest_seqc_loop
  mov   al, #0x00
  seg   es
  mov   ah, [bx]
  out   dx, ax
  inc   bx
  seg   es
  mov   ax, 0x36[bx]
  push  ax
  cmp   ax, #VGAREG_VGA_CRTC_ADDRESS
  jne   no_color
  mov   dx, #VGAREG_READ_MISC_OUTPUT
  in    al, dx
  or    al, #0x01 ;; set CRTC color address
  mov   dx, #VGAREG_WRITE_MISC_OUTPUT
  out   dx, al
no_color:
  pop   dx
  mov   ax, #0x0011 ;; Disable CRTC write protection
  out   dx, ax
  xor   cl, cl ;; VGA CRTC registers
rest_crtc_loop:
  mov   al, cl
  seg   es
  mov   ah, [bx]
  out   dx, ax
  inc   bx
  inc   cl
  cmp   cl, #0x18
  jbe   rest_crtc_loop
  mov   dx, #VGAREG_ACTL_RESET
  in    al, dx
  xor   cl, cl ;; VGA attribute controller registers
  mov   dx, #VGAREG_ACTL_ADDRESS
rest_actl_loop:
  mov   al, cl
  or    al, #0x20
  out   dx, al
  seg   es
  mov   al, [bx]
  out   dx, al
  inc   bx
  inc   cl
  cmp   cl, #0x13
  jbe   rest_actl_loop
  xor   cl, cl ;; VGA graphics controller registers
  mov   dx, #VGAREG_GRDC_ADDRESS
rest_grdc_loop:
  mov   al, cl
  seg   es
  mov   ah, [bx]
  out   dx, ax
  inc   bx
  inc   cl
  cmp   cl, #0x08
  jbe   rest_grdc_loop
  add   bx, #6
  pop   ax
  push  bx
  mov   bx, ax
  seg   es
  mov   al, [bx]
  mov   dx, #VGAREG_SEQU_ADDRESS
  out   dx, al
  inc   bx
  seg   es
  mov   al, [bx]
  call  get_crtc_address
  out   dx, al
  inc   bx
  seg   es
  mov   al, [bx]
  mov   dx, #VGAREG_GRDC_ADDRESS
  out   dx, al
  inc   bx
  mov   dx, #VGAREG_ACTL_RESET
  in    al, dx
  mov   dx, #VGAREG_ACTL_ADDRESS
  seg   es
  mov   al, [bx]
  out   dx, al
  mov   dx, #VGAREG_ACTL_RESET
  in    al, dx
  inc   bx
  seg   es
  mov   al, [bx]
  mov   dx, #VGAREG_VGA_WRITE_FEATURE_CTL
  pop   bx
  pop   dx
  pop   cx
no_rest_hw_state:
  test  cx, #0x02
  jz    no_rest_bda_state
  push  ds
  push  cx
  push  si
  push  di
  push  es
  pop   ds
  mov   si, bx
  mov   ax, #BIOSMEM_SEG
  mov   es, ax
  mov   di, #BIOSMEM_CURRENT_MODE
  mov   cx, #0x1e
  cld
  rep
     movsb
  mov   di, #BIOSMEM_NB_ROWS
  mov   cx, #0x07
  cld
  rep
     movsb
  xor   ax, ax
  mov   es, ax
  lodsd
  seg   es
  mov   0x007c, eax ;; INT 0x1F
  lodsd
  seg   es
  mov   0x010c, eax ;; INT 0x43
  mov   bx, si
  push  ds
  pop   es
  pop   di
  pop   si
  pop   cx
  pop   ds
no_rest_bda_state:
  test  cx, #0x04
  jz    no_rest_dac_state
  push  cx
  push  dx
  inc   bx
  seg   es
  mov   al, [bx]
  push  ax
  inc   bx
  seg   es
  mov   al, [bx]
  mov   dx, #VGAREG_PEL_MASK
  out   dx, al
  inc   bx
  mov   al, #0x00
  mov   dx, #VGAREG_DAC_WRITE_ADDRESS
  out   dx, al
  mov   cx, #0x0300
  cld
  mov   dx, #VGAREG_DAC_DATA
dac_write_loop:
  seg   es
  mov   al, [bx]
  out   dx, al
  inc   bx
  loop  dac_write_loop
  pop   ax
  mov   dx, #VGAREG_DAC_WRITE_ADDRESS
  out   dx, al
  seg   es
  mov   al, [bx]
  push  bx
  mov   bh, al
  mov   bl, #0x14
  call  biosfn_set_single_palette_reg
  pop   bx
  inc   bx
  pop   dx
  pop   cx
no_rest_dac_state:
  ret

biosfn_restore_video_state:
  push  bx
  call  restore_vga_state
  pop   bx
  mov   ax, #0x001c
  ret
ASM_END

// ============================================================================================
//
// Video Utils
//
// ============================================================================================

// --------------------------------------------------------------------------------------------
static Bit8u find_vga_entry(mode)
Bit8u mode;
{
 Bit8u i,line=0xFF;
 for(i=0;i<=MODE_MAX;i++)
  if(vga_modes[i].svgamode==mode)
   {line=i;
    break;
   }
 return line;
}

/* =========================================================== */
/*
 * Misc Utils
*/
/* =========================================================== */

// --------------------------------------------------------------------------------------------
static void memsetb(seg,offset,value,count)
  Bit16u seg;
  Bit16u offset;
  Bit16u value;
  Bit16u count;
{
ASM_START
  push bp
  mov  bp, sp

    push ax
    push cx
    push es
    push di

    mov  cx, 10[bp] ; count
    cmp  cx, #0x00
    je   memsetb_end
    mov  ax, 4[bp] ; segment
    mov  es, ax
    mov  ax, 6[bp] ; offset
    mov  di, ax
    mov  al, 8[bp] ; value
    cld
    rep
     stosb

memsetb_end:
    pop di
    pop es
    pop cx
    pop ax

  pop bp
ASM_END
}

// --------------------------------------------------------------------------------------------
static void memsetw(seg,offset,value,count)
  Bit16u seg;
  Bit16u offset;
  Bit16u value;
  Bit16u count;
{
ASM_START
  push bp
  mov  bp, sp

    push ax
    push cx
    push es
    push di

    mov  cx, 10[bp] ; count
    cmp  cx, #0x00
    je   memsetw_end
    mov  ax, 4[bp] ; segment
    mov  es, ax
    mov  ax, 6[bp] ; offset
    mov  di, ax
    mov  ax, 8[bp] ; value
    cld
    rep
     stosw

memsetw_end:
    pop di
    pop es
    pop cx
    pop ax

  pop bp
ASM_END
}

// --------------------------------------------------------------------------------------------
static void memcpyb(dseg,doffset,sseg,soffset,count)
  Bit16u dseg;
  Bit16u doffset;
  Bit16u sseg;
  Bit16u soffset;
  Bit16u count;
{
ASM_START
  push bp
  mov  bp, sp

    push ax
    push cx
    push es
    push di
    push ds
    push si

    mov  cx, 12[bp] ; count
    cmp  cx, #0x0000
    je   memcpyb_end
    mov  ax, 4[bp] ; dsegment
    mov  es, ax
    mov  ax, 6[bp] ; doffset
    mov  di, ax
    mov  ax, 8[bp] ; ssegment
    mov  ds, ax
    mov  ax, 10[bp] ; soffset
    mov  si, ax
    cld
    rep
     movsb

memcpyb_end:
    pop si
    pop ds
    pop di
    pop es
    pop cx
    pop ax

  pop bp
ASM_END
}

// --------------------------------------------------------------------------------------------
static void memcpyw(dseg,doffset,sseg,soffset,count)
  Bit16u dseg;
  Bit16u doffset;
  Bit16u sseg;
  Bit16u soffset;
  Bit16u count;
{
ASM_START
  push bp
  mov  bp, sp

    push ax
    push cx
    push es
    push di
    push ds
    push si

    mov  cx, 12[bp] ; count
    cmp  cx, #0x0000
    je   memcpyw_end
    mov  ax, 4[bp] ; dsegment
    mov  es, ax
    mov  ax, 6[bp] ; doffset
    mov  di, ax
    mov  ax, 8[bp] ; ssegment
    mov  ds, ax
    mov  ax, 10[bp] ; soffset
    mov  si, ax
    cld
    rep
     movsw

memcpyw_end:
    pop si
    pop ds
    pop di
    pop es
    pop cx
    pop ax

  pop bp
ASM_END
}

/* =========================================================== */
/*
 * These functions where ripped from Kevin's rombios.c
*/
/* =========================================================== */

// --------------------------------------------------------------------------------------------
static Bit8u
read_byte(seg, offset)
  Bit16u seg;
  Bit16u offset;
{
ASM_START
  push bp
  mov  bp, sp

    push bx
    push ds
    mov  ax, 4[bp] ; segment
    mov  ds, ax
    mov  bx, 6[bp] ; offset
    mov  al, [bx]
    ;; al = return value (byte)
    pop  ds
    pop  bx

  pop  bp
ASM_END
}

// --------------------------------------------------------------------------------------------
static Bit8u
read_bda_byte(offset)
  Bit16u offset;
{
ASM_START
  push bp
  mov  bp, sp

    push bx
    push ds
    mov  ax, #BIOSMEM_SEG ; segment
    mov  ds, ax
    mov  bx, 4[bp] ; offset
    mov  al, [bx]
    ;; al = return value (byte)
    pop  ds
    pop  bx

  pop  bp
ASM_END
}

// --------------------------------------------------------------------------------------------
static Bit16u
read_word(seg, offset)
  Bit16u seg;
  Bit16u offset;
{
ASM_START
  push bp
  mov  bp, sp

    push bx
    push ds
    mov  ax, 4[bp] ; segment
    mov  ds, ax
    mov  bx, 6[bp] ; offset
    mov  ax, [bx]
    ;; ax = return value (word)
    pop  ds
    pop  bx

  pop  bp
ASM_END
}

// --------------------------------------------------------------------------------------------
static Bit16u
read_bda_word(offset)
  Bit16u offset;
{
ASM_START
  push bp
  mov  bp, sp

    push bx
    push ds
    mov  ax, #BIOSMEM_SEG ; segment
    mov  ds, ax
    mov  bx, 4[bp] ; offset
    mov  ax, [bx]
    ;; ax = return value (word)
    pop  ds
    pop  bx

  pop  bp
ASM_END
}

// --------------------------------------------------------------------------------------------
static void
write_byte(seg, offset, data)
  Bit16u seg;
  Bit16u offset;
  Bit8u  data;
{
ASM_START
  push bp
  mov  bp, sp

    push ax
    push bx
    push ds
    mov  ax, 4[bp] ; segment
    mov  ds, ax
    mov  bx, 6[bp] ; offset
    mov  al, 8[bp] ; data byte
    mov  [bx], al  ; write data byte
    pop  ds
    pop  bx
    pop  ax

  pop  bp
ASM_END
}

// --------------------------------------------------------------------------------------------
static void
write_bda_byte(offset, data)
  Bit16u offset;
  Bit8u  data;
{
ASM_START
  push bp
  mov  bp, sp

    push ax
    push bx
    push ds
    mov  ax, #BIOSMEM_SEG ; segment
    mov  ds, ax
    mov  bx, 4[bp] ; offset
    mov  al, 6[bp] ; data byte
    mov  [bx], al  ; write data byte
    pop  ds
    pop  bx
    pop  ax

  pop  bp
ASM_END
}

// --------------------------------------------------------------------------------------------
static void
write_word(seg, offset, data)
  Bit16u seg;
  Bit16u offset;
  Bit16u data;
{
ASM_START
  push bp
  mov  bp, sp

    push ax
    push bx
    push ds
    mov  ax, 4[bp] ; segment
    mov  ds, ax
    mov  bx, 6[bp] ; offset
    mov  ax, 8[bp] ; data word
    mov  [bx], ax  ; write data word
    pop  ds
    pop  bx
    pop  ax

  pop  bp
ASM_END
}

// --------------------------------------------------------------------------------------------
static void
write_bda_word(offset, data)
  Bit16u offset;
  Bit16u data;
{
ASM_START
  push bp
  mov  bp, sp

    push ax
    push bx
    push ds
    mov  ax, #BIOSMEM_SEG ; segment
    mov  ds, ax
    mov  bx, 4[bp] ; offset
    mov  ax, 6[bp] ; data word
    mov  [bx], ax  ; write data word
    pop  ds
    pop  bx
    pop  ax

  pop  bp
ASM_END
}

// --------------------------------------------------------------------------------------------
 Bit8u
inb(port)
  Bit16u port;
{
ASM_START
  push bp
  mov  bp, sp

    push dx
    mov  dx, 4[bp]
    in   al, dx
    pop  dx

  pop  bp
ASM_END
}

  Bit16u
inw(port)
  Bit16u port;
{
ASM_START
  push bp
  mov  bp, sp

    push dx
    mov  dx, 4[bp]
    in   ax, dx
    pop  dx

  pop  bp
ASM_END
}

// --------------------------------------------------------------------------------------------
  void
outb(port, val)
  Bit16u port;
  Bit8u  val;
{
ASM_START
  push bp
  mov  bp, sp

    push ax
    push dx
    mov  dx, 4[bp]
    mov  al, 6[bp]
    out  dx, al
    pop  dx
    pop  ax

  pop  bp
ASM_END
}

// --------------------------------------------------------------------------------------------
  void
outw(port, val)
  Bit16u port;
  Bit16u  val;
{
ASM_START
  push bp
  mov  bp, sp

    push ax
    push dx
    mov  dx, 4[bp]
    mov  ax, 6[bp]
    out  dx, ax
    pop  dx
    pop  ax

  pop  bp
ASM_END
}

Bit16u get_SS()
{
ASM_START
  mov  ax, ss
ASM_END
}

#ifdef DEBUG
void unimplemented()
{
 printf("--> Unimplemented\n");
}

void unknown()
{
 printf("--> Unknown int10\n");
}
#endif

// --------------------------------------------------------------------------------------------
#if defined(USE_BX_INFO) || defined(DEBUG) || defined(CIRRUS_DEBUG)
void printf(s)
  Bit8u *s;
{
  Bit8u c, format_char;
  Boolean  in_format;
  unsigned format_width, i;
  Bit16u  *arg_ptr;
  Bit16u   arg_seg, arg, digit, nibble, shift_count;

  arg_ptr = &s;
  arg_seg = get_SS();

  in_format = 0;
  format_width = 0;

  while (c = read_byte(0xc000, s)) {
    if ( c == '%' ) {
      in_format = 1;
      format_width = 0;
      }
    else if (in_format) {
      if ( (c>='0') && (c<='9') ) {
        format_width = (format_width * 10) + (c - '0');
        }
      else if (c == 'x') {
        arg_ptr++; // increment to next arg
        arg = read_word(arg_seg, arg_ptr);
        if (format_width == 0)
          format_width = 4;
        i = 0;
        digit = format_width - 1;
        for (i=0; i<format_width; i++) {
          nibble = (arg >> (4 * digit)) & 0x000f;
          if (nibble <= 9)
            outb(0x0500, nibble + '0');
          else
            outb(0x0500, (nibble - 10) + 'A');
          digit--;
          }
        in_format = 0;
        }
      //else if (c == 'd') {
      //  in_format = 0;
      //  }
      }
    else {
      outb(0x0500, c);
      }
    s ++;
    }
}
#endif

ASM_START
#if defined(VBE) || defined(CIRRUS)
  ; get LFB address from PCI
  ; in - ax: PCI device vendor
  ; out - ax: LFB address (high 16 bit)
  ;; NOTE - may be called in protected mode
_pci_get_lfb_addr:
  push bx
  push cx
  push dx
  push eax
    mov bx, ax
    xor cx, cx
    mov dl, #0x00
    call pci_read_reg
    cmp ax, #0xffff
    jz pci_get_lfb_addr_fail
 pci_get_lfb_addr_next_dev:
    mov dl, #0x00
    call pci_read_reg
    cmp ax, bx ;; check vendor
    jz pci_get_lfb_addr_found
    add cx, #0x8
    cmp cx, #0x200 ;; search bus #0 and #1
    jb pci_get_lfb_addr_next_dev
 pci_get_lfb_addr_fail:
    xor dx, dx ;; no LFB
    jmp pci_get_lfb_addr_return
 pci_get_lfb_addr_found:
    mov dl, #0x10 ;; I/O space #0
    call pci_read_reg
    test ax, #0xfff1
    jz pci_get_lfb_addr_success
    mov dl, #0x14 ;; I/O space #1
    call pci_read_reg
    test ax, #0xfff1
    jnz pci_get_lfb_addr_fail
 pci_get_lfb_addr_success:
    shr eax, #16
    mov dx, ax ;; LFB address
 pci_get_lfb_addr_return:
  pop eax
  mov ax, dx
  pop dx
  pop cx
  pop bx
  ret
#endif

  ; read PCI register
  ; in - cx: device/function
  ; in - dl: register
  ; out - eax: value
pci_read_reg:
  mov eax, #0x00800000
  mov ax, cx
  shl eax, #8
  mov al, dl
  mov dx, #0xcf8
  out dx, eax
  add dl, #4
  in  eax, dx
  ret

  ; get CRTC address from VGA misc output setting
  ; out - dx: CRTC address
get_crtc_address:
  push ax
  mov  dx, #VGAREG_READ_MISC_OUTPUT
  in   al, dx
  and  al, #0x01
  shl  al, #5
  mov  dx, #VGAREG_MDA_CRTC_ADDRESS
  add  dl, al
  pop  ax
  ret
ASM_END

#ifdef VBE
#include "vbe.c"
#endif

#ifdef CIRRUS
#include "clext.c"
#endif

#ifdef BANSHEE
#include "banshee.c"
#endif

// --------------------------------------------------------------------------------------------

ASM_START
;; DATA_SEG_DEFS_HERE
ASM_END

ASM_START
.ascii "vgabios ends here"
.byte  0x00
vgabios_end:
.byte 0xCB
;; BLOCK_STRINGS_BEGIN

ASM_END