blob: f07e85bd339b5ab37912f3e06c37f6fa06ba40e7 [file] [log] [blame]
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001// VGA bios implementation
2//
Kevin O'Connordab0a742013-12-03 11:50:49 -05003// Copyright (C) 2009-2013 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04004// 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
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04008#include "biosvar.h" // GET_BDA
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04009#include "bregs.h" // struct bregs
Kevin O'Connore91ec7c2012-01-14 16:30:49 -050010#include "clext.h" // clext_1012
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040011#include "config.h" // CONFIG_*
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040012#include "output.h" // dprintf
Kevin O'Connor2e57c812013-09-14 22:29:32 -040013#include "std/vbe.h" // VBE_RETURN_STATUS_FAILED
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040014#include "stdvga.h" // stdvga_set_cursor_shape
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040015#include "string.h" // memset_far
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040016#include "vgabios.h" // calc_page_size
17#include "vgahw.h" // vgahw_set_mode
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040018
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040019
20/****************************************************************
21 * Helper functions
22 ****************************************************************/
23
Kevin O'Connor3876b532012-01-24 00:07:44 -050024// Return the bits per pixel in system memory for a given mode.
25int
26vga_bpp(struct vgamode_s *vmode_g)
27{
28 switch (GET_GLOBAL(vmode_g->memmodel)) {
29 case MM_TEXT:
30 return 16;
31 case MM_PLANAR:
32 return 1;
33 }
34 u8 depth = GET_GLOBAL(vmode_g->depth);
35 if (depth > 8)
36 return ALIGN(depth, 8);
37 return depth;
38}
39
Kevin O'Connor83047be2012-01-07 18:27:19 -050040u16
41calc_page_size(u8 memmodel, u16 width, u16 height)
42{
43 switch (memmodel) {
44 case MM_TEXT:
45 return ALIGN(width * height * 2, 2*1024);
46 case MM_CGA:
47 return 16*1024;
48 default:
49 return ALIGN(width * height / 8, 8*1024);
50 }
51}
52
Kevin O'Connorc9aecfc2014-10-22 20:57:37 -040053// Determine cursor shape (taking into account possible cursor scaling)
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -040054u16
Kevin O'Connorc9aecfc2014-10-22 20:57:37 -040055get_cursor_shape(void)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040056{
Kevin O'Connorc9aecfc2014-10-22 20:57:37 -040057 u16 cursor_type = GET_BDA(cursor_type);
Kevin O'Connor479fd722014-10-22 20:27:28 -040058 u8 emulate_cursor = (GET_BDA(video_ctl) & 1) == 0;
Kevin O'Connorc9aecfc2014-10-22 20:57:37 -040059 if (!emulate_cursor)
60 return cursor_type;
61 u8 start = (cursor_type >> 8) & 0x3f;
62 u8 end = cursor_type & 0x1f;
Kevin O'Connordd2be772009-05-16 15:41:23 -040063 u16 cheight = GET_BDA(char_height);
Kevin O'Connorc9aecfc2014-10-22 20:57:37 -040064 if (cheight <= 8 || end >= 8 || start >= 0x20)
65 return cursor_type;
66 if (end != (start + 1))
67 start = ((start + 1) * cheight / 8) - 1;
68 else
69 start = ((end + 1) * cheight / 8) - 2;
70 end = ((end + 1) * cheight / 8) - 1;
71 return (start << 8) | end;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040072}
73
Kevin O'Connorc9aecfc2014-10-22 20:57:37 -040074static void
75set_cursor_shape(u16 cursor_type)
Kevin O'Connor0818e1a2009-05-16 18:00:19 -040076{
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -040077 vgafb_set_swcursor(0);
Kevin O'Connorc9aecfc2014-10-22 20:57:37 -040078 SET_BDA(cursor_type, cursor_type);
79 if (CONFIG_VGA_STDVGA_PORTS)
80 stdvga_set_cursor_shape(get_cursor_shape());
Kevin O'Connor0818e1a2009-05-16 18:00:19 -040081}
82
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040083static void
Kevin O'Connor918b1562009-05-25 11:05:18 -040084set_cursor_pos(struct cursorpos cp)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040085{
Kevin O'Connor6ee837b2012-02-13 20:09:02 -050086 u8 page = cp.page, x = cp.x, y = cp.y;
87
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040088 // Should not happen...
Kevin O'Connor6ee837b2012-02-13 20:09:02 -050089 if (page > 7)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040090 return;
91
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -040092 vgafb_set_swcursor(0);
93
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040094 // Bios cursor pos
Kevin O'Connor6ee837b2012-02-13 20:09:02 -050095 SET_BDA(cursor_pos[page], (y << 8) | x);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040096
Kevin O'Connorefbf4d62014-02-09 11:50:21 -050097 if (!CONFIG_VGA_STDVGA_PORTS)
98 return;
99
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400100 // Set the hardware cursor
Kevin O'Connordd2be772009-05-16 15:41:23 -0400101 u8 current = GET_BDA(video_page);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400102 if (cp.page != current)
Kevin O'Connordd2be772009-05-16 15:41:23 -0400103 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400104
Kevin O'Connor83047be2012-01-07 18:27:19 -0500105 // Calculate the memory address
Kevin O'Connora02d4182014-04-05 22:48:05 -0400106 stdvga_set_cursor_pos((int)text_address(cp));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400107}
108
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -0400109struct cursorpos
Kevin O'Connor918b1562009-05-25 11:05:18 -0400110get_cursor_pos(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400111{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400112 if (page == 0xff)
113 // special case - use current page
114 page = GET_BDA(video_page);
115 if (page > 7) {
116 struct cursorpos cp = { 0, 0, 0xfe };
117 return cp;
118 }
Kevin O'Connor918b1562009-05-25 11:05:18 -0400119 u16 xy = GET_BDA(cursor_pos[page]);
120 struct cursorpos cp = {xy, xy>>8, page};
121 return cp;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400122}
123
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400124static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400125set_active_page(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400126{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400127 if (page > 7)
128 return;
129
130 // Get the mode
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500131 struct vgamode_s *vmode_g = get_current_mode();
Kevin O'Connor5727c292009-05-16 17:29:32 -0400132 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400133 return;
134
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -0400135 vgafb_set_swcursor(0);
136
Kevin O'Connor83047be2012-01-07 18:27:19 -0500137 // Calculate memory address of start of page
Kevin O'Connora02d4182014-04-05 22:48:05 -0400138 struct cursorpos cp = {0, 0, page};
139 int address = (int)text_address(cp);
Kevin O'Connord61fc532012-01-27 20:37:45 -0500140 vgahw_set_displaystart(vmode_g, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400141
142 // And change the BIOS page
Kevin O'Connor83047be2012-01-07 18:27:19 -0500143 SET_BDA(video_pagestart, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400144 SET_BDA(video_page, page);
145
Kevin O'Connora12c2152009-05-13 22:06:16 -0400146 dprintf(1, "Set active page %02x address %04x\n", page, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400147
148 // Display the cursor, now the page is active
Kevin O'Connora02d4182014-04-05 22:48:05 -0400149 set_cursor_pos(get_cursor_pos(page));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400150}
151
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400152static void
153set_scan_lines(u8 lines)
154{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500155 stdvga_set_scan_lines(lines);
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400156 SET_BDA(char_height, lines);
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500157 u16 vde = stdvga_get_vde();
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400158 u8 rows = vde / lines;
159 SET_BDA(video_rows, rows - 1);
160 u16 cols = GET_BDA(video_cols);
Kevin O'Connor83047be2012-01-07 18:27:19 -0500161 SET_BDA(video_pagesize, calc_page_size(MM_TEXT, cols, rows));
Kevin O'Connor479fd722014-10-22 20:27:28 -0400162 if (lines == 8)
Kevin O'Connorc9aecfc2014-10-22 20:57:37 -0400163 set_cursor_shape(0x0607);
Kevin O'Connor479fd722014-10-22 20:27:28 -0400164 else
Kevin O'Connorc9aecfc2014-10-22 20:57:37 -0400165 set_cursor_shape(((lines - 3) << 8) | (lines - 2));
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400166}
167
168
169/****************************************************************
170 * Character writing
171 ****************************************************************/
172
Kevin O'Connor0910dde2014-02-08 16:43:30 -0500173// Write a character to the screen and calculate new cursor position.
174static void
175write_char(struct cursorpos *pcp, struct carattr ca)
Kevin O'Connor09262412009-05-25 11:44:11 -0400176{
Kevin O'Connor0910dde2014-02-08 16:43:30 -0500177 vgafb_write_char(*pcp, ca);
178 pcp->x++;
179 // Do we need to wrap ?
180 if (pcp->x == GET_BDA(video_cols)) {
181 pcp->x = 0;
182 pcp->y++;
183 }
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400184}
185
186// Write a character to the screen at a given position. Implement
187// special characters and scroll the screen if necessary.
188static void
189write_teletype(struct cursorpos *pcp, struct carattr ca)
190{
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400191 switch (ca.car) {
192 case 7:
193 //FIXME should beep
194 break;
195 case 8:
Kevin O'Connor0910dde2014-02-08 16:43:30 -0500196 if (pcp->x > 0)
197 pcp->x--;
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400198 break;
199 case '\r':
Kevin O'Connor0910dde2014-02-08 16:43:30 -0500200 pcp->x = 0;
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400201 break;
202 case '\n':
Kevin O'Connor0910dde2014-02-08 16:43:30 -0500203 pcp->y++;
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400204 break;
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400205 default:
Kevin O'Connor0910dde2014-02-08 16:43:30 -0500206 write_char(pcp, ca);
207 break;
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400208 }
209
Kevin O'Connor82221b22009-05-26 00:20:40 -0400210 // Do we need to scroll ?
Kevin O'Connor0910dde2014-02-08 16:43:30 -0500211 u16 nbrows = GET_BDA(video_rows);
212 if (pcp->y > nbrows) {
213 pcp->y--;
Kevin O'Connor7fd2af62014-03-20 21:16:28 -0400214
Kevin O'Connor7fd2af62014-03-20 21:16:28 -0400215 struct cursorpos dest = {0, 0, pcp->page};
216 struct cursorpos src = {0, 1, pcp->page};
217 struct cursorpos size = {GET_BDA(video_cols), nbrows};
Kevin O'Connor7217ae72014-10-17 21:47:10 -0400218 vgafb_move_chars(dest, src, size);
Kevin O'Connor7fd2af62014-03-20 21:16:28 -0400219
220 struct cursorpos clr = {0, nbrows, pcp->page};
221 struct carattr attr = {' ', 0, 0};
222 struct cursorpos clrsize = {GET_BDA(video_cols), 1};
Kevin O'Connor7217ae72014-10-17 21:47:10 -0400223 vgafb_clear_chars(clr, attr, clrsize);
Kevin O'Connor82221b22009-05-26 00:20:40 -0400224 }
Kevin O'Connor82221b22009-05-26 00:20:40 -0400225}
226
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400227
228/****************************************************************
229 * Save and restore bda state
230 ****************************************************************/
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400231
Kevin O'Connor20dc4192014-02-05 20:52:25 -0500232struct saveBDAstate {
233 u8 bda_0x49[28];
234 u8 bda_0x84[6];
235 u16 vbe_mode;
236 struct segoff_s font0;
237 struct segoff_s font1;
238};
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400239
Kevin O'Connor20dc4192014-02-05 20:52:25 -0500240int
241bda_save_restore(int cmd, u16 seg, void *data)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400242{
Kevin O'Connor20dc4192014-02-05 20:52:25 -0500243 if (!(cmd & SR_BDA))
244 return 0;
245 struct saveBDAstate *info = data;
246 if (cmd & SR_SAVE) {
247 memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49
248 , sizeof(info->bda_0x49));
249 memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84
250 , sizeof(info->bda_0x84));
Kevin O'Connor9978d492014-10-17 21:17:48 -0400251 SET_FARVAR(seg, info->vbe_mode, GET_BDA_EXT(vbe_mode));
Kevin O'Connor20dc4192014-02-05 20:52:25 -0500252 SET_FARVAR(seg, info->font0, GET_IVT(0x1f));
253 SET_FARVAR(seg, info->font1, GET_IVT(0x43));
254 }
255 if (cmd & SR_RESTORE) {
256 memcpy_far(SEG_BDA, (void*)0x49, seg, info->bda_0x49
257 , sizeof(info->bda_0x49));
258 memcpy_far(SEG_BDA, (void*)0x84, seg, info->bda_0x84
259 , sizeof(info->bda_0x84));
Kevin O'Connorf7f22632014-10-17 21:37:23 -0400260 u16 vbe_mode = GET_FARVAR(seg, info->vbe_mode);
261 SET_BDA_EXT(vbe_mode, vbe_mode);
Kevin O'Connor18449682015-03-17 10:52:16 -0400262 struct vgamode_s *vmode_g = vgahw_find_mode(vbe_mode & ~MF_VBEFLAGS);
Kevin O'Connorf7f22632014-10-17 21:37:23 -0400263 SET_BDA_EXT(vgamode_offset, (u32)vmode_g);
Kevin O'Connor20dc4192014-02-05 20:52:25 -0500264 SET_IVT(0x1f, GET_FARVAR(seg, info->font0));
265 SET_IVT(0x43, GET_FARVAR(seg, info->font1));
266 }
267 return sizeof(*info);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400268}
269
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500270
271/****************************************************************
272 * Mode setting
273 ****************************************************************/
274
275struct vgamode_s *
276get_current_mode(void)
277{
Kevin O'Connorf7f22632014-10-17 21:37:23 -0400278 return (void*)(GET_BDA_EXT(vgamode_offset)+0);
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500279}
280
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500281// Setup BDA after a mode switch.
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500282int
283vga_set_mode(int mode, int flags)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400284{
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500285 dprintf(1, "set VGA mode %x\n", mode);
286 struct vgamode_s *vmode_g = vgahw_find_mode(mode);
287 if (!vmode_g)
288 return VBE_RETURN_STATUS_FAILED;
289
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -0400290 vgafb_set_swcursor(0);
291
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500292 int ret = vgahw_set_mode(vmode_g, flags);
293 if (ret)
294 return ret;
295
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400296 // Set the BIOS mem
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500297 int width = GET_GLOBAL(vmode_g->width);
298 int height = GET_GLOBAL(vmode_g->height);
Kevin O'Connor83047be2012-01-07 18:27:19 -0500299 u8 memmodel = GET_GLOBAL(vmode_g->memmodel);
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500300 int cheight = GET_GLOBAL(vmode_g->cheight);
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500301 if (mode < 0x100)
302 SET_BDA(video_mode, mode);
303 else
304 SET_BDA(video_mode, 0xff);
Kevin O'Connor9978d492014-10-17 21:17:48 -0400305 SET_BDA_EXT(vbe_mode, mode | (flags & MF_VBEFLAGS));
Kevin O'Connorf7f22632014-10-17 21:37:23 -0400306 SET_BDA_EXT(vgamode_offset, (u32)vmode_g);
Kevin O'Connor3b9b9f22015-04-09 12:24:18 -0400307 if (CONFIG_VGA_ALLOCATE_EXTRA_STACK)
Kevin O'Connor251e2632015-03-17 11:37:25 -0400308 // Disable extra stack if it appears a modern OS is in use.
309 // This works around bugs in some versions of Windows (Vista
310 // and possibly later) when the stack is in the e-segment.
311 MASK_BDA_EXT(flags, BF_EXTRA_STACK
312 , (flags & MF_LEGACY) ? BF_EXTRA_STACK : 0);
Kevin O'Connor83047be2012-01-07 18:27:19 -0500313 if (memmodel == MM_TEXT) {
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500314 SET_BDA(video_cols, width);
315 SET_BDA(video_rows, height-1);
316 SET_BDA(cursor_type, 0x0607);
317 } else {
318 int cwidth = GET_GLOBAL(vmode_g->cwidth);
319 SET_BDA(video_cols, width / cwidth);
320 SET_BDA(video_rows, (height / cheight) - 1);
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -0400321 SET_BDA(cursor_type, vga_emulate_text() ? 0x0607 : 0x0000);
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500322 }
Kevin O'Connor83047be2012-01-07 18:27:19 -0500323 SET_BDA(video_pagesize, calc_page_size(memmodel, width, height));
Kevin O'Connorefbf4d62014-02-09 11:50:21 -0500324 SET_BDA(crtc_address, CONFIG_VGA_STDVGA_PORTS ? stdvga_get_crtc() : 0);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400325 SET_BDA(char_height, cheight);
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500326 SET_BDA(video_ctl, 0x60 | (flags & MF_NOCLEARMEM ? 0x80 : 0x00));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400327 SET_BDA(video_switches, 0xF9);
328 SET_BDA(modeset_ctl, GET_BDA(modeset_ctl) & 0x7f);
Kevin O'Connorcecbc5d2011-12-31 17:24:11 -0500329 int i;
330 for (i=0; i<8; i++)
331 SET_BDA(cursor_pos[i], 0x0000);
332 SET_BDA(video_pagestart, 0x0000);
333 SET_BDA(video_page, 0x00);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400334
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400335 // Set the ints 0x1F and 0x43
Kevin O'Connor9f985422009-09-09 11:34:39 -0400336 SET_IVT(0x1f, SEGOFF(get_global_seg(), (u32)&vgafont8[128 * 8]));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400337
338 switch (cheight) {
339 case 8:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400340 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont8));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400341 break;
342 case 14:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400343 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont14));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400344 break;
345 case 16:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400346 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont16));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400347 break;
348 }
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500349
350 return 0;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400351}
352
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500353
354/****************************************************************
355 * VGA int 10 handler
356 ****************************************************************/
357
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400358static void
Julian Pidancet87879e22011-12-19 05:08:00 +0000359handle_1000(struct bregs *regs)
360{
Kevin O'Connor5108c692011-12-31 19:13:45 -0500361 int mode = regs->al & 0x7f;
Julian Pidancet87879e22011-12-19 05:08:00 +0000362
363 // Set regs->al
364 if (mode > 7)
365 regs->al = 0x20;
366 else if (mode == 6)
367 regs->al = 0x3f;
368 else
369 regs->al = 0x30;
370
Kevin O'Connorb7b92932013-03-09 13:04:47 -0500371 int flags = MF_LEGACY | (GET_BDA(modeset_ctl) & (MF_NOPALETTE|MF_GRAYSUM));
Kevin O'Connor5108c692011-12-31 19:13:45 -0500372 if (regs->al & 0x80)
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500373 flags |= MF_NOCLEARMEM;
374
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500375 vga_set_mode(mode, flags);
Julian Pidancet87879e22011-12-19 05:08:00 +0000376}
377
378static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400379handle_1001(struct bregs *regs)
380{
Kevin O'Connorc9aecfc2014-10-22 20:57:37 -0400381 set_cursor_shape(regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400382}
383
384static void
385handle_1002(struct bregs *regs)
386{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400387 struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
388 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400389}
390
391static void
392handle_1003(struct bregs *regs)
393{
Kevin O'Connorc9aecfc2014-10-22 20:57:37 -0400394 regs->cx = GET_BDA(cursor_type);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400395 struct cursorpos cp = get_cursor_pos(regs->bh);
396 regs->dl = cp.x;
397 regs->dh = cp.y;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400398}
399
400// Read light pen pos (unimplemented)
401static void
402handle_1004(struct bregs *regs)
403{
404 debug_stub(regs);
405 regs->ax = regs->bx = regs->cx = regs->dx = 0;
406}
407
408static void
409handle_1005(struct bregs *regs)
410{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400411 set_active_page(regs->al);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400412}
413
414static void
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400415verify_scroll(struct bregs *regs, int dir)
416{
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500417 u8 ulx = regs->cl, uly = regs->ch, lrx = regs->dl, lry = regs->dh;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400418 u16 nbrows = GET_BDA(video_rows) + 1;
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500419 if (lry >= nbrows)
420 lry = nbrows - 1;
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400421 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500422 if (lrx >= nbcols)
423 lrx = nbcols - 1;
Kevin O'Connor7fd2af62014-03-20 21:16:28 -0400424 int wincols = lrx - ulx + 1, winrows = lry - uly + 1;
425 if (wincols <= 0 || winrows <= 0)
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400426 return;
427
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500428 u8 page = GET_BDA(video_page);
Kevin O'Connor7fd2af62014-03-20 21:16:28 -0400429 int clearlines = regs->al, movelines = winrows - clearlines;
430 if (!clearlines || movelines <= 0) {
431 // Clear whole area.
432 struct cursorpos clr = {ulx, uly, page};
433 struct carattr attr = {' ', regs->bh, 1};
434 struct cursorpos clrsize = {wincols, winrows};
Kevin O'Connor7217ae72014-10-17 21:47:10 -0400435 vgafb_clear_chars(clr, attr, clrsize);
Kevin O'Connor7fd2af62014-03-20 21:16:28 -0400436 return;
437 }
438
439 if (dir > 0) {
440 // Normal scroll
441 struct cursorpos dest = {ulx, uly, page};
442 struct cursorpos src = {ulx, uly + clearlines, page};
443 struct cursorpos size = {wincols, movelines};
Kevin O'Connor7217ae72014-10-17 21:47:10 -0400444 vgafb_move_chars(dest, src, size);
Kevin O'Connor7fd2af62014-03-20 21:16:28 -0400445
446 struct cursorpos clr = {ulx, uly + movelines, page};
447 struct carattr attr = {' ', regs->bh, 1};
448 struct cursorpos clrsize = {wincols, clearlines};
Kevin O'Connor7217ae72014-10-17 21:47:10 -0400449 vgafb_clear_chars(clr, attr, clrsize);
Kevin O'Connor7fd2af62014-03-20 21:16:28 -0400450 } else {
451 // Scroll down
452 struct cursorpos dest = {ulx, uly + clearlines, page};
453 struct cursorpos src = {ulx, uly, page};
454 struct cursorpos size = {wincols, movelines};
Kevin O'Connor7217ae72014-10-17 21:47:10 -0400455 vgafb_move_chars(dest, src, size);
Kevin O'Connor7fd2af62014-03-20 21:16:28 -0400456
457 struct cursorpos clr = {ulx, uly, page};
458 struct carattr attr = {' ', regs->bh, 1};
459 struct cursorpos clrsize = {wincols, clearlines};
Kevin O'Connor7217ae72014-10-17 21:47:10 -0400460 vgafb_clear_chars(clr, attr, clrsize);
Kevin O'Connor7fd2af62014-03-20 21:16:28 -0400461 }
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400462}
463
464static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400465handle_1006(struct bregs *regs)
466{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400467 verify_scroll(regs, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400468}
469
470static void
471handle_1007(struct bregs *regs)
472{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400473 verify_scroll(regs, -1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400474}
475
476static void
477handle_1008(struct bregs *regs)
478{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400479 struct carattr ca = vgafb_read_char(get_cursor_pos(regs->bh));
Kevin O'Connor09262412009-05-25 11:44:11 -0400480 regs->al = ca.car;
481 regs->ah = ca.attr;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400482}
483
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400484static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400485handle_1009(struct bregs *regs)
486{
Kevin O'Connor09262412009-05-25 11:44:11 -0400487 struct carattr ca = {regs->al, regs->bl, 1};
Kevin O'Connor0910dde2014-02-08 16:43:30 -0500488 struct cursorpos cp = get_cursor_pos(regs->bh);
489 int count = regs->cx;
490 while (count--)
491 write_char(&cp, ca);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400492}
493
Kevin O'Connor0910dde2014-02-08 16:43:30 -0500494static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400495handle_100a(struct bregs *regs)
496{
Kevin O'Connor09262412009-05-25 11:44:11 -0400497 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connor0910dde2014-02-08 16:43:30 -0500498 struct cursorpos cp = get_cursor_pos(regs->bh);
499 int count = regs->cx;
500 while (count--)
501 write_char(&cp, ca);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400502}
503
504
505static void
506handle_100b00(struct bregs *regs)
507{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500508 stdvga_set_border_color(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400509}
510
511static void
512handle_100b01(struct bregs *regs)
513{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500514 stdvga_set_palette(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400515}
516
517static void
518handle_100bXX(struct bregs *regs)
519{
520 debug_stub(regs);
521}
522
523static void
524handle_100b(struct bregs *regs)
525{
Kevin O'Connorefbf4d62014-02-09 11:50:21 -0500526 if (!CONFIG_VGA_STDVGA_PORTS) {
527 handle_100bXX(regs);
528 return;
529 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400530 switch (regs->bh) {
531 case 0x00: handle_100b00(regs); break;
532 case 0x01: handle_100b01(regs); break;
533 default: handle_100bXX(regs); break;
534 }
535}
536
537
538static void
539handle_100c(struct bregs *regs)
540{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400541 // XXX - page (regs->bh) is unused
542 vgafb_write_pixel(regs->al, regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400543}
544
545static void
546handle_100d(struct bregs *regs)
547{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400548 // XXX - page (regs->bh) is unused
549 regs->al = vgafb_read_pixel(regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400550}
551
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400552static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400553handle_100e(struct bregs *regs)
554{
555 // Ralf Brown Interrupt list is WRONG on bh(page)
556 // We do output only on the current page !
Kevin O'Connor09262412009-05-25 11:44:11 -0400557 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connor116a0442009-05-26 00:47:32 -0400558 struct cursorpos cp = get_cursor_pos(0xff);
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400559 write_teletype(&cp, ca);
Kevin O'Connor116a0442009-05-26 00:47:32 -0400560 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400561}
562
563static void
564handle_100f(struct bregs *regs)
565{
Kevin O'Connore7132042009-05-25 00:10:35 -0400566 regs->bh = GET_BDA(video_page);
567 regs->al = GET_BDA(video_mode) | (GET_BDA(video_ctl) & 0x80);
568 regs->ah = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400569}
570
571
572static void
573handle_101000(struct bregs *regs)
574{
575 if (regs->bl > 0x14)
576 return;
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500577 stdvga_attr_write(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400578}
579
580static void
581handle_101001(struct bregs *regs)
582{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500583 stdvga_set_overscan_border_color(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400584}
585
586static void
587handle_101002(struct bregs *regs)
588{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500589 stdvga_set_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400590}
591
592static void
593handle_101003(struct bregs *regs)
594{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500595 stdvga_toggle_intensity(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400596}
597
598static void
599handle_101007(struct bregs *regs)
600{
601 if (regs->bl > 0x14)
602 return;
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500603 regs->bh = stdvga_attr_read(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400604}
605
606static void
607handle_101008(struct bregs *regs)
608{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500609 regs->bh = stdvga_get_overscan_border_color();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400610}
611
612static void
613handle_101009(struct bregs *regs)
614{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500615 stdvga_get_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400616}
617
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400618static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400619handle_101010(struct bregs *regs)
620{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400621 u8 rgb[3] = {regs->dh, regs->ch, regs->cl};
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500622 stdvga_dac_write(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400623}
624
625static void
626handle_101012(struct bregs *regs)
627{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500628 stdvga_dac_write(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400629}
630
631static void
632handle_101013(struct bregs *regs)
633{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500634 stdvga_select_video_dac_color_page(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400635}
636
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400637static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400638handle_101015(struct bregs *regs)
639{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400640 u8 rgb[3];
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500641 stdvga_dac_read(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400642 regs->dh = rgb[0];
643 regs->ch = rgb[1];
644 regs->cl = rgb[2];
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400645}
646
647static void
648handle_101017(struct bregs *regs)
649{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500650 stdvga_dac_read(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400651}
652
653static void
654handle_101018(struct bregs *regs)
655{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500656 stdvga_pelmask_write(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400657}
658
659static void
660handle_101019(struct bregs *regs)
661{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500662 regs->bl = stdvga_pelmask_read();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400663}
664
665static void
666handle_10101a(struct bregs *regs)
667{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500668 stdvga_read_video_dac_state(&regs->bl, &regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400669}
670
671static void
672handle_10101b(struct bregs *regs)
673{
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500674 stdvga_perform_gray_scale_summing(regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400675}
676
677static void
678handle_1010XX(struct bregs *regs)
679{
680 debug_stub(regs);
681}
682
683static void
684handle_1010(struct bregs *regs)
685{
Kevin O'Connorefbf4d62014-02-09 11:50:21 -0500686 if (!CONFIG_VGA_STDVGA_PORTS) {
687 handle_1010XX(regs);
688 return;
689 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400690 switch (regs->al) {
691 case 0x00: handle_101000(regs); break;
692 case 0x01: handle_101001(regs); break;
693 case 0x02: handle_101002(regs); break;
694 case 0x03: handle_101003(regs); break;
695 case 0x07: handle_101007(regs); break;
696 case 0x08: handle_101008(regs); break;
697 case 0x09: handle_101009(regs); break;
698 case 0x10: handle_101010(regs); break;
699 case 0x12: handle_101012(regs); break;
700 case 0x13: handle_101013(regs); break;
701 case 0x15: handle_101015(regs); break;
702 case 0x17: handle_101017(regs); break;
703 case 0x18: handle_101018(regs); break;
704 case 0x19: handle_101019(regs); break;
705 case 0x1a: handle_10101a(regs); break;
706 case 0x1b: handle_10101b(regs); break;
707 default: handle_1010XX(regs); break;
708 }
709}
710
711
712static void
713handle_101100(struct bregs *regs)
714{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500715 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
716 , regs->dx, regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400717}
718
719static void
720handle_101101(struct bregs *regs)
721{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500722 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400723}
724
725static void
726handle_101102(struct bregs *regs)
727{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500728 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400729}
730
731static void
732handle_101103(struct bregs *regs)
733{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500734 stdvga_set_text_block_specifier(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400735}
736
737static void
738handle_101104(struct bregs *regs)
739{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500740 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400741}
742
743static void
744handle_101110(struct bregs *regs)
745{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500746 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
747 , regs->dx, regs->bl, regs->bh);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400748 set_scan_lines(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400749}
750
751static void
752handle_101111(struct bregs *regs)
753{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500754 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400755 set_scan_lines(14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400756}
757
758static void
759handle_101112(struct bregs *regs)
760{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500761 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400762 set_scan_lines(8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400763}
764
765static void
766handle_101114(struct bregs *regs)
767{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500768 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400769 set_scan_lines(16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400770}
771
772static void
Paolo Bonzini4bd8aeb2013-01-10 13:41:36 +0100773handle_101120(struct bregs *regs)
774{
775 SET_IVT(0x1f, SEGOFF(regs->es, regs->bp));
776}
777
778void
779load_gfx_font(u16 seg, u16 off, u8 height, u8 bl, u8 dl)
780{
781 u8 rows;
782
783 SET_IVT(0x43, SEGOFF(seg, off));
784 switch(bl) {
785 case 0:
786 rows = dl;
787 break;
788 case 1:
789 rows = 14;
790 break;
791 case 3:
792 rows = 43;
793 break;
794 case 2:
795 default:
796 rows = 25;
797 break;
798 }
799 SET_BDA(video_rows, rows - 1);
800 SET_BDA(char_height, height);
801}
802
803static void
804handle_101121(struct bregs *regs)
805{
806 load_gfx_font(regs->es, regs->bp, regs->cx, regs->bl, regs->dl);
807}
808
809static void
810handle_101122(struct bregs *regs)
811{
812 load_gfx_font(get_global_seg(), (u32)vgafont14, 14, regs->bl, regs->dl);
813}
814
815static void
816handle_101123(struct bregs *regs)
817{
818 load_gfx_font(get_global_seg(), (u32)vgafont8, 8, regs->bl, regs->dl);
819}
820
821static void
822handle_101124(struct bregs *regs)
823{
824 load_gfx_font(get_global_seg(), (u32)vgafont16, 16, regs->bl, regs->dl);
825}
826
827static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400828handle_101130(struct bregs *regs)
829{
Kevin O'Connore7132042009-05-25 00:10:35 -0400830 switch (regs->bh) {
831 case 0x00: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500832 struct segoff_s so = GET_IVT(0x1f);
833 regs->es = so.seg;
834 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400835 break;
836 }
837 case 0x01: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500838 struct segoff_s so = GET_IVT(0x43);
839 regs->es = so.seg;
840 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400841 break;
842 }
843 case 0x02:
844 regs->es = get_global_seg();
845 regs->bp = (u32)vgafont14;
846 break;
847 case 0x03:
848 regs->es = get_global_seg();
849 regs->bp = (u32)vgafont8;
850 break;
851 case 0x04:
852 regs->es = get_global_seg();
853 regs->bp = (u32)vgafont8 + 128 * 8;
854 break;
855 case 0x05:
856 regs->es = get_global_seg();
857 regs->bp = (u32)vgafont14alt;
858 break;
859 case 0x06:
860 regs->es = get_global_seg();
861 regs->bp = (u32)vgafont16;
862 break;
863 case 0x07:
864 regs->es = get_global_seg();
865 regs->bp = (u32)vgafont16alt;
866 break;
867 default:
868 dprintf(1, "Get font info BH(%02x) was discarded\n", regs->bh);
869 return;
870 }
871 // Set byte/char of on screen font
872 regs->cx = GET_BDA(char_height) & 0xff;
873
874 // Set Highest char row
Kevin O'Connor4c85a262012-02-12 11:50:52 -0500875 regs->dl = GET_BDA(video_rows);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400876}
877
878static void
879handle_1011XX(struct bregs *regs)
880{
881 debug_stub(regs);
882}
883
884static void
885handle_1011(struct bregs *regs)
886{
Kevin O'Connorefbf4d62014-02-09 11:50:21 -0500887 if (CONFIG_VGA_STDVGA_PORTS) {
888 switch (regs->al) {
Kevin O'Connorc58799c2014-05-28 09:09:05 -0400889 case 0x00: handle_101100(regs); return;
890 case 0x01: handle_101101(regs); return;
891 case 0x02: handle_101102(regs); return;
892 case 0x03: handle_101103(regs); return;
893 case 0x04: handle_101104(regs); return;
894 case 0x10: handle_101110(regs); return;
895 case 0x11: handle_101111(regs); return;
896 case 0x12: handle_101112(regs); return;
897 case 0x14: handle_101114(regs); return;
Kevin O'Connorefbf4d62014-02-09 11:50:21 -0500898 }
899 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400900 switch (regs->al) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400901 case 0x30: handle_101130(regs); break;
Paolo Bonzini4bd8aeb2013-01-10 13:41:36 +0100902 case 0x20: handle_101120(regs); break;
903 case 0x21: handle_101121(regs); break;
904 case 0x22: handle_101122(regs); break;
905 case 0x23: handle_101123(regs); break;
906 case 0x24: handle_101124(regs); break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400907 default: handle_1011XX(regs); break;
908 }
909}
910
911
912static void
913handle_101210(struct bregs *regs)
914{
Kevin O'Connore7132042009-05-25 00:10:35 -0400915 u16 crtc_addr = GET_BDA(crtc_address);
916 if (crtc_addr == VGAREG_MDA_CRTC_ADDRESS)
917 regs->bx = 0x0103;
918 else
919 regs->bx = 0x0003;
920 regs->cx = GET_BDA(video_switches) & 0x0f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400921}
922
923static void
924handle_101230(struct bregs *regs)
925{
Kevin O'Connore7132042009-05-25 00:10:35 -0400926 u8 mctl = GET_BDA(modeset_ctl);
927 u8 vswt = GET_BDA(video_switches);
928 switch (regs->al) {
929 case 0x00:
930 // 200 lines
931 mctl = (mctl & ~0x10) | 0x80;
932 vswt = (vswt & ~0x0f) | 0x08;
933 break;
934 case 0x01:
935 // 350 lines
936 mctl &= ~0x90;
937 vswt = (vswt & ~0x0f) | 0x09;
938 break;
939 case 0x02:
940 // 400 lines
941 mctl = (mctl & ~0x80) | 0x10;
942 vswt = (vswt & ~0x0f) | 0x09;
943 break;
944 default:
945 dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
946 break;
947 }
948 SET_BDA(modeset_ctl, mctl);
949 SET_BDA(video_switches, vswt);
950 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400951}
952
953static void
954handle_101231(struct bregs *regs)
955{
Kevin O'Connore7132042009-05-25 00:10:35 -0400956 u8 v = (regs->al & 0x01) << 3;
957 u8 mctl = GET_BDA(video_ctl) & ~0x08;
958 SET_BDA(video_ctl, mctl | v);
959 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400960}
961
962static void
963handle_101232(struct bregs *regs)
964{
Kevin O'Connorefbf4d62014-02-09 11:50:21 -0500965 if (CONFIG_VGA_STDVGA_PORTS) {
966 stdvga_enable_video_addressing(regs->al);
967 regs->al = 0x12;
968 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400969}
970
971static void
972handle_101233(struct bregs *regs)
973{
Kevin O'Connore7132042009-05-25 00:10:35 -0400974 u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
975 u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
976 SET_BDA(modeset_ctl, v | v2);
977 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400978}
979
980static void
981handle_101234(struct bregs *regs)
982{
Kevin O'Connor479fd722014-10-22 20:27:28 -0400983 SET_BDA(video_ctl, (GET_BDA(video_ctl) & ~0x01) | (regs->al & 0x01));
Kevin O'Connore7132042009-05-25 00:10:35 -0400984 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400985}
986
987static void
988handle_101235(struct bregs *regs)
989{
990 debug_stub(regs);
991 regs->al = 0x12;
992}
993
994static void
995handle_101236(struct bregs *regs)
996{
997 debug_stub(regs);
998 regs->al = 0x12;
999}
1000
1001static void
1002handle_1012XX(struct bregs *regs)
1003{
1004 debug_stub(regs);
1005}
1006
1007static void
1008handle_1012(struct bregs *regs)
1009{
Kevin O'Connore91ec7c2012-01-14 16:30:49 -05001010 if (CONFIG_VGA_CIRRUS && regs->bl >= 0x80) {
1011 clext_1012(regs);
1012 return;
1013 }
1014
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001015 switch (regs->bl) {
1016 case 0x10: handle_101210(regs); break;
1017 case 0x30: handle_101230(regs); break;
1018 case 0x31: handle_101231(regs); break;
1019 case 0x32: handle_101232(regs); break;
1020 case 0x33: handle_101233(regs); break;
1021 case 0x34: handle_101234(regs); break;
1022 case 0x35: handle_101235(regs); break;
1023 case 0x36: handle_101236(regs); break;
1024 default: handle_1012XX(regs); break;
1025 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001026}
1027
1028
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001029// Write string
Kevin O'Connor0ad77f02009-05-31 20:46:43 -04001030static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001031handle_1013(struct bregs *regs)
1032{
Kevin O'Connor6ee837b2012-02-13 20:09:02 -05001033 struct cursorpos cp;
1034 if (regs->dh == 0xff)
1035 // if row=0xff special case : use current cursor position
1036 cp = get_cursor_pos(regs->bh);
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001037 else
Kevin O'Connor6ee837b2012-02-13 20:09:02 -05001038 cp = (struct cursorpos) {regs->dl, regs->dh, regs->bh};
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001039
Kevin O'Connor6ee837b2012-02-13 20:09:02 -05001040 u16 count = regs->cx;
1041 u8 *offset_far = (void*)(regs->bp + 0);
1042 u8 attr = regs->bl;
1043 while (count--) {
1044 u8 car = GET_FARVAR(regs->es, *offset_far);
1045 offset_far++;
1046 if (regs->al & 2) {
1047 attr = GET_FARVAR(regs->es, *offset_far);
1048 offset_far++;
1049 }
1050
1051 struct carattr ca = {car, attr, 1};
1052 write_teletype(&cp, ca);
1053 }
1054
1055 if (regs->al & 1)
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001056 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001057}
1058
1059
1060static void
1061handle_101a00(struct bregs *regs)
1062{
Kevin O'Connore7132042009-05-25 00:10:35 -04001063 regs->bx = GET_BDA(dcc_index);
1064 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001065}
1066
1067static void
1068handle_101a01(struct bregs *regs)
1069{
Kevin O'Connore7132042009-05-25 00:10:35 -04001070 SET_BDA(dcc_index, regs->bl);
1071 dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
1072 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001073}
1074
1075static void
1076handle_101aXX(struct bregs *regs)
1077{
1078 debug_stub(regs);
1079}
1080
1081static void
1082handle_101a(struct bregs *regs)
1083{
1084 switch (regs->al) {
1085 case 0x00: handle_101a00(regs); break;
1086 case 0x01: handle_101a01(regs); break;
1087 default: handle_101aXX(regs); break;
1088 }
1089}
1090
1091
Kevin O'Connor12900b12014-10-23 16:37:08 -04001092struct video_func_static static_functionality VAR16 = {
1093 .modes = 0x00, // Filled in by stdvga_build_video_param()
Kevin O'Connor63977902014-10-23 16:24:36 -04001094 .scanlines = 0x07, // 200, 350, 400 scan lines
1095 .cblocks = 0x02, // mamimum number of visible charsets in text mode
1096 .active_cblocks = 0x08, // total number of charset blocks in text mode
1097 .misc_flags = 0x0ce7,
Kevin O'Connorefb45232012-01-14 22:55:11 -05001098};
1099
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001100static void
1101handle_101b(struct bregs *regs)
1102{
Kevin O'Connorca668642009-05-21 23:06:08 -04001103 u16 seg = regs->es;
Kevin O'Connor63977902014-10-23 16:24:36 -04001104 struct video_func_info *info = (void*)(regs->di+0);
Kevin O'Connorca668642009-05-21 23:06:08 -04001105 memset_far(seg, info, 0, sizeof(*info));
1106 // Address of static functionality table
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001107 SET_FARVAR(seg, info->static_functionality
Kevin O'Connor63977902014-10-23 16:24:36 -04001108 , SEGOFF(get_global_seg(), (u32)&static_functionality));
Kevin O'Connorca668642009-05-21 23:06:08 -04001109
1110 // Hard coded copy from BIOS area. Should it be cleaner ?
Kevin O'Connor9f985422009-09-09 11:34:39 -04001111 memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49
1112 , sizeof(info->bda_0x49));
1113 memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84
1114 , sizeof(info->bda_0x84));
Kevin O'Connorca668642009-05-21 23:06:08 -04001115
1116 SET_FARVAR(seg, info->dcc_index, GET_BDA(dcc_index));
1117 SET_FARVAR(seg, info->colors, 16);
1118 SET_FARVAR(seg, info->pages, 8);
1119 SET_FARVAR(seg, info->scan_lines, 2);
1120 SET_FARVAR(seg, info->video_mem, 3);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001121 regs->al = 0x1B;
1122}
1123
1124
1125static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001126handle_101c(struct bregs *regs)
1127{
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001128 u16 seg = regs->es;
1129 void *data = (void*)(regs->bx+0);
1130 u16 states = regs->cx;
Kevin O'Connor20dc4192014-02-05 20:52:25 -05001131 u8 cmd = regs->al;
1132 if (states & ~0x07 || cmd > 2)
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001133 goto fail;
Kevin O'Connor20dc4192014-02-05 20:52:25 -05001134 int ret = vgahw_save_restore(states | (cmd<<8), seg, data);
1135 if (ret < 0)
1136 goto fail;
1137 if (cmd == 0)
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001138 regs->bx = ret / 64;
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001139 regs->al = 0x1c;
1140fail:
1141 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001142}
1143
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001144static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001145handle_10XX(struct bregs *regs)
1146{
1147 debug_stub(regs);
1148}
1149
1150// INT 10h Video Support Service Entry Point
1151void VISIBLE16
1152handle_10(struct bregs *regs)
1153{
1154 debug_enter(regs, DEBUG_VGA_10);
1155 switch (regs->ah) {
1156 case 0x00: handle_1000(regs); break;
1157 case 0x01: handle_1001(regs); break;
1158 case 0x02: handle_1002(regs); break;
1159 case 0x03: handle_1003(regs); break;
1160 case 0x04: handle_1004(regs); break;
1161 case 0x05: handle_1005(regs); break;
1162 case 0x06: handle_1006(regs); break;
1163 case 0x07: handle_1007(regs); break;
1164 case 0x08: handle_1008(regs); break;
1165 case 0x09: handle_1009(regs); break;
1166 case 0x0a: handle_100a(regs); break;
1167 case 0x0b: handle_100b(regs); break;
1168 case 0x0c: handle_100c(regs); break;
1169 case 0x0d: handle_100d(regs); break;
1170 case 0x0e: handle_100e(regs); break;
1171 case 0x0f: handle_100f(regs); break;
1172 case 0x10: handle_1010(regs); break;
1173 case 0x11: handle_1011(regs); break;
1174 case 0x12: handle_1012(regs); break;
1175 case 0x13: handle_1013(regs); break;
1176 case 0x1a: handle_101a(regs); break;
1177 case 0x1b: handle_101b(regs); break;
1178 case 0x1c: handle_101c(regs); break;
1179 case 0x4f: handle_104f(regs); break;
1180 default: handle_10XX(regs); break;
1181 }
1182}