blob: f70b2b6130510c62cbe49d6bda69e7aff191b934 [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'Connor1f2c3072009-05-06 23:35:59 -040053static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040054set_cursor_shape(u8 start, u8 end)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040055{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040056 start &= 0x3f;
57 end &= 0x1f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040058
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040059 u16 curs = (start << 8) + end;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040060 SET_BDA(cursor_type, curs);
61
Kevin O'Connordd2be772009-05-16 15:41:23 -040062 u8 modeset_ctl = GET_BDA(modeset_ctl);
63 u16 cheight = GET_BDA(char_height);
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040064 if ((modeset_ctl & 0x01) && (cheight > 8) && (end < 8) && (start < 0x20)) {
65 if (end != (start + 1))
66 start = ((start + 1) * cheight / 8) - 1;
Kevin O'Connordd2be772009-05-16 15:41:23 -040067 else
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040068 start = ((end + 1) * cheight / 8) - 2;
69 end = ((end + 1) * cheight / 8) - 1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040070 }
Kevin O'Connor88ca7412011-12-31 04:24:20 -050071 stdvga_set_cursor_shape(start, end);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040072}
73
Kevin O'Connor0818e1a2009-05-16 18:00:19 -040074static u16
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040075get_cursor_shape(u8 page)
Kevin O'Connor0818e1a2009-05-16 18:00:19 -040076{
77 if (page > 7)
78 return 0;
79 // FIXME should handle VGA 14/16 lines
80 return GET_BDA(cursor_type);
81}
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
92 // Bios cursor pos
Kevin O'Connor6ee837b2012-02-13 20:09:02 -050093 SET_BDA(cursor_pos[page], (y << 8) | x);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040094
95 // Set the hardware cursor
Kevin O'Connordd2be772009-05-16 15:41:23 -040096 u8 current = GET_BDA(video_page);
Kevin O'Connor918b1562009-05-25 11:05:18 -040097 if (cp.page != current)
Kevin O'Connordd2be772009-05-16 15:41:23 -040098 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040099
Kevin O'Connor83047be2012-01-07 18:27:19 -0500100 // Calculate the memory address
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500101 int address = (GET_BDA(video_pagesize) * page
102 + (x + y * GET_BDA(video_cols)) * 2);
Kevin O'Connor16920072012-01-27 22:59:46 -0500103 stdvga_set_cursor_pos(address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400104}
105
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400106static struct cursorpos
Kevin O'Connor918b1562009-05-25 11:05:18 -0400107get_cursor_pos(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400108{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400109 if (page == 0xff)
110 // special case - use current page
111 page = GET_BDA(video_page);
112 if (page > 7) {
113 struct cursorpos cp = { 0, 0, 0xfe };
114 return cp;
115 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400116 // FIXME should handle VGA 14/16 lines
Kevin O'Connor918b1562009-05-25 11:05:18 -0400117 u16 xy = GET_BDA(cursor_pos[page]);
118 struct cursorpos cp = {xy, xy>>8, page};
119 return cp;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400120}
121
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400122static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400123set_active_page(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400124{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400125 if (page > 7)
126 return;
127
128 // Get the mode
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500129 struct vgamode_s *vmode_g = get_current_mode();
Kevin O'Connor5727c292009-05-16 17:29:32 -0400130 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400131 return;
132
Kevin O'Connor4c85a262012-02-12 11:50:52 -0500133 // Get cursor pos for the given page
Kevin O'Connor918b1562009-05-25 11:05:18 -0400134 struct cursorpos cp = get_cursor_pos(page);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400135
Kevin O'Connor83047be2012-01-07 18:27:19 -0500136 // Calculate memory address of start of page
Kevin O'Connord61fc532012-01-27 20:37:45 -0500137 int address = GET_BDA(video_pagesize) * page;
138 vgahw_set_displaystart(vmode_g, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400139
140 // And change the BIOS page
Kevin O'Connor83047be2012-01-07 18:27:19 -0500141 SET_BDA(video_pagestart, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400142 SET_BDA(video_page, page);
143
Kevin O'Connora12c2152009-05-13 22:06:16 -0400144 dprintf(1, "Set active page %02x address %04x\n", page, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400145
146 // Display the cursor, now the page is active
Kevin O'Connor918b1562009-05-25 11:05:18 -0400147 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400148}
149
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400150static void
151set_scan_lines(u8 lines)
152{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500153 stdvga_set_scan_lines(lines);
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400154 if (lines == 8)
155 set_cursor_shape(0x06, 0x07);
156 else
157 set_cursor_shape(lines - 4, lines - 3);
158 SET_BDA(char_height, lines);
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500159 u16 vde = stdvga_get_vde();
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400160 u8 rows = vde / lines;
161 SET_BDA(video_rows, rows - 1);
162 u16 cols = GET_BDA(video_cols);
Kevin O'Connor83047be2012-01-07 18:27:19 -0500163 SET_BDA(video_pagesize, calc_page_size(MM_TEXT, cols, rows));
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400164}
165
166
167/****************************************************************
168 * Character writing
169 ****************************************************************/
170
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400171// Scroll the screen one line. This function is designed to be called
172// tail-recursive to reduce stack usage.
173static void noinline
174scroll_one(u16 nbrows, u16 nbcols, u8 page)
Kevin O'Connor09262412009-05-25 11:44:11 -0400175{
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400176 struct cursorpos ul = {0, 0, page};
177 struct cursorpos lr = {nbcols-1, nbrows-1, page};
178 vgafb_scroll(1, -1, ul, lr);
179}
180
181// Write a character to the screen at a given position. Implement
182// special characters and scroll the screen if necessary.
183static void
184write_teletype(struct cursorpos *pcp, struct carattr ca)
185{
186 struct cursorpos cp = *pcp;
187
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400188 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -0400189 u16 nbrows = GET_BDA(video_rows) + 1;
190 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400191
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400192 switch (ca.car) {
193 case 7:
194 //FIXME should beep
195 break;
196 case 8:
197 if (cp.x > 0)
198 cp.x--;
199 break;
200 case '\r':
201 cp.x = 0;
202 break;
203 case '\n':
204 cp.y++;
205 break;
206 case '\t':
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500207 ca.car = ' ';
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400208 do {
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500209 vgafb_write_char(cp, ca);
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400210 cp.x++;
211 } while (cp.x < nbcols && cp.x % 8);
212 break;
213 default:
214 vgafb_write_char(cp, ca);
215 cp.x++;
216 }
217
Kevin O'Connor82221b22009-05-26 00:20:40 -0400218 // Do we need to wrap ?
219 if (cp.x == nbcols) {
220 cp.x = 0;
221 cp.y++;
222 }
223 // Do we need to scroll ?
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400224 if (cp.y < nbrows) {
225 *pcp = cp;
226 return;
Kevin O'Connor82221b22009-05-26 00:20:40 -0400227 }
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400228 // Scroll screen
229 cp.y--;
230 *pcp = cp;
231 scroll_one(nbrows, nbcols, cp.page);
Kevin O'Connor82221b22009-05-26 00:20:40 -0400232}
233
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400234
235/****************************************************************
236 * Save and restore bda state
237 ****************************************************************/
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400238
Kevin O'Connor9f857fc2012-02-04 11:59:02 -0500239void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400240save_bda_state(u16 seg, struct saveBDAstate *info)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400241{
Kevin O'Connor2469f892012-02-04 12:40:02 -0500242 SET_FARVAR(seg, info->video_mode, GET_BDA(vbe_mode));
Kevin O'Connorca668642009-05-21 23:06:08 -0400243 SET_FARVAR(seg, info->video_cols, GET_BDA(video_cols));
244 SET_FARVAR(seg, info->video_pagesize, GET_BDA(video_pagesize));
245 SET_FARVAR(seg, info->crtc_address, GET_BDA(crtc_address));
246 SET_FARVAR(seg, info->video_rows, GET_BDA(video_rows));
247 SET_FARVAR(seg, info->char_height, GET_BDA(char_height));
248 SET_FARVAR(seg, info->video_ctl, GET_BDA(video_ctl));
249 SET_FARVAR(seg, info->video_switches, GET_BDA(video_switches));
250 SET_FARVAR(seg, info->modeset_ctl, GET_BDA(modeset_ctl));
251 SET_FARVAR(seg, info->cursor_type, GET_BDA(cursor_type));
Kevin O'Connor9f857fc2012-02-04 11:59:02 -0500252 int i;
Kevin O'Connorca668642009-05-21 23:06:08 -0400253 for (i=0; i<8; i++)
254 SET_FARVAR(seg, info->cursor_pos[i], GET_BDA(cursor_pos[i]));
255 SET_FARVAR(seg, info->video_pagestart, GET_BDA(video_pagestart));
256 SET_FARVAR(seg, info->video_page, GET_BDA(video_page));
257 /* current font */
Kevin O'Connor9f985422009-09-09 11:34:39 -0400258 SET_FARVAR(seg, info->font0, GET_IVT(0x1f));
259 SET_FARVAR(seg, info->font1, GET_IVT(0x43));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400260}
261
Kevin O'Connor9f857fc2012-02-04 11:59:02 -0500262void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400263restore_bda_state(u16 seg, struct saveBDAstate *info)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400264{
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500265 u16 mode = GET_FARVAR(seg, info->video_mode);
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500266 SET_BDA(vbe_mode, mode);
Kevin O'Connor2469f892012-02-04 12:40:02 -0500267 if (mode < 0x100)
268 SET_BDA(video_mode, mode);
269 else
270 SET_BDA(video_mode, 0xff);
Kevin O'Connorca668642009-05-21 23:06:08 -0400271 SET_BDA(video_cols, GET_FARVAR(seg, info->video_cols));
272 SET_BDA(video_pagesize, GET_FARVAR(seg, info->video_pagesize));
273 SET_BDA(crtc_address, GET_FARVAR(seg, info->crtc_address));
274 SET_BDA(video_rows, GET_FARVAR(seg, info->video_rows));
275 SET_BDA(char_height, GET_FARVAR(seg, info->char_height));
276 SET_BDA(video_ctl, GET_FARVAR(seg, info->video_ctl));
277 SET_BDA(video_switches, GET_FARVAR(seg, info->video_switches));
278 SET_BDA(modeset_ctl, GET_FARVAR(seg, info->modeset_ctl));
279 SET_BDA(cursor_type, GET_FARVAR(seg, info->cursor_type));
Kevin O'Connor9f857fc2012-02-04 11:59:02 -0500280 int i;
Kevin O'Connorca668642009-05-21 23:06:08 -0400281 for (i = 0; i < 8; i++)
282 SET_BDA(cursor_pos[i], GET_FARVAR(seg, info->cursor_pos[i]));
283 SET_BDA(video_pagestart, GET_FARVAR(seg, info->video_pagestart));
284 SET_BDA(video_page, GET_FARVAR(seg, info->video_page));
285 /* current font */
Kevin O'Connor9f985422009-09-09 11:34:39 -0400286 SET_IVT(0x1f, GET_FARVAR(seg, info->font0));
287 SET_IVT(0x43, GET_FARVAR(seg, info->font1));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400288}
289
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500290
291/****************************************************************
292 * Mode setting
293 ****************************************************************/
294
295struct vgamode_s *
296get_current_mode(void)
297{
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500298 return vgahw_find_mode(GET_BDA(vbe_mode) & ~MF_VBEFLAGS);
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500299}
300
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500301// Setup BDA after a mode switch.
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500302int
303vga_set_mode(int mode, int flags)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400304{
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500305 dprintf(1, "set VGA mode %x\n", mode);
306 struct vgamode_s *vmode_g = vgahw_find_mode(mode);
307 if (!vmode_g)
308 return VBE_RETURN_STATUS_FAILED;
309
310 int ret = vgahw_set_mode(vmode_g, flags);
311 if (ret)
312 return ret;
313
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400314 // Set the BIOS mem
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500315 int width = GET_GLOBAL(vmode_g->width);
316 int height = GET_GLOBAL(vmode_g->height);
Kevin O'Connor83047be2012-01-07 18:27:19 -0500317 u8 memmodel = GET_GLOBAL(vmode_g->memmodel);
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500318 int cheight = GET_GLOBAL(vmode_g->cheight);
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500319 if (mode < 0x100)
320 SET_BDA(video_mode, mode);
321 else
322 SET_BDA(video_mode, 0xff);
323 SET_BDA(vbe_mode, mode | (flags & MF_VBEFLAGS));
Kevin O'Connor83047be2012-01-07 18:27:19 -0500324 if (memmodel == MM_TEXT) {
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500325 SET_BDA(video_cols, width);
326 SET_BDA(video_rows, height-1);
327 SET_BDA(cursor_type, 0x0607);
328 } else {
329 int cwidth = GET_GLOBAL(vmode_g->cwidth);
330 SET_BDA(video_cols, width / cwidth);
331 SET_BDA(video_rows, (height / cheight) - 1);
332 SET_BDA(cursor_type, 0x0000);
333 }
Kevin O'Connor83047be2012-01-07 18:27:19 -0500334 SET_BDA(video_pagesize, calc_page_size(memmodel, width, height));
Kevin O'Connorcecbc5d2011-12-31 17:24:11 -0500335 SET_BDA(crtc_address, stdvga_get_crtc());
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400336 SET_BDA(char_height, cheight);
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500337 SET_BDA(video_ctl, 0x60 | (flags & MF_NOCLEARMEM ? 0x80 : 0x00));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400338 SET_BDA(video_switches, 0xF9);
339 SET_BDA(modeset_ctl, GET_BDA(modeset_ctl) & 0x7f);
Kevin O'Connorcecbc5d2011-12-31 17:24:11 -0500340 int i;
341 for (i=0; i<8; i++)
342 SET_BDA(cursor_pos[i], 0x0000);
343 SET_BDA(video_pagestart, 0x0000);
344 SET_BDA(video_page, 0x00);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400345
346 // FIXME We nearly have the good tables. to be reworked
347 SET_BDA(dcc_index, 0x08); // 8 is VGA should be ok for now
Kevin O'Connor9f985422009-09-09 11:34:39 -0400348 SET_BDA(video_savetable
Kevin O'Connor815e4472011-12-21 09:05:32 -0500349 , SEGOFF(get_global_seg(), (u32)&video_save_pointer_table));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400350
351 // FIXME
352 SET_BDA(video_msr, 0x00); // Unavailable on vanilla vga, but...
353 SET_BDA(video_pal, 0x00); // Unavailable on vanilla vga, but...
354
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400355 // Set the ints 0x1F and 0x43
Kevin O'Connor9f985422009-09-09 11:34:39 -0400356 SET_IVT(0x1f, SEGOFF(get_global_seg(), (u32)&vgafont8[128 * 8]));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400357
358 switch (cheight) {
359 case 8:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400360 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont8));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400361 break;
362 case 14:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400363 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont14));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400364 break;
365 case 16:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400366 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont16));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400367 break;
368 }
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500369
370 return 0;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400371}
372
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500373
374/****************************************************************
375 * VGA int 10 handler
376 ****************************************************************/
377
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400378static void
Julian Pidancet87879e22011-12-19 05:08:00 +0000379handle_1000(struct bregs *regs)
380{
Kevin O'Connor5108c692011-12-31 19:13:45 -0500381 int mode = regs->al & 0x7f;
Julian Pidancet87879e22011-12-19 05:08:00 +0000382
383 // Set regs->al
384 if (mode > 7)
385 regs->al = 0x20;
386 else if (mode == 6)
387 regs->al = 0x3f;
388 else
389 regs->al = 0x30;
390
Kevin O'Connorb7b92932013-03-09 13:04:47 -0500391 int flags = MF_LEGACY | (GET_BDA(modeset_ctl) & (MF_NOPALETTE|MF_GRAYSUM));
Kevin O'Connor5108c692011-12-31 19:13:45 -0500392 if (regs->al & 0x80)
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500393 flags |= MF_NOCLEARMEM;
394
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500395 vga_set_mode(mode, flags);
Julian Pidancet87879e22011-12-19 05:08:00 +0000396}
397
398static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400399handle_1001(struct bregs *regs)
400{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400401 set_cursor_shape(regs->ch, regs->cl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400402}
403
404static void
405handle_1002(struct bregs *regs)
406{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400407 struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
408 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400409}
410
411static void
412handle_1003(struct bregs *regs)
413{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400414 regs->cx = get_cursor_shape(regs->bh);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400415 struct cursorpos cp = get_cursor_pos(regs->bh);
416 regs->dl = cp.x;
417 regs->dh = cp.y;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400418}
419
420// Read light pen pos (unimplemented)
421static void
422handle_1004(struct bregs *regs)
423{
424 debug_stub(regs);
425 regs->ax = regs->bx = regs->cx = regs->dx = 0;
426}
427
428static void
429handle_1005(struct bregs *regs)
430{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400431 set_active_page(regs->al);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400432}
433
434static void
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400435verify_scroll(struct bregs *regs, int dir)
436{
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500437 u8 ulx = regs->cl, uly = regs->ch, lrx = regs->dl, lry = regs->dh;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400438 u16 nbrows = GET_BDA(video_rows) + 1;
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500439 if (lry >= nbrows)
440 lry = nbrows - 1;
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400441 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500442 if (lrx >= nbcols)
443 lrx = nbcols - 1;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400444
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500445 if (ulx > lrx || uly > lry)
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400446 return;
447
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500448 int nblines = regs->al;
449 if (!nblines || nblines > lry - uly + 1)
450 nblines = lry - uly + 1;
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400451
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500452 u8 page = GET_BDA(video_page);
453 struct cursorpos ul = {ulx, uly, page};
454 struct cursorpos lr = {lrx, lry, page};
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400455 vgafb_scroll(dir * nblines, regs->bh, ul, lr);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400456}
457
458static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400459handle_1006(struct bregs *regs)
460{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400461 verify_scroll(regs, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400462}
463
464static void
465handle_1007(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_1008(struct bregs *regs)
472{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400473 struct carattr ca = vgafb_read_char(get_cursor_pos(regs->bh));
Kevin O'Connor09262412009-05-25 11:44:11 -0400474 regs->al = ca.car;
475 regs->ah = ca.attr;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400476}
477
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400478static void noinline
Kevin O'Connord3b38152009-05-26 00:05:37 -0400479write_chars(u8 page, struct carattr ca, u16 count)
480{
Kevin O'Connor7b975e52012-03-05 17:11:50 -0500481 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connord3b38152009-05-26 00:05:37 -0400482 struct cursorpos cp = get_cursor_pos(page);
483 while (count--) {
484 vgafb_write_char(cp, ca);
485 cp.x++;
Kevin O'Connor7b975e52012-03-05 17:11:50 -0500486 if (cp.x >= nbcols) {
487 cp.x -= nbcols;
488 cp.y++;
489 }
Kevin O'Connord3b38152009-05-26 00:05:37 -0400490 }
491}
492
493static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400494handle_1009(struct bregs *regs)
495{
Kevin O'Connor09262412009-05-25 11:44:11 -0400496 struct carattr ca = {regs->al, regs->bl, 1};
Kevin O'Connord3b38152009-05-26 00:05:37 -0400497 write_chars(regs->bh, ca, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400498}
499
500static void
501handle_100a(struct bregs *regs)
502{
Kevin O'Connor09262412009-05-25 11:44:11 -0400503 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connord3b38152009-05-26 00:05:37 -0400504 write_chars(regs->bh, ca, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400505}
506
507
508static void
509handle_100b00(struct bregs *regs)
510{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500511 stdvga_set_border_color(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400512}
513
514static void
515handle_100b01(struct bregs *regs)
516{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500517 stdvga_set_palette(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400518}
519
520static void
521handle_100bXX(struct bregs *regs)
522{
523 debug_stub(regs);
524}
525
526static void
527handle_100b(struct bregs *regs)
528{
529 switch (regs->bh) {
530 case 0x00: handle_100b00(regs); break;
531 case 0x01: handle_100b01(regs); break;
532 default: handle_100bXX(regs); break;
533 }
534}
535
536
537static void
538handle_100c(struct bregs *regs)
539{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400540 // XXX - page (regs->bh) is unused
541 vgafb_write_pixel(regs->al, regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400542}
543
544static void
545handle_100d(struct bregs *regs)
546{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400547 // XXX - page (regs->bh) is unused
548 regs->al = vgafb_read_pixel(regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400549}
550
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400551static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400552handle_100e(struct bregs *regs)
553{
554 // Ralf Brown Interrupt list is WRONG on bh(page)
555 // We do output only on the current page !
Kevin O'Connor09262412009-05-25 11:44:11 -0400556 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connor116a0442009-05-26 00:47:32 -0400557 struct cursorpos cp = get_cursor_pos(0xff);
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400558 write_teletype(&cp, ca);
Kevin O'Connor116a0442009-05-26 00:47:32 -0400559 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400560}
561
562static void
563handle_100f(struct bregs *regs)
564{
Kevin O'Connore7132042009-05-25 00:10:35 -0400565 regs->bh = GET_BDA(video_page);
566 regs->al = GET_BDA(video_mode) | (GET_BDA(video_ctl) & 0x80);
567 regs->ah = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400568}
569
570
571static void
572handle_101000(struct bregs *regs)
573{
574 if (regs->bl > 0x14)
575 return;
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500576 stdvga_attr_write(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400577}
578
579static void
580handle_101001(struct bregs *regs)
581{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500582 stdvga_set_overscan_border_color(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400583}
584
585static void
586handle_101002(struct bregs *regs)
587{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500588 stdvga_set_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400589}
590
591static void
592handle_101003(struct bregs *regs)
593{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500594 stdvga_toggle_intensity(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400595}
596
597static void
598handle_101007(struct bregs *regs)
599{
600 if (regs->bl > 0x14)
601 return;
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500602 regs->bh = stdvga_attr_read(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400603}
604
605static void
606handle_101008(struct bregs *regs)
607{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500608 regs->bh = stdvga_get_overscan_border_color();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400609}
610
611static void
612handle_101009(struct bregs *regs)
613{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500614 stdvga_get_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400615}
616
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400617static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400618handle_101010(struct bregs *regs)
619{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400620 u8 rgb[3] = {regs->dh, regs->ch, regs->cl};
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500621 stdvga_dac_write(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400622}
623
624static void
625handle_101012(struct bregs *regs)
626{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500627 stdvga_dac_write(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400628}
629
630static void
631handle_101013(struct bregs *regs)
632{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500633 stdvga_select_video_dac_color_page(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400634}
635
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400636static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400637handle_101015(struct bregs *regs)
638{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400639 u8 rgb[3];
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500640 stdvga_dac_read(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400641 regs->dh = rgb[0];
642 regs->ch = rgb[1];
643 regs->cl = rgb[2];
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400644}
645
646static void
647handle_101017(struct bregs *regs)
648{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500649 stdvga_dac_read(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400650}
651
652static void
653handle_101018(struct bregs *regs)
654{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500655 stdvga_pelmask_write(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400656}
657
658static void
659handle_101019(struct bregs *regs)
660{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500661 regs->bl = stdvga_pelmask_read();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400662}
663
664static void
665handle_10101a(struct bregs *regs)
666{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500667 stdvga_read_video_dac_state(&regs->bl, &regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400668}
669
670static void
671handle_10101b(struct bregs *regs)
672{
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500673 stdvga_perform_gray_scale_summing(regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400674}
675
676static void
677handle_1010XX(struct bregs *regs)
678{
679 debug_stub(regs);
680}
681
682static void
683handle_1010(struct bregs *regs)
684{
685 switch (regs->al) {
686 case 0x00: handle_101000(regs); break;
687 case 0x01: handle_101001(regs); break;
688 case 0x02: handle_101002(regs); break;
689 case 0x03: handle_101003(regs); break;
690 case 0x07: handle_101007(regs); break;
691 case 0x08: handle_101008(regs); break;
692 case 0x09: handle_101009(regs); break;
693 case 0x10: handle_101010(regs); break;
694 case 0x12: handle_101012(regs); break;
695 case 0x13: handle_101013(regs); break;
696 case 0x15: handle_101015(regs); break;
697 case 0x17: handle_101017(regs); break;
698 case 0x18: handle_101018(regs); break;
699 case 0x19: handle_101019(regs); break;
700 case 0x1a: handle_10101a(regs); break;
701 case 0x1b: handle_10101b(regs); break;
702 default: handle_1010XX(regs); break;
703 }
704}
705
706
707static void
708handle_101100(struct bregs *regs)
709{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500710 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
711 , regs->dx, regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400712}
713
714static void
715handle_101101(struct bregs *regs)
716{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500717 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400718}
719
720static void
721handle_101102(struct bregs *regs)
722{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500723 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400724}
725
726static void
727handle_101103(struct bregs *regs)
728{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500729 stdvga_set_text_block_specifier(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400730}
731
732static void
733handle_101104(struct bregs *regs)
734{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500735 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400736}
737
738static void
739handle_101110(struct bregs *regs)
740{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500741 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
742 , regs->dx, regs->bl, regs->bh);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400743 set_scan_lines(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400744}
745
746static void
747handle_101111(struct bregs *regs)
748{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500749 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400750 set_scan_lines(14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400751}
752
753static void
754handle_101112(struct bregs *regs)
755{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500756 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400757 set_scan_lines(8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400758}
759
760static void
761handle_101114(struct bregs *regs)
762{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500763 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400764 set_scan_lines(16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400765}
766
767static void
Paolo Bonzini4bd8aeb2013-01-10 13:41:36 +0100768handle_101120(struct bregs *regs)
769{
770 SET_IVT(0x1f, SEGOFF(regs->es, regs->bp));
771}
772
773void
774load_gfx_font(u16 seg, u16 off, u8 height, u8 bl, u8 dl)
775{
776 u8 rows;
777
778 SET_IVT(0x43, SEGOFF(seg, off));
779 switch(bl) {
780 case 0:
781 rows = dl;
782 break;
783 case 1:
784 rows = 14;
785 break;
786 case 3:
787 rows = 43;
788 break;
789 case 2:
790 default:
791 rows = 25;
792 break;
793 }
794 SET_BDA(video_rows, rows - 1);
795 SET_BDA(char_height, height);
796}
797
798static void
799handle_101121(struct bregs *regs)
800{
801 load_gfx_font(regs->es, regs->bp, regs->cx, regs->bl, regs->dl);
802}
803
804static void
805handle_101122(struct bregs *regs)
806{
807 load_gfx_font(get_global_seg(), (u32)vgafont14, 14, regs->bl, regs->dl);
808}
809
810static void
811handle_101123(struct bregs *regs)
812{
813 load_gfx_font(get_global_seg(), (u32)vgafont8, 8, regs->bl, regs->dl);
814}
815
816static void
817handle_101124(struct bregs *regs)
818{
819 load_gfx_font(get_global_seg(), (u32)vgafont16, 16, regs->bl, regs->dl);
820}
821
822static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400823handle_101130(struct bregs *regs)
824{
Kevin O'Connore7132042009-05-25 00:10:35 -0400825 switch (regs->bh) {
826 case 0x00: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500827 struct segoff_s so = GET_IVT(0x1f);
828 regs->es = so.seg;
829 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400830 break;
831 }
832 case 0x01: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500833 struct segoff_s so = GET_IVT(0x43);
834 regs->es = so.seg;
835 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400836 break;
837 }
838 case 0x02:
839 regs->es = get_global_seg();
840 regs->bp = (u32)vgafont14;
841 break;
842 case 0x03:
843 regs->es = get_global_seg();
844 regs->bp = (u32)vgafont8;
845 break;
846 case 0x04:
847 regs->es = get_global_seg();
848 regs->bp = (u32)vgafont8 + 128 * 8;
849 break;
850 case 0x05:
851 regs->es = get_global_seg();
852 regs->bp = (u32)vgafont14alt;
853 break;
854 case 0x06:
855 regs->es = get_global_seg();
856 regs->bp = (u32)vgafont16;
857 break;
858 case 0x07:
859 regs->es = get_global_seg();
860 regs->bp = (u32)vgafont16alt;
861 break;
862 default:
863 dprintf(1, "Get font info BH(%02x) was discarded\n", regs->bh);
864 return;
865 }
866 // Set byte/char of on screen font
867 regs->cx = GET_BDA(char_height) & 0xff;
868
869 // Set Highest char row
Kevin O'Connor4c85a262012-02-12 11:50:52 -0500870 regs->dl = GET_BDA(video_rows);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400871}
872
873static void
874handle_1011XX(struct bregs *regs)
875{
876 debug_stub(regs);
877}
878
879static void
880handle_1011(struct bregs *regs)
881{
882 switch (regs->al) {
883 case 0x00: handle_101100(regs); break;
884 case 0x01: handle_101101(regs); break;
885 case 0x02: handle_101102(regs); break;
886 case 0x03: handle_101103(regs); break;
887 case 0x04: handle_101104(regs); break;
888 case 0x10: handle_101110(regs); break;
889 case 0x11: handle_101111(regs); break;
890 case 0x12: handle_101112(regs); break;
891 case 0x14: handle_101114(regs); break;
892 case 0x30: handle_101130(regs); break;
Paolo Bonzini4bd8aeb2013-01-10 13:41:36 +0100893 case 0x20: handle_101120(regs); break;
894 case 0x21: handle_101121(regs); break;
895 case 0x22: handle_101122(regs); break;
896 case 0x23: handle_101123(regs); break;
897 case 0x24: handle_101124(regs); break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400898 default: handle_1011XX(regs); break;
899 }
900}
901
902
903static void
904handle_101210(struct bregs *regs)
905{
Kevin O'Connore7132042009-05-25 00:10:35 -0400906 u16 crtc_addr = GET_BDA(crtc_address);
907 if (crtc_addr == VGAREG_MDA_CRTC_ADDRESS)
908 regs->bx = 0x0103;
909 else
910 regs->bx = 0x0003;
911 regs->cx = GET_BDA(video_switches) & 0x0f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400912}
913
914static void
915handle_101230(struct bregs *regs)
916{
Kevin O'Connore7132042009-05-25 00:10:35 -0400917 u8 mctl = GET_BDA(modeset_ctl);
918 u8 vswt = GET_BDA(video_switches);
919 switch (regs->al) {
920 case 0x00:
921 // 200 lines
922 mctl = (mctl & ~0x10) | 0x80;
923 vswt = (vswt & ~0x0f) | 0x08;
924 break;
925 case 0x01:
926 // 350 lines
927 mctl &= ~0x90;
928 vswt = (vswt & ~0x0f) | 0x09;
929 break;
930 case 0x02:
931 // 400 lines
932 mctl = (mctl & ~0x80) | 0x10;
933 vswt = (vswt & ~0x0f) | 0x09;
934 break;
935 default:
936 dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
937 break;
938 }
939 SET_BDA(modeset_ctl, mctl);
940 SET_BDA(video_switches, vswt);
941 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400942}
943
944static void
945handle_101231(struct bregs *regs)
946{
Kevin O'Connore7132042009-05-25 00:10:35 -0400947 u8 v = (regs->al & 0x01) << 3;
948 u8 mctl = GET_BDA(video_ctl) & ~0x08;
949 SET_BDA(video_ctl, mctl | v);
950 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400951}
952
953static void
954handle_101232(struct bregs *regs)
955{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500956 stdvga_enable_video_addressing(regs->al);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400957 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400958}
959
960static void
961handle_101233(struct bregs *regs)
962{
Kevin O'Connore7132042009-05-25 00:10:35 -0400963 u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
964 u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
965 SET_BDA(modeset_ctl, v | v2);
966 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400967}
968
969static void
970handle_101234(struct bregs *regs)
971{
Kevin O'Connore7132042009-05-25 00:10:35 -0400972 u8 v = (regs->al & 0x01) ^ 0x01;
973 u8 v2 = GET_BDA(modeset_ctl) & ~0x01;
974 SET_BDA(modeset_ctl, v | v2);
975 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400976}
977
978static void
979handle_101235(struct bregs *regs)
980{
981 debug_stub(regs);
982 regs->al = 0x12;
983}
984
985static void
986handle_101236(struct bregs *regs)
987{
988 debug_stub(regs);
989 regs->al = 0x12;
990}
991
992static void
993handle_1012XX(struct bregs *regs)
994{
995 debug_stub(regs);
996}
997
998static void
999handle_1012(struct bregs *regs)
1000{
Kevin O'Connore91ec7c2012-01-14 16:30:49 -05001001 if (CONFIG_VGA_CIRRUS && regs->bl >= 0x80) {
1002 clext_1012(regs);
1003 return;
1004 }
1005
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001006 switch (regs->bl) {
1007 case 0x10: handle_101210(regs); break;
1008 case 0x30: handle_101230(regs); break;
1009 case 0x31: handle_101231(regs); break;
1010 case 0x32: handle_101232(regs); break;
1011 case 0x33: handle_101233(regs); break;
1012 case 0x34: handle_101234(regs); break;
1013 case 0x35: handle_101235(regs); break;
1014 case 0x36: handle_101236(regs); break;
1015 default: handle_1012XX(regs); break;
1016 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001017}
1018
1019
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001020// Write string
Kevin O'Connor0ad77f02009-05-31 20:46:43 -04001021static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001022handle_1013(struct bregs *regs)
1023{
Kevin O'Connor6ee837b2012-02-13 20:09:02 -05001024 struct cursorpos cp;
1025 if (regs->dh == 0xff)
1026 // if row=0xff special case : use current cursor position
1027 cp = get_cursor_pos(regs->bh);
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001028 else
Kevin O'Connor6ee837b2012-02-13 20:09:02 -05001029 cp = (struct cursorpos) {regs->dl, regs->dh, regs->bh};
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001030
Kevin O'Connor6ee837b2012-02-13 20:09:02 -05001031 u16 count = regs->cx;
1032 u8 *offset_far = (void*)(regs->bp + 0);
1033 u8 attr = regs->bl;
1034 while (count--) {
1035 u8 car = GET_FARVAR(regs->es, *offset_far);
1036 offset_far++;
1037 if (regs->al & 2) {
1038 attr = GET_FARVAR(regs->es, *offset_far);
1039 offset_far++;
1040 }
1041
1042 struct carattr ca = {car, attr, 1};
1043 write_teletype(&cp, ca);
1044 }
1045
1046 if (regs->al & 1)
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001047 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001048}
1049
1050
1051static void
1052handle_101a00(struct bregs *regs)
1053{
Kevin O'Connore7132042009-05-25 00:10:35 -04001054 regs->bx = GET_BDA(dcc_index);
1055 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001056}
1057
1058static void
1059handle_101a01(struct bregs *regs)
1060{
Kevin O'Connore7132042009-05-25 00:10:35 -04001061 SET_BDA(dcc_index, regs->bl);
1062 dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
1063 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001064}
1065
1066static void
1067handle_101aXX(struct bregs *regs)
1068{
1069 debug_stub(regs);
1070}
1071
1072static void
1073handle_101a(struct bregs *regs)
1074{
1075 switch (regs->al) {
1076 case 0x00: handle_101a00(regs); break;
1077 case 0x01: handle_101a01(regs); break;
1078 default: handle_101aXX(regs); break;
1079 }
1080}
1081
1082
Kevin O'Connorefb45232012-01-14 22:55:11 -05001083static u8 static_functionality[0x10] VAR16 = {
1084 /* 0 */ 0xff, // All modes supported #1
1085 /* 1 */ 0xe0, // All modes supported #2
1086 /* 2 */ 0x0f, // All modes supported #3
1087 /* 3 */ 0x00, 0x00, 0x00, 0x00, // reserved
1088 /* 7 */ 0x07, // 200, 350, 400 scan lines
1089 /* 8 */ 0x02, // mamimum number of visible charsets in text mode
1090 /* 9 */ 0x08, // total number of charset blocks in text mode
1091 /* a */ 0xe7, // Change to add new functions
1092 /* b */ 0x0c, // Change to add new functions
1093 /* c */ 0x00, // reserved
1094 /* d */ 0x00, // reserved
1095 /* e */ 0x00, // Change to add new functions
1096 /* f */ 0x00 // reserved
1097};
1098
Kevin O'Connorca668642009-05-21 23:06:08 -04001099struct funcInfo {
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001100 struct segoff_s static_functionality;
Kevin O'Connorca668642009-05-21 23:06:08 -04001101 u8 bda_0x49[30];
1102 u8 bda_0x84[3];
1103 u8 dcc_index;
1104 u8 dcc_alt;
1105 u16 colors;
1106 u8 pages;
1107 u8 scan_lines;
1108 u8 primary_char;
1109 u8 secondar_char;
1110 u8 misc;
1111 u8 non_vga_mode;
1112 u8 reserved_2f[2];
1113 u8 video_mem;
1114 u8 save_flags;
1115 u8 disp_info;
1116 u8 reserved_34[12];
Kevin O'Connorf5ec1e02014-02-05 18:49:44 -05001117} PACKED;
Kevin O'Connorca668642009-05-21 23:06:08 -04001118
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001119static void
1120handle_101b(struct bregs *regs)
1121{
Kevin O'Connorca668642009-05-21 23:06:08 -04001122 u16 seg = regs->es;
1123 struct funcInfo *info = (void*)(regs->di+0);
1124 memset_far(seg, info, 0, sizeof(*info));
1125 // Address of static functionality table
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001126 SET_FARVAR(seg, info->static_functionality
1127 , SEGOFF(get_global_seg(), (u32)static_functionality));
Kevin O'Connorca668642009-05-21 23:06:08 -04001128
1129 // Hard coded copy from BIOS area. Should it be cleaner ?
Kevin O'Connor9f985422009-09-09 11:34:39 -04001130 memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49
1131 , sizeof(info->bda_0x49));
1132 memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84
1133 , sizeof(info->bda_0x84));
Kevin O'Connorca668642009-05-21 23:06:08 -04001134
1135 SET_FARVAR(seg, info->dcc_index, GET_BDA(dcc_index));
1136 SET_FARVAR(seg, info->colors, 16);
1137 SET_FARVAR(seg, info->pages, 8);
1138 SET_FARVAR(seg, info->scan_lines, 2);
1139 SET_FARVAR(seg, info->video_mem, 3);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001140 regs->al = 0x1B;
1141}
1142
1143
1144static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001145handle_101c(struct bregs *regs)
1146{
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001147 u16 seg = regs->es;
1148 void *data = (void*)(regs->bx+0);
1149 u16 states = regs->cx;
1150 if (states & ~0x07)
1151 goto fail;
1152 int ret;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001153 switch (regs->al) {
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001154 case 0x00:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001155 ret = vgahw_size_state(states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001156 if (ret < 0)
1157 goto fail;
1158 regs->bx = ret / 64;
1159 break;
1160 case 0x01:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001161 ret = vgahw_save_state(seg, data, states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001162 if (ret)
1163 goto fail;
1164 break;
1165 case 0x02:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001166 ret = vgahw_restore_state(seg, data, states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001167 if (ret)
1168 goto fail;
1169 break;
1170 default:
1171 goto fail;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001172 }
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001173 regs->al = 0x1c;
1174fail:
1175 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001176}
1177
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001178static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001179handle_10XX(struct bregs *regs)
1180{
1181 debug_stub(regs);
1182}
1183
1184// INT 10h Video Support Service Entry Point
1185void VISIBLE16
1186handle_10(struct bregs *regs)
1187{
1188 debug_enter(regs, DEBUG_VGA_10);
1189 switch (regs->ah) {
1190 case 0x00: handle_1000(regs); break;
1191 case 0x01: handle_1001(regs); break;
1192 case 0x02: handle_1002(regs); break;
1193 case 0x03: handle_1003(regs); break;
1194 case 0x04: handle_1004(regs); break;
1195 case 0x05: handle_1005(regs); break;
1196 case 0x06: handle_1006(regs); break;
1197 case 0x07: handle_1007(regs); break;
1198 case 0x08: handle_1008(regs); break;
1199 case 0x09: handle_1009(regs); break;
1200 case 0x0a: handle_100a(regs); break;
1201 case 0x0b: handle_100b(regs); break;
1202 case 0x0c: handle_100c(regs); break;
1203 case 0x0d: handle_100d(regs); break;
1204 case 0x0e: handle_100e(regs); break;
1205 case 0x0f: handle_100f(regs); break;
1206 case 0x10: handle_1010(regs); break;
1207 case 0x11: handle_1011(regs); break;
1208 case 0x12: handle_1012(regs); break;
1209 case 0x13: handle_1013(regs); break;
1210 case 0x1a: handle_101a(regs); break;
1211 case 0x1b: handle_101b(regs); break;
1212 case 0x1c: handle_101c(regs); break;
1213 case 0x4f: handle_104f(regs); break;
1214 default: handle_10XX(regs); break;
1215 }
1216}