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