blob: d36b62a379b98ed099f378c718f15c749e01147b [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'Connorefbf4d62014-02-09 11:50:21 -050062 if (!CONFIG_VGA_STDVGA_PORTS)
63 return;
64
Kevin O'Connordd2be772009-05-16 15:41:23 -040065 u8 modeset_ctl = GET_BDA(modeset_ctl);
66 u16 cheight = GET_BDA(char_height);
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040067 if ((modeset_ctl & 0x01) && (cheight > 8) && (end < 8) && (start < 0x20)) {
68 if (end != (start + 1))
69 start = ((start + 1) * cheight / 8) - 1;
Kevin O'Connordd2be772009-05-16 15:41:23 -040070 else
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040071 start = ((end + 1) * cheight / 8) - 2;
72 end = ((end + 1) * cheight / 8) - 1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040073 }
Kevin O'Connor88ca7412011-12-31 04:24:20 -050074 stdvga_set_cursor_shape(start, end);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040075}
76
Kevin O'Connor0818e1a2009-05-16 18:00:19 -040077static u16
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040078get_cursor_shape(u8 page)
Kevin O'Connor0818e1a2009-05-16 18:00:19 -040079{
80 if (page > 7)
81 return 0;
82 // FIXME should handle VGA 14/16 lines
83 return GET_BDA(cursor_type);
84}
85
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040086static void
Kevin O'Connor918b1562009-05-25 11:05:18 -040087set_cursor_pos(struct cursorpos cp)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040088{
Kevin O'Connor6ee837b2012-02-13 20:09:02 -050089 u8 page = cp.page, x = cp.x, y = cp.y;
90
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040091 // Should not happen...
Kevin O'Connor6ee837b2012-02-13 20:09:02 -050092 if (page > 7)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040093 return;
94
95 // Bios cursor pos
Kevin O'Connor6ee837b2012-02-13 20:09:02 -050096 SET_BDA(cursor_pos[page], (y << 8) | x);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040097
Kevin O'Connorefbf4d62014-02-09 11:50:21 -050098 if (!CONFIG_VGA_STDVGA_PORTS)
99 return;
100
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400101 // Set the hardware cursor
Kevin O'Connordd2be772009-05-16 15:41:23 -0400102 u8 current = GET_BDA(video_page);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400103 if (cp.page != current)
Kevin O'Connordd2be772009-05-16 15:41:23 -0400104 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400105
Kevin O'Connor83047be2012-01-07 18:27:19 -0500106 // Calculate the memory address
Kevin O'Connora02d4182014-04-05 22:48:05 -0400107 stdvga_set_cursor_pos((int)text_address(cp));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400108}
109
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400110static struct cursorpos
Kevin O'Connor918b1562009-05-25 11:05:18 -0400111get_cursor_pos(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400112{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400113 if (page == 0xff)
114 // special case - use current page
115 page = GET_BDA(video_page);
116 if (page > 7) {
117 struct cursorpos cp = { 0, 0, 0xfe };
118 return cp;
119 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400120 // FIXME should handle VGA 14/16 lines
Kevin O'Connor918b1562009-05-25 11:05:18 -0400121 u16 xy = GET_BDA(cursor_pos[page]);
122 struct cursorpos cp = {xy, xy>>8, page};
123 return cp;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400124}
125
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400126static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400127set_active_page(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400128{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400129 if (page > 7)
130 return;
131
132 // Get the mode
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500133 struct vgamode_s *vmode_g = get_current_mode();
Kevin O'Connor5727c292009-05-16 17:29:32 -0400134 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400135 return;
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 if (lines == 8)
157 set_cursor_shape(0x06, 0x07);
158 else
159 set_cursor_shape(lines - 4, lines - 3);
160 SET_BDA(char_height, lines);
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500161 u16 vde = stdvga_get_vde();
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400162 u8 rows = vde / lines;
163 SET_BDA(video_rows, rows - 1);
164 u16 cols = GET_BDA(video_cols);
Kevin O'Connor83047be2012-01-07 18:27:19 -0500165 SET_BDA(video_pagesize, calc_page_size(MM_TEXT, cols, rows));
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
215 struct vgamode_s *vmode_g = get_current_mode();
216 if (!vmode_g)
217 return;
218
219 struct cursorpos dest = {0, 0, pcp->page};
220 struct cursorpos src = {0, 1, pcp->page};
221 struct cursorpos size = {GET_BDA(video_cols), nbrows};
222 vgafb_move_chars(vmode_g, dest, src, size);
223
224 struct cursorpos clr = {0, nbrows, pcp->page};
225 struct carattr attr = {' ', 0, 0};
226 struct cursorpos clrsize = {GET_BDA(video_cols), 1};
227 vgafb_clear_chars(vmode_g, clr, attr, clrsize);
Kevin O'Connor82221b22009-05-26 00:20:40 -0400228 }
Kevin O'Connor82221b22009-05-26 00:20:40 -0400229}
230
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400231
232/****************************************************************
233 * Save and restore bda state
234 ****************************************************************/
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400235
Kevin O'Connor20dc4192014-02-05 20:52:25 -0500236struct saveBDAstate {
237 u8 bda_0x49[28];
238 u8 bda_0x84[6];
239 u16 vbe_mode;
240 struct segoff_s font0;
241 struct segoff_s font1;
242};
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400243
Kevin O'Connor20dc4192014-02-05 20:52:25 -0500244int
245bda_save_restore(int cmd, u16 seg, void *data)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400246{
Kevin O'Connor20dc4192014-02-05 20:52:25 -0500247 if (!(cmd & SR_BDA))
248 return 0;
249 struct saveBDAstate *info = data;
250 if (cmd & SR_SAVE) {
251 memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49
252 , sizeof(info->bda_0x49));
253 memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84
254 , sizeof(info->bda_0x84));
Kevin O'Connor9978d492014-10-17 21:17:48 -0400255 SET_FARVAR(seg, info->vbe_mode, GET_BDA_EXT(vbe_mode));
Kevin O'Connor20dc4192014-02-05 20:52:25 -0500256 SET_FARVAR(seg, info->font0, GET_IVT(0x1f));
257 SET_FARVAR(seg, info->font1, GET_IVT(0x43));
258 }
259 if (cmd & SR_RESTORE) {
260 memcpy_far(SEG_BDA, (void*)0x49, seg, info->bda_0x49
261 , sizeof(info->bda_0x49));
262 memcpy_far(SEG_BDA, (void*)0x84, seg, info->bda_0x84
263 , sizeof(info->bda_0x84));
Kevin O'Connor9978d492014-10-17 21:17:48 -0400264 SET_BDA_EXT(vbe_mode, GET_FARVAR(seg, info->vbe_mode));
Kevin O'Connor20dc4192014-02-05 20:52:25 -0500265 SET_IVT(0x1f, GET_FARVAR(seg, info->font0));
266 SET_IVT(0x43, GET_FARVAR(seg, info->font1));
267 }
268 return sizeof(*info);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400269}
270
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500271
272/****************************************************************
273 * Mode setting
274 ****************************************************************/
275
276struct vgamode_s *
277get_current_mode(void)
278{
Kevin O'Connor9978d492014-10-17 21:17:48 -0400279 return vgahw_find_mode(GET_BDA_EXT(vbe_mode) & ~MF_VBEFLAGS);
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500280}
281
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500282// Setup BDA after a mode switch.
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500283int
284vga_set_mode(int mode, int flags)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400285{
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500286 dprintf(1, "set VGA mode %x\n", mode);
287 struct vgamode_s *vmode_g = vgahw_find_mode(mode);
288 if (!vmode_g)
289 return VBE_RETURN_STATUS_FAILED;
290
291 int ret = vgahw_set_mode(vmode_g, flags);
292 if (ret)
293 return ret;
294
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400295 // Set the BIOS mem
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500296 int width = GET_GLOBAL(vmode_g->width);
297 int height = GET_GLOBAL(vmode_g->height);
Kevin O'Connor83047be2012-01-07 18:27:19 -0500298 u8 memmodel = GET_GLOBAL(vmode_g->memmodel);
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500299 int cheight = GET_GLOBAL(vmode_g->cheight);
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500300 if (mode < 0x100)
301 SET_BDA(video_mode, mode);
302 else
303 SET_BDA(video_mode, 0xff);
Kevin O'Connor9978d492014-10-17 21:17:48 -0400304 SET_BDA_EXT(vbe_mode, mode | (flags & MF_VBEFLAGS));
Kevin O'Connor83047be2012-01-07 18:27:19 -0500305 if (memmodel == MM_TEXT) {
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500306 SET_BDA(video_cols, width);
307 SET_BDA(video_rows, height-1);
308 SET_BDA(cursor_type, 0x0607);
309 } else {
310 int cwidth = GET_GLOBAL(vmode_g->cwidth);
311 SET_BDA(video_cols, width / cwidth);
312 SET_BDA(video_rows, (height / cheight) - 1);
313 SET_BDA(cursor_type, 0x0000);
314 }
Kevin O'Connor83047be2012-01-07 18:27:19 -0500315 SET_BDA(video_pagesize, calc_page_size(memmodel, width, height));
Kevin O'Connorefbf4d62014-02-09 11:50:21 -0500316 SET_BDA(crtc_address, CONFIG_VGA_STDVGA_PORTS ? stdvga_get_crtc() : 0);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400317 SET_BDA(char_height, cheight);
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500318 SET_BDA(video_ctl, 0x60 | (flags & MF_NOCLEARMEM ? 0x80 : 0x00));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400319 SET_BDA(video_switches, 0xF9);
320 SET_BDA(modeset_ctl, GET_BDA(modeset_ctl) & 0x7f);
Kevin O'Connorcecbc5d2011-12-31 17:24:11 -0500321 int i;
322 for (i=0; i<8; i++)
323 SET_BDA(cursor_pos[i], 0x0000);
324 SET_BDA(video_pagestart, 0x0000);
325 SET_BDA(video_page, 0x00);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400326
327 // FIXME We nearly have the good tables. to be reworked
328 SET_BDA(dcc_index, 0x08); // 8 is VGA should be ok for now
Kevin O'Connor9f985422009-09-09 11:34:39 -0400329 SET_BDA(video_savetable
Kevin O'Connor815e4472011-12-21 09:05:32 -0500330 , SEGOFF(get_global_seg(), (u32)&video_save_pointer_table));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400331
332 // FIXME
333 SET_BDA(video_msr, 0x00); // Unavailable on vanilla vga, but...
334 SET_BDA(video_pal, 0x00); // Unavailable on vanilla vga, but...
335
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400336 // Set the ints 0x1F and 0x43
Kevin O'Connor9f985422009-09-09 11:34:39 -0400337 SET_IVT(0x1f, SEGOFF(get_global_seg(), (u32)&vgafont8[128 * 8]));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400338
339 switch (cheight) {
340 case 8:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400341 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont8));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400342 break;
343 case 14:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400344 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont14));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400345 break;
346 case 16:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400347 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont16));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400348 break;
349 }
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500350
351 return 0;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400352}
353
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500354
355/****************************************************************
356 * VGA int 10 handler
357 ****************************************************************/
358
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400359static void
Julian Pidancet87879e22011-12-19 05:08:00 +0000360handle_1000(struct bregs *regs)
361{
Kevin O'Connor5108c692011-12-31 19:13:45 -0500362 int mode = regs->al & 0x7f;
Julian Pidancet87879e22011-12-19 05:08:00 +0000363
364 // Set regs->al
365 if (mode > 7)
366 regs->al = 0x20;
367 else if (mode == 6)
368 regs->al = 0x3f;
369 else
370 regs->al = 0x30;
371
Kevin O'Connorb7b92932013-03-09 13:04:47 -0500372 int flags = MF_LEGACY | (GET_BDA(modeset_ctl) & (MF_NOPALETTE|MF_GRAYSUM));
Kevin O'Connor5108c692011-12-31 19:13:45 -0500373 if (regs->al & 0x80)
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500374 flags |= MF_NOCLEARMEM;
375
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500376 vga_set_mode(mode, flags);
Julian Pidancet87879e22011-12-19 05:08:00 +0000377}
378
379static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400380handle_1001(struct bregs *regs)
381{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400382 set_cursor_shape(regs->ch, regs->cl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400383}
384
385static void
386handle_1002(struct bregs *regs)
387{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400388 struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
389 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400390}
391
392static void
393handle_1003(struct bregs *regs)
394{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400395 regs->cx = get_cursor_shape(regs->bh);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400396 struct cursorpos cp = get_cursor_pos(regs->bh);
397 regs->dl = cp.x;
398 regs->dh = cp.y;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400399}
400
401// Read light pen pos (unimplemented)
402static void
403handle_1004(struct bregs *regs)
404{
405 debug_stub(regs);
406 regs->ax = regs->bx = regs->cx = regs->dx = 0;
407}
408
409static void
410handle_1005(struct bregs *regs)
411{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400412 set_active_page(regs->al);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400413}
414
415static void
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400416verify_scroll(struct bregs *regs, int dir)
417{
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500418 u8 ulx = regs->cl, uly = regs->ch, lrx = regs->dl, lry = regs->dh;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400419 u16 nbrows = GET_BDA(video_rows) + 1;
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500420 if (lry >= nbrows)
421 lry = nbrows - 1;
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400422 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500423 if (lrx >= nbcols)
424 lrx = nbcols - 1;
Kevin O'Connor7fd2af62014-03-20 21:16:28 -0400425 int wincols = lrx - ulx + 1, winrows = lry - uly + 1;
426 if (wincols <= 0 || winrows <= 0)
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400427 return;
428
Kevin O'Connor7fd2af62014-03-20 21:16:28 -0400429 struct vgamode_s *vmode_g = get_current_mode();
430 if (!vmode_g)
431 return;
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400432
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500433 u8 page = GET_BDA(video_page);
Kevin O'Connor7fd2af62014-03-20 21:16:28 -0400434 int clearlines = regs->al, movelines = winrows - clearlines;
435 if (!clearlines || movelines <= 0) {
436 // Clear whole area.
437 struct cursorpos clr = {ulx, uly, page};
438 struct carattr attr = {' ', regs->bh, 1};
439 struct cursorpos clrsize = {wincols, winrows};
440 vgafb_clear_chars(vmode_g, clr, attr, clrsize);
441 return;
442 }
443
444 if (dir > 0) {
445 // Normal scroll
446 struct cursorpos dest = {ulx, uly, page};
447 struct cursorpos src = {ulx, uly + clearlines, page};
448 struct cursorpos size = {wincols, movelines};
449 vgafb_move_chars(vmode_g, dest, src, size);
450
451 struct cursorpos clr = {ulx, uly + movelines, page};
452 struct carattr attr = {' ', regs->bh, 1};
453 struct cursorpos clrsize = {wincols, clearlines};
454 vgafb_clear_chars(vmode_g, clr, attr, clrsize);
455 } else {
456 // Scroll down
457 struct cursorpos dest = {ulx, uly + clearlines, page};
458 struct cursorpos src = {ulx, uly, page};
459 struct cursorpos size = {wincols, movelines};
460 vgafb_move_chars(vmode_g, dest, src, size);
461
462 struct cursorpos clr = {ulx, uly, page};
463 struct carattr attr = {' ', regs->bh, 1};
464 struct cursorpos clrsize = {wincols, clearlines};
465 vgafb_clear_chars(vmode_g, clr, attr, clrsize);
466 }
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400467}
468
469static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400470handle_1006(struct bregs *regs)
471{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400472 verify_scroll(regs, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400473}
474
475static void
476handle_1007(struct bregs *regs)
477{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400478 verify_scroll(regs, -1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400479}
480
481static void
482handle_1008(struct bregs *regs)
483{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400484 struct carattr ca = vgafb_read_char(get_cursor_pos(regs->bh));
Kevin O'Connor09262412009-05-25 11:44:11 -0400485 regs->al = ca.car;
486 regs->ah = ca.attr;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400487}
488
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400489static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400490handle_1009(struct bregs *regs)
491{
Kevin O'Connor09262412009-05-25 11:44:11 -0400492 struct carattr ca = {regs->al, regs->bl, 1};
Kevin O'Connor0910dde2014-02-08 16:43:30 -0500493 struct cursorpos cp = get_cursor_pos(regs->bh);
494 int count = regs->cx;
495 while (count--)
496 write_char(&cp, ca);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400497}
498
Kevin O'Connor0910dde2014-02-08 16:43:30 -0500499static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400500handle_100a(struct bregs *regs)
501{
Kevin O'Connor09262412009-05-25 11:44:11 -0400502 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connor0910dde2014-02-08 16:43:30 -0500503 struct cursorpos cp = get_cursor_pos(regs->bh);
504 int count = regs->cx;
505 while (count--)
506 write_char(&cp, ca);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400507}
508
509
510static void
511handle_100b00(struct bregs *regs)
512{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500513 stdvga_set_border_color(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400514}
515
516static void
517handle_100b01(struct bregs *regs)
518{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500519 stdvga_set_palette(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400520}
521
522static void
523handle_100bXX(struct bregs *regs)
524{
525 debug_stub(regs);
526}
527
528static void
529handle_100b(struct bregs *regs)
530{
Kevin O'Connorefbf4d62014-02-09 11:50:21 -0500531 if (!CONFIG_VGA_STDVGA_PORTS) {
532 handle_100bXX(regs);
533 return;
534 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400535 switch (regs->bh) {
536 case 0x00: handle_100b00(regs); break;
537 case 0x01: handle_100b01(regs); break;
538 default: handle_100bXX(regs); break;
539 }
540}
541
542
543static void
544handle_100c(struct bregs *regs)
545{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400546 // XXX - page (regs->bh) is unused
547 vgafb_write_pixel(regs->al, regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400548}
549
550static void
551handle_100d(struct bregs *regs)
552{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400553 // XXX - page (regs->bh) is unused
554 regs->al = vgafb_read_pixel(regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400555}
556
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400557static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400558handle_100e(struct bregs *regs)
559{
560 // Ralf Brown Interrupt list is WRONG on bh(page)
561 // We do output only on the current page !
Kevin O'Connor09262412009-05-25 11:44:11 -0400562 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connor116a0442009-05-26 00:47:32 -0400563 struct cursorpos cp = get_cursor_pos(0xff);
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400564 write_teletype(&cp, ca);
Kevin O'Connor116a0442009-05-26 00:47:32 -0400565 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400566}
567
568static void
569handle_100f(struct bregs *regs)
570{
Kevin O'Connore7132042009-05-25 00:10:35 -0400571 regs->bh = GET_BDA(video_page);
572 regs->al = GET_BDA(video_mode) | (GET_BDA(video_ctl) & 0x80);
573 regs->ah = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400574}
575
576
577static void
578handle_101000(struct bregs *regs)
579{
580 if (regs->bl > 0x14)
581 return;
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500582 stdvga_attr_write(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400583}
584
585static void
586handle_101001(struct bregs *regs)
587{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500588 stdvga_set_overscan_border_color(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400589}
590
591static void
592handle_101002(struct bregs *regs)
593{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500594 stdvga_set_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400595}
596
597static void
598handle_101003(struct bregs *regs)
599{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500600 stdvga_toggle_intensity(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400601}
602
603static void
604handle_101007(struct bregs *regs)
605{
606 if (regs->bl > 0x14)
607 return;
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500608 regs->bh = stdvga_attr_read(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400609}
610
611static void
612handle_101008(struct bregs *regs)
613{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500614 regs->bh = stdvga_get_overscan_border_color();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400615}
616
617static void
618handle_101009(struct bregs *regs)
619{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500620 stdvga_get_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400621}
622
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400623static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400624handle_101010(struct bregs *regs)
625{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400626 u8 rgb[3] = {regs->dh, regs->ch, regs->cl};
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500627 stdvga_dac_write(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400628}
629
630static void
631handle_101012(struct bregs *regs)
632{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500633 stdvga_dac_write(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400634}
635
636static void
637handle_101013(struct bregs *regs)
638{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500639 stdvga_select_video_dac_color_page(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400640}
641
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400642static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400643handle_101015(struct bregs *regs)
644{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400645 u8 rgb[3];
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500646 stdvga_dac_read(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400647 regs->dh = rgb[0];
648 regs->ch = rgb[1];
649 regs->cl = rgb[2];
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400650}
651
652static void
653handle_101017(struct bregs *regs)
654{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500655 stdvga_dac_read(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400656}
657
658static void
659handle_101018(struct bregs *regs)
660{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500661 stdvga_pelmask_write(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400662}
663
664static void
665handle_101019(struct bregs *regs)
666{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500667 regs->bl = stdvga_pelmask_read();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400668}
669
670static void
671handle_10101a(struct bregs *regs)
672{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500673 stdvga_read_video_dac_state(&regs->bl, &regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400674}
675
676static void
677handle_10101b(struct bregs *regs)
678{
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500679 stdvga_perform_gray_scale_summing(regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400680}
681
682static void
683handle_1010XX(struct bregs *regs)
684{
685 debug_stub(regs);
686}
687
688static void
689handle_1010(struct bregs *regs)
690{
Kevin O'Connorefbf4d62014-02-09 11:50:21 -0500691 if (!CONFIG_VGA_STDVGA_PORTS) {
692 handle_1010XX(regs);
693 return;
694 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400695 switch (regs->al) {
696 case 0x00: handle_101000(regs); break;
697 case 0x01: handle_101001(regs); break;
698 case 0x02: handle_101002(regs); break;
699 case 0x03: handle_101003(regs); break;
700 case 0x07: handle_101007(regs); break;
701 case 0x08: handle_101008(regs); break;
702 case 0x09: handle_101009(regs); break;
703 case 0x10: handle_101010(regs); break;
704 case 0x12: handle_101012(regs); break;
705 case 0x13: handle_101013(regs); break;
706 case 0x15: handle_101015(regs); break;
707 case 0x17: handle_101017(regs); break;
708 case 0x18: handle_101018(regs); break;
709 case 0x19: handle_101019(regs); break;
710 case 0x1a: handle_10101a(regs); break;
711 case 0x1b: handle_10101b(regs); break;
712 default: handle_1010XX(regs); break;
713 }
714}
715
716
717static void
718handle_101100(struct bregs *regs)
719{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500720 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
721 , regs->dx, regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400722}
723
724static void
725handle_101101(struct bregs *regs)
726{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500727 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400728}
729
730static void
731handle_101102(struct bregs *regs)
732{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500733 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400734}
735
736static void
737handle_101103(struct bregs *regs)
738{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500739 stdvga_set_text_block_specifier(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400740}
741
742static void
743handle_101104(struct bregs *regs)
744{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500745 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400746}
747
748static void
749handle_101110(struct bregs *regs)
750{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500751 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
752 , regs->dx, regs->bl, regs->bh);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400753 set_scan_lines(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400754}
755
756static void
757handle_101111(struct bregs *regs)
758{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500759 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400760 set_scan_lines(14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400761}
762
763static void
764handle_101112(struct bregs *regs)
765{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500766 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400767 set_scan_lines(8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400768}
769
770static void
771handle_101114(struct bregs *regs)
772{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500773 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400774 set_scan_lines(16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400775}
776
777static void
Paolo Bonzini4bd8aeb2013-01-10 13:41:36 +0100778handle_101120(struct bregs *regs)
779{
780 SET_IVT(0x1f, SEGOFF(regs->es, regs->bp));
781}
782
783void
784load_gfx_font(u16 seg, u16 off, u8 height, u8 bl, u8 dl)
785{
786 u8 rows;
787
788 SET_IVT(0x43, SEGOFF(seg, off));
789 switch(bl) {
790 case 0:
791 rows = dl;
792 break;
793 case 1:
794 rows = 14;
795 break;
796 case 3:
797 rows = 43;
798 break;
799 case 2:
800 default:
801 rows = 25;
802 break;
803 }
804 SET_BDA(video_rows, rows - 1);
805 SET_BDA(char_height, height);
806}
807
808static void
809handle_101121(struct bregs *regs)
810{
811 load_gfx_font(regs->es, regs->bp, regs->cx, regs->bl, regs->dl);
812}
813
814static void
815handle_101122(struct bregs *regs)
816{
817 load_gfx_font(get_global_seg(), (u32)vgafont14, 14, regs->bl, regs->dl);
818}
819
820static void
821handle_101123(struct bregs *regs)
822{
823 load_gfx_font(get_global_seg(), (u32)vgafont8, 8, regs->bl, regs->dl);
824}
825
826static void
827handle_101124(struct bregs *regs)
828{
829 load_gfx_font(get_global_seg(), (u32)vgafont16, 16, regs->bl, regs->dl);
830}
831
832static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400833handle_101130(struct bregs *regs)
834{
Kevin O'Connore7132042009-05-25 00:10:35 -0400835 switch (regs->bh) {
836 case 0x00: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500837 struct segoff_s so = GET_IVT(0x1f);
838 regs->es = so.seg;
839 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400840 break;
841 }
842 case 0x01: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500843 struct segoff_s so = GET_IVT(0x43);
844 regs->es = so.seg;
845 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400846 break;
847 }
848 case 0x02:
849 regs->es = get_global_seg();
850 regs->bp = (u32)vgafont14;
851 break;
852 case 0x03:
853 regs->es = get_global_seg();
854 regs->bp = (u32)vgafont8;
855 break;
856 case 0x04:
857 regs->es = get_global_seg();
858 regs->bp = (u32)vgafont8 + 128 * 8;
859 break;
860 case 0x05:
861 regs->es = get_global_seg();
862 regs->bp = (u32)vgafont14alt;
863 break;
864 case 0x06:
865 regs->es = get_global_seg();
866 regs->bp = (u32)vgafont16;
867 break;
868 case 0x07:
869 regs->es = get_global_seg();
870 regs->bp = (u32)vgafont16alt;
871 break;
872 default:
873 dprintf(1, "Get font info BH(%02x) was discarded\n", regs->bh);
874 return;
875 }
876 // Set byte/char of on screen font
877 regs->cx = GET_BDA(char_height) & 0xff;
878
879 // Set Highest char row
Kevin O'Connor4c85a262012-02-12 11:50:52 -0500880 regs->dl = GET_BDA(video_rows);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400881}
882
883static void
884handle_1011XX(struct bregs *regs)
885{
886 debug_stub(regs);
887}
888
889static void
890handle_1011(struct bregs *regs)
891{
Kevin O'Connorefbf4d62014-02-09 11:50:21 -0500892 if (CONFIG_VGA_STDVGA_PORTS) {
893 switch (regs->al) {
Kevin O'Connorc58799c2014-05-28 09:09:05 -0400894 case 0x00: handle_101100(regs); return;
895 case 0x01: handle_101101(regs); return;
896 case 0x02: handle_101102(regs); return;
897 case 0x03: handle_101103(regs); return;
898 case 0x04: handle_101104(regs); return;
899 case 0x10: handle_101110(regs); return;
900 case 0x11: handle_101111(regs); return;
901 case 0x12: handle_101112(regs); return;
902 case 0x14: handle_101114(regs); return;
Kevin O'Connorefbf4d62014-02-09 11:50:21 -0500903 }
904 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400905 switch (regs->al) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400906 case 0x30: handle_101130(regs); break;
Paolo Bonzini4bd8aeb2013-01-10 13:41:36 +0100907 case 0x20: handle_101120(regs); break;
908 case 0x21: handle_101121(regs); break;
909 case 0x22: handle_101122(regs); break;
910 case 0x23: handle_101123(regs); break;
911 case 0x24: handle_101124(regs); break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400912 default: handle_1011XX(regs); break;
913 }
914}
915
916
917static void
918handle_101210(struct bregs *regs)
919{
Kevin O'Connore7132042009-05-25 00:10:35 -0400920 u16 crtc_addr = GET_BDA(crtc_address);
921 if (crtc_addr == VGAREG_MDA_CRTC_ADDRESS)
922 regs->bx = 0x0103;
923 else
924 regs->bx = 0x0003;
925 regs->cx = GET_BDA(video_switches) & 0x0f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400926}
927
928static void
929handle_101230(struct bregs *regs)
930{
Kevin O'Connore7132042009-05-25 00:10:35 -0400931 u8 mctl = GET_BDA(modeset_ctl);
932 u8 vswt = GET_BDA(video_switches);
933 switch (regs->al) {
934 case 0x00:
935 // 200 lines
936 mctl = (mctl & ~0x10) | 0x80;
937 vswt = (vswt & ~0x0f) | 0x08;
938 break;
939 case 0x01:
940 // 350 lines
941 mctl &= ~0x90;
942 vswt = (vswt & ~0x0f) | 0x09;
943 break;
944 case 0x02:
945 // 400 lines
946 mctl = (mctl & ~0x80) | 0x10;
947 vswt = (vswt & ~0x0f) | 0x09;
948 break;
949 default:
950 dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
951 break;
952 }
953 SET_BDA(modeset_ctl, mctl);
954 SET_BDA(video_switches, vswt);
955 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400956}
957
958static void
959handle_101231(struct bregs *regs)
960{
Kevin O'Connore7132042009-05-25 00:10:35 -0400961 u8 v = (regs->al & 0x01) << 3;
962 u8 mctl = GET_BDA(video_ctl) & ~0x08;
963 SET_BDA(video_ctl, mctl | v);
964 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400965}
966
967static void
968handle_101232(struct bregs *regs)
969{
Kevin O'Connorefbf4d62014-02-09 11:50:21 -0500970 if (CONFIG_VGA_STDVGA_PORTS) {
971 stdvga_enable_video_addressing(regs->al);
972 regs->al = 0x12;
973 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400974}
975
976static void
977handle_101233(struct bregs *regs)
978{
Kevin O'Connore7132042009-05-25 00:10:35 -0400979 u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
980 u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
981 SET_BDA(modeset_ctl, v | v2);
982 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400983}
984
985static void
986handle_101234(struct bregs *regs)
987{
Kevin O'Connore7132042009-05-25 00:10:35 -0400988 u8 v = (regs->al & 0x01) ^ 0x01;
989 u8 v2 = GET_BDA(modeset_ctl) & ~0x01;
990 SET_BDA(modeset_ctl, v | v2);
991 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400992}
993
994static void
995handle_101235(struct bregs *regs)
996{
997 debug_stub(regs);
998 regs->al = 0x12;
999}
1000
1001static void
1002handle_101236(struct bregs *regs)
1003{
1004 debug_stub(regs);
1005 regs->al = 0x12;
1006}
1007
1008static void
1009handle_1012XX(struct bregs *regs)
1010{
1011 debug_stub(regs);
1012}
1013
1014static void
1015handle_1012(struct bregs *regs)
1016{
Kevin O'Connore91ec7c2012-01-14 16:30:49 -05001017 if (CONFIG_VGA_CIRRUS && regs->bl >= 0x80) {
1018 clext_1012(regs);
1019 return;
1020 }
1021
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001022 switch (regs->bl) {
1023 case 0x10: handle_101210(regs); break;
1024 case 0x30: handle_101230(regs); break;
1025 case 0x31: handle_101231(regs); break;
1026 case 0x32: handle_101232(regs); break;
1027 case 0x33: handle_101233(regs); break;
1028 case 0x34: handle_101234(regs); break;
1029 case 0x35: handle_101235(regs); break;
1030 case 0x36: handle_101236(regs); break;
1031 default: handle_1012XX(regs); break;
1032 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001033}
1034
1035
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001036// Write string
Kevin O'Connor0ad77f02009-05-31 20:46:43 -04001037static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001038handle_1013(struct bregs *regs)
1039{
Kevin O'Connor6ee837b2012-02-13 20:09:02 -05001040 struct cursorpos cp;
1041 if (regs->dh == 0xff)
1042 // if row=0xff special case : use current cursor position
1043 cp = get_cursor_pos(regs->bh);
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001044 else
Kevin O'Connor6ee837b2012-02-13 20:09:02 -05001045 cp = (struct cursorpos) {regs->dl, regs->dh, regs->bh};
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001046
Kevin O'Connor6ee837b2012-02-13 20:09:02 -05001047 u16 count = regs->cx;
1048 u8 *offset_far = (void*)(regs->bp + 0);
1049 u8 attr = regs->bl;
1050 while (count--) {
1051 u8 car = GET_FARVAR(regs->es, *offset_far);
1052 offset_far++;
1053 if (regs->al & 2) {
1054 attr = GET_FARVAR(regs->es, *offset_far);
1055 offset_far++;
1056 }
1057
1058 struct carattr ca = {car, attr, 1};
1059 write_teletype(&cp, ca);
1060 }
1061
1062 if (regs->al & 1)
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001063 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001064}
1065
1066
1067static void
1068handle_101a00(struct bregs *regs)
1069{
Kevin O'Connore7132042009-05-25 00:10:35 -04001070 regs->bx = GET_BDA(dcc_index);
1071 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001072}
1073
1074static void
1075handle_101a01(struct bregs *regs)
1076{
Kevin O'Connore7132042009-05-25 00:10:35 -04001077 SET_BDA(dcc_index, regs->bl);
1078 dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
1079 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001080}
1081
1082static void
1083handle_101aXX(struct bregs *regs)
1084{
1085 debug_stub(regs);
1086}
1087
1088static void
1089handle_101a(struct bregs *regs)
1090{
1091 switch (regs->al) {
1092 case 0x00: handle_101a00(regs); break;
1093 case 0x01: handle_101a01(regs); break;
1094 default: handle_101aXX(regs); break;
1095 }
1096}
1097
1098
Kevin O'Connorefb45232012-01-14 22:55:11 -05001099static u8 static_functionality[0x10] VAR16 = {
1100 /* 0 */ 0xff, // All modes supported #1
1101 /* 1 */ 0xe0, // All modes supported #2
1102 /* 2 */ 0x0f, // All modes supported #3
1103 /* 3 */ 0x00, 0x00, 0x00, 0x00, // reserved
1104 /* 7 */ 0x07, // 200, 350, 400 scan lines
1105 /* 8 */ 0x02, // mamimum number of visible charsets in text mode
1106 /* 9 */ 0x08, // total number of charset blocks in text mode
1107 /* a */ 0xe7, // Change to add new functions
1108 /* b */ 0x0c, // Change to add new functions
1109 /* c */ 0x00, // reserved
1110 /* d */ 0x00, // reserved
1111 /* e */ 0x00, // Change to add new functions
1112 /* f */ 0x00 // reserved
1113};
1114
Kevin O'Connorca668642009-05-21 23:06:08 -04001115struct funcInfo {
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001116 struct segoff_s static_functionality;
Kevin O'Connorca668642009-05-21 23:06:08 -04001117 u8 bda_0x49[30];
1118 u8 bda_0x84[3];
1119 u8 dcc_index;
1120 u8 dcc_alt;
1121 u16 colors;
1122 u8 pages;
1123 u8 scan_lines;
1124 u8 primary_char;
1125 u8 secondar_char;
1126 u8 misc;
1127 u8 non_vga_mode;
1128 u8 reserved_2f[2];
1129 u8 video_mem;
1130 u8 save_flags;
1131 u8 disp_info;
1132 u8 reserved_34[12];
Kevin O'Connorf5ec1e02014-02-05 18:49:44 -05001133} PACKED;
Kevin O'Connorca668642009-05-21 23:06:08 -04001134
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001135static void
1136handle_101b(struct bregs *regs)
1137{
Kevin O'Connorca668642009-05-21 23:06:08 -04001138 u16 seg = regs->es;
1139 struct funcInfo *info = (void*)(regs->di+0);
1140 memset_far(seg, info, 0, sizeof(*info));
1141 // Address of static functionality table
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001142 SET_FARVAR(seg, info->static_functionality
1143 , SEGOFF(get_global_seg(), (u32)static_functionality));
Kevin O'Connorca668642009-05-21 23:06:08 -04001144
1145 // Hard coded copy from BIOS area. Should it be cleaner ?
Kevin O'Connor9f985422009-09-09 11:34:39 -04001146 memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49
1147 , sizeof(info->bda_0x49));
1148 memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84
1149 , sizeof(info->bda_0x84));
Kevin O'Connorca668642009-05-21 23:06:08 -04001150
1151 SET_FARVAR(seg, info->dcc_index, GET_BDA(dcc_index));
1152 SET_FARVAR(seg, info->colors, 16);
1153 SET_FARVAR(seg, info->pages, 8);
1154 SET_FARVAR(seg, info->scan_lines, 2);
1155 SET_FARVAR(seg, info->video_mem, 3);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001156 regs->al = 0x1B;
1157}
1158
1159
1160static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001161handle_101c(struct bregs *regs)
1162{
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001163 u16 seg = regs->es;
1164 void *data = (void*)(regs->bx+0);
1165 u16 states = regs->cx;
Kevin O'Connor20dc4192014-02-05 20:52:25 -05001166 u8 cmd = regs->al;
1167 if (states & ~0x07 || cmd > 2)
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001168 goto fail;
Kevin O'Connor20dc4192014-02-05 20:52:25 -05001169 int ret = vgahw_save_restore(states | (cmd<<8), seg, data);
1170 if (ret < 0)
1171 goto fail;
1172 if (cmd == 0)
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001173 regs->bx = ret / 64;
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001174 regs->al = 0x1c;
1175fail:
1176 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001177}
1178
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001179static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001180handle_10XX(struct bregs *regs)
1181{
1182 debug_stub(regs);
1183}
1184
1185// INT 10h Video Support Service Entry Point
1186void VISIBLE16
1187handle_10(struct bregs *regs)
1188{
1189 debug_enter(regs, DEBUG_VGA_10);
1190 switch (regs->ah) {
1191 case 0x00: handle_1000(regs); break;
1192 case 0x01: handle_1001(regs); break;
1193 case 0x02: handle_1002(regs); break;
1194 case 0x03: handle_1003(regs); break;
1195 case 0x04: handle_1004(regs); break;
1196 case 0x05: handle_1005(regs); break;
1197 case 0x06: handle_1006(regs); break;
1198 case 0x07: handle_1007(regs); break;
1199 case 0x08: handle_1008(regs); break;
1200 case 0x09: handle_1009(regs); break;
1201 case 0x0a: handle_100a(regs); break;
1202 case 0x0b: handle_100b(regs); break;
1203 case 0x0c: handle_100c(regs); break;
1204 case 0x0d: handle_100d(regs); break;
1205 case 0x0e: handle_100e(regs); break;
1206 case 0x0f: handle_100f(regs); break;
1207 case 0x10: handle_1010(regs); break;
1208 case 0x11: handle_1011(regs); break;
1209 case 0x12: handle_1012(regs); break;
1210 case 0x13: handle_1013(regs); break;
1211 case 0x1a: handle_101a(regs); break;
1212 case 0x1b: handle_101b(regs); break;
1213 case 0x1c: handle_101c(regs); break;
1214 case 0x4f: handle_104f(regs); break;
1215 default: handle_10XX(regs); break;
1216 }
1217}