blob: 506dd8d7b34b2d22ea4a0733cbcf72cf8c189573 [file] [log] [blame]
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001// VGA bios implementation
2//
Kevin O'Connor4c85a262012-02-12 11:50:52 -05003// Copyright (C) 2009-2012 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 "bregs.h" // struct bregs
9#include "biosvar.h" // GET_BDA
10#include "util.h" // memset
Kevin O'Connor10dff3d2012-01-09 19:19:44 -050011#include "vgabios.h" // calc_page_size
Julian Pidancet7c6509c2011-12-19 05:07:55 +000012#include "optionroms.h" // struct pci_data
13#include "config.h" // CONFIG_*
Kevin O'Connor10dff3d2012-01-09 19:19:44 -050014#include "stdvga.h" // stdvga_set_cursor_shape
Kevin O'Connore91ec7c2012-01-14 16:30:49 -050015#include "clext.h" // clext_1012
Kevin O'Connor5108c692011-12-31 19:13:45 -050016#include "vgahw.h" // vgahw_set_mode
Kevin O'Connore6bc4c12012-01-21 11:26:37 -050017#include "vbe.h" // VBE_RETURN_STATUS_FAILED
Kevin O'Connor5d369d82013-09-02 20:48:46 -040018#include "hw/pci.h" // pci_config_readw
19#include "hw/pci_regs.h" // PCI_VENDOR_ID
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040020
Kevin O'Connoraad3b692012-01-14 23:15:40 -050021// Standard Video Save Pointer Table
22struct VideoSavePointer_s {
23 struct segoff_s videoparam;
24 struct segoff_s paramdynamicsave;
25 struct segoff_s textcharset;
26 struct segoff_s graphcharset;
27 struct segoff_s secsavepointer;
28 u8 reserved[8];
29} PACKED;
30
31static struct VideoSavePointer_s video_save_pointer_table VAR16;
32
33struct VideoParam_s video_param_table[29] VAR16;
34
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040035
Julian Pidancet7c6509c2011-12-19 05:07:55 +000036/****************************************************************
37 * PCI Data
38 ****************************************************************/
Kevin O'Connor4c85a262012-02-12 11:50:52 -050039
Kevin O'Connor89a2f962013-02-18 23:36:03 -050040struct pci_data rom_pci_data VAR16 VISIBLE16 = {
Julian Pidancet7c6509c2011-12-19 05:07:55 +000041 .signature = PCI_ROM_SIGNATURE,
42 .vendor = CONFIG_VGA_VID,
43 .device = CONFIG_VGA_DID,
44 .dlen = 0x18,
45 .class_hi = 0x300,
46 .irevision = 1,
47 .type = PCIROM_CODETYPE_X86,
48 .indicator = 0x80,
49};
Kevin O'Connor4c85a262012-02-12 11:50:52 -050050
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040051
52/****************************************************************
53 * Helper functions
54 ****************************************************************/
55
Kevin O'Connor3876b532012-01-24 00:07:44 -050056// Return the bits per pixel in system memory for a given mode.
57int
58vga_bpp(struct vgamode_s *vmode_g)
59{
60 switch (GET_GLOBAL(vmode_g->memmodel)) {
61 case MM_TEXT:
62 return 16;
63 case MM_PLANAR:
64 return 1;
65 }
66 u8 depth = GET_GLOBAL(vmode_g->depth);
67 if (depth > 8)
68 return ALIGN(depth, 8);
69 return depth;
70}
71
Kevin O'Connor83047be2012-01-07 18:27:19 -050072u16
73calc_page_size(u8 memmodel, u16 width, u16 height)
74{
75 switch (memmodel) {
76 case MM_TEXT:
77 return ALIGN(width * height * 2, 2*1024);
78 case MM_CGA:
79 return 16*1024;
80 default:
81 return ALIGN(width * height / 8, 8*1024);
82 }
83}
84
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040085static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040086set_cursor_shape(u8 start, u8 end)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040087{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040088 start &= 0x3f;
89 end &= 0x1f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040090
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040091 u16 curs = (start << 8) + end;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040092 SET_BDA(cursor_type, curs);
93
Kevin O'Connordd2be772009-05-16 15:41:23 -040094 u8 modeset_ctl = GET_BDA(modeset_ctl);
95 u16 cheight = GET_BDA(char_height);
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040096 if ((modeset_ctl & 0x01) && (cheight > 8) && (end < 8) && (start < 0x20)) {
97 if (end != (start + 1))
98 start = ((start + 1) * cheight / 8) - 1;
Kevin O'Connordd2be772009-05-16 15:41:23 -040099 else
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400100 start = ((end + 1) * cheight / 8) - 2;
101 end = ((end + 1) * cheight / 8) - 1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400102 }
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500103 stdvga_set_cursor_shape(start, end);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400104}
105
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400106static u16
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400107get_cursor_shape(u8 page)
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400108{
109 if (page > 7)
110 return 0;
111 // FIXME should handle VGA 14/16 lines
112 return GET_BDA(cursor_type);
113}
114
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400115static void
Kevin O'Connor918b1562009-05-25 11:05:18 -0400116set_cursor_pos(struct cursorpos cp)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400117{
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500118 u8 page = cp.page, x = cp.x, y = cp.y;
119
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400120 // Should not happen...
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500121 if (page > 7)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400122 return;
123
124 // Bios cursor pos
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500125 SET_BDA(cursor_pos[page], (y << 8) | x);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400126
127 // Set the hardware cursor
Kevin O'Connordd2be772009-05-16 15:41:23 -0400128 u8 current = GET_BDA(video_page);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400129 if (cp.page != current)
Kevin O'Connordd2be772009-05-16 15:41:23 -0400130 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400131
Kevin O'Connor83047be2012-01-07 18:27:19 -0500132 // Calculate the memory address
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500133 int address = (GET_BDA(video_pagesize) * page
134 + (x + y * GET_BDA(video_cols)) * 2);
Kevin O'Connor16920072012-01-27 22:59:46 -0500135 stdvga_set_cursor_pos(address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400136}
137
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400138static struct cursorpos
Kevin O'Connor918b1562009-05-25 11:05:18 -0400139get_cursor_pos(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400140{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400141 if (page == 0xff)
142 // special case - use current page
143 page = GET_BDA(video_page);
144 if (page > 7) {
145 struct cursorpos cp = { 0, 0, 0xfe };
146 return cp;
147 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400148 // FIXME should handle VGA 14/16 lines
Kevin O'Connor918b1562009-05-25 11:05:18 -0400149 u16 xy = GET_BDA(cursor_pos[page]);
150 struct cursorpos cp = {xy, xy>>8, page};
151 return cp;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400152}
153
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400154static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400155set_active_page(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400156{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400157 if (page > 7)
158 return;
159
160 // Get the mode
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500161 struct vgamode_s *vmode_g = get_current_mode();
Kevin O'Connor5727c292009-05-16 17:29:32 -0400162 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400163 return;
164
Kevin O'Connor4c85a262012-02-12 11:50:52 -0500165 // Get cursor pos for the given page
Kevin O'Connor918b1562009-05-25 11:05:18 -0400166 struct cursorpos cp = get_cursor_pos(page);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400167
Kevin O'Connor83047be2012-01-07 18:27:19 -0500168 // Calculate memory address of start of page
Kevin O'Connord61fc532012-01-27 20:37:45 -0500169 int address = GET_BDA(video_pagesize) * page;
170 vgahw_set_displaystart(vmode_g, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400171
172 // And change the BIOS page
Kevin O'Connor83047be2012-01-07 18:27:19 -0500173 SET_BDA(video_pagestart, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400174 SET_BDA(video_page, page);
175
Kevin O'Connora12c2152009-05-13 22:06:16 -0400176 dprintf(1, "Set active page %02x address %04x\n", page, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400177
178 // Display the cursor, now the page is active
Kevin O'Connor918b1562009-05-25 11:05:18 -0400179 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400180}
181
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400182static void
183set_scan_lines(u8 lines)
184{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500185 stdvga_set_scan_lines(lines);
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400186 if (lines == 8)
187 set_cursor_shape(0x06, 0x07);
188 else
189 set_cursor_shape(lines - 4, lines - 3);
190 SET_BDA(char_height, lines);
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500191 u16 vde = stdvga_get_vde();
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400192 u8 rows = vde / lines;
193 SET_BDA(video_rows, rows - 1);
194 u16 cols = GET_BDA(video_cols);
Kevin O'Connor83047be2012-01-07 18:27:19 -0500195 SET_BDA(video_pagesize, calc_page_size(MM_TEXT, cols, rows));
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400196}
197
198
199/****************************************************************
200 * Character writing
201 ****************************************************************/
202
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400203// Scroll the screen one line. This function is designed to be called
204// tail-recursive to reduce stack usage.
205static void noinline
206scroll_one(u16 nbrows, u16 nbcols, u8 page)
Kevin O'Connor09262412009-05-25 11:44:11 -0400207{
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400208 struct cursorpos ul = {0, 0, page};
209 struct cursorpos lr = {nbcols-1, nbrows-1, page};
210 vgafb_scroll(1, -1, ul, lr);
211}
212
213// Write a character to the screen at a given position. Implement
214// special characters and scroll the screen if necessary.
215static void
216write_teletype(struct cursorpos *pcp, struct carattr ca)
217{
218 struct cursorpos cp = *pcp;
219
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400220 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -0400221 u16 nbrows = GET_BDA(video_rows) + 1;
222 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400223
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400224 switch (ca.car) {
225 case 7:
226 //FIXME should beep
227 break;
228 case 8:
229 if (cp.x > 0)
230 cp.x--;
231 break;
232 case '\r':
233 cp.x = 0;
234 break;
235 case '\n':
236 cp.y++;
237 break;
238 case '\t':
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500239 ca.car = ' ';
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400240 do {
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500241 vgafb_write_char(cp, ca);
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400242 cp.x++;
243 } while (cp.x < nbcols && cp.x % 8);
244 break;
245 default:
246 vgafb_write_char(cp, ca);
247 cp.x++;
248 }
249
Kevin O'Connor82221b22009-05-26 00:20:40 -0400250 // Do we need to wrap ?
251 if (cp.x == nbcols) {
252 cp.x = 0;
253 cp.y++;
254 }
255 // Do we need to scroll ?
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400256 if (cp.y < nbrows) {
257 *pcp = cp;
258 return;
Kevin O'Connor82221b22009-05-26 00:20:40 -0400259 }
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400260 // Scroll screen
261 cp.y--;
262 *pcp = cp;
263 scroll_one(nbrows, nbcols, cp.page);
Kevin O'Connor82221b22009-05-26 00:20:40 -0400264}
265
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400266
267/****************************************************************
268 * Save and restore bda state
269 ****************************************************************/
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400270
Kevin O'Connor9f857fc2012-02-04 11:59:02 -0500271void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400272save_bda_state(u16 seg, struct saveBDAstate *info)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400273{
Kevin O'Connor2469f892012-02-04 12:40:02 -0500274 SET_FARVAR(seg, info->video_mode, GET_BDA(vbe_mode));
Kevin O'Connorca668642009-05-21 23:06:08 -0400275 SET_FARVAR(seg, info->video_cols, GET_BDA(video_cols));
276 SET_FARVAR(seg, info->video_pagesize, GET_BDA(video_pagesize));
277 SET_FARVAR(seg, info->crtc_address, GET_BDA(crtc_address));
278 SET_FARVAR(seg, info->video_rows, GET_BDA(video_rows));
279 SET_FARVAR(seg, info->char_height, GET_BDA(char_height));
280 SET_FARVAR(seg, info->video_ctl, GET_BDA(video_ctl));
281 SET_FARVAR(seg, info->video_switches, GET_BDA(video_switches));
282 SET_FARVAR(seg, info->modeset_ctl, GET_BDA(modeset_ctl));
283 SET_FARVAR(seg, info->cursor_type, GET_BDA(cursor_type));
Kevin O'Connor9f857fc2012-02-04 11:59:02 -0500284 int i;
Kevin O'Connorca668642009-05-21 23:06:08 -0400285 for (i=0; i<8; i++)
286 SET_FARVAR(seg, info->cursor_pos[i], GET_BDA(cursor_pos[i]));
287 SET_FARVAR(seg, info->video_pagestart, GET_BDA(video_pagestart));
288 SET_FARVAR(seg, info->video_page, GET_BDA(video_page));
289 /* current font */
Kevin O'Connor9f985422009-09-09 11:34:39 -0400290 SET_FARVAR(seg, info->font0, GET_IVT(0x1f));
291 SET_FARVAR(seg, info->font1, GET_IVT(0x43));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400292}
293
Kevin O'Connor9f857fc2012-02-04 11:59:02 -0500294void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400295restore_bda_state(u16 seg, struct saveBDAstate *info)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400296{
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500297 u16 mode = GET_FARVAR(seg, info->video_mode);
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500298 SET_BDA(vbe_mode, mode);
Kevin O'Connor2469f892012-02-04 12:40:02 -0500299 if (mode < 0x100)
300 SET_BDA(video_mode, mode);
301 else
302 SET_BDA(video_mode, 0xff);
Kevin O'Connorca668642009-05-21 23:06:08 -0400303 SET_BDA(video_cols, GET_FARVAR(seg, info->video_cols));
304 SET_BDA(video_pagesize, GET_FARVAR(seg, info->video_pagesize));
305 SET_BDA(crtc_address, GET_FARVAR(seg, info->crtc_address));
306 SET_BDA(video_rows, GET_FARVAR(seg, info->video_rows));
307 SET_BDA(char_height, GET_FARVAR(seg, info->char_height));
308 SET_BDA(video_ctl, GET_FARVAR(seg, info->video_ctl));
309 SET_BDA(video_switches, GET_FARVAR(seg, info->video_switches));
310 SET_BDA(modeset_ctl, GET_FARVAR(seg, info->modeset_ctl));
311 SET_BDA(cursor_type, GET_FARVAR(seg, info->cursor_type));
Kevin O'Connor9f857fc2012-02-04 11:59:02 -0500312 int i;
Kevin O'Connorca668642009-05-21 23:06:08 -0400313 for (i = 0; i < 8; i++)
314 SET_BDA(cursor_pos[i], GET_FARVAR(seg, info->cursor_pos[i]));
315 SET_BDA(video_pagestart, GET_FARVAR(seg, info->video_pagestart));
316 SET_BDA(video_page, GET_FARVAR(seg, info->video_page));
317 /* current font */
Kevin O'Connor9f985422009-09-09 11:34:39 -0400318 SET_IVT(0x1f, GET_FARVAR(seg, info->font0));
319 SET_IVT(0x43, GET_FARVAR(seg, info->font1));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400320}
321
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500322
323/****************************************************************
324 * Mode setting
325 ****************************************************************/
326
327struct vgamode_s *
328get_current_mode(void)
329{
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500330 return vgahw_find_mode(GET_BDA(vbe_mode) & ~MF_VBEFLAGS);
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500331}
332
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500333// Setup BDA after a mode switch.
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500334int
335vga_set_mode(int mode, int flags)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400336{
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500337 dprintf(1, "set VGA mode %x\n", mode);
338 struct vgamode_s *vmode_g = vgahw_find_mode(mode);
339 if (!vmode_g)
340 return VBE_RETURN_STATUS_FAILED;
341
342 int ret = vgahw_set_mode(vmode_g, flags);
343 if (ret)
344 return ret;
345
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400346 // Set the BIOS mem
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500347 int width = GET_GLOBAL(vmode_g->width);
348 int height = GET_GLOBAL(vmode_g->height);
Kevin O'Connor83047be2012-01-07 18:27:19 -0500349 u8 memmodel = GET_GLOBAL(vmode_g->memmodel);
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500350 int cheight = GET_GLOBAL(vmode_g->cheight);
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500351 if (mode < 0x100)
352 SET_BDA(video_mode, mode);
353 else
354 SET_BDA(video_mode, 0xff);
355 SET_BDA(vbe_mode, mode | (flags & MF_VBEFLAGS));
Kevin O'Connor83047be2012-01-07 18:27:19 -0500356 if (memmodel == MM_TEXT) {
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500357 SET_BDA(video_cols, width);
358 SET_BDA(video_rows, height-1);
359 SET_BDA(cursor_type, 0x0607);
360 } else {
361 int cwidth = GET_GLOBAL(vmode_g->cwidth);
362 SET_BDA(video_cols, width / cwidth);
363 SET_BDA(video_rows, (height / cheight) - 1);
364 SET_BDA(cursor_type, 0x0000);
365 }
Kevin O'Connor83047be2012-01-07 18:27:19 -0500366 SET_BDA(video_pagesize, calc_page_size(memmodel, width, height));
Kevin O'Connorcecbc5d2011-12-31 17:24:11 -0500367 SET_BDA(crtc_address, stdvga_get_crtc());
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400368 SET_BDA(char_height, cheight);
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500369 SET_BDA(video_ctl, 0x60 | (flags & MF_NOCLEARMEM ? 0x80 : 0x00));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400370 SET_BDA(video_switches, 0xF9);
371 SET_BDA(modeset_ctl, GET_BDA(modeset_ctl) & 0x7f);
Kevin O'Connorcecbc5d2011-12-31 17:24:11 -0500372 int i;
373 for (i=0; i<8; i++)
374 SET_BDA(cursor_pos[i], 0x0000);
375 SET_BDA(video_pagestart, 0x0000);
376 SET_BDA(video_page, 0x00);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400377
378 // FIXME We nearly have the good tables. to be reworked
379 SET_BDA(dcc_index, 0x08); // 8 is VGA should be ok for now
Kevin O'Connor9f985422009-09-09 11:34:39 -0400380 SET_BDA(video_savetable
Kevin O'Connor815e4472011-12-21 09:05:32 -0500381 , SEGOFF(get_global_seg(), (u32)&video_save_pointer_table));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400382
383 // FIXME
384 SET_BDA(video_msr, 0x00); // Unavailable on vanilla vga, but...
385 SET_BDA(video_pal, 0x00); // Unavailable on vanilla vga, but...
386
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400387 // Set the ints 0x1F and 0x43
Kevin O'Connor9f985422009-09-09 11:34:39 -0400388 SET_IVT(0x1f, SEGOFF(get_global_seg(), (u32)&vgafont8[128 * 8]));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400389
390 switch (cheight) {
391 case 8:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400392 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont8));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400393 break;
394 case 14:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400395 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont14));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400396 break;
397 case 16:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400398 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont16));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400399 break;
400 }
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500401
402 return 0;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400403}
404
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500405
406/****************************************************************
407 * VGA int 10 handler
408 ****************************************************************/
409
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400410static void
Julian Pidancet87879e22011-12-19 05:08:00 +0000411handle_1000(struct bregs *regs)
412{
Kevin O'Connor5108c692011-12-31 19:13:45 -0500413 int mode = regs->al & 0x7f;
Julian Pidancet87879e22011-12-19 05:08:00 +0000414
415 // Set regs->al
416 if (mode > 7)
417 regs->al = 0x20;
418 else if (mode == 6)
419 regs->al = 0x3f;
420 else
421 regs->al = 0x30;
422
Kevin O'Connorb7b92932013-03-09 13:04:47 -0500423 int flags = MF_LEGACY | (GET_BDA(modeset_ctl) & (MF_NOPALETTE|MF_GRAYSUM));
Kevin O'Connor5108c692011-12-31 19:13:45 -0500424 if (regs->al & 0x80)
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500425 flags |= MF_NOCLEARMEM;
426
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500427 vga_set_mode(mode, flags);
Julian Pidancet87879e22011-12-19 05:08:00 +0000428}
429
430static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400431handle_1001(struct bregs *regs)
432{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400433 set_cursor_shape(regs->ch, regs->cl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400434}
435
436static void
437handle_1002(struct bregs *regs)
438{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400439 struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
440 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400441}
442
443static void
444handle_1003(struct bregs *regs)
445{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400446 regs->cx = get_cursor_shape(regs->bh);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400447 struct cursorpos cp = get_cursor_pos(regs->bh);
448 regs->dl = cp.x;
449 regs->dh = cp.y;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400450}
451
452// Read light pen pos (unimplemented)
453static void
454handle_1004(struct bregs *regs)
455{
456 debug_stub(regs);
457 regs->ax = regs->bx = regs->cx = regs->dx = 0;
458}
459
460static void
461handle_1005(struct bregs *regs)
462{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400463 set_active_page(regs->al);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400464}
465
466static void
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400467verify_scroll(struct bregs *regs, int dir)
468{
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500469 u8 ulx = regs->cl, uly = regs->ch, lrx = regs->dl, lry = regs->dh;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400470 u16 nbrows = GET_BDA(video_rows) + 1;
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500471 if (lry >= nbrows)
472 lry = nbrows - 1;
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400473 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500474 if (lrx >= nbcols)
475 lrx = nbcols - 1;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400476
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500477 if (ulx > lrx || uly > lry)
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400478 return;
479
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500480 int nblines = regs->al;
481 if (!nblines || nblines > lry - uly + 1)
482 nblines = lry - uly + 1;
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400483
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500484 u8 page = GET_BDA(video_page);
485 struct cursorpos ul = {ulx, uly, page};
486 struct cursorpos lr = {lrx, lry, page};
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400487 vgafb_scroll(dir * nblines, regs->bh, ul, lr);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400488}
489
490static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400491handle_1006(struct bregs *regs)
492{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400493 verify_scroll(regs, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400494}
495
496static void
497handle_1007(struct bregs *regs)
498{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400499 verify_scroll(regs, -1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400500}
501
502static void
503handle_1008(struct bregs *regs)
504{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400505 struct carattr ca = vgafb_read_char(get_cursor_pos(regs->bh));
Kevin O'Connor09262412009-05-25 11:44:11 -0400506 regs->al = ca.car;
507 regs->ah = ca.attr;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400508}
509
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400510static void noinline
Kevin O'Connord3b38152009-05-26 00:05:37 -0400511write_chars(u8 page, struct carattr ca, u16 count)
512{
Kevin O'Connor7b975e52012-03-05 17:11:50 -0500513 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connord3b38152009-05-26 00:05:37 -0400514 struct cursorpos cp = get_cursor_pos(page);
515 while (count--) {
516 vgafb_write_char(cp, ca);
517 cp.x++;
Kevin O'Connor7b975e52012-03-05 17:11:50 -0500518 if (cp.x >= nbcols) {
519 cp.x -= nbcols;
520 cp.y++;
521 }
Kevin O'Connord3b38152009-05-26 00:05:37 -0400522 }
523}
524
525static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400526handle_1009(struct bregs *regs)
527{
Kevin O'Connor09262412009-05-25 11:44:11 -0400528 struct carattr ca = {regs->al, regs->bl, 1};
Kevin O'Connord3b38152009-05-26 00:05:37 -0400529 write_chars(regs->bh, ca, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400530}
531
532static void
533handle_100a(struct bregs *regs)
534{
Kevin O'Connor09262412009-05-25 11:44:11 -0400535 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connord3b38152009-05-26 00:05:37 -0400536 write_chars(regs->bh, ca, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400537}
538
539
540static void
541handle_100b00(struct bregs *regs)
542{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500543 stdvga_set_border_color(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400544}
545
546static void
547handle_100b01(struct bregs *regs)
548{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500549 stdvga_set_palette(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400550}
551
552static void
553handle_100bXX(struct bregs *regs)
554{
555 debug_stub(regs);
556}
557
558static void
559handle_100b(struct bregs *regs)
560{
561 switch (regs->bh) {
562 case 0x00: handle_100b00(regs); break;
563 case 0x01: handle_100b01(regs); break;
564 default: handle_100bXX(regs); break;
565 }
566}
567
568
569static void
570handle_100c(struct bregs *regs)
571{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400572 // XXX - page (regs->bh) is unused
573 vgafb_write_pixel(regs->al, regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400574}
575
576static void
577handle_100d(struct bregs *regs)
578{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400579 // XXX - page (regs->bh) is unused
580 regs->al = vgafb_read_pixel(regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400581}
582
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400583static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400584handle_100e(struct bregs *regs)
585{
586 // Ralf Brown Interrupt list is WRONG on bh(page)
587 // We do output only on the current page !
Kevin O'Connor09262412009-05-25 11:44:11 -0400588 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connor116a0442009-05-26 00:47:32 -0400589 struct cursorpos cp = get_cursor_pos(0xff);
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400590 write_teletype(&cp, ca);
Kevin O'Connor116a0442009-05-26 00:47:32 -0400591 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400592}
593
594static void
595handle_100f(struct bregs *regs)
596{
Kevin O'Connore7132042009-05-25 00:10:35 -0400597 regs->bh = GET_BDA(video_page);
598 regs->al = GET_BDA(video_mode) | (GET_BDA(video_ctl) & 0x80);
599 regs->ah = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400600}
601
602
603static void
604handle_101000(struct bregs *regs)
605{
606 if (regs->bl > 0x14)
607 return;
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500608 stdvga_attr_write(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400609}
610
611static void
612handle_101001(struct bregs *regs)
613{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500614 stdvga_set_overscan_border_color(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400615}
616
617static void
618handle_101002(struct bregs *regs)
619{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500620 stdvga_set_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400621}
622
623static void
624handle_101003(struct bregs *regs)
625{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500626 stdvga_toggle_intensity(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400627}
628
629static void
630handle_101007(struct bregs *regs)
631{
632 if (regs->bl > 0x14)
633 return;
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500634 regs->bh = stdvga_attr_read(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400635}
636
637static void
638handle_101008(struct bregs *regs)
639{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500640 regs->bh = stdvga_get_overscan_border_color();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400641}
642
643static void
644handle_101009(struct bregs *regs)
645{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500646 stdvga_get_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400647}
648
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400649static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400650handle_101010(struct bregs *regs)
651{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400652 u8 rgb[3] = {regs->dh, regs->ch, regs->cl};
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500653 stdvga_dac_write(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400654}
655
656static void
657handle_101012(struct bregs *regs)
658{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500659 stdvga_dac_write(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400660}
661
662static void
663handle_101013(struct bregs *regs)
664{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500665 stdvga_select_video_dac_color_page(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400666}
667
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400668static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400669handle_101015(struct bregs *regs)
670{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400671 u8 rgb[3];
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500672 stdvga_dac_read(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400673 regs->dh = rgb[0];
674 regs->ch = rgb[1];
675 regs->cl = rgb[2];
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400676}
677
678static void
679handle_101017(struct bregs *regs)
680{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500681 stdvga_dac_read(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400682}
683
684static void
685handle_101018(struct bregs *regs)
686{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500687 stdvga_pelmask_write(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400688}
689
690static void
691handle_101019(struct bregs *regs)
692{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500693 regs->bl = stdvga_pelmask_read();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400694}
695
696static void
697handle_10101a(struct bregs *regs)
698{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500699 stdvga_read_video_dac_state(&regs->bl, &regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400700}
701
702static void
703handle_10101b(struct bregs *regs)
704{
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500705 stdvga_perform_gray_scale_summing(regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400706}
707
708static void
709handle_1010XX(struct bregs *regs)
710{
711 debug_stub(regs);
712}
713
714static void
715handle_1010(struct bregs *regs)
716{
717 switch (regs->al) {
718 case 0x00: handle_101000(regs); break;
719 case 0x01: handle_101001(regs); break;
720 case 0x02: handle_101002(regs); break;
721 case 0x03: handle_101003(regs); break;
722 case 0x07: handle_101007(regs); break;
723 case 0x08: handle_101008(regs); break;
724 case 0x09: handle_101009(regs); break;
725 case 0x10: handle_101010(regs); break;
726 case 0x12: handle_101012(regs); break;
727 case 0x13: handle_101013(regs); break;
728 case 0x15: handle_101015(regs); break;
729 case 0x17: handle_101017(regs); break;
730 case 0x18: handle_101018(regs); break;
731 case 0x19: handle_101019(regs); break;
732 case 0x1a: handle_10101a(regs); break;
733 case 0x1b: handle_10101b(regs); break;
734 default: handle_1010XX(regs); break;
735 }
736}
737
738
739static void
740handle_101100(struct bregs *regs)
741{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500742 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
743 , regs->dx, regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400744}
745
746static void
747handle_101101(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'Connor1f2c3072009-05-06 23:35:59 -0400750}
751
752static void
753handle_101102(struct bregs *regs)
754{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500755 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400756}
757
758static void
759handle_101103(struct bregs *regs)
760{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500761 stdvga_set_text_block_specifier(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400762}
763
764static void
765handle_101104(struct bregs *regs)
766{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500767 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400768}
769
770static void
771handle_101110(struct bregs *regs)
772{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500773 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
774 , regs->dx, regs->bl, regs->bh);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400775 set_scan_lines(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400776}
777
778static void
779handle_101111(struct bregs *regs)
780{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500781 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400782 set_scan_lines(14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400783}
784
785static void
786handle_101112(struct bregs *regs)
787{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500788 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400789 set_scan_lines(8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400790}
791
792static void
793handle_101114(struct bregs *regs)
794{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500795 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400796 set_scan_lines(16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400797}
798
799static void
Paolo Bonzini4bd8aeb2013-01-10 13:41:36 +0100800handle_101120(struct bregs *regs)
801{
802 SET_IVT(0x1f, SEGOFF(regs->es, regs->bp));
803}
804
805void
806load_gfx_font(u16 seg, u16 off, u8 height, u8 bl, u8 dl)
807{
808 u8 rows;
809
810 SET_IVT(0x43, SEGOFF(seg, off));
811 switch(bl) {
812 case 0:
813 rows = dl;
814 break;
815 case 1:
816 rows = 14;
817 break;
818 case 3:
819 rows = 43;
820 break;
821 case 2:
822 default:
823 rows = 25;
824 break;
825 }
826 SET_BDA(video_rows, rows - 1);
827 SET_BDA(char_height, height);
828}
829
830static void
831handle_101121(struct bregs *regs)
832{
833 load_gfx_font(regs->es, regs->bp, regs->cx, regs->bl, regs->dl);
834}
835
836static void
837handle_101122(struct bregs *regs)
838{
839 load_gfx_font(get_global_seg(), (u32)vgafont14, 14, regs->bl, regs->dl);
840}
841
842static void
843handle_101123(struct bregs *regs)
844{
845 load_gfx_font(get_global_seg(), (u32)vgafont8, 8, regs->bl, regs->dl);
846}
847
848static void
849handle_101124(struct bregs *regs)
850{
851 load_gfx_font(get_global_seg(), (u32)vgafont16, 16, regs->bl, regs->dl);
852}
853
854static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400855handle_101130(struct bregs *regs)
856{
Kevin O'Connore7132042009-05-25 00:10:35 -0400857 switch (regs->bh) {
858 case 0x00: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500859 struct segoff_s so = GET_IVT(0x1f);
860 regs->es = so.seg;
861 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400862 break;
863 }
864 case 0x01: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500865 struct segoff_s so = GET_IVT(0x43);
866 regs->es = so.seg;
867 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400868 break;
869 }
870 case 0x02:
871 regs->es = get_global_seg();
872 regs->bp = (u32)vgafont14;
873 break;
874 case 0x03:
875 regs->es = get_global_seg();
876 regs->bp = (u32)vgafont8;
877 break;
878 case 0x04:
879 regs->es = get_global_seg();
880 regs->bp = (u32)vgafont8 + 128 * 8;
881 break;
882 case 0x05:
883 regs->es = get_global_seg();
884 regs->bp = (u32)vgafont14alt;
885 break;
886 case 0x06:
887 regs->es = get_global_seg();
888 regs->bp = (u32)vgafont16;
889 break;
890 case 0x07:
891 regs->es = get_global_seg();
892 regs->bp = (u32)vgafont16alt;
893 break;
894 default:
895 dprintf(1, "Get font info BH(%02x) was discarded\n", regs->bh);
896 return;
897 }
898 // Set byte/char of on screen font
899 regs->cx = GET_BDA(char_height) & 0xff;
900
901 // Set Highest char row
Kevin O'Connor4c85a262012-02-12 11:50:52 -0500902 regs->dl = GET_BDA(video_rows);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400903}
904
905static void
906handle_1011XX(struct bregs *regs)
907{
908 debug_stub(regs);
909}
910
911static void
912handle_1011(struct bregs *regs)
913{
914 switch (regs->al) {
915 case 0x00: handle_101100(regs); break;
916 case 0x01: handle_101101(regs); break;
917 case 0x02: handle_101102(regs); break;
918 case 0x03: handle_101103(regs); break;
919 case 0x04: handle_101104(regs); break;
920 case 0x10: handle_101110(regs); break;
921 case 0x11: handle_101111(regs); break;
922 case 0x12: handle_101112(regs); break;
923 case 0x14: handle_101114(regs); break;
924 case 0x30: handle_101130(regs); break;
Paolo Bonzini4bd8aeb2013-01-10 13:41:36 +0100925 case 0x20: handle_101120(regs); break;
926 case 0x21: handle_101121(regs); break;
927 case 0x22: handle_101122(regs); break;
928 case 0x23: handle_101123(regs); break;
929 case 0x24: handle_101124(regs); break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400930 default: handle_1011XX(regs); break;
931 }
932}
933
934
935static void
936handle_101210(struct bregs *regs)
937{
Kevin O'Connore7132042009-05-25 00:10:35 -0400938 u16 crtc_addr = GET_BDA(crtc_address);
939 if (crtc_addr == VGAREG_MDA_CRTC_ADDRESS)
940 regs->bx = 0x0103;
941 else
942 regs->bx = 0x0003;
943 regs->cx = GET_BDA(video_switches) & 0x0f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400944}
945
946static void
947handle_101230(struct bregs *regs)
948{
Kevin O'Connore7132042009-05-25 00:10:35 -0400949 u8 mctl = GET_BDA(modeset_ctl);
950 u8 vswt = GET_BDA(video_switches);
951 switch (regs->al) {
952 case 0x00:
953 // 200 lines
954 mctl = (mctl & ~0x10) | 0x80;
955 vswt = (vswt & ~0x0f) | 0x08;
956 break;
957 case 0x01:
958 // 350 lines
959 mctl &= ~0x90;
960 vswt = (vswt & ~0x0f) | 0x09;
961 break;
962 case 0x02:
963 // 400 lines
964 mctl = (mctl & ~0x80) | 0x10;
965 vswt = (vswt & ~0x0f) | 0x09;
966 break;
967 default:
968 dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
969 break;
970 }
971 SET_BDA(modeset_ctl, mctl);
972 SET_BDA(video_switches, vswt);
973 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400974}
975
976static void
977handle_101231(struct bregs *regs)
978{
Kevin O'Connore7132042009-05-25 00:10:35 -0400979 u8 v = (regs->al & 0x01) << 3;
980 u8 mctl = GET_BDA(video_ctl) & ~0x08;
981 SET_BDA(video_ctl, mctl | v);
982 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400983}
984
985static void
986handle_101232(struct bregs *regs)
987{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500988 stdvga_enable_video_addressing(regs->al);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400989 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400990}
991
992static void
993handle_101233(struct bregs *regs)
994{
Kevin O'Connore7132042009-05-25 00:10:35 -0400995 u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
996 u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
997 SET_BDA(modeset_ctl, v | v2);
998 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400999}
1000
1001static void
1002handle_101234(struct bregs *regs)
1003{
Kevin O'Connore7132042009-05-25 00:10:35 -04001004 u8 v = (regs->al & 0x01) ^ 0x01;
1005 u8 v2 = GET_BDA(modeset_ctl) & ~0x01;
1006 SET_BDA(modeset_ctl, v | v2);
1007 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001008}
1009
1010static void
1011handle_101235(struct bregs *regs)
1012{
1013 debug_stub(regs);
1014 regs->al = 0x12;
1015}
1016
1017static void
1018handle_101236(struct bregs *regs)
1019{
1020 debug_stub(regs);
1021 regs->al = 0x12;
1022}
1023
1024static void
1025handle_1012XX(struct bregs *regs)
1026{
1027 debug_stub(regs);
1028}
1029
1030static void
1031handle_1012(struct bregs *regs)
1032{
Kevin O'Connore91ec7c2012-01-14 16:30:49 -05001033 if (CONFIG_VGA_CIRRUS && regs->bl >= 0x80) {
1034 clext_1012(regs);
1035 return;
1036 }
1037
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001038 switch (regs->bl) {
1039 case 0x10: handle_101210(regs); break;
1040 case 0x30: handle_101230(regs); break;
1041 case 0x31: handle_101231(regs); break;
1042 case 0x32: handle_101232(regs); break;
1043 case 0x33: handle_101233(regs); break;
1044 case 0x34: handle_101234(regs); break;
1045 case 0x35: handle_101235(regs); break;
1046 case 0x36: handle_101236(regs); break;
1047 default: handle_1012XX(regs); break;
1048 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001049}
1050
1051
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001052// Write string
Kevin O'Connor0ad77f02009-05-31 20:46:43 -04001053static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001054handle_1013(struct bregs *regs)
1055{
Kevin O'Connor6ee837b2012-02-13 20:09:02 -05001056 struct cursorpos cp;
1057 if (regs->dh == 0xff)
1058 // if row=0xff special case : use current cursor position
1059 cp = get_cursor_pos(regs->bh);
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001060 else
Kevin O'Connor6ee837b2012-02-13 20:09:02 -05001061 cp = (struct cursorpos) {regs->dl, regs->dh, regs->bh};
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001062
Kevin O'Connor6ee837b2012-02-13 20:09:02 -05001063 u16 count = regs->cx;
1064 u8 *offset_far = (void*)(regs->bp + 0);
1065 u8 attr = regs->bl;
1066 while (count--) {
1067 u8 car = GET_FARVAR(regs->es, *offset_far);
1068 offset_far++;
1069 if (regs->al & 2) {
1070 attr = GET_FARVAR(regs->es, *offset_far);
1071 offset_far++;
1072 }
1073
1074 struct carattr ca = {car, attr, 1};
1075 write_teletype(&cp, ca);
1076 }
1077
1078 if (regs->al & 1)
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001079 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001080}
1081
1082
1083static void
1084handle_101a00(struct bregs *regs)
1085{
Kevin O'Connore7132042009-05-25 00:10:35 -04001086 regs->bx = GET_BDA(dcc_index);
1087 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001088}
1089
1090static void
1091handle_101a01(struct bregs *regs)
1092{
Kevin O'Connore7132042009-05-25 00:10:35 -04001093 SET_BDA(dcc_index, regs->bl);
1094 dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
1095 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001096}
1097
1098static void
1099handle_101aXX(struct bregs *regs)
1100{
1101 debug_stub(regs);
1102}
1103
1104static void
1105handle_101a(struct bregs *regs)
1106{
1107 switch (regs->al) {
1108 case 0x00: handle_101a00(regs); break;
1109 case 0x01: handle_101a01(regs); break;
1110 default: handle_101aXX(regs); break;
1111 }
1112}
1113
1114
Kevin O'Connorefb45232012-01-14 22:55:11 -05001115static u8 static_functionality[0x10] VAR16 = {
1116 /* 0 */ 0xff, // All modes supported #1
1117 /* 1 */ 0xe0, // All modes supported #2
1118 /* 2 */ 0x0f, // All modes supported #3
1119 /* 3 */ 0x00, 0x00, 0x00, 0x00, // reserved
1120 /* 7 */ 0x07, // 200, 350, 400 scan lines
1121 /* 8 */ 0x02, // mamimum number of visible charsets in text mode
1122 /* 9 */ 0x08, // total number of charset blocks in text mode
1123 /* a */ 0xe7, // Change to add new functions
1124 /* b */ 0x0c, // Change to add new functions
1125 /* c */ 0x00, // reserved
1126 /* d */ 0x00, // reserved
1127 /* e */ 0x00, // Change to add new functions
1128 /* f */ 0x00 // reserved
1129};
1130
Kevin O'Connorca668642009-05-21 23:06:08 -04001131struct funcInfo {
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001132 struct segoff_s static_functionality;
Kevin O'Connorca668642009-05-21 23:06:08 -04001133 u8 bda_0x49[30];
1134 u8 bda_0x84[3];
1135 u8 dcc_index;
1136 u8 dcc_alt;
1137 u16 colors;
1138 u8 pages;
1139 u8 scan_lines;
1140 u8 primary_char;
1141 u8 secondar_char;
1142 u8 misc;
1143 u8 non_vga_mode;
1144 u8 reserved_2f[2];
1145 u8 video_mem;
1146 u8 save_flags;
1147 u8 disp_info;
1148 u8 reserved_34[12];
1149};
1150
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001151static void
1152handle_101b(struct bregs *regs)
1153{
Kevin O'Connorca668642009-05-21 23:06:08 -04001154 u16 seg = regs->es;
1155 struct funcInfo *info = (void*)(regs->di+0);
1156 memset_far(seg, info, 0, sizeof(*info));
1157 // Address of static functionality table
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001158 SET_FARVAR(seg, info->static_functionality
1159 , SEGOFF(get_global_seg(), (u32)static_functionality));
Kevin O'Connorca668642009-05-21 23:06:08 -04001160
1161 // Hard coded copy from BIOS area. Should it be cleaner ?
Kevin O'Connor9f985422009-09-09 11:34:39 -04001162 memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49
1163 , sizeof(info->bda_0x49));
1164 memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84
1165 , sizeof(info->bda_0x84));
Kevin O'Connorca668642009-05-21 23:06:08 -04001166
1167 SET_FARVAR(seg, info->dcc_index, GET_BDA(dcc_index));
1168 SET_FARVAR(seg, info->colors, 16);
1169 SET_FARVAR(seg, info->pages, 8);
1170 SET_FARVAR(seg, info->scan_lines, 2);
1171 SET_FARVAR(seg, info->video_mem, 3);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001172 regs->al = 0x1B;
1173}
1174
1175
1176static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001177handle_101c(struct bregs *regs)
1178{
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001179 u16 seg = regs->es;
1180 void *data = (void*)(regs->bx+0);
1181 u16 states = regs->cx;
1182 if (states & ~0x07)
1183 goto fail;
1184 int ret;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001185 switch (regs->al) {
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001186 case 0x00:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001187 ret = vgahw_size_state(states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001188 if (ret < 0)
1189 goto fail;
1190 regs->bx = ret / 64;
1191 break;
1192 case 0x01:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001193 ret = vgahw_save_state(seg, data, states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001194 if (ret)
1195 goto fail;
1196 break;
1197 case 0x02:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001198 ret = vgahw_restore_state(seg, data, states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001199 if (ret)
1200 goto fail;
1201 break;
1202 default:
1203 goto fail;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001204 }
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001205 regs->al = 0x1c;
1206fail:
1207 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001208}
1209
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001210static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001211handle_10XX(struct bregs *regs)
1212{
1213 debug_stub(regs);
1214}
1215
1216// INT 10h Video Support Service Entry Point
1217void VISIBLE16
1218handle_10(struct bregs *regs)
1219{
1220 debug_enter(regs, DEBUG_VGA_10);
1221 switch (regs->ah) {
1222 case 0x00: handle_1000(regs); break;
1223 case 0x01: handle_1001(regs); break;
1224 case 0x02: handle_1002(regs); break;
1225 case 0x03: handle_1003(regs); break;
1226 case 0x04: handle_1004(regs); break;
1227 case 0x05: handle_1005(regs); break;
1228 case 0x06: handle_1006(regs); break;
1229 case 0x07: handle_1007(regs); break;
1230 case 0x08: handle_1008(regs); break;
1231 case 0x09: handle_1009(regs); break;
1232 case 0x0a: handle_100a(regs); break;
1233 case 0x0b: handle_100b(regs); break;
1234 case 0x0c: handle_100c(regs); break;
1235 case 0x0d: handle_100d(regs); break;
1236 case 0x0e: handle_100e(regs); break;
1237 case 0x0f: handle_100f(regs); break;
1238 case 0x10: handle_1010(regs); break;
1239 case 0x11: handle_1011(regs); break;
1240 case 0x12: handle_1012(regs); break;
1241 case 0x13: handle_1013(regs); break;
1242 case 0x1a: handle_101a(regs); break;
1243 case 0x1b: handle_101b(regs); break;
1244 case 0x1c: handle_101c(regs); break;
1245 case 0x4f: handle_104f(regs); break;
1246 default: handle_10XX(regs); break;
1247 }
1248}
1249
1250
1251/****************************************************************
1252 * VGA post
1253 ****************************************************************/
1254
1255static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -05001256init_bios_area(void)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001257{
1258 // init detected hardware BIOS Area
1259 // set 80x25 color (not clear from RBIL but usual)
Kevin O'Connore51316d2012-06-10 09:09:22 -04001260 set_equipment_flags(0x30, 0x20);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001261
1262 // the default char height
1263 SET_BDA(char_height, 0x10);
1264
1265 // Clear the screen
1266 SET_BDA(video_ctl, 0x60);
1267
1268 // Set the basic screen we have
1269 SET_BDA(video_switches, 0xf9);
1270
1271 // Set the basic modeset options
1272 SET_BDA(modeset_ctl, 0x51);
1273
1274 // Set the default MSR
1275 SET_BDA(video_msr, 0x09);
1276}
1277
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -05001278int VgaBDF VAR16 = -1;
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001279int HaveRunInit VAR16;
Kevin O'Connor161d2012011-12-31 19:42:21 -05001280
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001281void VISIBLE16
1282vga_post(struct bregs *regs)
1283{
Kevin O'Connord83c87b2013-01-21 01:14:12 -05001284 debug_serial_preinit();
Kevin O'Connor9d840882012-02-11 11:02:03 -05001285 dprintf(1, "Start SeaVGABIOS (version %s)\n", VERSION);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001286 debug_enter(regs, DEBUG_VGA_POST);
1287
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001288 if (CONFIG_VGA_PCI && !GET_GLOBAL(HaveRunInit)) {
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -05001289 u16 bdf = regs->ax;
Kevin O'Connor2af8ba12012-01-29 12:17:33 -05001290 if ((pci_config_readw(bdf, PCI_VENDOR_ID)
1291 == GET_GLOBAL(rom_pci_data.vendor))
1292 && (pci_config_readw(bdf, PCI_DEVICE_ID)
1293 == GET_GLOBAL(rom_pci_data.device)))
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -05001294 SET_VGA(VgaBDF, bdf);
1295 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001296
Kevin O'Connord83c87b2013-01-21 01:14:12 -05001297 int ret = vgahw_setup();
Kevin O'Connor161d2012011-12-31 19:42:21 -05001298 if (ret) {
1299 dprintf(1, "Failed to initialize VGA hardware. Exiting.\n");
1300 return;
1301 }
Kevin O'Connor4c52fb42011-12-24 00:44:07 -05001302
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001303 if (GET_GLOBAL(HaveRunInit))
1304 return;
1305
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001306 init_bios_area();
1307
Kevin O'Connoraad3b692012-01-14 23:15:40 -05001308 SET_VGA(video_save_pointer_table.videoparam
1309 , SEGOFF(get_global_seg(), (u32)video_param_table));
1310 stdvga_build_video_param();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001311
1312 extern void entry_10(void);
Kevin O'Connor9f985422009-09-09 11:34:39 -04001313 SET_IVT(0x10, SEGOFF(get_global_seg(), (u32)entry_10));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001314
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001315 SET_VGA(HaveRunInit, 1);
1316
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001317 // Fixup checksum
1318 extern u8 _rom_header_size, _rom_header_checksum;
1319 SET_VGA(_rom_header_checksum, 0);
Julian Pidanceta23a9512012-03-05 14:20:45 +00001320 u8 sum = -checksum_far(get_global_seg(), 0,
1321 GET_GLOBAL(_rom_header_size) * 512);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001322 SET_VGA(_rom_header_checksum, sum);
1323}