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