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