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