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