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