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