blob: 2f24e7828c91a99e16a8ac0f414754c2dc034473 [file] [log] [blame]
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001// VGA bios implementation
2//
3// Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
4// 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
8
9// TODO:
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040010// * review correctness of converted asm by comparing with RBIL
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040011//
Kevin O'Connor6ace78f2009-05-14 19:24:49 -040012// * convert vbe/clext code
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040013
14#include "bregs.h" // struct bregs
15#include "biosvar.h" // GET_BDA
16#include "util.h" // memset
Kevin O'Connor10dff3d2012-01-09 19:19:44 -050017#include "vgabios.h" // calc_page_size
Julian Pidancet7c6509c2011-12-19 05:07:55 +000018#include "optionroms.h" // struct pci_data
19#include "config.h" // CONFIG_*
Kevin O'Connor10dff3d2012-01-09 19:19:44 -050020#include "stdvga.h" // stdvga_set_cursor_shape
Kevin O'Connore91ec7c2012-01-14 16:30:49 -050021#include "clext.h" // clext_1012
Kevin O'Connor5108c692011-12-31 19:13:45 -050022#include "vgahw.h" // vgahw_set_mode
Kevin O'Connore6bc4c12012-01-21 11:26:37 -050023#include "vbe.h" // VBE_RETURN_STATUS_FAILED
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -050024#include "pci.h" // pci_config_readw
25#include "pci_regs.h" // PCI_VENDOR_ID
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040026
27// XXX
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040028#define DEBUG_VGA_POST 1
29#define DEBUG_VGA_10 3
30
Kevin O'Connoraad3b692012-01-14 23:15:40 -050031// Standard Video Save Pointer Table
32struct VideoSavePointer_s {
33 struct segoff_s videoparam;
34 struct segoff_s paramdynamicsave;
35 struct segoff_s textcharset;
36 struct segoff_s graphcharset;
37 struct segoff_s secsavepointer;
38 u8 reserved[8];
39} PACKED;
40
41static struct VideoSavePointer_s video_save_pointer_table VAR16;
42
43struct VideoParam_s video_param_table[29] VAR16;
44
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040045
Julian Pidancet7c6509c2011-12-19 05:07:55 +000046/****************************************************************
47 * PCI Data
48 ****************************************************************/
49#if CONFIG_VGA_PCI == 1
50struct pci_data rom_pci_data VAR16VISIBLE = {
51 .signature = PCI_ROM_SIGNATURE,
52 .vendor = CONFIG_VGA_VID,
53 .device = CONFIG_VGA_DID,
54 .dlen = 0x18,
55 .class_hi = 0x300,
56 .irevision = 1,
57 .type = PCIROM_CODETYPE_X86,
58 .indicator = 0x80,
59};
60#endif
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040061
62/****************************************************************
63 * Helper functions
64 ****************************************************************/
65
Kevin O'Connor3876b532012-01-24 00:07:44 -050066// Return the bits per pixel in system memory for a given mode.
67int
68vga_bpp(struct vgamode_s *vmode_g)
69{
70 switch (GET_GLOBAL(vmode_g->memmodel)) {
71 case MM_TEXT:
72 return 16;
73 case MM_PLANAR:
74 return 1;
75 }
76 u8 depth = GET_GLOBAL(vmode_g->depth);
77 if (depth > 8)
78 return ALIGN(depth, 8);
79 return depth;
80}
81
Kevin O'Connor83047be2012-01-07 18:27:19 -050082u16
83calc_page_size(u8 memmodel, u16 width, u16 height)
84{
85 switch (memmodel) {
86 case MM_TEXT:
87 return ALIGN(width * height * 2, 2*1024);
88 case MM_CGA:
89 return 16*1024;
90 default:
91 return ALIGN(width * height / 8, 8*1024);
92 }
93}
94
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040095static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040096set_cursor_shape(u8 start, u8 end)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040097{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040098 start &= 0x3f;
99 end &= 0x1f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400100
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400101 u16 curs = (start << 8) + end;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400102 SET_BDA(cursor_type, curs);
103
Kevin O'Connordd2be772009-05-16 15:41:23 -0400104 u8 modeset_ctl = GET_BDA(modeset_ctl);
105 u16 cheight = GET_BDA(char_height);
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400106 if ((modeset_ctl & 0x01) && (cheight > 8) && (end < 8) && (start < 0x20)) {
107 if (end != (start + 1))
108 start = ((start + 1) * cheight / 8) - 1;
Kevin O'Connordd2be772009-05-16 15:41:23 -0400109 else
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400110 start = ((end + 1) * cheight / 8) - 2;
111 end = ((end + 1) * cheight / 8) - 1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400112 }
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500113 stdvga_set_cursor_shape(start, end);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400114}
115
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400116static u16
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400117get_cursor_shape(u8 page)
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400118{
119 if (page > 7)
120 return 0;
121 // FIXME should handle VGA 14/16 lines
122 return GET_BDA(cursor_type);
123}
124
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400125static void
Kevin O'Connor918b1562009-05-25 11:05:18 -0400126set_cursor_pos(struct cursorpos cp)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400127{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400128 // Should not happen...
Kevin O'Connor918b1562009-05-25 11:05:18 -0400129 if (cp.page > 7)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400130 return;
131
132 // Bios cursor pos
Kevin O'Connor918b1562009-05-25 11:05:18 -0400133 SET_BDA(cursor_pos[cp.page], (cp.y << 8) | cp.x);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400134
135 // Set the hardware cursor
Kevin O'Connordd2be772009-05-16 15:41:23 -0400136 u8 current = GET_BDA(video_page);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400137 if (cp.page != current)
Kevin O'Connordd2be772009-05-16 15:41:23 -0400138 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400139
Kevin O'Connor83047be2012-01-07 18:27:19 -0500140 // Calculate the memory address
Kevin O'Connor16920072012-01-27 22:59:46 -0500141 int address = (GET_BDA(video_pagesize) * cp.page
142 + (cp.x + cp.y * GET_BDA(video_cols)) * 2);
143 stdvga_set_cursor_pos(address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400144}
145
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400146static struct cursorpos
Kevin O'Connor918b1562009-05-25 11:05:18 -0400147get_cursor_pos(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400148{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400149 if (page == 0xff)
150 // special case - use current page
151 page = GET_BDA(video_page);
152 if (page > 7) {
153 struct cursorpos cp = { 0, 0, 0xfe };
154 return cp;
155 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400156 // FIXME should handle VGA 14/16 lines
Kevin O'Connor918b1562009-05-25 11:05:18 -0400157 u16 xy = GET_BDA(cursor_pos[page]);
158 struct cursorpos cp = {xy, xy>>8, page};
159 return cp;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400160}
161
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400162static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400163set_active_page(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400164{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400165 if (page > 7)
166 return;
167
168 // Get the mode
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500169 struct vgamode_s *vmode_g = get_current_mode();
Kevin O'Connor5727c292009-05-16 17:29:32 -0400170 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400171 return;
172
173 // Get pos curs pos for the right page
Kevin O'Connor918b1562009-05-25 11:05:18 -0400174 struct cursorpos cp = get_cursor_pos(page);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400175
Kevin O'Connor83047be2012-01-07 18:27:19 -0500176 // Calculate memory address of start of page
Kevin O'Connord61fc532012-01-27 20:37:45 -0500177 int address = GET_BDA(video_pagesize) * page;
178 vgahw_set_displaystart(vmode_g, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400179
180 // And change the BIOS page
Kevin O'Connor83047be2012-01-07 18:27:19 -0500181 SET_BDA(video_pagestart, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400182 SET_BDA(video_page, page);
183
Kevin O'Connora12c2152009-05-13 22:06:16 -0400184 dprintf(1, "Set active page %02x address %04x\n", page, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400185
186 // Display the cursor, now the page is active
Kevin O'Connor918b1562009-05-25 11:05:18 -0400187 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400188}
189
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400190static void
191set_scan_lines(u8 lines)
192{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500193 stdvga_set_scan_lines(lines);
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400194 if (lines == 8)
195 set_cursor_shape(0x06, 0x07);
196 else
197 set_cursor_shape(lines - 4, lines - 3);
198 SET_BDA(char_height, lines);
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500199 u16 vde = stdvga_get_vde();
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400200 u8 rows = vde / lines;
201 SET_BDA(video_rows, rows - 1);
202 u16 cols = GET_BDA(video_cols);
Kevin O'Connor83047be2012-01-07 18:27:19 -0500203 SET_BDA(video_pagesize, calc_page_size(MM_TEXT, cols, rows));
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400204}
205
206
207/****************************************************************
208 * Character writing
209 ****************************************************************/
210
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400211// Scroll the screen one line. This function is designed to be called
212// tail-recursive to reduce stack usage.
213static void noinline
214scroll_one(u16 nbrows, u16 nbcols, u8 page)
Kevin O'Connor09262412009-05-25 11:44:11 -0400215{
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400216 struct cursorpos ul = {0, 0, page};
217 struct cursorpos lr = {nbcols-1, nbrows-1, page};
218 vgafb_scroll(1, -1, ul, lr);
219}
220
221// Write a character to the screen at a given position. Implement
222// special characters and scroll the screen if necessary.
223static void
224write_teletype(struct cursorpos *pcp, struct carattr ca)
225{
226 struct cursorpos cp = *pcp;
227
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400228 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -0400229 u16 nbrows = GET_BDA(video_rows) + 1;
230 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400231
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400232 switch (ca.car) {
233 case 7:
234 //FIXME should beep
235 break;
236 case 8:
237 if (cp.x > 0)
238 cp.x--;
239 break;
240 case '\r':
241 cp.x = 0;
242 break;
243 case '\n':
244 cp.y++;
245 break;
246 case '\t':
247 do {
248 struct carattr dummyca = {' ', ca.attr, ca.use_attr};
249 vgafb_write_char(cp, dummyca);
250 cp.x++;
251 } while (cp.x < nbcols && cp.x % 8);
252 break;
253 default:
254 vgafb_write_char(cp, ca);
255 cp.x++;
256 }
257
Kevin O'Connor82221b22009-05-26 00:20:40 -0400258 // Do we need to wrap ?
259 if (cp.x == nbcols) {
260 cp.x = 0;
261 cp.y++;
262 }
263 // Do we need to scroll ?
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400264 if (cp.y < nbrows) {
265 *pcp = cp;
266 return;
Kevin O'Connor82221b22009-05-26 00:20:40 -0400267 }
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400268 // Scroll screen
269 cp.y--;
270 *pcp = cp;
271 scroll_one(nbrows, nbcols, cp.page);
Kevin O'Connor82221b22009-05-26 00:20:40 -0400272}
273
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400274// Write out a buffer of alternating characters and attributes.
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400275static void
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400276write_attr_string(struct cursorpos *pcp, u16 count, u16 seg, u8 *offset_far)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400277{
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400278 while (count--) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400279 u8 car = GET_FARVAR(seg, *offset_far);
280 offset_far++;
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400281 u8 attr = GET_FARVAR(seg, *offset_far);
282 offset_far++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400283
Kevin O'Connor09262412009-05-25 11:44:11 -0400284 struct carattr ca = {car, attr, 1};
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400285 write_teletype(pcp, ca);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400286 }
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400287}
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400288
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400289// Write out a buffer of characters.
290static void
291write_string(struct cursorpos *pcp, u8 attr, u16 count, u16 seg, u8 *offset_far)
292{
293 while (count--) {
294 u8 car = GET_FARVAR(seg, *offset_far);
295 offset_far++;
296
297 struct carattr ca = {car, attr, 1};
298 write_teletype(pcp, ca);
299 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400300}
301
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400302
303/****************************************************************
304 * Save and restore bda state
305 ****************************************************************/
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400306
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400307static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400308save_bda_state(u16 seg, struct saveBDAstate *info)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400309{
Kevin O'Connorca668642009-05-21 23:06:08 -0400310 SET_FARVAR(seg, info->video_mode, GET_BDA(video_mode));
311 SET_FARVAR(seg, info->video_cols, GET_BDA(video_cols));
312 SET_FARVAR(seg, info->video_pagesize, GET_BDA(video_pagesize));
313 SET_FARVAR(seg, info->crtc_address, GET_BDA(crtc_address));
314 SET_FARVAR(seg, info->video_rows, GET_BDA(video_rows));
315 SET_FARVAR(seg, info->char_height, GET_BDA(char_height));
316 SET_FARVAR(seg, info->video_ctl, GET_BDA(video_ctl));
317 SET_FARVAR(seg, info->video_switches, GET_BDA(video_switches));
318 SET_FARVAR(seg, info->modeset_ctl, GET_BDA(modeset_ctl));
319 SET_FARVAR(seg, info->cursor_type, GET_BDA(cursor_type));
320 u16 i;
321 for (i=0; i<8; i++)
322 SET_FARVAR(seg, info->cursor_pos[i], GET_BDA(cursor_pos[i]));
323 SET_FARVAR(seg, info->video_pagestart, GET_BDA(video_pagestart));
324 SET_FARVAR(seg, info->video_page, GET_BDA(video_page));
325 /* current font */
Kevin O'Connor9f985422009-09-09 11:34:39 -0400326 SET_FARVAR(seg, info->font0, GET_IVT(0x1f));
327 SET_FARVAR(seg, info->font1, GET_IVT(0x43));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400328}
329
Kevin O'Connorca668642009-05-21 23:06:08 -0400330static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400331restore_bda_state(u16 seg, struct saveBDAstate *info)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400332{
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500333 u16 mode = GET_FARVAR(seg, info->video_mode);
334 SET_BDA(video_mode, mode);
335 SET_BDA(vbe_mode, mode);
Kevin O'Connorca668642009-05-21 23:06:08 -0400336 SET_BDA(video_cols, GET_FARVAR(seg, info->video_cols));
337 SET_BDA(video_pagesize, GET_FARVAR(seg, info->video_pagesize));
338 SET_BDA(crtc_address, GET_FARVAR(seg, info->crtc_address));
339 SET_BDA(video_rows, GET_FARVAR(seg, info->video_rows));
340 SET_BDA(char_height, GET_FARVAR(seg, info->char_height));
341 SET_BDA(video_ctl, GET_FARVAR(seg, info->video_ctl));
342 SET_BDA(video_switches, GET_FARVAR(seg, info->video_switches));
343 SET_BDA(modeset_ctl, GET_FARVAR(seg, info->modeset_ctl));
344 SET_BDA(cursor_type, GET_FARVAR(seg, info->cursor_type));
345 u16 i;
346 for (i = 0; i < 8; i++)
347 SET_BDA(cursor_pos[i], GET_FARVAR(seg, info->cursor_pos[i]));
348 SET_BDA(video_pagestart, GET_FARVAR(seg, info->video_pagestart));
349 SET_BDA(video_page, GET_FARVAR(seg, info->video_page));
350 /* current font */
Kevin O'Connor9f985422009-09-09 11:34:39 -0400351 SET_IVT(0x1f, GET_FARVAR(seg, info->font0));
352 SET_IVT(0x43, GET_FARVAR(seg, info->font1));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400353}
354
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500355
356/****************************************************************
357 * Mode setting
358 ****************************************************************/
359
360struct vgamode_s *
361get_current_mode(void)
362{
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500363 return vgahw_find_mode(GET_BDA(vbe_mode) & ~MF_VBEFLAGS);
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500364}
365
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500366// Setup BDA after a mode switch.
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500367int
368vga_set_mode(int mode, int flags)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400369{
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500370 dprintf(1, "set VGA mode %x\n", mode);
371 struct vgamode_s *vmode_g = vgahw_find_mode(mode);
372 if (!vmode_g)
373 return VBE_RETURN_STATUS_FAILED;
374
375 int ret = vgahw_set_mode(vmode_g, flags);
376 if (ret)
377 return ret;
378
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400379 // Set the BIOS mem
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500380 int width = GET_GLOBAL(vmode_g->width);
381 int height = GET_GLOBAL(vmode_g->height);
Kevin O'Connor83047be2012-01-07 18:27:19 -0500382 u8 memmodel = GET_GLOBAL(vmode_g->memmodel);
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500383 int cheight = GET_GLOBAL(vmode_g->cheight);
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500384 if (mode < 0x100)
385 SET_BDA(video_mode, mode);
386 else
387 SET_BDA(video_mode, 0xff);
388 SET_BDA(vbe_mode, mode | (flags & MF_VBEFLAGS));
Kevin O'Connor83047be2012-01-07 18:27:19 -0500389 if (memmodel == MM_TEXT) {
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500390 SET_BDA(video_cols, width);
391 SET_BDA(video_rows, height-1);
392 SET_BDA(cursor_type, 0x0607);
393 } else {
394 int cwidth = GET_GLOBAL(vmode_g->cwidth);
395 SET_BDA(video_cols, width / cwidth);
396 SET_BDA(video_rows, (height / cheight) - 1);
397 SET_BDA(cursor_type, 0x0000);
398 }
Kevin O'Connor83047be2012-01-07 18:27:19 -0500399 SET_BDA(video_pagesize, calc_page_size(memmodel, width, height));
Kevin O'Connorcecbc5d2011-12-31 17:24:11 -0500400 SET_BDA(crtc_address, stdvga_get_crtc());
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400401 SET_BDA(char_height, cheight);
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500402 SET_BDA(video_ctl, 0x60 | (flags & MF_NOCLEARMEM ? 0x80 : 0x00));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400403 SET_BDA(video_switches, 0xF9);
404 SET_BDA(modeset_ctl, GET_BDA(modeset_ctl) & 0x7f);
Kevin O'Connorcecbc5d2011-12-31 17:24:11 -0500405 int i;
406 for (i=0; i<8; i++)
407 SET_BDA(cursor_pos[i], 0x0000);
408 SET_BDA(video_pagestart, 0x0000);
409 SET_BDA(video_page, 0x00);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400410
411 // FIXME We nearly have the good tables. to be reworked
412 SET_BDA(dcc_index, 0x08); // 8 is VGA should be ok for now
Kevin O'Connor9f985422009-09-09 11:34:39 -0400413 SET_BDA(video_savetable
Kevin O'Connor815e4472011-12-21 09:05:32 -0500414 , SEGOFF(get_global_seg(), (u32)&video_save_pointer_table));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400415
416 // FIXME
417 SET_BDA(video_msr, 0x00); // Unavailable on vanilla vga, but...
418 SET_BDA(video_pal, 0x00); // Unavailable on vanilla vga, but...
419
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400420 // Set the ints 0x1F and 0x43
Kevin O'Connor9f985422009-09-09 11:34:39 -0400421 SET_IVT(0x1f, SEGOFF(get_global_seg(), (u32)&vgafont8[128 * 8]));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400422
423 switch (cheight) {
424 case 8:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400425 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont8));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400426 break;
427 case 14:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400428 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont14));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400429 break;
430 case 16:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400431 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont16));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400432 break;
433 }
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500434
435 return 0;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400436}
437
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500438
439/****************************************************************
440 * VGA int 10 handler
441 ****************************************************************/
442
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400443static void
Julian Pidancet87879e22011-12-19 05:08:00 +0000444handle_1000(struct bregs *regs)
445{
Kevin O'Connor5108c692011-12-31 19:13:45 -0500446 int mode = regs->al & 0x7f;
Julian Pidancet87879e22011-12-19 05:08:00 +0000447
448 // Set regs->al
449 if (mode > 7)
450 regs->al = 0x20;
451 else if (mode == 6)
452 regs->al = 0x3f;
453 else
454 regs->al = 0x30;
455
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500456 int flags = GET_BDA(modeset_ctl) & (MF_NOPALETTE|MF_GRAYSUM);
Kevin O'Connor5108c692011-12-31 19:13:45 -0500457 if (regs->al & 0x80)
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500458 flags |= MF_NOCLEARMEM;
459
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500460 vga_set_mode(mode, flags);
Julian Pidancet87879e22011-12-19 05:08:00 +0000461}
462
463static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400464handle_1001(struct bregs *regs)
465{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400466 set_cursor_shape(regs->ch, regs->cl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400467}
468
469static void
470handle_1002(struct bregs *regs)
471{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400472 struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
473 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400474}
475
476static void
477handle_1003(struct bregs *regs)
478{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400479 regs->cx = get_cursor_shape(regs->bh);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400480 struct cursorpos cp = get_cursor_pos(regs->bh);
481 regs->dl = cp.x;
482 regs->dh = cp.y;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400483}
484
485// Read light pen pos (unimplemented)
486static void
487handle_1004(struct bregs *regs)
488{
489 debug_stub(regs);
490 regs->ax = regs->bx = regs->cx = regs->dx = 0;
491}
492
493static void
494handle_1005(struct bregs *regs)
495{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400496 set_active_page(regs->al);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400497}
498
499static void
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400500verify_scroll(struct bregs *regs, int dir)
501{
502 u8 page = GET_BDA(video_page);
503 struct cursorpos ul = {regs->cl, regs->ch, page};
504 struct cursorpos lr = {regs->dl, regs->dh, page};
505
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400506 u16 nbrows = GET_BDA(video_rows) + 1;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400507 if (lr.y >= nbrows)
508 lr.y = nbrows - 1;
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400509 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400510 if (lr.x >= nbcols)
511 lr.x = nbcols - 1;
512
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400513 if (ul.x > lr.x || ul.y > lr.y)
514 return;
515
516 u16 nblines = regs->al;
517 if (!nblines || nblines > lr.y - ul.y + 1)
518 nblines = lr.y - ul.y + 1;
519
520 vgafb_scroll(dir * nblines, regs->bh, ul, lr);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400521}
522
523static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400524handle_1006(struct bregs *regs)
525{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400526 verify_scroll(regs, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400527}
528
529static void
530handle_1007(struct bregs *regs)
531{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400532 verify_scroll(regs, -1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400533}
534
535static void
536handle_1008(struct bregs *regs)
537{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400538 struct carattr ca = vgafb_read_char(get_cursor_pos(regs->bh));
Kevin O'Connor09262412009-05-25 11:44:11 -0400539 regs->al = ca.car;
540 regs->ah = ca.attr;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400541}
542
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400543static void noinline
Kevin O'Connord3b38152009-05-26 00:05:37 -0400544write_chars(u8 page, struct carattr ca, u16 count)
545{
546 struct cursorpos cp = get_cursor_pos(page);
547 while (count--) {
548 vgafb_write_char(cp, ca);
549 cp.x++;
550 }
551}
552
553static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400554handle_1009(struct bregs *regs)
555{
Kevin O'Connor09262412009-05-25 11:44:11 -0400556 struct carattr ca = {regs->al, regs->bl, 1};
Kevin O'Connord3b38152009-05-26 00:05:37 -0400557 write_chars(regs->bh, ca, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400558}
559
560static void
561handle_100a(struct bregs *regs)
562{
Kevin O'Connor09262412009-05-25 11:44:11 -0400563 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connord3b38152009-05-26 00:05:37 -0400564 write_chars(regs->bh, ca, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400565}
566
567
568static void
569handle_100b00(struct bregs *regs)
570{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500571 stdvga_set_border_color(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400572}
573
574static void
575handle_100b01(struct bregs *regs)
576{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500577 stdvga_set_palette(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400578}
579
580static void
581handle_100bXX(struct bregs *regs)
582{
583 debug_stub(regs);
584}
585
586static void
587handle_100b(struct bregs *regs)
588{
589 switch (regs->bh) {
590 case 0x00: handle_100b00(regs); break;
591 case 0x01: handle_100b01(regs); break;
592 default: handle_100bXX(regs); break;
593 }
594}
595
596
597static void
598handle_100c(struct bregs *regs)
599{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400600 // XXX - page (regs->bh) is unused
601 vgafb_write_pixel(regs->al, regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400602}
603
604static void
605handle_100d(struct bregs *regs)
606{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400607 // XXX - page (regs->bh) is unused
608 regs->al = vgafb_read_pixel(regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400609}
610
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400611static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400612handle_100e(struct bregs *regs)
613{
614 // Ralf Brown Interrupt list is WRONG on bh(page)
615 // We do output only on the current page !
Kevin O'Connor09262412009-05-25 11:44:11 -0400616 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connor116a0442009-05-26 00:47:32 -0400617 struct cursorpos cp = get_cursor_pos(0xff);
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400618 write_teletype(&cp, ca);
Kevin O'Connor116a0442009-05-26 00:47:32 -0400619 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400620}
621
622static void
623handle_100f(struct bregs *regs)
624{
Kevin O'Connore7132042009-05-25 00:10:35 -0400625 regs->bh = GET_BDA(video_page);
626 regs->al = GET_BDA(video_mode) | (GET_BDA(video_ctl) & 0x80);
627 regs->ah = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400628}
629
630
631static void
632handle_101000(struct bregs *regs)
633{
634 if (regs->bl > 0x14)
635 return;
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500636 stdvga_attr_write(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400637}
638
639static void
640handle_101001(struct bregs *regs)
641{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500642 stdvga_set_overscan_border_color(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400643}
644
645static void
646handle_101002(struct bregs *regs)
647{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500648 stdvga_set_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400649}
650
651static void
652handle_101003(struct bregs *regs)
653{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500654 stdvga_toggle_intensity(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400655}
656
657static void
658handle_101007(struct bregs *regs)
659{
660 if (regs->bl > 0x14)
661 return;
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500662 regs->bh = stdvga_attr_read(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400663}
664
665static void
666handle_101008(struct bregs *regs)
667{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500668 regs->bh = stdvga_get_overscan_border_color();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400669}
670
671static void
672handle_101009(struct bregs *regs)
673{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500674 stdvga_get_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400675}
676
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400677static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400678handle_101010(struct bregs *regs)
679{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400680 u8 rgb[3] = {regs->dh, regs->ch, regs->cl};
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500681 stdvga_dac_write(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400682}
683
684static void
685handle_101012(struct bregs *regs)
686{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500687 stdvga_dac_write(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400688}
689
690static void
691handle_101013(struct bregs *regs)
692{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500693 stdvga_select_video_dac_color_page(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400694}
695
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400696static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400697handle_101015(struct bregs *regs)
698{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400699 u8 rgb[3];
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500700 stdvga_dac_read(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400701 regs->dh = rgb[0];
702 regs->ch = rgb[1];
703 regs->cl = rgb[2];
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400704}
705
706static void
707handle_101017(struct bregs *regs)
708{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500709 stdvga_dac_read(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400710}
711
712static void
713handle_101018(struct bregs *regs)
714{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500715 stdvga_pelmask_write(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400716}
717
718static void
719handle_101019(struct bregs *regs)
720{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500721 regs->bl = stdvga_pelmask_read();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400722}
723
724static void
725handle_10101a(struct bregs *regs)
726{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500727 stdvga_read_video_dac_state(&regs->bl, &regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400728}
729
730static void
731handle_10101b(struct bregs *regs)
732{
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500733 stdvga_perform_gray_scale_summing(regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400734}
735
736static void
737handle_1010XX(struct bregs *regs)
738{
739 debug_stub(regs);
740}
741
742static void
743handle_1010(struct bregs *regs)
744{
745 switch (regs->al) {
746 case 0x00: handle_101000(regs); break;
747 case 0x01: handle_101001(regs); break;
748 case 0x02: handle_101002(regs); break;
749 case 0x03: handle_101003(regs); break;
750 case 0x07: handle_101007(regs); break;
751 case 0x08: handle_101008(regs); break;
752 case 0x09: handle_101009(regs); break;
753 case 0x10: handle_101010(regs); break;
754 case 0x12: handle_101012(regs); break;
755 case 0x13: handle_101013(regs); break;
756 case 0x15: handle_101015(regs); break;
757 case 0x17: handle_101017(regs); break;
758 case 0x18: handle_101018(regs); break;
759 case 0x19: handle_101019(regs); break;
760 case 0x1a: handle_10101a(regs); break;
761 case 0x1b: handle_10101b(regs); break;
762 default: handle_1010XX(regs); break;
763 }
764}
765
766
767static void
768handle_101100(struct bregs *regs)
769{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500770 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
771 , regs->dx, regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400772}
773
774static void
775handle_101101(struct bregs *regs)
776{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500777 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400778}
779
780static void
781handle_101102(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'Connor1f2c3072009-05-06 23:35:59 -0400784}
785
786static void
787handle_101103(struct bregs *regs)
788{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500789 stdvga_set_text_block_specifier(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400790}
791
792static void
793handle_101104(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'Connor1f2c3072009-05-06 23:35:59 -0400796}
797
798static void
799handle_101110(struct bregs *regs)
800{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500801 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
802 , regs->dx, regs->bl, regs->bh);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400803 set_scan_lines(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400804}
805
806static void
807handle_101111(struct bregs *regs)
808{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500809 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400810 set_scan_lines(14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400811}
812
813static void
814handle_101112(struct bregs *regs)
815{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500816 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400817 set_scan_lines(8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400818}
819
820static void
821handle_101114(struct bregs *regs)
822{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500823 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400824 set_scan_lines(16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400825}
826
827static void
828handle_101130(struct bregs *regs)
829{
Kevin O'Connore7132042009-05-25 00:10:35 -0400830 switch (regs->bh) {
831 case 0x00: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500832 struct segoff_s so = GET_IVT(0x1f);
833 regs->es = so.seg;
834 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400835 break;
836 }
837 case 0x01: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500838 struct segoff_s so = GET_IVT(0x43);
839 regs->es = so.seg;
840 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400841 break;
842 }
843 case 0x02:
844 regs->es = get_global_seg();
845 regs->bp = (u32)vgafont14;
846 break;
847 case 0x03:
848 regs->es = get_global_seg();
849 regs->bp = (u32)vgafont8;
850 break;
851 case 0x04:
852 regs->es = get_global_seg();
853 regs->bp = (u32)vgafont8 + 128 * 8;
854 break;
855 case 0x05:
856 regs->es = get_global_seg();
857 regs->bp = (u32)vgafont14alt;
858 break;
859 case 0x06:
860 regs->es = get_global_seg();
861 regs->bp = (u32)vgafont16;
862 break;
863 case 0x07:
864 regs->es = get_global_seg();
865 regs->bp = (u32)vgafont16alt;
866 break;
867 default:
868 dprintf(1, "Get font info BH(%02x) was discarded\n", regs->bh);
869 return;
870 }
871 // Set byte/char of on screen font
872 regs->cx = GET_BDA(char_height) & 0xff;
873
874 // Set Highest char row
875 regs->dx = GET_BDA(video_rows);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400876}
877
878static void
879handle_1011XX(struct bregs *regs)
880{
881 debug_stub(regs);
882}
883
884static void
885handle_1011(struct bregs *regs)
886{
887 switch (regs->al) {
888 case 0x00: handle_101100(regs); break;
889 case 0x01: handle_101101(regs); break;
890 case 0x02: handle_101102(regs); break;
891 case 0x03: handle_101103(regs); break;
892 case 0x04: handle_101104(regs); break;
893 case 0x10: handle_101110(regs); break;
894 case 0x11: handle_101111(regs); break;
895 case 0x12: handle_101112(regs); break;
896 case 0x14: handle_101114(regs); break;
897 case 0x30: handle_101130(regs); break;
898 default: handle_1011XX(regs); break;
899 }
900}
901
902
903static void
904handle_101210(struct bregs *regs)
905{
Kevin O'Connore7132042009-05-25 00:10:35 -0400906 u16 crtc_addr = GET_BDA(crtc_address);
907 if (crtc_addr == VGAREG_MDA_CRTC_ADDRESS)
908 regs->bx = 0x0103;
909 else
910 regs->bx = 0x0003;
911 regs->cx = GET_BDA(video_switches) & 0x0f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400912}
913
914static void
915handle_101230(struct bregs *regs)
916{
Kevin O'Connore7132042009-05-25 00:10:35 -0400917 u8 mctl = GET_BDA(modeset_ctl);
918 u8 vswt = GET_BDA(video_switches);
919 switch (regs->al) {
920 case 0x00:
921 // 200 lines
922 mctl = (mctl & ~0x10) | 0x80;
923 vswt = (vswt & ~0x0f) | 0x08;
924 break;
925 case 0x01:
926 // 350 lines
927 mctl &= ~0x90;
928 vswt = (vswt & ~0x0f) | 0x09;
929 break;
930 case 0x02:
931 // 400 lines
932 mctl = (mctl & ~0x80) | 0x10;
933 vswt = (vswt & ~0x0f) | 0x09;
934 break;
935 default:
936 dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
937 break;
938 }
939 SET_BDA(modeset_ctl, mctl);
940 SET_BDA(video_switches, vswt);
941 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400942}
943
944static void
945handle_101231(struct bregs *regs)
946{
Kevin O'Connore7132042009-05-25 00:10:35 -0400947 u8 v = (regs->al & 0x01) << 3;
948 u8 mctl = GET_BDA(video_ctl) & ~0x08;
949 SET_BDA(video_ctl, mctl | v);
950 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400951}
952
953static void
954handle_101232(struct bregs *regs)
955{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500956 stdvga_enable_video_addressing(regs->al);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400957 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400958}
959
960static void
961handle_101233(struct bregs *regs)
962{
Kevin O'Connore7132042009-05-25 00:10:35 -0400963 u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
964 u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
965 SET_BDA(modeset_ctl, v | v2);
966 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400967}
968
969static void
970handle_101234(struct bregs *regs)
971{
Kevin O'Connore7132042009-05-25 00:10:35 -0400972 u8 v = (regs->al & 0x01) ^ 0x01;
973 u8 v2 = GET_BDA(modeset_ctl) & ~0x01;
974 SET_BDA(modeset_ctl, v | v2);
975 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400976}
977
978static void
979handle_101235(struct bregs *regs)
980{
981 debug_stub(regs);
982 regs->al = 0x12;
983}
984
985static void
986handle_101236(struct bregs *regs)
987{
988 debug_stub(regs);
989 regs->al = 0x12;
990}
991
992static void
993handle_1012XX(struct bregs *regs)
994{
995 debug_stub(regs);
996}
997
998static void
999handle_1012(struct bregs *regs)
1000{
Kevin O'Connore91ec7c2012-01-14 16:30:49 -05001001 if (CONFIG_VGA_CIRRUS && regs->bl >= 0x80) {
1002 clext_1012(regs);
1003 return;
1004 }
1005
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001006 switch (regs->bl) {
1007 case 0x10: handle_101210(regs); break;
1008 case 0x30: handle_101230(regs); break;
1009 case 0x31: handle_101231(regs); break;
1010 case 0x32: handle_101232(regs); break;
1011 case 0x33: handle_101233(regs); break;
1012 case 0x34: handle_101234(regs); break;
1013 case 0x35: handle_101235(regs); break;
1014 case 0x36: handle_101236(regs); break;
1015 default: handle_1012XX(regs); break;
1016 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001017}
1018
1019
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001020// Write string
Kevin O'Connor0ad77f02009-05-31 20:46:43 -04001021static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001022handle_1013(struct bregs *regs)
1023{
Kevin O'Connor918b1562009-05-25 11:05:18 -04001024 struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -04001025 // if row=0xff special case : use current cursor position
1026 if (cp.y == 0xff)
1027 cp = get_cursor_pos(cp.page);
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001028 u8 flag = regs->al;
1029 if (flag & 2)
1030 write_attr_string(&cp, regs->cx, regs->es, (void*)(regs->bp + 0));
1031 else
1032 write_string(&cp, regs->bl, regs->cx, regs->es, (void*)(regs->bp + 0));
1033
1034 if (flag & 1)
1035 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001036}
1037
1038
1039static void
1040handle_101a00(struct bregs *regs)
1041{
Kevin O'Connore7132042009-05-25 00:10:35 -04001042 regs->bx = GET_BDA(dcc_index);
1043 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001044}
1045
1046static void
1047handle_101a01(struct bregs *regs)
1048{
Kevin O'Connore7132042009-05-25 00:10:35 -04001049 SET_BDA(dcc_index, regs->bl);
1050 dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
1051 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001052}
1053
1054static void
1055handle_101aXX(struct bregs *regs)
1056{
1057 debug_stub(regs);
1058}
1059
1060static void
1061handle_101a(struct bregs *regs)
1062{
1063 switch (regs->al) {
1064 case 0x00: handle_101a00(regs); break;
1065 case 0x01: handle_101a01(regs); break;
1066 default: handle_101aXX(regs); break;
1067 }
1068}
1069
1070
Kevin O'Connorefb45232012-01-14 22:55:11 -05001071static u8 static_functionality[0x10] VAR16 = {
1072 /* 0 */ 0xff, // All modes supported #1
1073 /* 1 */ 0xe0, // All modes supported #2
1074 /* 2 */ 0x0f, // All modes supported #3
1075 /* 3 */ 0x00, 0x00, 0x00, 0x00, // reserved
1076 /* 7 */ 0x07, // 200, 350, 400 scan lines
1077 /* 8 */ 0x02, // mamimum number of visible charsets in text mode
1078 /* 9 */ 0x08, // total number of charset blocks in text mode
1079 /* a */ 0xe7, // Change to add new functions
1080 /* b */ 0x0c, // Change to add new functions
1081 /* c */ 0x00, // reserved
1082 /* d */ 0x00, // reserved
1083 /* e */ 0x00, // Change to add new functions
1084 /* f */ 0x00 // reserved
1085};
1086
Kevin O'Connorca668642009-05-21 23:06:08 -04001087struct funcInfo {
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001088 struct segoff_s static_functionality;
Kevin O'Connorca668642009-05-21 23:06:08 -04001089 u8 bda_0x49[30];
1090 u8 bda_0x84[3];
1091 u8 dcc_index;
1092 u8 dcc_alt;
1093 u16 colors;
1094 u8 pages;
1095 u8 scan_lines;
1096 u8 primary_char;
1097 u8 secondar_char;
1098 u8 misc;
1099 u8 non_vga_mode;
1100 u8 reserved_2f[2];
1101 u8 video_mem;
1102 u8 save_flags;
1103 u8 disp_info;
1104 u8 reserved_34[12];
1105};
1106
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001107static void
1108handle_101b(struct bregs *regs)
1109{
Kevin O'Connorca668642009-05-21 23:06:08 -04001110 u16 seg = regs->es;
1111 struct funcInfo *info = (void*)(regs->di+0);
1112 memset_far(seg, info, 0, sizeof(*info));
1113 // Address of static functionality table
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001114 SET_FARVAR(seg, info->static_functionality
1115 , SEGOFF(get_global_seg(), (u32)static_functionality));
Kevin O'Connorca668642009-05-21 23:06:08 -04001116
1117 // Hard coded copy from BIOS area. Should it be cleaner ?
Kevin O'Connor9f985422009-09-09 11:34:39 -04001118 memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49
1119 , sizeof(info->bda_0x49));
1120 memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84
1121 , sizeof(info->bda_0x84));
Kevin O'Connorca668642009-05-21 23:06:08 -04001122
1123 SET_FARVAR(seg, info->dcc_index, GET_BDA(dcc_index));
1124 SET_FARVAR(seg, info->colors, 16);
1125 SET_FARVAR(seg, info->pages, 8);
1126 SET_FARVAR(seg, info->scan_lines, 2);
1127 SET_FARVAR(seg, info->video_mem, 3);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001128 regs->al = 0x1B;
1129}
1130
1131
1132static void
1133handle_101c00(struct bregs *regs)
1134{
Kevin O'Connorca668642009-05-21 23:06:08 -04001135 u16 flags = regs->cx;
1136 u16 size = 0;
1137 if (flags & 1)
1138 size += sizeof(struct saveVideoHardware);
1139 if (flags & 2)
1140 size += sizeof(struct saveBDAstate);
1141 if (flags & 4)
1142 size += sizeof(struct saveDACcolors);
1143 regs->bx = size;
1144 regs->al = 0x1c;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001145}
1146
1147static void
1148handle_101c01(struct bregs *regs)
1149{
Kevin O'Connorca668642009-05-21 23:06:08 -04001150 u16 flags = regs->cx;
1151 u16 seg = regs->es;
1152 void *data = (void*)(regs->bx+0);
1153 if (flags & 1) {
Kevin O'Connor88ca7412011-12-31 04:24:20 -05001154 stdvga_save_state(seg, data);
Kevin O'Connorca668642009-05-21 23:06:08 -04001155 data += sizeof(struct saveVideoHardware);
1156 }
1157 if (flags & 2) {
Kevin O'Connor227a2bb2009-05-31 22:00:20 -04001158 save_bda_state(seg, data);
Kevin O'Connorca668642009-05-21 23:06:08 -04001159 data += sizeof(struct saveBDAstate);
1160 }
1161 if (flags & 4)
Kevin O'Connor88ca7412011-12-31 04:24:20 -05001162 stdvga_save_dac_state(seg, data);
Kevin O'Connorca668642009-05-21 23:06:08 -04001163 regs->al = 0x1c;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001164}
1165
1166static void
1167handle_101c02(struct bregs *regs)
1168{
Kevin O'Connorca668642009-05-21 23:06:08 -04001169 u16 flags = regs->cx;
1170 u16 seg = regs->es;
1171 void *data = (void*)(regs->bx+0);
1172 if (flags & 1) {
Kevin O'Connor88ca7412011-12-31 04:24:20 -05001173 stdvga_restore_state(seg, data);
Kevin O'Connorca668642009-05-21 23:06:08 -04001174 data += sizeof(struct saveVideoHardware);
1175 }
1176 if (flags & 2) {
Kevin O'Connor227a2bb2009-05-31 22:00:20 -04001177 restore_bda_state(seg, data);
Kevin O'Connorca668642009-05-21 23:06:08 -04001178 data += sizeof(struct saveBDAstate);
1179 }
1180 if (flags & 4)
Kevin O'Connor88ca7412011-12-31 04:24:20 -05001181 stdvga_restore_dac_state(seg, data);
Kevin O'Connorca668642009-05-21 23:06:08 -04001182 regs->al = 0x1c;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001183}
1184
1185static void
1186handle_101cXX(struct bregs *regs)
1187{
1188 debug_stub(regs);
1189}
1190
1191static void
1192handle_101c(struct bregs *regs)
1193{
1194 switch (regs->al) {
1195 case 0x00: handle_101c00(regs); break;
1196 case 0x01: handle_101c01(regs); break;
1197 case 0x02: handle_101c02(regs); break;
1198 default: handle_101cXX(regs); break;
1199 }
1200}
1201
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001202static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001203handle_10XX(struct bregs *regs)
1204{
1205 debug_stub(regs);
1206}
1207
1208// INT 10h Video Support Service Entry Point
1209void VISIBLE16
1210handle_10(struct bregs *regs)
1211{
1212 debug_enter(regs, DEBUG_VGA_10);
1213 switch (regs->ah) {
1214 case 0x00: handle_1000(regs); break;
1215 case 0x01: handle_1001(regs); break;
1216 case 0x02: handle_1002(regs); break;
1217 case 0x03: handle_1003(regs); break;
1218 case 0x04: handle_1004(regs); break;
1219 case 0x05: handle_1005(regs); break;
1220 case 0x06: handle_1006(regs); break;
1221 case 0x07: handle_1007(regs); break;
1222 case 0x08: handle_1008(regs); break;
1223 case 0x09: handle_1009(regs); break;
1224 case 0x0a: handle_100a(regs); break;
1225 case 0x0b: handle_100b(regs); break;
1226 case 0x0c: handle_100c(regs); break;
1227 case 0x0d: handle_100d(regs); break;
1228 case 0x0e: handle_100e(regs); break;
1229 case 0x0f: handle_100f(regs); break;
1230 case 0x10: handle_1010(regs); break;
1231 case 0x11: handle_1011(regs); break;
1232 case 0x12: handle_1012(regs); break;
1233 case 0x13: handle_1013(regs); break;
1234 case 0x1a: handle_101a(regs); break;
1235 case 0x1b: handle_101b(regs); break;
1236 case 0x1c: handle_101c(regs); break;
1237 case 0x4f: handle_104f(regs); break;
1238 default: handle_10XX(regs); break;
1239 }
1240}
1241
1242
1243/****************************************************************
1244 * VGA post
1245 ****************************************************************/
1246
1247static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -05001248init_bios_area(void)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001249{
1250 // init detected hardware BIOS Area
1251 // set 80x25 color (not clear from RBIL but usual)
1252 u16 eqf = GET_BDA(equipment_list_flags);
1253 SET_BDA(equipment_list_flags, (eqf & 0xffcf) | 0x20);
1254
1255 // Just for the first int10 find its children
1256
1257 // the default char height
1258 SET_BDA(char_height, 0x10);
1259
1260 // Clear the screen
1261 SET_BDA(video_ctl, 0x60);
1262
1263 // Set the basic screen we have
1264 SET_BDA(video_switches, 0xf9);
1265
1266 // Set the basic modeset options
1267 SET_BDA(modeset_ctl, 0x51);
1268
1269 // Set the default MSR
1270 SET_BDA(video_msr, 0x09);
1271}
1272
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -05001273int VgaBDF VAR16 = -1;
Kevin O'Connor161d2012011-12-31 19:42:21 -05001274
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001275void VISIBLE16
1276vga_post(struct bregs *regs)
1277{
1278 debug_enter(regs, DEBUG_VGA_POST);
1279
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -05001280 if (CONFIG_VGA_PCI) {
1281 u16 bdf = regs->ax;
1282 if (pci_config_readw(bdf, PCI_VENDOR_ID) == CONFIG_VGA_VID
1283 && pci_config_readw(bdf, PCI_DEVICE_ID) == CONFIG_VGA_DID)
1284 SET_VGA(VgaBDF, bdf);
1285 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001286
Kevin O'Connor161d2012011-12-31 19:42:21 -05001287 int ret = vgahw_init();
1288 if (ret) {
1289 dprintf(1, "Failed to initialize VGA hardware. Exiting.\n");
1290 return;
1291 }
Kevin O'Connor4c52fb42011-12-24 00:44:07 -05001292
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001293 init_bios_area();
1294
Kevin O'Connoraad3b692012-01-14 23:15:40 -05001295 SET_VGA(video_save_pointer_table.videoparam
1296 , SEGOFF(get_global_seg(), (u32)video_param_table));
1297 stdvga_build_video_param();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001298
1299 extern void entry_10(void);
Kevin O'Connor9f985422009-09-09 11:34:39 -04001300 SET_IVT(0x10, SEGOFF(get_global_seg(), (u32)entry_10));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001301
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001302 // XXX - clear screen and display info
1303
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001304 // Fixup checksum
1305 extern u8 _rom_header_size, _rom_header_checksum;
1306 SET_VGA(_rom_header_checksum, 0);
Kevin O'Connord113a992009-05-16 21:05:02 -04001307 u8 sum = -checksum_far(get_global_seg(), 0, _rom_header_size * 512);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001308 SET_VGA(_rom_header_checksum, sum);
1309}