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