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