blob: 58e467d70c41f58b2af5037ebc57adddfc154dfc [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'Connor8cf8f8e2012-01-16 19:05:27 -050018#include "pci.h" // pci_config_readw
19#include "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
Julian Pidancet7c6509c2011-12-19 05:07:55 +000040struct pci_data rom_pci_data VAR16VISIBLE = {
41 .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'Connor821d6b42011-12-31 18:19:22 -0500423 int flags = 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{
513 struct cursorpos cp = get_cursor_pos(page);
514 while (count--) {
515 vgafb_write_char(cp, ca);
516 cp.x++;
517 }
518}
519
520static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400521handle_1009(struct bregs *regs)
522{
Kevin O'Connor09262412009-05-25 11:44:11 -0400523 struct carattr ca = {regs->al, regs->bl, 1};
Kevin O'Connord3b38152009-05-26 00:05:37 -0400524 write_chars(regs->bh, ca, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400525}
526
527static void
528handle_100a(struct bregs *regs)
529{
Kevin O'Connor09262412009-05-25 11:44:11 -0400530 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connord3b38152009-05-26 00:05:37 -0400531 write_chars(regs->bh, ca, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400532}
533
534
535static void
536handle_100b00(struct bregs *regs)
537{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500538 stdvga_set_border_color(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400539}
540
541static void
542handle_100b01(struct bregs *regs)
543{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500544 stdvga_set_palette(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400545}
546
547static void
548handle_100bXX(struct bregs *regs)
549{
550 debug_stub(regs);
551}
552
553static void
554handle_100b(struct bregs *regs)
555{
556 switch (regs->bh) {
557 case 0x00: handle_100b00(regs); break;
558 case 0x01: handle_100b01(regs); break;
559 default: handle_100bXX(regs); break;
560 }
561}
562
563
564static void
565handle_100c(struct bregs *regs)
566{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400567 // XXX - page (regs->bh) is unused
568 vgafb_write_pixel(regs->al, regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400569}
570
571static void
572handle_100d(struct bregs *regs)
573{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400574 // XXX - page (regs->bh) is unused
575 regs->al = vgafb_read_pixel(regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400576}
577
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400578static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400579handle_100e(struct bregs *regs)
580{
581 // Ralf Brown Interrupt list is WRONG on bh(page)
582 // We do output only on the current page !
Kevin O'Connor09262412009-05-25 11:44:11 -0400583 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connor116a0442009-05-26 00:47:32 -0400584 struct cursorpos cp = get_cursor_pos(0xff);
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400585 write_teletype(&cp, ca);
Kevin O'Connor116a0442009-05-26 00:47:32 -0400586 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400587}
588
589static void
590handle_100f(struct bregs *regs)
591{
Kevin O'Connore7132042009-05-25 00:10:35 -0400592 regs->bh = GET_BDA(video_page);
593 regs->al = GET_BDA(video_mode) | (GET_BDA(video_ctl) & 0x80);
594 regs->ah = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400595}
596
597
598static void
599handle_101000(struct bregs *regs)
600{
601 if (regs->bl > 0x14)
602 return;
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500603 stdvga_attr_write(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400604}
605
606static void
607handle_101001(struct bregs *regs)
608{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500609 stdvga_set_overscan_border_color(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400610}
611
612static void
613handle_101002(struct bregs *regs)
614{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500615 stdvga_set_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400616}
617
618static void
619handle_101003(struct bregs *regs)
620{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500621 stdvga_toggle_intensity(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400622}
623
624static void
625handle_101007(struct bregs *regs)
626{
627 if (regs->bl > 0x14)
628 return;
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500629 regs->bh = stdvga_attr_read(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400630}
631
632static void
633handle_101008(struct bregs *regs)
634{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500635 regs->bh = stdvga_get_overscan_border_color();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400636}
637
638static void
639handle_101009(struct bregs *regs)
640{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500641 stdvga_get_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400642}
643
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400644static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400645handle_101010(struct bregs *regs)
646{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400647 u8 rgb[3] = {regs->dh, regs->ch, regs->cl};
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500648 stdvga_dac_write(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400649}
650
651static void
652handle_101012(struct bregs *regs)
653{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500654 stdvga_dac_write(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400655}
656
657static void
658handle_101013(struct bregs *regs)
659{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500660 stdvga_select_video_dac_color_page(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400661}
662
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400663static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400664handle_101015(struct bregs *regs)
665{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400666 u8 rgb[3];
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500667 stdvga_dac_read(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400668 regs->dh = rgb[0];
669 regs->ch = rgb[1];
670 regs->cl = rgb[2];
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400671}
672
673static void
674handle_101017(struct bregs *regs)
675{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500676 stdvga_dac_read(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400677}
678
679static void
680handle_101018(struct bregs *regs)
681{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500682 stdvga_pelmask_write(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400683}
684
685static void
686handle_101019(struct bregs *regs)
687{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500688 regs->bl = stdvga_pelmask_read();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400689}
690
691static void
692handle_10101a(struct bregs *regs)
693{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500694 stdvga_read_video_dac_state(&regs->bl, &regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400695}
696
697static void
698handle_10101b(struct bregs *regs)
699{
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500700 stdvga_perform_gray_scale_summing(regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400701}
702
703static void
704handle_1010XX(struct bregs *regs)
705{
706 debug_stub(regs);
707}
708
709static void
710handle_1010(struct bregs *regs)
711{
712 switch (regs->al) {
713 case 0x00: handle_101000(regs); break;
714 case 0x01: handle_101001(regs); break;
715 case 0x02: handle_101002(regs); break;
716 case 0x03: handle_101003(regs); break;
717 case 0x07: handle_101007(regs); break;
718 case 0x08: handle_101008(regs); break;
719 case 0x09: handle_101009(regs); break;
720 case 0x10: handle_101010(regs); break;
721 case 0x12: handle_101012(regs); break;
722 case 0x13: handle_101013(regs); break;
723 case 0x15: handle_101015(regs); break;
724 case 0x17: handle_101017(regs); break;
725 case 0x18: handle_101018(regs); break;
726 case 0x19: handle_101019(regs); break;
727 case 0x1a: handle_10101a(regs); break;
728 case 0x1b: handle_10101b(regs); break;
729 default: handle_1010XX(regs); break;
730 }
731}
732
733
734static void
735handle_101100(struct bregs *regs)
736{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500737 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
738 , regs->dx, regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400739}
740
741static void
742handle_101101(struct bregs *regs)
743{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500744 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400745}
746
747static void
748handle_101102(struct bregs *regs)
749{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500750 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400751}
752
753static void
754handle_101103(struct bregs *regs)
755{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500756 stdvga_set_text_block_specifier(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400757}
758
759static void
760handle_101104(struct bregs *regs)
761{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500762 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400763}
764
765static void
766handle_101110(struct bregs *regs)
767{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500768 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
769 , regs->dx, regs->bl, regs->bh);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400770 set_scan_lines(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400771}
772
773static void
774handle_101111(struct bregs *regs)
775{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500776 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400777 set_scan_lines(14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400778}
779
780static void
781handle_101112(struct bregs *regs)
782{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500783 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400784 set_scan_lines(8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400785}
786
787static void
788handle_101114(struct bregs *regs)
789{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500790 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400791 set_scan_lines(16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400792}
793
794static void
795handle_101130(struct bregs *regs)
796{
Kevin O'Connore7132042009-05-25 00:10:35 -0400797 switch (regs->bh) {
798 case 0x00: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500799 struct segoff_s so = GET_IVT(0x1f);
800 regs->es = so.seg;
801 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400802 break;
803 }
804 case 0x01: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500805 struct segoff_s so = GET_IVT(0x43);
806 regs->es = so.seg;
807 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400808 break;
809 }
810 case 0x02:
811 regs->es = get_global_seg();
812 regs->bp = (u32)vgafont14;
813 break;
814 case 0x03:
815 regs->es = get_global_seg();
816 regs->bp = (u32)vgafont8;
817 break;
818 case 0x04:
819 regs->es = get_global_seg();
820 regs->bp = (u32)vgafont8 + 128 * 8;
821 break;
822 case 0x05:
823 regs->es = get_global_seg();
824 regs->bp = (u32)vgafont14alt;
825 break;
826 case 0x06:
827 regs->es = get_global_seg();
828 regs->bp = (u32)vgafont16;
829 break;
830 case 0x07:
831 regs->es = get_global_seg();
832 regs->bp = (u32)vgafont16alt;
833 break;
834 default:
835 dprintf(1, "Get font info BH(%02x) was discarded\n", regs->bh);
836 return;
837 }
838 // Set byte/char of on screen font
839 regs->cx = GET_BDA(char_height) & 0xff;
840
841 // Set Highest char row
Kevin O'Connor4c85a262012-02-12 11:50:52 -0500842 regs->dl = GET_BDA(video_rows);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400843}
844
845static void
846handle_1011XX(struct bregs *regs)
847{
848 debug_stub(regs);
849}
850
851static void
852handle_1011(struct bregs *regs)
853{
854 switch (regs->al) {
855 case 0x00: handle_101100(regs); break;
856 case 0x01: handle_101101(regs); break;
857 case 0x02: handle_101102(regs); break;
858 case 0x03: handle_101103(regs); break;
859 case 0x04: handle_101104(regs); break;
860 case 0x10: handle_101110(regs); break;
861 case 0x11: handle_101111(regs); break;
862 case 0x12: handle_101112(regs); break;
863 case 0x14: handle_101114(regs); break;
864 case 0x30: handle_101130(regs); break;
865 default: handle_1011XX(regs); break;
866 }
867}
868
869
870static void
871handle_101210(struct bregs *regs)
872{
Kevin O'Connore7132042009-05-25 00:10:35 -0400873 u16 crtc_addr = GET_BDA(crtc_address);
874 if (crtc_addr == VGAREG_MDA_CRTC_ADDRESS)
875 regs->bx = 0x0103;
876 else
877 regs->bx = 0x0003;
878 regs->cx = GET_BDA(video_switches) & 0x0f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400879}
880
881static void
882handle_101230(struct bregs *regs)
883{
Kevin O'Connore7132042009-05-25 00:10:35 -0400884 u8 mctl = GET_BDA(modeset_ctl);
885 u8 vswt = GET_BDA(video_switches);
886 switch (regs->al) {
887 case 0x00:
888 // 200 lines
889 mctl = (mctl & ~0x10) | 0x80;
890 vswt = (vswt & ~0x0f) | 0x08;
891 break;
892 case 0x01:
893 // 350 lines
894 mctl &= ~0x90;
895 vswt = (vswt & ~0x0f) | 0x09;
896 break;
897 case 0x02:
898 // 400 lines
899 mctl = (mctl & ~0x80) | 0x10;
900 vswt = (vswt & ~0x0f) | 0x09;
901 break;
902 default:
903 dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
904 break;
905 }
906 SET_BDA(modeset_ctl, mctl);
907 SET_BDA(video_switches, vswt);
908 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400909}
910
911static void
912handle_101231(struct bregs *regs)
913{
Kevin O'Connore7132042009-05-25 00:10:35 -0400914 u8 v = (regs->al & 0x01) << 3;
915 u8 mctl = GET_BDA(video_ctl) & ~0x08;
916 SET_BDA(video_ctl, mctl | v);
917 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400918}
919
920static void
921handle_101232(struct bregs *regs)
922{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500923 stdvga_enable_video_addressing(regs->al);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400924 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400925}
926
927static void
928handle_101233(struct bregs *regs)
929{
Kevin O'Connore7132042009-05-25 00:10:35 -0400930 u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
931 u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
932 SET_BDA(modeset_ctl, v | v2);
933 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400934}
935
936static void
937handle_101234(struct bregs *regs)
938{
Kevin O'Connore7132042009-05-25 00:10:35 -0400939 u8 v = (regs->al & 0x01) ^ 0x01;
940 u8 v2 = GET_BDA(modeset_ctl) & ~0x01;
941 SET_BDA(modeset_ctl, v | v2);
942 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400943}
944
945static void
946handle_101235(struct bregs *regs)
947{
948 debug_stub(regs);
949 regs->al = 0x12;
950}
951
952static void
953handle_101236(struct bregs *regs)
954{
955 debug_stub(regs);
956 regs->al = 0x12;
957}
958
959static void
960handle_1012XX(struct bregs *regs)
961{
962 debug_stub(regs);
963}
964
965static void
966handle_1012(struct bregs *regs)
967{
Kevin O'Connore91ec7c2012-01-14 16:30:49 -0500968 if (CONFIG_VGA_CIRRUS && regs->bl >= 0x80) {
969 clext_1012(regs);
970 return;
971 }
972
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400973 switch (regs->bl) {
974 case 0x10: handle_101210(regs); break;
975 case 0x30: handle_101230(regs); break;
976 case 0x31: handle_101231(regs); break;
977 case 0x32: handle_101232(regs); break;
978 case 0x33: handle_101233(regs); break;
979 case 0x34: handle_101234(regs); break;
980 case 0x35: handle_101235(regs); break;
981 case 0x36: handle_101236(regs); break;
982 default: handle_1012XX(regs); break;
983 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400984}
985
986
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400987// Write string
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400988static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400989handle_1013(struct bregs *regs)
990{
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500991 struct cursorpos cp;
992 if (regs->dh == 0xff)
993 // if row=0xff special case : use current cursor position
994 cp = get_cursor_pos(regs->bh);
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400995 else
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500996 cp = (struct cursorpos) {regs->dl, regs->dh, regs->bh};
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400997
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500998 u16 count = regs->cx;
999 u8 *offset_far = (void*)(regs->bp + 0);
1000 u8 attr = regs->bl;
1001 while (count--) {
1002 u8 car = GET_FARVAR(regs->es, *offset_far);
1003 offset_far++;
1004 if (regs->al & 2) {
1005 attr = GET_FARVAR(regs->es, *offset_far);
1006 offset_far++;
1007 }
1008
1009 struct carattr ca = {car, attr, 1};
1010 write_teletype(&cp, ca);
1011 }
1012
1013 if (regs->al & 1)
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001014 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001015}
1016
1017
1018static void
1019handle_101a00(struct bregs *regs)
1020{
Kevin O'Connore7132042009-05-25 00:10:35 -04001021 regs->bx = GET_BDA(dcc_index);
1022 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001023}
1024
1025static void
1026handle_101a01(struct bregs *regs)
1027{
Kevin O'Connore7132042009-05-25 00:10:35 -04001028 SET_BDA(dcc_index, regs->bl);
1029 dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
1030 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001031}
1032
1033static void
1034handle_101aXX(struct bregs *regs)
1035{
1036 debug_stub(regs);
1037}
1038
1039static void
1040handle_101a(struct bregs *regs)
1041{
1042 switch (regs->al) {
1043 case 0x00: handle_101a00(regs); break;
1044 case 0x01: handle_101a01(regs); break;
1045 default: handle_101aXX(regs); break;
1046 }
1047}
1048
1049
Kevin O'Connorefb45232012-01-14 22:55:11 -05001050static u8 static_functionality[0x10] VAR16 = {
1051 /* 0 */ 0xff, // All modes supported #1
1052 /* 1 */ 0xe0, // All modes supported #2
1053 /* 2 */ 0x0f, // All modes supported #3
1054 /* 3 */ 0x00, 0x00, 0x00, 0x00, // reserved
1055 /* 7 */ 0x07, // 200, 350, 400 scan lines
1056 /* 8 */ 0x02, // mamimum number of visible charsets in text mode
1057 /* 9 */ 0x08, // total number of charset blocks in text mode
1058 /* a */ 0xe7, // Change to add new functions
1059 /* b */ 0x0c, // Change to add new functions
1060 /* c */ 0x00, // reserved
1061 /* d */ 0x00, // reserved
1062 /* e */ 0x00, // Change to add new functions
1063 /* f */ 0x00 // reserved
1064};
1065
Kevin O'Connorca668642009-05-21 23:06:08 -04001066struct funcInfo {
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001067 struct segoff_s static_functionality;
Kevin O'Connorca668642009-05-21 23:06:08 -04001068 u8 bda_0x49[30];
1069 u8 bda_0x84[3];
1070 u8 dcc_index;
1071 u8 dcc_alt;
1072 u16 colors;
1073 u8 pages;
1074 u8 scan_lines;
1075 u8 primary_char;
1076 u8 secondar_char;
1077 u8 misc;
1078 u8 non_vga_mode;
1079 u8 reserved_2f[2];
1080 u8 video_mem;
1081 u8 save_flags;
1082 u8 disp_info;
1083 u8 reserved_34[12];
1084};
1085
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001086static void
1087handle_101b(struct bregs *regs)
1088{
Kevin O'Connorca668642009-05-21 23:06:08 -04001089 u16 seg = regs->es;
1090 struct funcInfo *info = (void*)(regs->di+0);
1091 memset_far(seg, info, 0, sizeof(*info));
1092 // Address of static functionality table
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001093 SET_FARVAR(seg, info->static_functionality
1094 , SEGOFF(get_global_seg(), (u32)static_functionality));
Kevin O'Connorca668642009-05-21 23:06:08 -04001095
1096 // Hard coded copy from BIOS area. Should it be cleaner ?
Kevin O'Connor9f985422009-09-09 11:34:39 -04001097 memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49
1098 , sizeof(info->bda_0x49));
1099 memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84
1100 , sizeof(info->bda_0x84));
Kevin O'Connorca668642009-05-21 23:06:08 -04001101
1102 SET_FARVAR(seg, info->dcc_index, GET_BDA(dcc_index));
1103 SET_FARVAR(seg, info->colors, 16);
1104 SET_FARVAR(seg, info->pages, 8);
1105 SET_FARVAR(seg, info->scan_lines, 2);
1106 SET_FARVAR(seg, info->video_mem, 3);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001107 regs->al = 0x1B;
1108}
1109
1110
1111static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001112handle_101c(struct bregs *regs)
1113{
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001114 u16 seg = regs->es;
1115 void *data = (void*)(regs->bx+0);
1116 u16 states = regs->cx;
1117 if (states & ~0x07)
1118 goto fail;
1119 int ret;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001120 switch (regs->al) {
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001121 case 0x00:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001122 ret = vgahw_size_state(states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001123 if (ret < 0)
1124 goto fail;
1125 regs->bx = ret / 64;
1126 break;
1127 case 0x01:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001128 ret = vgahw_save_state(seg, data, states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001129 if (ret)
1130 goto fail;
1131 break;
1132 case 0x02:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001133 ret = vgahw_restore_state(seg, data, states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001134 if (ret)
1135 goto fail;
1136 break;
1137 default:
1138 goto fail;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001139 }
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001140 regs->al = 0x1c;
1141fail:
1142 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001143}
1144
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001145static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001146handle_10XX(struct bregs *regs)
1147{
1148 debug_stub(regs);
1149}
1150
1151// INT 10h Video Support Service Entry Point
1152void VISIBLE16
1153handle_10(struct bregs *regs)
1154{
1155 debug_enter(regs, DEBUG_VGA_10);
1156 switch (regs->ah) {
1157 case 0x00: handle_1000(regs); break;
1158 case 0x01: handle_1001(regs); break;
1159 case 0x02: handle_1002(regs); break;
1160 case 0x03: handle_1003(regs); break;
1161 case 0x04: handle_1004(regs); break;
1162 case 0x05: handle_1005(regs); break;
1163 case 0x06: handle_1006(regs); break;
1164 case 0x07: handle_1007(regs); break;
1165 case 0x08: handle_1008(regs); break;
1166 case 0x09: handle_1009(regs); break;
1167 case 0x0a: handle_100a(regs); break;
1168 case 0x0b: handle_100b(regs); break;
1169 case 0x0c: handle_100c(regs); break;
1170 case 0x0d: handle_100d(regs); break;
1171 case 0x0e: handle_100e(regs); break;
1172 case 0x0f: handle_100f(regs); break;
1173 case 0x10: handle_1010(regs); break;
1174 case 0x11: handle_1011(regs); break;
1175 case 0x12: handle_1012(regs); break;
1176 case 0x13: handle_1013(regs); break;
1177 case 0x1a: handle_101a(regs); break;
1178 case 0x1b: handle_101b(regs); break;
1179 case 0x1c: handle_101c(regs); break;
1180 case 0x4f: handle_104f(regs); break;
1181 default: handle_10XX(regs); break;
1182 }
1183}
1184
1185
1186/****************************************************************
1187 * VGA post
1188 ****************************************************************/
1189
1190static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -05001191init_bios_area(void)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001192{
1193 // init detected hardware BIOS Area
1194 // set 80x25 color (not clear from RBIL but usual)
1195 u16 eqf = GET_BDA(equipment_list_flags);
1196 SET_BDA(equipment_list_flags, (eqf & 0xffcf) | 0x20);
1197
1198 // Just for the first int10 find its children
1199
1200 // the default char height
1201 SET_BDA(char_height, 0x10);
1202
1203 // Clear the screen
1204 SET_BDA(video_ctl, 0x60);
1205
1206 // Set the basic screen we have
1207 SET_BDA(video_switches, 0xf9);
1208
1209 // Set the basic modeset options
1210 SET_BDA(modeset_ctl, 0x51);
1211
1212 // Set the default MSR
1213 SET_BDA(video_msr, 0x09);
1214}
1215
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -05001216int VgaBDF VAR16 = -1;
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001217int HaveRunInit VAR16;
Kevin O'Connor161d2012011-12-31 19:42:21 -05001218
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001219void VISIBLE16
1220vga_post(struct bregs *regs)
1221{
Kevin O'Connor9d840882012-02-11 11:02:03 -05001222 debug_serial_setup();
1223 dprintf(1, "Start SeaVGABIOS (version %s)\n", VERSION);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001224 debug_enter(regs, DEBUG_VGA_POST);
1225
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001226 if (CONFIG_VGA_PCI && !GET_GLOBAL(HaveRunInit)) {
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -05001227 u16 bdf = regs->ax;
Kevin O'Connor2af8ba12012-01-29 12:17:33 -05001228 if ((pci_config_readw(bdf, PCI_VENDOR_ID)
1229 == GET_GLOBAL(rom_pci_data.vendor))
1230 && (pci_config_readw(bdf, PCI_DEVICE_ID)
1231 == GET_GLOBAL(rom_pci_data.device)))
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -05001232 SET_VGA(VgaBDF, bdf);
1233 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001234
Kevin O'Connor161d2012011-12-31 19:42:21 -05001235 int ret = vgahw_init();
1236 if (ret) {
1237 dprintf(1, "Failed to initialize VGA hardware. Exiting.\n");
1238 return;
1239 }
Kevin O'Connor4c52fb42011-12-24 00:44:07 -05001240
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001241 if (GET_GLOBAL(HaveRunInit))
1242 return;
1243
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001244 init_bios_area();
1245
Kevin O'Connoraad3b692012-01-14 23:15:40 -05001246 SET_VGA(video_save_pointer_table.videoparam
1247 , SEGOFF(get_global_seg(), (u32)video_param_table));
1248 stdvga_build_video_param();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001249
1250 extern void entry_10(void);
Kevin O'Connor9f985422009-09-09 11:34:39 -04001251 SET_IVT(0x10, SEGOFF(get_global_seg(), (u32)entry_10));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001252
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001253 SET_VGA(HaveRunInit, 1);
1254
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001255 // Fixup checksum
1256 extern u8 _rom_header_size, _rom_header_checksum;
1257 SET_VGA(_rom_header_checksum, 0);
Kevin O'Connord113a992009-05-16 21:05:02 -04001258 u8 sum = -checksum_far(get_global_seg(), 0, _rom_header_size * 512);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001259 SET_VGA(_rom_header_checksum, sum);
1260}