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