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