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