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