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