854ed38d63247b70ec8a6457d4d344ae49e61df6
[seabios.git] / vgasrc / vgabios.c
1 // VGA bios implementation
2 //
3 // Copyright (C) 2009  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2001-2008 the LGPL VGABios developers Team
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8
9 // TODO:
10 //  * review correctness of converted asm by comparing with RBIL
11 //
12 //  * convert vbe/clext code
13
14 #include "bregs.h" // struct bregs
15 #include "biosvar.h" // GET_BDA
16 #include "util.h" // memset
17 #include "vgabios.h" // find_vga_entry
18 #include "optionroms.h" // struct pci_data
19 #include "config.h" // CONFIG_*
20 #include "stdvga.h" // stdvga_set_mode
21 #include "geodevga.h" // geodevga_init
22 #include "bochsvga.h" // bochsvga_init
23 #include "clext.h" // clext_init
24 #include "vgahw.h" // vgahw_set_mode
25
26 // XXX
27 #define DEBUG_VGA_POST 1
28 #define DEBUG_VGA_10 3
29
30
31 /****************************************************************
32  * PCI Data
33  ****************************************************************/
34 #if CONFIG_VGA_PCI == 1
35 struct pci_data rom_pci_data VAR16VISIBLE = {
36     .signature = PCI_ROM_SIGNATURE,
37     .vendor = CONFIG_VGA_VID,
38     .device = CONFIG_VGA_DID,
39     .dlen = 0x18,
40     .class_hi = 0x300,
41     .irevision = 1,
42     .type = PCIROM_CODETYPE_X86,
43     .indicator = 0x80,
44 };
45 #endif
46
47 /****************************************************************
48  * Helper functions
49  ****************************************************************/
50
51 u16
52 calc_page_size(u8 memmodel, u16 width, u16 height)
53 {
54     switch (memmodel) {
55     case MM_TEXT:
56         return ALIGN(width * height * 2, 2*1024);
57     case MM_CGA:
58         return 16*1024;
59     default:
60         return ALIGN(width * height / 8, 8*1024);
61     }
62 }
63
64 static void
65 set_cursor_shape(u8 start, u8 end)
66 {
67     start &= 0x3f;
68     end &= 0x1f;
69
70     u16 curs = (start << 8) + end;
71     SET_BDA(cursor_type, curs);
72
73     u8 modeset_ctl = GET_BDA(modeset_ctl);
74     u16 cheight = GET_BDA(char_height);
75     if ((modeset_ctl & 0x01) && (cheight > 8) && (end < 8) && (start < 0x20)) {
76         if (end != (start + 1))
77             start = ((start + 1) * cheight / 8) - 1;
78         else
79             start = ((end + 1) * cheight / 8) - 2;
80         end = ((end + 1) * cheight / 8) - 1;
81     }
82     stdvga_set_cursor_shape(start, end);
83 }
84
85 static u16
86 get_cursor_shape(u8 page)
87 {
88     if (page > 7)
89         return 0;
90     // FIXME should handle VGA 14/16 lines
91     return GET_BDA(cursor_type);
92 }
93
94 static void
95 set_cursor_pos(struct cursorpos cp)
96 {
97     // Should not happen...
98     if (cp.page > 7)
99         return;
100
101     // Bios cursor pos
102     SET_BDA(cursor_pos[cp.page], (cp.y << 8) | cp.x);
103
104     // Set the hardware cursor
105     u8 current = GET_BDA(video_page);
106     if (cp.page != current)
107         return;
108
109     // Calculate the memory address
110     u16 nbcols = GET_BDA(video_cols);
111     u16 address = (GET_BDA(video_pagesize) * cp.page
112                    + (cp.x + cp.y * nbcols) * 2);
113
114     stdvga_set_cursor_pos(address / 2);
115 }
116
117 static struct cursorpos
118 get_cursor_pos(u8 page)
119 {
120     if (page == 0xff)
121         // special case - use current page
122         page = GET_BDA(video_page);
123     if (page > 7) {
124         struct cursorpos cp = { 0, 0, 0xfe };
125         return cp;
126     }
127     // FIXME should handle VGA 14/16 lines
128     u16 xy = GET_BDA(cursor_pos[page]);
129     struct cursorpos cp = {xy, xy>>8, page};
130     return cp;
131 }
132
133 static void
134 set_active_page(u8 page)
135 {
136     if (page > 7)
137         return;
138
139     // Get the mode
140     struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
141     if (!vmode_g)
142         return;
143
144     // Get pos curs pos for the right page
145     struct cursorpos cp = get_cursor_pos(page);
146
147     // Calculate memory address of start of page
148     u8 memmodel = GET_GLOBAL(vmode_g->memmodel);
149     u16 address = GET_BDA(video_pagesize) * page;
150     stdvga_set_active_page(memmodel == MM_TEXT ? address / 2 : address);
151
152     // And change the BIOS page
153     SET_BDA(video_pagestart, address);
154     SET_BDA(video_page, page);
155
156     dprintf(1, "Set active page %02x address %04x\n", page, address);
157
158     // Display the cursor, now the page is active
159     set_cursor_pos(cp);
160 }
161
162 static void
163 set_scan_lines(u8 lines)
164 {
165     stdvga_set_scan_lines(lines);
166     if (lines == 8)
167         set_cursor_shape(0x06, 0x07);
168     else
169         set_cursor_shape(lines - 4, lines - 3);
170     SET_BDA(char_height, lines);
171     u16 vde = stdvga_get_vde();
172     u8 rows = vde / lines;
173     SET_BDA(video_rows, rows - 1);
174     u16 cols = GET_BDA(video_cols);
175     SET_BDA(video_pagesize, calc_page_size(MM_TEXT, cols, rows));
176 }
177
178
179 /****************************************************************
180  * Character writing
181  ****************************************************************/
182
183 // Scroll the screen one line.  This function is designed to be called
184 // tail-recursive to reduce stack usage.
185 static void noinline
186 scroll_one(u16 nbrows, u16 nbcols, u8 page)
187 {
188     struct cursorpos ul = {0, 0, page};
189     struct cursorpos lr = {nbcols-1, nbrows-1, page};
190     vgafb_scroll(1, -1, ul, lr);
191 }
192
193 // Write a character to the screen at a given position.  Implement
194 // special characters and scroll the screen if necessary.
195 static void
196 write_teletype(struct cursorpos *pcp, struct carattr ca)
197 {
198     struct cursorpos cp = *pcp;
199
200     // Get the dimensions
201     u16 nbrows = GET_BDA(video_rows) + 1;
202     u16 nbcols = GET_BDA(video_cols);
203
204     switch (ca.car) {
205     case 7:
206         //FIXME should beep
207         break;
208     case 8:
209         if (cp.x > 0)
210             cp.x--;
211         break;
212     case '\r':
213         cp.x = 0;
214         break;
215     case '\n':
216         cp.y++;
217         break;
218     case '\t':
219         do {
220             struct carattr dummyca = {' ', ca.attr, ca.use_attr};
221             vgafb_write_char(cp, dummyca);
222             cp.x++;
223         } while (cp.x < nbcols && cp.x % 8);
224         break;
225     default:
226         vgafb_write_char(cp, ca);
227         cp.x++;
228     }
229
230     // Do we need to wrap ?
231     if (cp.x == nbcols) {
232         cp.x = 0;
233         cp.y++;
234     }
235     // Do we need to scroll ?
236     if (cp.y < nbrows) {
237         *pcp = cp;
238         return;
239     }
240     // Scroll screen
241     cp.y--;
242     *pcp = cp;
243     scroll_one(nbrows, nbcols, cp.page);
244 }
245
246 // Write out a buffer of alternating characters and attributes.
247 static void
248 write_attr_string(struct cursorpos *pcp, u16 count, u16 seg, u8 *offset_far)
249 {
250     while (count--) {
251         u8 car = GET_FARVAR(seg, *offset_far);
252         offset_far++;
253         u8 attr = GET_FARVAR(seg, *offset_far);
254         offset_far++;
255
256         struct carattr ca = {car, attr, 1};
257         write_teletype(pcp, ca);
258     }
259 }
260
261 // Write out a buffer of characters.
262 static void
263 write_string(struct cursorpos *pcp, u8 attr, u16 count, u16 seg, u8 *offset_far)
264 {
265     while (count--) {
266         u8 car = GET_FARVAR(seg, *offset_far);
267         offset_far++;
268
269         struct carattr ca = {car, attr, 1};
270         write_teletype(pcp, ca);
271     }
272 }
273
274
275 /****************************************************************
276  * Save and restore bda state
277  ****************************************************************/
278
279 static void
280 save_bda_state(u16 seg, struct saveBDAstate *info)
281 {
282     SET_FARVAR(seg, info->video_mode, GET_BDA(video_mode));
283     SET_FARVAR(seg, info->video_cols, GET_BDA(video_cols));
284     SET_FARVAR(seg, info->video_pagesize, GET_BDA(video_pagesize));
285     SET_FARVAR(seg, info->crtc_address, GET_BDA(crtc_address));
286     SET_FARVAR(seg, info->video_rows, GET_BDA(video_rows));
287     SET_FARVAR(seg, info->char_height, GET_BDA(char_height));
288     SET_FARVAR(seg, info->video_ctl, GET_BDA(video_ctl));
289     SET_FARVAR(seg, info->video_switches, GET_BDA(video_switches));
290     SET_FARVAR(seg, info->modeset_ctl, GET_BDA(modeset_ctl));
291     SET_FARVAR(seg, info->cursor_type, GET_BDA(cursor_type));
292     u16 i;
293     for (i=0; i<8; i++)
294         SET_FARVAR(seg, info->cursor_pos[i], GET_BDA(cursor_pos[i]));
295     SET_FARVAR(seg, info->video_pagestart, GET_BDA(video_pagestart));
296     SET_FARVAR(seg, info->video_page, GET_BDA(video_page));
297     /* current font */
298     SET_FARVAR(seg, info->font0, GET_IVT(0x1f));
299     SET_FARVAR(seg, info->font1, GET_IVT(0x43));
300 }
301
302 static void
303 restore_bda_state(u16 seg, struct saveBDAstate *info)
304 {
305     SET_BDA(video_mode, GET_FARVAR(seg, info->video_mode));
306     SET_BDA(video_cols, GET_FARVAR(seg, info->video_cols));
307     SET_BDA(video_pagesize, GET_FARVAR(seg, info->video_pagesize));
308     SET_BDA(crtc_address, GET_FARVAR(seg, info->crtc_address));
309     SET_BDA(video_rows, GET_FARVAR(seg, info->video_rows));
310     SET_BDA(char_height, GET_FARVAR(seg, info->char_height));
311     SET_BDA(video_ctl, GET_FARVAR(seg, info->video_ctl));
312     SET_BDA(video_switches, GET_FARVAR(seg, info->video_switches));
313     SET_BDA(modeset_ctl, GET_FARVAR(seg, info->modeset_ctl));
314     SET_BDA(cursor_type, GET_FARVAR(seg, info->cursor_type));
315     u16 i;
316     for (i = 0; i < 8; i++)
317         SET_BDA(cursor_pos[i], GET_FARVAR(seg, info->cursor_pos[i]));
318     SET_BDA(video_pagestart, GET_FARVAR(seg, info->video_pagestart));
319     SET_BDA(video_page, GET_FARVAR(seg, info->video_page));
320     /* current font */
321     SET_IVT(0x1f, GET_FARVAR(seg, info->font0));
322     SET_IVT(0x43, GET_FARVAR(seg, info->font1));
323 }
324
325 // Setup BDA after a mode switch.
326 void
327 modeswitch_set_bda(int mode, int flags, struct vgamode_s *vmode_g)
328 {
329     // Set the BIOS mem
330     int width = GET_GLOBAL(vmode_g->width);
331     int height = GET_GLOBAL(vmode_g->height);
332     u8 memmodel = GET_GLOBAL(vmode_g->memmodel);
333     int cheight = GET_GLOBAL(vmode_g->cheight);
334     SET_BDA(video_mode, mode);
335     if (memmodel == MM_TEXT) {
336         SET_BDA(video_cols, width);
337         SET_BDA(video_rows, height-1);
338         SET_BDA(cursor_type, 0x0607);
339     } else {
340         int cwidth = GET_GLOBAL(vmode_g->cwidth);
341         SET_BDA(video_cols, width / cwidth);
342         SET_BDA(video_rows, (height / cheight) - 1);
343         SET_BDA(cursor_type, 0x0000);
344     }
345     SET_BDA(video_pagesize, calc_page_size(memmodel, width, height));
346     SET_BDA(crtc_address, stdvga_get_crtc());
347     SET_BDA(char_height, cheight);
348     SET_BDA(video_ctl, 0x60 | (flags & MF_NOCLEARMEM ? 0x80 : 0x00));
349     SET_BDA(video_switches, 0xF9);
350     SET_BDA(modeset_ctl, GET_BDA(modeset_ctl) & 0x7f);
351     int i;
352     for (i=0; i<8; i++)
353         SET_BDA(cursor_pos[i], 0x0000);
354     SET_BDA(video_pagestart, 0x0000);
355     SET_BDA(video_page, 0x00);
356
357     // FIXME We nearly have the good tables. to be reworked
358     SET_BDA(dcc_index, 0x08);   // 8 is VGA should be ok for now
359     SET_BDA(video_savetable
360             , SEGOFF(get_global_seg(), (u32)&video_save_pointer_table));
361
362     // FIXME
363     SET_BDA(video_msr, 0x00); // Unavailable on vanilla vga, but...
364     SET_BDA(video_pal, 0x00); // Unavailable on vanilla vga, but...
365
366     // Set the ints 0x1F and 0x43
367     SET_IVT(0x1f, SEGOFF(get_global_seg(), (u32)&vgafont8[128 * 8]));
368
369     switch (cheight) {
370     case 8:
371         SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont8));
372         break;
373     case 14:
374         SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont14));
375         break;
376     case 16:
377         SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont16));
378         break;
379     }
380 }
381
382
383 /****************************************************************
384  * VGA int 10 handler
385  ****************************************************************/
386
387 static void
388 handle_1000(struct bregs *regs)
389 {
390     int mode = regs->al & 0x7f;
391
392     // Set regs->al
393     if (mode > 7)
394         regs->al = 0x20;
395     else if (mode == 6)
396         regs->al = 0x3f;
397     else
398         regs->al = 0x30;
399
400     int flags = GET_BDA(modeset_ctl) & (MF_NOPALETTE|MF_GRAYSUM);
401     if (regs->al & 0x80)
402         flags |= MF_NOCLEARMEM;
403
404     vgahw_set_mode(mode, flags);
405 }
406
407 static void
408 handle_1001(struct bregs *regs)
409 {
410     set_cursor_shape(regs->ch, regs->cl);
411 }
412
413 static void
414 handle_1002(struct bregs *regs)
415 {
416     struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
417     set_cursor_pos(cp);
418 }
419
420 static void
421 handle_1003(struct bregs *regs)
422 {
423     regs->cx = get_cursor_shape(regs->bh);
424     struct cursorpos cp = get_cursor_pos(regs->bh);
425     regs->dl = cp.x;
426     regs->dh = cp.y;
427 }
428
429 // Read light pen pos (unimplemented)
430 static void
431 handle_1004(struct bregs *regs)
432 {
433     debug_stub(regs);
434     regs->ax = regs->bx = regs->cx = regs->dx = 0;
435 }
436
437 static void
438 handle_1005(struct bregs *regs)
439 {
440     set_active_page(regs->al);
441 }
442
443 static void
444 verify_scroll(struct bregs *regs, int dir)
445 {
446     u8 page = GET_BDA(video_page);
447     struct cursorpos ul = {regs->cl, regs->ch, page};
448     struct cursorpos lr = {regs->dl, regs->dh, page};
449
450     u16 nbrows = GET_BDA(video_rows) + 1;
451     if (lr.y >= nbrows)
452         lr.y = nbrows - 1;
453     u16 nbcols = GET_BDA(video_cols);
454     if (lr.x >= nbcols)
455         lr.x = nbcols - 1;
456
457     if (ul.x > lr.x || ul.y > lr.y)
458         return;
459
460     u16 nblines = regs->al;
461     if (!nblines || nblines > lr.y - ul.y + 1)
462         nblines = lr.y - ul.y + 1;
463
464     vgafb_scroll(dir * nblines, regs->bh, ul, lr);
465 }
466
467 static void
468 handle_1006(struct bregs *regs)
469 {
470     verify_scroll(regs, 1);
471 }
472
473 static void
474 handle_1007(struct bregs *regs)
475 {
476     verify_scroll(regs, -1);
477 }
478
479 static void
480 handle_1008(struct bregs *regs)
481 {
482     struct carattr ca = vgafb_read_char(get_cursor_pos(regs->bh));
483     regs->al = ca.car;
484     regs->ah = ca.attr;
485 }
486
487 static void noinline
488 write_chars(u8 page, struct carattr ca, u16 count)
489 {
490     struct cursorpos cp = get_cursor_pos(page);
491     while (count--) {
492         vgafb_write_char(cp, ca);
493         cp.x++;
494     }
495 }
496
497 static void
498 handle_1009(struct bregs *regs)
499 {
500     struct carattr ca = {regs->al, regs->bl, 1};
501     write_chars(regs->bh, ca, regs->cx);
502 }
503
504 static void
505 handle_100a(struct bregs *regs)
506 {
507     struct carattr ca = {regs->al, regs->bl, 0};
508     write_chars(regs->bh, ca, regs->cx);
509 }
510
511
512 static void
513 handle_100b00(struct bregs *regs)
514 {
515     stdvga_set_border_color(regs->bl);
516 }
517
518 static void
519 handle_100b01(struct bregs *regs)
520 {
521     stdvga_set_palette(regs->bl);
522 }
523
524 static void
525 handle_100bXX(struct bregs *regs)
526 {
527     debug_stub(regs);
528 }
529
530 static void
531 handle_100b(struct bregs *regs)
532 {
533     switch (regs->bh) {
534     case 0x00: handle_100b00(regs); break;
535     case 0x01: handle_100b01(regs); break;
536     default:   handle_100bXX(regs); break;
537     }
538 }
539
540
541 static void
542 handle_100c(struct bregs *regs)
543 {
544     // XXX - page (regs->bh) is unused
545     vgafb_write_pixel(regs->al, regs->cx, regs->dx);
546 }
547
548 static void
549 handle_100d(struct bregs *regs)
550 {
551     // XXX - page (regs->bh) is unused
552     regs->al = vgafb_read_pixel(regs->cx, regs->dx);
553 }
554
555 static void noinline
556 handle_100e(struct bregs *regs)
557 {
558     // Ralf Brown Interrupt list is WRONG on bh(page)
559     // We do output only on the current page !
560     struct carattr ca = {regs->al, regs->bl, 0};
561     struct cursorpos cp = get_cursor_pos(0xff);
562     write_teletype(&cp, ca);
563     set_cursor_pos(cp);
564 }
565
566 static void
567 handle_100f(struct bregs *regs)
568 {
569     regs->bh = GET_BDA(video_page);
570     regs->al = GET_BDA(video_mode) | (GET_BDA(video_ctl) & 0x80);
571     regs->ah = GET_BDA(video_cols);
572 }
573
574
575 static void
576 handle_101000(struct bregs *regs)
577 {
578     if (regs->bl > 0x14)
579         return;
580     stdvga_set_single_palette_reg(regs->bl, regs->bh);
581 }
582
583 static void
584 handle_101001(struct bregs *regs)
585 {
586     stdvga_set_overscan_border_color(regs->bh);
587 }
588
589 static void
590 handle_101002(struct bregs *regs)
591 {
592     stdvga_set_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
593 }
594
595 static void
596 handle_101003(struct bregs *regs)
597 {
598     stdvga_toggle_intensity(regs->bl);
599 }
600
601 static void
602 handle_101007(struct bregs *regs)
603 {
604     if (regs->bl > 0x14)
605         return;
606     regs->bh = stdvga_get_single_palette_reg(regs->bl);
607 }
608
609 static void
610 handle_101008(struct bregs *regs)
611 {
612     regs->bh = stdvga_get_overscan_border_color();
613 }
614
615 static void
616 handle_101009(struct bregs *regs)
617 {
618     stdvga_get_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
619 }
620
621 static void noinline
622 handle_101010(struct bregs *regs)
623 {
624     u8 rgb[3] = {regs->dh, regs->ch, regs->cl};
625     stdvga_set_dac_regs(GET_SEG(SS), rgb, regs->bx, 1);
626 }
627
628 static void
629 handle_101012(struct bregs *regs)
630 {
631     stdvga_set_dac_regs(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
632 }
633
634 static void
635 handle_101013(struct bregs *regs)
636 {
637     stdvga_select_video_dac_color_page(regs->bl, regs->bh);
638 }
639
640 static void noinline
641 handle_101015(struct bregs *regs)
642 {
643     u8 rgb[3];
644     stdvga_get_dac_regs(GET_SEG(SS), rgb, regs->bx, 1);
645     regs->dh = rgb[0];
646     regs->ch = rgb[1];
647     regs->cl = rgb[2];
648 }
649
650 static void
651 handle_101017(struct bregs *regs)
652 {
653     stdvga_get_dac_regs(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
654 }
655
656 static void
657 handle_101018(struct bregs *regs)
658 {
659     stdvga_set_pel_mask(regs->bl);
660 }
661
662 static void
663 handle_101019(struct bregs *regs)
664 {
665     regs->bl = stdvga_get_pel_mask();
666 }
667
668 static void
669 handle_10101a(struct bregs *regs)
670 {
671     stdvga_read_video_dac_state(&regs->bl, &regs->bh);
672 }
673
674 static void
675 handle_10101b(struct bregs *regs)
676 {
677     stdvga_perform_gray_scale_summing(regs->bx, regs->cx);
678 }
679
680 static void
681 handle_1010XX(struct bregs *regs)
682 {
683     debug_stub(regs);
684 }
685
686 static void
687 handle_1010(struct bregs *regs)
688 {
689     switch (regs->al) {
690     case 0x00: handle_101000(regs); break;
691     case 0x01: handle_101001(regs); break;
692     case 0x02: handle_101002(regs); break;
693     case 0x03: handle_101003(regs); break;
694     case 0x07: handle_101007(regs); break;
695     case 0x08: handle_101008(regs); break;
696     case 0x09: handle_101009(regs); break;
697     case 0x10: handle_101010(regs); break;
698     case 0x12: handle_101012(regs); break;
699     case 0x13: handle_101013(regs); break;
700     case 0x15: handle_101015(regs); break;
701     case 0x17: handle_101017(regs); break;
702     case 0x18: handle_101018(regs); break;
703     case 0x19: handle_101019(regs); break;
704     case 0x1a: handle_10101a(regs); break;
705     case 0x1b: handle_10101b(regs); break;
706     default:   handle_1010XX(regs); break;
707     }
708 }
709
710
711 static void
712 handle_101100(struct bregs *regs)
713 {
714     stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
715                      , regs->dx, regs->bl, regs->bh);
716 }
717
718 static void
719 handle_101101(struct bregs *regs)
720 {
721     stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
722 }
723
724 static void
725 handle_101102(struct bregs *regs)
726 {
727     stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
728 }
729
730 static void
731 handle_101103(struct bregs *regs)
732 {
733     stdvga_set_text_block_specifier(regs->bl);
734 }
735
736 static void
737 handle_101104(struct bregs *regs)
738 {
739     stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
740 }
741
742 static void
743 handle_101110(struct bregs *regs)
744 {
745     stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
746                      , regs->dx, regs->bl, regs->bh);
747     set_scan_lines(regs->bh);
748 }
749
750 static void
751 handle_101111(struct bregs *regs)
752 {
753     stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
754     set_scan_lines(14);
755 }
756
757 static void
758 handle_101112(struct bregs *regs)
759 {
760     stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
761     set_scan_lines(8);
762 }
763
764 static void
765 handle_101114(struct bregs *regs)
766 {
767     stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
768     set_scan_lines(16);
769 }
770
771 static void
772 handle_101130(struct bregs *regs)
773 {
774     switch (regs->bh) {
775     case 0x00: {
776         struct segoff_s so = GET_IVT(0x1f);
777         regs->es = so.seg;
778         regs->bp = so.offset;
779         break;
780     }
781     case 0x01: {
782         struct segoff_s so = GET_IVT(0x43);
783         regs->es = so.seg;
784         regs->bp = so.offset;
785         break;
786     }
787     case 0x02:
788         regs->es = get_global_seg();
789         regs->bp = (u32)vgafont14;
790         break;
791     case 0x03:
792         regs->es = get_global_seg();
793         regs->bp = (u32)vgafont8;
794         break;
795     case 0x04:
796         regs->es = get_global_seg();
797         regs->bp = (u32)vgafont8 + 128 * 8;
798         break;
799     case 0x05:
800         regs->es = get_global_seg();
801         regs->bp = (u32)vgafont14alt;
802         break;
803     case 0x06:
804         regs->es = get_global_seg();
805         regs->bp = (u32)vgafont16;
806         break;
807     case 0x07:
808         regs->es = get_global_seg();
809         regs->bp = (u32)vgafont16alt;
810         break;
811     default:
812         dprintf(1, "Get font info BH(%02x) was discarded\n", regs->bh);
813         return;
814     }
815     // Set byte/char of on screen font
816     regs->cx = GET_BDA(char_height) & 0xff;
817
818     // Set Highest char row
819     regs->dx = GET_BDA(video_rows);
820 }
821
822 static void
823 handle_1011XX(struct bregs *regs)
824 {
825     debug_stub(regs);
826 }
827
828 static void
829 handle_1011(struct bregs *regs)
830 {
831     switch (regs->al) {
832     case 0x00: handle_101100(regs); break;
833     case 0x01: handle_101101(regs); break;
834     case 0x02: handle_101102(regs); break;
835     case 0x03: handle_101103(regs); break;
836     case 0x04: handle_101104(regs); break;
837     case 0x10: handle_101110(regs); break;
838     case 0x11: handle_101111(regs); break;
839     case 0x12: handle_101112(regs); break;
840     case 0x14: handle_101114(regs); break;
841     case 0x30: handle_101130(regs); break;
842     default:   handle_1011XX(regs); break;
843     }
844 }
845
846
847 static void
848 handle_101210(struct bregs *regs)
849 {
850     u16 crtc_addr = GET_BDA(crtc_address);
851     if (crtc_addr == VGAREG_MDA_CRTC_ADDRESS)
852         regs->bx = 0x0103;
853     else
854         regs->bx = 0x0003;
855     regs->cx = GET_BDA(video_switches) & 0x0f;
856 }
857
858 static void
859 handle_101230(struct bregs *regs)
860 {
861     u8 mctl = GET_BDA(modeset_ctl);
862     u8 vswt = GET_BDA(video_switches);
863     switch (regs->al) {
864     case 0x00:
865         // 200 lines
866         mctl = (mctl & ~0x10) | 0x80;
867         vswt = (vswt & ~0x0f) | 0x08;
868         break;
869     case 0x01:
870         // 350 lines
871         mctl &= ~0x90;
872         vswt = (vswt & ~0x0f) | 0x09;
873         break;
874     case 0x02:
875         // 400 lines
876         mctl = (mctl & ~0x80) | 0x10;
877         vswt = (vswt & ~0x0f) | 0x09;
878         break;
879     default:
880         dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
881         break;
882     }
883     SET_BDA(modeset_ctl, mctl);
884     SET_BDA(video_switches, vswt);
885     regs->al = 0x12;
886 }
887
888 static void
889 handle_101231(struct bregs *regs)
890 {
891     u8 v = (regs->al & 0x01) << 3;
892     u8 mctl = GET_BDA(video_ctl) & ~0x08;
893     SET_BDA(video_ctl, mctl | v);
894     regs->al = 0x12;
895 }
896
897 static void
898 handle_101232(struct bregs *regs)
899 {
900     stdvga_enable_video_addressing(regs->al);
901     regs->al = 0x12;
902 }
903
904 static void
905 handle_101233(struct bregs *regs)
906 {
907     u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
908     u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
909     SET_BDA(modeset_ctl, v | v2);
910     regs->al = 0x12;
911 }
912
913 static void
914 handle_101234(struct bregs *regs)
915 {
916     u8 v = (regs->al & 0x01) ^ 0x01;
917     u8 v2 = GET_BDA(modeset_ctl) & ~0x01;
918     SET_BDA(modeset_ctl, v | v2);
919     regs->al = 0x12;
920 }
921
922 static void
923 handle_101235(struct bregs *regs)
924 {
925     debug_stub(regs);
926     regs->al = 0x12;
927 }
928
929 static void
930 handle_101236(struct bregs *regs)
931 {
932     debug_stub(regs);
933     regs->al = 0x12;
934 }
935
936 static void
937 handle_1012XX(struct bregs *regs)
938 {
939     debug_stub(regs);
940 }
941
942 static void
943 handle_1012(struct bregs *regs)
944 {
945     switch (regs->bl) {
946     case 0x10: handle_101210(regs); break;
947     case 0x30: handle_101230(regs); break;
948     case 0x31: handle_101231(regs); break;
949     case 0x32: handle_101232(regs); break;
950     case 0x33: handle_101233(regs); break;
951     case 0x34: handle_101234(regs); break;
952     case 0x35: handle_101235(regs); break;
953     case 0x36: handle_101236(regs); break;
954     default:   handle_1012XX(regs); break;
955     }
956
957     // XXX - cirrus has 1280, 1281, 1282, 1285, 129a, 12a0, 12a1, 12a2, 12ae
958 }
959
960
961 // Write string
962 static void noinline
963 handle_1013(struct bregs *regs)
964 {
965     struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
966     // if row=0xff special case : use current cursor position
967     if (cp.y == 0xff)
968         cp = get_cursor_pos(cp.page);
969     u8 flag = regs->al;
970     if (flag & 2)
971         write_attr_string(&cp, regs->cx, regs->es, (void*)(regs->bp + 0));
972     else
973         write_string(&cp, regs->bl, regs->cx, regs->es, (void*)(regs->bp + 0));
974
975     if (flag & 1)
976         set_cursor_pos(cp);
977 }
978
979
980 static void
981 handle_101a00(struct bregs *regs)
982 {
983     regs->bx = GET_BDA(dcc_index);
984     regs->al = 0x1a;
985 }
986
987 static void
988 handle_101a01(struct bregs *regs)
989 {
990     SET_BDA(dcc_index, regs->bl);
991     dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
992     regs->al = 0x1a;
993 }
994
995 static void
996 handle_101aXX(struct bregs *regs)
997 {
998     debug_stub(regs);
999 }
1000
1001 static void
1002 handle_101a(struct bregs *regs)
1003 {
1004     switch (regs->al) {
1005     case 0x00: handle_101a00(regs); break;
1006     case 0x01: handle_101a01(regs); break;
1007     default:   handle_101aXX(regs); break;
1008     }
1009 }
1010
1011
1012 struct funcInfo {
1013     struct segoff_s static_functionality;
1014     u8 bda_0x49[30];
1015     u8 bda_0x84[3];
1016     u8 dcc_index;
1017     u8 dcc_alt;
1018     u16 colors;
1019     u8 pages;
1020     u8 scan_lines;
1021     u8 primary_char;
1022     u8 secondar_char;
1023     u8 misc;
1024     u8 non_vga_mode;
1025     u8 reserved_2f[2];
1026     u8 video_mem;
1027     u8 save_flags;
1028     u8 disp_info;
1029     u8 reserved_34[12];
1030 };
1031
1032 static void
1033 handle_101b(struct bregs *regs)
1034 {
1035     u16 seg = regs->es;
1036     struct funcInfo *info = (void*)(regs->di+0);
1037     memset_far(seg, info, 0, sizeof(*info));
1038     // Address of static functionality table
1039     SET_FARVAR(seg, info->static_functionality
1040                , SEGOFF(get_global_seg(), (u32)static_functionality));
1041
1042     // Hard coded copy from BIOS area. Should it be cleaner ?
1043     memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49
1044                , sizeof(info->bda_0x49));
1045     memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84
1046                , sizeof(info->bda_0x84));
1047
1048     SET_FARVAR(seg, info->dcc_index, GET_BDA(dcc_index));
1049     SET_FARVAR(seg, info->colors, 16);
1050     SET_FARVAR(seg, info->pages, 8);
1051     SET_FARVAR(seg, info->scan_lines, 2);
1052     SET_FARVAR(seg, info->video_mem, 3);
1053     regs->al = 0x1B;
1054 }
1055
1056
1057 static void
1058 handle_101c00(struct bregs *regs)
1059 {
1060     u16 flags = regs->cx;
1061     u16 size = 0;
1062     if (flags & 1)
1063         size += sizeof(struct saveVideoHardware);
1064     if (flags & 2)
1065         size += sizeof(struct saveBDAstate);
1066     if (flags & 4)
1067         size += sizeof(struct saveDACcolors);
1068     regs->bx = size;
1069     regs->al = 0x1c;
1070 }
1071
1072 static void
1073 handle_101c01(struct bregs *regs)
1074 {
1075     u16 flags = regs->cx;
1076     u16 seg = regs->es;
1077     void *data = (void*)(regs->bx+0);
1078     if (flags & 1) {
1079         stdvga_save_state(seg, data);
1080         data += sizeof(struct saveVideoHardware);
1081     }
1082     if (flags & 2) {
1083         save_bda_state(seg, data);
1084         data += sizeof(struct saveBDAstate);
1085     }
1086     if (flags & 4)
1087         stdvga_save_dac_state(seg, data);
1088     regs->al = 0x1c;
1089 }
1090
1091 static void
1092 handle_101c02(struct bregs *regs)
1093 {
1094     u16 flags = regs->cx;
1095     u16 seg = regs->es;
1096     void *data = (void*)(regs->bx+0);
1097     if (flags & 1) {
1098         stdvga_restore_state(seg, data);
1099         data += sizeof(struct saveVideoHardware);
1100     }
1101     if (flags & 2) {
1102         restore_bda_state(seg, data);
1103         data += sizeof(struct saveBDAstate);
1104     }
1105     if (flags & 4)
1106         stdvga_restore_dac_state(seg, data);
1107     regs->al = 0x1c;
1108 }
1109
1110 static void
1111 handle_101cXX(struct bregs *regs)
1112 {
1113     debug_stub(regs);
1114 }
1115
1116 static void
1117 handle_101c(struct bregs *regs)
1118 {
1119     switch (regs->al) {
1120     case 0x00: handle_101c00(regs); break;
1121     case 0x01: handle_101c01(regs); break;
1122     case 0x02: handle_101c02(regs); break;
1123     default:   handle_101cXX(regs); break;
1124     }
1125 }
1126
1127 static void
1128 handle_10XX(struct bregs *regs)
1129 {
1130     debug_stub(regs);
1131 }
1132
1133 // INT 10h Video Support Service Entry Point
1134 void VISIBLE16
1135 handle_10(struct bregs *regs)
1136 {
1137     debug_enter(regs, DEBUG_VGA_10);
1138     switch (regs->ah) {
1139     case 0x00: handle_1000(regs); break;
1140     case 0x01: handle_1001(regs); break;
1141     case 0x02: handle_1002(regs); break;
1142     case 0x03: handle_1003(regs); break;
1143     case 0x04: handle_1004(regs); break;
1144     case 0x05: handle_1005(regs); break;
1145     case 0x06: handle_1006(regs); break;
1146     case 0x07: handle_1007(regs); break;
1147     case 0x08: handle_1008(regs); break;
1148     case 0x09: handle_1009(regs); break;
1149     case 0x0a: handle_100a(regs); break;
1150     case 0x0b: handle_100b(regs); break;
1151     case 0x0c: handle_100c(regs); break;
1152     case 0x0d: handle_100d(regs); break;
1153     case 0x0e: handle_100e(regs); break;
1154     case 0x0f: handle_100f(regs); break;
1155     case 0x10: handle_1010(regs); break;
1156     case 0x11: handle_1011(regs); break;
1157     case 0x12: handle_1012(regs); break;
1158     case 0x13: handle_1013(regs); break;
1159     case 0x1a: handle_101a(regs); break;
1160     case 0x1b: handle_101b(regs); break;
1161     case 0x1c: handle_101c(regs); break;
1162     case 0x4f: handle_104f(regs); break;
1163     default:   handle_10XX(regs); break;
1164     }
1165 }
1166
1167
1168 /****************************************************************
1169  * VGA post
1170  ****************************************************************/
1171
1172 static void
1173 init_bios_area(void)
1174 {
1175     // init detected hardware BIOS Area
1176     // set 80x25 color (not clear from RBIL but usual)
1177     u16 eqf = GET_BDA(equipment_list_flags);
1178     SET_BDA(equipment_list_flags, (eqf & 0xffcf) | 0x20);
1179
1180     // Just for the first int10 find its children
1181
1182     // the default char height
1183     SET_BDA(char_height, 0x10);
1184
1185     // Clear the screen
1186     SET_BDA(video_ctl, 0x60);
1187
1188     // Set the basic screen we have
1189     SET_BDA(video_switches, 0xf9);
1190
1191     // Set the basic modeset options
1192     SET_BDA(modeset_ctl, 0x51);
1193
1194     // Set the  default MSR
1195     SET_BDA(video_msr, 0x09);
1196 }
1197
1198 u16 VgaBDF VAR16;
1199
1200 void VISIBLE16
1201 vga_post(struct bregs *regs)
1202 {
1203     debug_enter(regs, DEBUG_VGA_POST);
1204
1205     SET_VGA(VgaBDF, regs->ax);
1206
1207     int ret = vgahw_init();
1208     if (ret) {
1209         dprintf(1, "Failed to initialize VGA hardware.  Exiting.\n");
1210         return;
1211     }
1212
1213     init_bios_area();
1214
1215     build_video_param();
1216
1217     extern void entry_10(void);
1218     SET_IVT(0x10, SEGOFF(get_global_seg(), (u32)entry_10));
1219
1220     // XXX - clear screen and display info
1221
1222     // Fixup checksum
1223     extern u8 _rom_header_size, _rom_header_checksum;
1224     SET_VGA(_rom_header_checksum, 0);
1225     u8 sum = -checksum_far(get_global_seg(), 0, _rom_header_size * 512);
1226     SET_VGA(_rom_header_checksum, sum);
1227 }