blob: b13d274591da9a18a5f25e37eea7572be0568099 [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{
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
800handle_101130(struct bregs *regs)
801{
Kevin O'Connore7132042009-05-25 00:10:35 -0400802 switch (regs->bh) {
803 case 0x00: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500804 struct segoff_s so = GET_IVT(0x1f);
805 regs->es = so.seg;
806 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400807 break;
808 }
809 case 0x01: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500810 struct segoff_s so = GET_IVT(0x43);
811 regs->es = so.seg;
812 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400813 break;
814 }
815 case 0x02:
816 regs->es = get_global_seg();
817 regs->bp = (u32)vgafont14;
818 break;
819 case 0x03:
820 regs->es = get_global_seg();
821 regs->bp = (u32)vgafont8;
822 break;
823 case 0x04:
824 regs->es = get_global_seg();
825 regs->bp = (u32)vgafont8 + 128 * 8;
826 break;
827 case 0x05:
828 regs->es = get_global_seg();
829 regs->bp = (u32)vgafont14alt;
830 break;
831 case 0x06:
832 regs->es = get_global_seg();
833 regs->bp = (u32)vgafont16;
834 break;
835 case 0x07:
836 regs->es = get_global_seg();
837 regs->bp = (u32)vgafont16alt;
838 break;
839 default:
840 dprintf(1, "Get font info BH(%02x) was discarded\n", regs->bh);
841 return;
842 }
843 // Set byte/char of on screen font
844 regs->cx = GET_BDA(char_height) & 0xff;
845
846 // Set Highest char row
Kevin O'Connor4c85a262012-02-12 11:50:52 -0500847 regs->dl = GET_BDA(video_rows);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400848}
849
850static void
851handle_1011XX(struct bregs *regs)
852{
853 debug_stub(regs);
854}
855
856static void
857handle_1011(struct bregs *regs)
858{
859 switch (regs->al) {
860 case 0x00: handle_101100(regs); break;
861 case 0x01: handle_101101(regs); break;
862 case 0x02: handle_101102(regs); break;
863 case 0x03: handle_101103(regs); break;
864 case 0x04: handle_101104(regs); break;
865 case 0x10: handle_101110(regs); break;
866 case 0x11: handle_101111(regs); break;
867 case 0x12: handle_101112(regs); break;
868 case 0x14: handle_101114(regs); break;
869 case 0x30: handle_101130(regs); break;
870 default: handle_1011XX(regs); break;
871 }
872}
873
874
875static void
876handle_101210(struct bregs *regs)
877{
Kevin O'Connore7132042009-05-25 00:10:35 -0400878 u16 crtc_addr = GET_BDA(crtc_address);
879 if (crtc_addr == VGAREG_MDA_CRTC_ADDRESS)
880 regs->bx = 0x0103;
881 else
882 regs->bx = 0x0003;
883 regs->cx = GET_BDA(video_switches) & 0x0f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400884}
885
886static void
887handle_101230(struct bregs *regs)
888{
Kevin O'Connore7132042009-05-25 00:10:35 -0400889 u8 mctl = GET_BDA(modeset_ctl);
890 u8 vswt = GET_BDA(video_switches);
891 switch (regs->al) {
892 case 0x00:
893 // 200 lines
894 mctl = (mctl & ~0x10) | 0x80;
895 vswt = (vswt & ~0x0f) | 0x08;
896 break;
897 case 0x01:
898 // 350 lines
899 mctl &= ~0x90;
900 vswt = (vswt & ~0x0f) | 0x09;
901 break;
902 case 0x02:
903 // 400 lines
904 mctl = (mctl & ~0x80) | 0x10;
905 vswt = (vswt & ~0x0f) | 0x09;
906 break;
907 default:
908 dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
909 break;
910 }
911 SET_BDA(modeset_ctl, mctl);
912 SET_BDA(video_switches, vswt);
913 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400914}
915
916static void
917handle_101231(struct bregs *regs)
918{
Kevin O'Connore7132042009-05-25 00:10:35 -0400919 u8 v = (regs->al & 0x01) << 3;
920 u8 mctl = GET_BDA(video_ctl) & ~0x08;
921 SET_BDA(video_ctl, mctl | v);
922 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400923}
924
925static void
926handle_101232(struct bregs *regs)
927{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500928 stdvga_enable_video_addressing(regs->al);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400929 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400930}
931
932static void
933handle_101233(struct bregs *regs)
934{
Kevin O'Connore7132042009-05-25 00:10:35 -0400935 u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
936 u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
937 SET_BDA(modeset_ctl, v | v2);
938 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400939}
940
941static void
942handle_101234(struct bregs *regs)
943{
Kevin O'Connore7132042009-05-25 00:10:35 -0400944 u8 v = (regs->al & 0x01) ^ 0x01;
945 u8 v2 = GET_BDA(modeset_ctl) & ~0x01;
946 SET_BDA(modeset_ctl, v | v2);
947 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400948}
949
950static void
951handle_101235(struct bregs *regs)
952{
953 debug_stub(regs);
954 regs->al = 0x12;
955}
956
957static void
958handle_101236(struct bregs *regs)
959{
960 debug_stub(regs);
961 regs->al = 0x12;
962}
963
964static void
965handle_1012XX(struct bregs *regs)
966{
967 debug_stub(regs);
968}
969
970static void
971handle_1012(struct bregs *regs)
972{
Kevin O'Connore91ec7c2012-01-14 16:30:49 -0500973 if (CONFIG_VGA_CIRRUS && regs->bl >= 0x80) {
974 clext_1012(regs);
975 return;
976 }
977
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400978 switch (regs->bl) {
979 case 0x10: handle_101210(regs); break;
980 case 0x30: handle_101230(regs); break;
981 case 0x31: handle_101231(regs); break;
982 case 0x32: handle_101232(regs); break;
983 case 0x33: handle_101233(regs); break;
984 case 0x34: handle_101234(regs); break;
985 case 0x35: handle_101235(regs); break;
986 case 0x36: handle_101236(regs); break;
987 default: handle_1012XX(regs); break;
988 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400989}
990
991
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400992// Write string
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400993static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400994handle_1013(struct bregs *regs)
995{
Kevin O'Connor6ee837b2012-02-13 20:09:02 -0500996 struct cursorpos cp;
997 if (regs->dh == 0xff)
998 // if row=0xff special case : use current cursor position
999 cp = get_cursor_pos(regs->bh);
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001000 else
Kevin O'Connor6ee837b2012-02-13 20:09:02 -05001001 cp = (struct cursorpos) {regs->dl, regs->dh, regs->bh};
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001002
Kevin O'Connor6ee837b2012-02-13 20:09:02 -05001003 u16 count = regs->cx;
1004 u8 *offset_far = (void*)(regs->bp + 0);
1005 u8 attr = regs->bl;
1006 while (count--) {
1007 u8 car = GET_FARVAR(regs->es, *offset_far);
1008 offset_far++;
1009 if (regs->al & 2) {
1010 attr = GET_FARVAR(regs->es, *offset_far);
1011 offset_far++;
1012 }
1013
1014 struct carattr ca = {car, attr, 1};
1015 write_teletype(&cp, ca);
1016 }
1017
1018 if (regs->al & 1)
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001019 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001020}
1021
1022
1023static void
1024handle_101a00(struct bregs *regs)
1025{
Kevin O'Connore7132042009-05-25 00:10:35 -04001026 regs->bx = GET_BDA(dcc_index);
1027 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001028}
1029
1030static void
1031handle_101a01(struct bregs *regs)
1032{
Kevin O'Connore7132042009-05-25 00:10:35 -04001033 SET_BDA(dcc_index, regs->bl);
1034 dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
1035 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001036}
1037
1038static void
1039handle_101aXX(struct bregs *regs)
1040{
1041 debug_stub(regs);
1042}
1043
1044static void
1045handle_101a(struct bregs *regs)
1046{
1047 switch (regs->al) {
1048 case 0x00: handle_101a00(regs); break;
1049 case 0x01: handle_101a01(regs); break;
1050 default: handle_101aXX(regs); break;
1051 }
1052}
1053
1054
Kevin O'Connorefb45232012-01-14 22:55:11 -05001055static u8 static_functionality[0x10] VAR16 = {
1056 /* 0 */ 0xff, // All modes supported #1
1057 /* 1 */ 0xe0, // All modes supported #2
1058 /* 2 */ 0x0f, // All modes supported #3
1059 /* 3 */ 0x00, 0x00, 0x00, 0x00, // reserved
1060 /* 7 */ 0x07, // 200, 350, 400 scan lines
1061 /* 8 */ 0x02, // mamimum number of visible charsets in text mode
1062 /* 9 */ 0x08, // total number of charset blocks in text mode
1063 /* a */ 0xe7, // Change to add new functions
1064 /* b */ 0x0c, // Change to add new functions
1065 /* c */ 0x00, // reserved
1066 /* d */ 0x00, // reserved
1067 /* e */ 0x00, // Change to add new functions
1068 /* f */ 0x00 // reserved
1069};
1070
Kevin O'Connorca668642009-05-21 23:06:08 -04001071struct funcInfo {
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001072 struct segoff_s static_functionality;
Kevin O'Connorca668642009-05-21 23:06:08 -04001073 u8 bda_0x49[30];
1074 u8 bda_0x84[3];
1075 u8 dcc_index;
1076 u8 dcc_alt;
1077 u16 colors;
1078 u8 pages;
1079 u8 scan_lines;
1080 u8 primary_char;
1081 u8 secondar_char;
1082 u8 misc;
1083 u8 non_vga_mode;
1084 u8 reserved_2f[2];
1085 u8 video_mem;
1086 u8 save_flags;
1087 u8 disp_info;
1088 u8 reserved_34[12];
1089};
1090
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001091static void
1092handle_101b(struct bregs *regs)
1093{
Kevin O'Connorca668642009-05-21 23:06:08 -04001094 u16 seg = regs->es;
1095 struct funcInfo *info = (void*)(regs->di+0);
1096 memset_far(seg, info, 0, sizeof(*info));
1097 // Address of static functionality table
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001098 SET_FARVAR(seg, info->static_functionality
1099 , SEGOFF(get_global_seg(), (u32)static_functionality));
Kevin O'Connorca668642009-05-21 23:06:08 -04001100
1101 // Hard coded copy from BIOS area. Should it be cleaner ?
Kevin O'Connor9f985422009-09-09 11:34:39 -04001102 memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49
1103 , sizeof(info->bda_0x49));
1104 memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84
1105 , sizeof(info->bda_0x84));
Kevin O'Connorca668642009-05-21 23:06:08 -04001106
1107 SET_FARVAR(seg, info->dcc_index, GET_BDA(dcc_index));
1108 SET_FARVAR(seg, info->colors, 16);
1109 SET_FARVAR(seg, info->pages, 8);
1110 SET_FARVAR(seg, info->scan_lines, 2);
1111 SET_FARVAR(seg, info->video_mem, 3);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001112 regs->al = 0x1B;
1113}
1114
1115
1116static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001117handle_101c(struct bregs *regs)
1118{
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001119 u16 seg = regs->es;
1120 void *data = (void*)(regs->bx+0);
1121 u16 states = regs->cx;
1122 if (states & ~0x07)
1123 goto fail;
1124 int ret;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001125 switch (regs->al) {
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001126 case 0x00:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001127 ret = vgahw_size_state(states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001128 if (ret < 0)
1129 goto fail;
1130 regs->bx = ret / 64;
1131 break;
1132 case 0x01:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001133 ret = vgahw_save_state(seg, data, states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001134 if (ret)
1135 goto fail;
1136 break;
1137 case 0x02:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001138 ret = vgahw_restore_state(seg, data, states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001139 if (ret)
1140 goto fail;
1141 break;
1142 default:
1143 goto fail;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001144 }
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001145 regs->al = 0x1c;
1146fail:
1147 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001148}
1149
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001150static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001151handle_10XX(struct bregs *regs)
1152{
1153 debug_stub(regs);
1154}
1155
1156// INT 10h Video Support Service Entry Point
1157void VISIBLE16
1158handle_10(struct bregs *regs)
1159{
1160 debug_enter(regs, DEBUG_VGA_10);
1161 switch (regs->ah) {
1162 case 0x00: handle_1000(regs); break;
1163 case 0x01: handle_1001(regs); break;
1164 case 0x02: handle_1002(regs); break;
1165 case 0x03: handle_1003(regs); break;
1166 case 0x04: handle_1004(regs); break;
1167 case 0x05: handle_1005(regs); break;
1168 case 0x06: handle_1006(regs); break;
1169 case 0x07: handle_1007(regs); break;
1170 case 0x08: handle_1008(regs); break;
1171 case 0x09: handle_1009(regs); break;
1172 case 0x0a: handle_100a(regs); break;
1173 case 0x0b: handle_100b(regs); break;
1174 case 0x0c: handle_100c(regs); break;
1175 case 0x0d: handle_100d(regs); break;
1176 case 0x0e: handle_100e(regs); break;
1177 case 0x0f: handle_100f(regs); break;
1178 case 0x10: handle_1010(regs); break;
1179 case 0x11: handle_1011(regs); break;
1180 case 0x12: handle_1012(regs); break;
1181 case 0x13: handle_1013(regs); break;
1182 case 0x1a: handle_101a(regs); break;
1183 case 0x1b: handle_101b(regs); break;
1184 case 0x1c: handle_101c(regs); break;
1185 case 0x4f: handle_104f(regs); break;
1186 default: handle_10XX(regs); break;
1187 }
1188}
1189
1190
1191/****************************************************************
1192 * VGA post
1193 ****************************************************************/
1194
1195static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -05001196init_bios_area(void)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001197{
1198 // init detected hardware BIOS Area
1199 // set 80x25 color (not clear from RBIL but usual)
Kevin O'Connore51316d2012-06-10 09:09:22 -04001200 set_equipment_flags(0x30, 0x20);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001201
1202 // the default char height
1203 SET_BDA(char_height, 0x10);
1204
1205 // Clear the screen
1206 SET_BDA(video_ctl, 0x60);
1207
1208 // Set the basic screen we have
1209 SET_BDA(video_switches, 0xf9);
1210
1211 // Set the basic modeset options
1212 SET_BDA(modeset_ctl, 0x51);
1213
1214 // Set the default MSR
1215 SET_BDA(video_msr, 0x09);
1216}
1217
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -05001218int VgaBDF VAR16 = -1;
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001219int HaveRunInit VAR16;
Kevin O'Connor161d2012011-12-31 19:42:21 -05001220
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001221void VISIBLE16
1222vga_post(struct bregs *regs)
1223{
Kevin O'Connor9d840882012-02-11 11:02:03 -05001224 debug_serial_setup();
1225 dprintf(1, "Start SeaVGABIOS (version %s)\n", VERSION);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001226 debug_enter(regs, DEBUG_VGA_POST);
1227
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001228 if (CONFIG_VGA_PCI && !GET_GLOBAL(HaveRunInit)) {
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -05001229 u16 bdf = regs->ax;
Kevin O'Connor2af8ba12012-01-29 12:17:33 -05001230 if ((pci_config_readw(bdf, PCI_VENDOR_ID)
1231 == GET_GLOBAL(rom_pci_data.vendor))
1232 && (pci_config_readw(bdf, PCI_DEVICE_ID)
1233 == GET_GLOBAL(rom_pci_data.device)))
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -05001234 SET_VGA(VgaBDF, bdf);
1235 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001236
Kevin O'Connor161d2012011-12-31 19:42:21 -05001237 int ret = vgahw_init();
1238 if (ret) {
1239 dprintf(1, "Failed to initialize VGA hardware. Exiting.\n");
1240 return;
1241 }
Kevin O'Connor4c52fb42011-12-24 00:44:07 -05001242
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001243 if (GET_GLOBAL(HaveRunInit))
1244 return;
1245
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001246 init_bios_area();
1247
Kevin O'Connoraad3b692012-01-14 23:15:40 -05001248 SET_VGA(video_save_pointer_table.videoparam
1249 , SEGOFF(get_global_seg(), (u32)video_param_table));
1250 stdvga_build_video_param();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001251
1252 extern void entry_10(void);
Kevin O'Connor9f985422009-09-09 11:34:39 -04001253 SET_IVT(0x10, SEGOFF(get_global_seg(), (u32)entry_10));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001254
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001255 SET_VGA(HaveRunInit, 1);
1256
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001257 // Fixup checksum
1258 extern u8 _rom_header_size, _rom_header_checksum;
1259 SET_VGA(_rom_header_checksum, 0);
Julian Pidanceta23a9512012-03-05 14:20:45 +00001260 u8 sum = -checksum_far(get_global_seg(), 0,
1261 GET_GLOBAL(_rom_header_size) * 512);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001262 SET_VGA(_rom_header_checksum, sum);
1263}