blob: 7b6c50af95051fbf513cffd4af2a2d34c5d31997 [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'Connor9f857fc2012-02-04 11:59:02 -0500307void
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'Connor2469f892012-02-04 12:40:02 -0500310 SET_FARVAR(seg, info->video_mode, GET_BDA(vbe_mode));
Kevin O'Connorca668642009-05-21 23:06:08 -0400311 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));
Kevin O'Connor9f857fc2012-02-04 11:59:02 -0500320 int i;
Kevin O'Connorca668642009-05-21 23:06:08 -0400321 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'Connor9f857fc2012-02-04 11:59:02 -0500330void
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);
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500334 SET_BDA(vbe_mode, mode);
Kevin O'Connor2469f892012-02-04 12:40:02 -0500335 if (mode < 0x100)
336 SET_BDA(video_mode, mode);
337 else
338 SET_BDA(video_mode, 0xff);
Kevin O'Connorca668642009-05-21 23:06:08 -0400339 SET_BDA(video_cols, GET_FARVAR(seg, info->video_cols));
340 SET_BDA(video_pagesize, GET_FARVAR(seg, info->video_pagesize));
341 SET_BDA(crtc_address, GET_FARVAR(seg, info->crtc_address));
342 SET_BDA(video_rows, GET_FARVAR(seg, info->video_rows));
343 SET_BDA(char_height, GET_FARVAR(seg, info->char_height));
344 SET_BDA(video_ctl, GET_FARVAR(seg, info->video_ctl));
345 SET_BDA(video_switches, GET_FARVAR(seg, info->video_switches));
346 SET_BDA(modeset_ctl, GET_FARVAR(seg, info->modeset_ctl));
347 SET_BDA(cursor_type, GET_FARVAR(seg, info->cursor_type));
Kevin O'Connor9f857fc2012-02-04 11:59:02 -0500348 int i;
Kevin O'Connorca668642009-05-21 23:06:08 -0400349 for (i = 0; i < 8; i++)
350 SET_BDA(cursor_pos[i], GET_FARVAR(seg, info->cursor_pos[i]));
351 SET_BDA(video_pagestart, GET_FARVAR(seg, info->video_pagestart));
352 SET_BDA(video_page, GET_FARVAR(seg, info->video_page));
353 /* current font */
Kevin O'Connor9f985422009-09-09 11:34:39 -0400354 SET_IVT(0x1f, GET_FARVAR(seg, info->font0));
355 SET_IVT(0x43, GET_FARVAR(seg, info->font1));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400356}
357
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500358
359/****************************************************************
360 * Mode setting
361 ****************************************************************/
362
363struct vgamode_s *
364get_current_mode(void)
365{
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500366 return vgahw_find_mode(GET_BDA(vbe_mode) & ~MF_VBEFLAGS);
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500367}
368
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500369// Setup BDA after a mode switch.
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500370int
371vga_set_mode(int mode, int flags)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400372{
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500373 dprintf(1, "set VGA mode %x\n", mode);
374 struct vgamode_s *vmode_g = vgahw_find_mode(mode);
375 if (!vmode_g)
376 return VBE_RETURN_STATUS_FAILED;
377
378 int ret = vgahw_set_mode(vmode_g, flags);
379 if (ret)
380 return ret;
381
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400382 // Set the BIOS mem
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500383 int width = GET_GLOBAL(vmode_g->width);
384 int height = GET_GLOBAL(vmode_g->height);
Kevin O'Connor83047be2012-01-07 18:27:19 -0500385 u8 memmodel = GET_GLOBAL(vmode_g->memmodel);
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500386 int cheight = GET_GLOBAL(vmode_g->cheight);
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500387 if (mode < 0x100)
388 SET_BDA(video_mode, mode);
389 else
390 SET_BDA(video_mode, 0xff);
391 SET_BDA(vbe_mode, mode | (flags & MF_VBEFLAGS));
Kevin O'Connor83047be2012-01-07 18:27:19 -0500392 if (memmodel == MM_TEXT) {
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500393 SET_BDA(video_cols, width);
394 SET_BDA(video_rows, height-1);
395 SET_BDA(cursor_type, 0x0607);
396 } else {
397 int cwidth = GET_GLOBAL(vmode_g->cwidth);
398 SET_BDA(video_cols, width / cwidth);
399 SET_BDA(video_rows, (height / cheight) - 1);
400 SET_BDA(cursor_type, 0x0000);
401 }
Kevin O'Connor83047be2012-01-07 18:27:19 -0500402 SET_BDA(video_pagesize, calc_page_size(memmodel, width, height));
Kevin O'Connorcecbc5d2011-12-31 17:24:11 -0500403 SET_BDA(crtc_address, stdvga_get_crtc());
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400404 SET_BDA(char_height, cheight);
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500405 SET_BDA(video_ctl, 0x60 | (flags & MF_NOCLEARMEM ? 0x80 : 0x00));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400406 SET_BDA(video_switches, 0xF9);
407 SET_BDA(modeset_ctl, GET_BDA(modeset_ctl) & 0x7f);
Kevin O'Connorcecbc5d2011-12-31 17:24:11 -0500408 int i;
409 for (i=0; i<8; i++)
410 SET_BDA(cursor_pos[i], 0x0000);
411 SET_BDA(video_pagestart, 0x0000);
412 SET_BDA(video_page, 0x00);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400413
414 // FIXME We nearly have the good tables. to be reworked
415 SET_BDA(dcc_index, 0x08); // 8 is VGA should be ok for now
Kevin O'Connor9f985422009-09-09 11:34:39 -0400416 SET_BDA(video_savetable
Kevin O'Connor815e4472011-12-21 09:05:32 -0500417 , SEGOFF(get_global_seg(), (u32)&video_save_pointer_table));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400418
419 // FIXME
420 SET_BDA(video_msr, 0x00); // Unavailable on vanilla vga, but...
421 SET_BDA(video_pal, 0x00); // Unavailable on vanilla vga, but...
422
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400423 // Set the ints 0x1F and 0x43
Kevin O'Connor9f985422009-09-09 11:34:39 -0400424 SET_IVT(0x1f, SEGOFF(get_global_seg(), (u32)&vgafont8[128 * 8]));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400425
426 switch (cheight) {
427 case 8:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400428 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont8));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400429 break;
430 case 14:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400431 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont14));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400432 break;
433 case 16:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400434 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont16));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400435 break;
436 }
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500437
438 return 0;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400439}
440
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500441
442/****************************************************************
443 * VGA int 10 handler
444 ****************************************************************/
445
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400446static void
Julian Pidancet87879e22011-12-19 05:08:00 +0000447handle_1000(struct bregs *regs)
448{
Kevin O'Connor5108c692011-12-31 19:13:45 -0500449 int mode = regs->al & 0x7f;
Julian Pidancet87879e22011-12-19 05:08:00 +0000450
451 // Set regs->al
452 if (mode > 7)
453 regs->al = 0x20;
454 else if (mode == 6)
455 regs->al = 0x3f;
456 else
457 regs->al = 0x30;
458
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500459 int flags = GET_BDA(modeset_ctl) & (MF_NOPALETTE|MF_GRAYSUM);
Kevin O'Connor5108c692011-12-31 19:13:45 -0500460 if (regs->al & 0x80)
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500461 flags |= MF_NOCLEARMEM;
462
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500463 vga_set_mode(mode, flags);
Julian Pidancet87879e22011-12-19 05:08:00 +0000464}
465
466static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400467handle_1001(struct bregs *regs)
468{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400469 set_cursor_shape(regs->ch, regs->cl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400470}
471
472static void
473handle_1002(struct bregs *regs)
474{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400475 struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
476 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400477}
478
479static void
480handle_1003(struct bregs *regs)
481{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400482 regs->cx = get_cursor_shape(regs->bh);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400483 struct cursorpos cp = get_cursor_pos(regs->bh);
484 regs->dl = cp.x;
485 regs->dh = cp.y;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400486}
487
488// Read light pen pos (unimplemented)
489static void
490handle_1004(struct bregs *regs)
491{
492 debug_stub(regs);
493 regs->ax = regs->bx = regs->cx = regs->dx = 0;
494}
495
496static void
497handle_1005(struct bregs *regs)
498{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400499 set_active_page(regs->al);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400500}
501
502static void
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400503verify_scroll(struct bregs *regs, int dir)
504{
505 u8 page = GET_BDA(video_page);
506 struct cursorpos ul = {regs->cl, regs->ch, page};
507 struct cursorpos lr = {regs->dl, regs->dh, page};
508
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400509 u16 nbrows = GET_BDA(video_rows) + 1;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400510 if (lr.y >= nbrows)
511 lr.y = nbrows - 1;
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400512 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400513 if (lr.x >= nbcols)
514 lr.x = nbcols - 1;
515
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400516 if (ul.x > lr.x || ul.y > lr.y)
517 return;
518
519 u16 nblines = regs->al;
520 if (!nblines || nblines > lr.y - ul.y + 1)
521 nblines = lr.y - ul.y + 1;
522
523 vgafb_scroll(dir * nblines, regs->bh, ul, lr);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400524}
525
526static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400527handle_1006(struct bregs *regs)
528{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400529 verify_scroll(regs, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400530}
531
532static void
533handle_1007(struct bregs *regs)
534{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400535 verify_scroll(regs, -1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400536}
537
538static void
539handle_1008(struct bregs *regs)
540{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400541 struct carattr ca = vgafb_read_char(get_cursor_pos(regs->bh));
Kevin O'Connor09262412009-05-25 11:44:11 -0400542 regs->al = ca.car;
543 regs->ah = ca.attr;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400544}
545
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400546static void noinline
Kevin O'Connord3b38152009-05-26 00:05:37 -0400547write_chars(u8 page, struct carattr ca, u16 count)
548{
549 struct cursorpos cp = get_cursor_pos(page);
550 while (count--) {
551 vgafb_write_char(cp, ca);
552 cp.x++;
553 }
554}
555
556static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400557handle_1009(struct bregs *regs)
558{
Kevin O'Connor09262412009-05-25 11:44:11 -0400559 struct carattr ca = {regs->al, regs->bl, 1};
Kevin O'Connord3b38152009-05-26 00:05:37 -0400560 write_chars(regs->bh, ca, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400561}
562
563static void
564handle_100a(struct bregs *regs)
565{
Kevin O'Connor09262412009-05-25 11:44:11 -0400566 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connord3b38152009-05-26 00:05:37 -0400567 write_chars(regs->bh, ca, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400568}
569
570
571static void
572handle_100b00(struct bregs *regs)
573{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500574 stdvga_set_border_color(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400575}
576
577static void
578handle_100b01(struct bregs *regs)
579{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500580 stdvga_set_palette(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400581}
582
583static void
584handle_100bXX(struct bregs *regs)
585{
586 debug_stub(regs);
587}
588
589static void
590handle_100b(struct bregs *regs)
591{
592 switch (regs->bh) {
593 case 0x00: handle_100b00(regs); break;
594 case 0x01: handle_100b01(regs); break;
595 default: handle_100bXX(regs); break;
596 }
597}
598
599
600static void
601handle_100c(struct bregs *regs)
602{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400603 // XXX - page (regs->bh) is unused
604 vgafb_write_pixel(regs->al, regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400605}
606
607static void
608handle_100d(struct bregs *regs)
609{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400610 // XXX - page (regs->bh) is unused
611 regs->al = vgafb_read_pixel(regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400612}
613
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400614static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400615handle_100e(struct bregs *regs)
616{
617 // Ralf Brown Interrupt list is WRONG on bh(page)
618 // We do output only on the current page !
Kevin O'Connor09262412009-05-25 11:44:11 -0400619 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connor116a0442009-05-26 00:47:32 -0400620 struct cursorpos cp = get_cursor_pos(0xff);
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400621 write_teletype(&cp, ca);
Kevin O'Connor116a0442009-05-26 00:47:32 -0400622 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400623}
624
625static void
626handle_100f(struct bregs *regs)
627{
Kevin O'Connore7132042009-05-25 00:10:35 -0400628 regs->bh = GET_BDA(video_page);
629 regs->al = GET_BDA(video_mode) | (GET_BDA(video_ctl) & 0x80);
630 regs->ah = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400631}
632
633
634static void
635handle_101000(struct bregs *regs)
636{
637 if (regs->bl > 0x14)
638 return;
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500639 stdvga_attr_write(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400640}
641
642static void
643handle_101001(struct bregs *regs)
644{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500645 stdvga_set_overscan_border_color(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400646}
647
648static void
649handle_101002(struct bregs *regs)
650{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500651 stdvga_set_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400652}
653
654static void
655handle_101003(struct bregs *regs)
656{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500657 stdvga_toggle_intensity(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400658}
659
660static void
661handle_101007(struct bregs *regs)
662{
663 if (regs->bl > 0x14)
664 return;
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500665 regs->bh = stdvga_attr_read(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400666}
667
668static void
669handle_101008(struct bregs *regs)
670{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500671 regs->bh = stdvga_get_overscan_border_color();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400672}
673
674static void
675handle_101009(struct bregs *regs)
676{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500677 stdvga_get_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400678}
679
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400680static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400681handle_101010(struct bregs *regs)
682{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400683 u8 rgb[3] = {regs->dh, regs->ch, regs->cl};
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500684 stdvga_dac_write(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400685}
686
687static void
688handle_101012(struct bregs *regs)
689{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500690 stdvga_dac_write(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400691}
692
693static void
694handle_101013(struct bregs *regs)
695{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500696 stdvga_select_video_dac_color_page(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400697}
698
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400699static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400700handle_101015(struct bregs *regs)
701{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400702 u8 rgb[3];
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500703 stdvga_dac_read(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400704 regs->dh = rgb[0];
705 regs->ch = rgb[1];
706 regs->cl = rgb[2];
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400707}
708
709static void
710handle_101017(struct bregs *regs)
711{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500712 stdvga_dac_read(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400713}
714
715static void
716handle_101018(struct bregs *regs)
717{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500718 stdvga_pelmask_write(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400719}
720
721static void
722handle_101019(struct bregs *regs)
723{
Kevin O'Connor3471fdb2012-01-14 19:02:43 -0500724 regs->bl = stdvga_pelmask_read();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400725}
726
727static void
728handle_10101a(struct bregs *regs)
729{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500730 stdvga_read_video_dac_state(&regs->bl, &regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400731}
732
733static void
734handle_10101b(struct bregs *regs)
735{
Kevin O'Connor821d6b42011-12-31 18:19:22 -0500736 stdvga_perform_gray_scale_summing(regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400737}
738
739static void
740handle_1010XX(struct bregs *regs)
741{
742 debug_stub(regs);
743}
744
745static void
746handle_1010(struct bregs *regs)
747{
748 switch (regs->al) {
749 case 0x00: handle_101000(regs); break;
750 case 0x01: handle_101001(regs); break;
751 case 0x02: handle_101002(regs); break;
752 case 0x03: handle_101003(regs); break;
753 case 0x07: handle_101007(regs); break;
754 case 0x08: handle_101008(regs); break;
755 case 0x09: handle_101009(regs); break;
756 case 0x10: handle_101010(regs); break;
757 case 0x12: handle_101012(regs); break;
758 case 0x13: handle_101013(regs); break;
759 case 0x15: handle_101015(regs); break;
760 case 0x17: handle_101017(regs); break;
761 case 0x18: handle_101018(regs); break;
762 case 0x19: handle_101019(regs); break;
763 case 0x1a: handle_10101a(regs); break;
764 case 0x1b: handle_10101b(regs); break;
765 default: handle_1010XX(regs); break;
766 }
767}
768
769
770static void
771handle_101100(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'Connor1f2c3072009-05-06 23:35:59 -0400775}
776
777static void
778handle_101101(struct bregs *regs)
779{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500780 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400781}
782
783static void
784handle_101102(struct bregs *regs)
785{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500786 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400787}
788
789static void
790handle_101103(struct bregs *regs)
791{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500792 stdvga_set_text_block_specifier(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400793}
794
795static void
796handle_101104(struct bregs *regs)
797{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500798 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400799}
800
801static void
802handle_101110(struct bregs *regs)
803{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500804 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
805 , regs->dx, regs->bl, regs->bh);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400806 set_scan_lines(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400807}
808
809static void
810handle_101111(struct bregs *regs)
811{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500812 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400813 set_scan_lines(14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400814}
815
816static void
817handle_101112(struct bregs *regs)
818{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500819 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400820 set_scan_lines(8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400821}
822
823static void
824handle_101114(struct bregs *regs)
825{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500826 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400827 set_scan_lines(16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400828}
829
830static void
831handle_101130(struct bregs *regs)
832{
Kevin O'Connore7132042009-05-25 00:10:35 -0400833 switch (regs->bh) {
834 case 0x00: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500835 struct segoff_s so = GET_IVT(0x1f);
836 regs->es = so.seg;
837 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400838 break;
839 }
840 case 0x01: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500841 struct segoff_s so = GET_IVT(0x43);
842 regs->es = so.seg;
843 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400844 break;
845 }
846 case 0x02:
847 regs->es = get_global_seg();
848 regs->bp = (u32)vgafont14;
849 break;
850 case 0x03:
851 regs->es = get_global_seg();
852 regs->bp = (u32)vgafont8;
853 break;
854 case 0x04:
855 regs->es = get_global_seg();
856 regs->bp = (u32)vgafont8 + 128 * 8;
857 break;
858 case 0x05:
859 regs->es = get_global_seg();
860 regs->bp = (u32)vgafont14alt;
861 break;
862 case 0x06:
863 regs->es = get_global_seg();
864 regs->bp = (u32)vgafont16;
865 break;
866 case 0x07:
867 regs->es = get_global_seg();
868 regs->bp = (u32)vgafont16alt;
869 break;
870 default:
871 dprintf(1, "Get font info BH(%02x) was discarded\n", regs->bh);
872 return;
873 }
874 // Set byte/char of on screen font
875 regs->cx = GET_BDA(char_height) & 0xff;
876
877 // Set Highest char row
878 regs->dx = GET_BDA(video_rows);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400879}
880
881static void
882handle_1011XX(struct bregs *regs)
883{
884 debug_stub(regs);
885}
886
887static void
888handle_1011(struct bregs *regs)
889{
890 switch (regs->al) {
891 case 0x00: handle_101100(regs); break;
892 case 0x01: handle_101101(regs); break;
893 case 0x02: handle_101102(regs); break;
894 case 0x03: handle_101103(regs); break;
895 case 0x04: handle_101104(regs); break;
896 case 0x10: handle_101110(regs); break;
897 case 0x11: handle_101111(regs); break;
898 case 0x12: handle_101112(regs); break;
899 case 0x14: handle_101114(regs); break;
900 case 0x30: handle_101130(regs); break;
901 default: handle_1011XX(regs); break;
902 }
903}
904
905
906static void
907handle_101210(struct bregs *regs)
908{
Kevin O'Connore7132042009-05-25 00:10:35 -0400909 u16 crtc_addr = GET_BDA(crtc_address);
910 if (crtc_addr == VGAREG_MDA_CRTC_ADDRESS)
911 regs->bx = 0x0103;
912 else
913 regs->bx = 0x0003;
914 regs->cx = GET_BDA(video_switches) & 0x0f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400915}
916
917static void
918handle_101230(struct bregs *regs)
919{
Kevin O'Connore7132042009-05-25 00:10:35 -0400920 u8 mctl = GET_BDA(modeset_ctl);
921 u8 vswt = GET_BDA(video_switches);
922 switch (regs->al) {
923 case 0x00:
924 // 200 lines
925 mctl = (mctl & ~0x10) | 0x80;
926 vswt = (vswt & ~0x0f) | 0x08;
927 break;
928 case 0x01:
929 // 350 lines
930 mctl &= ~0x90;
931 vswt = (vswt & ~0x0f) | 0x09;
932 break;
933 case 0x02:
934 // 400 lines
935 mctl = (mctl & ~0x80) | 0x10;
936 vswt = (vswt & ~0x0f) | 0x09;
937 break;
938 default:
939 dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
940 break;
941 }
942 SET_BDA(modeset_ctl, mctl);
943 SET_BDA(video_switches, vswt);
944 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400945}
946
947static void
948handle_101231(struct bregs *regs)
949{
Kevin O'Connore7132042009-05-25 00:10:35 -0400950 u8 v = (regs->al & 0x01) << 3;
951 u8 mctl = GET_BDA(video_ctl) & ~0x08;
952 SET_BDA(video_ctl, mctl | v);
953 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400954}
955
956static void
957handle_101232(struct bregs *regs)
958{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500959 stdvga_enable_video_addressing(regs->al);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400960 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400961}
962
963static void
964handle_101233(struct bregs *regs)
965{
Kevin O'Connore7132042009-05-25 00:10:35 -0400966 u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
967 u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
968 SET_BDA(modeset_ctl, v | v2);
969 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400970}
971
972static void
973handle_101234(struct bregs *regs)
974{
Kevin O'Connore7132042009-05-25 00:10:35 -0400975 u8 v = (regs->al & 0x01) ^ 0x01;
976 u8 v2 = GET_BDA(modeset_ctl) & ~0x01;
977 SET_BDA(modeset_ctl, v | v2);
978 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400979}
980
981static void
982handle_101235(struct bregs *regs)
983{
984 debug_stub(regs);
985 regs->al = 0x12;
986}
987
988static void
989handle_101236(struct bregs *regs)
990{
991 debug_stub(regs);
992 regs->al = 0x12;
993}
994
995static void
996handle_1012XX(struct bregs *regs)
997{
998 debug_stub(regs);
999}
1000
1001static void
1002handle_1012(struct bregs *regs)
1003{
Kevin O'Connore91ec7c2012-01-14 16:30:49 -05001004 if (CONFIG_VGA_CIRRUS && regs->bl >= 0x80) {
1005 clext_1012(regs);
1006 return;
1007 }
1008
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001009 switch (regs->bl) {
1010 case 0x10: handle_101210(regs); break;
1011 case 0x30: handle_101230(regs); break;
1012 case 0x31: handle_101231(regs); break;
1013 case 0x32: handle_101232(regs); break;
1014 case 0x33: handle_101233(regs); break;
1015 case 0x34: handle_101234(regs); break;
1016 case 0x35: handle_101235(regs); break;
1017 case 0x36: handle_101236(regs); break;
1018 default: handle_1012XX(regs); break;
1019 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001020}
1021
1022
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001023// Write string
Kevin O'Connor0ad77f02009-05-31 20:46:43 -04001024static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001025handle_1013(struct bregs *regs)
1026{
Kevin O'Connor918b1562009-05-25 11:05:18 -04001027 struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -04001028 // if row=0xff special case : use current cursor position
1029 if (cp.y == 0xff)
1030 cp = get_cursor_pos(cp.page);
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001031 u8 flag = regs->al;
1032 if (flag & 2)
1033 write_attr_string(&cp, regs->cx, regs->es, (void*)(regs->bp + 0));
1034 else
1035 write_string(&cp, regs->bl, regs->cx, regs->es, (void*)(regs->bp + 0));
1036
1037 if (flag & 1)
1038 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001039}
1040
1041
1042static void
1043handle_101a00(struct bregs *regs)
1044{
Kevin O'Connore7132042009-05-25 00:10:35 -04001045 regs->bx = GET_BDA(dcc_index);
1046 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001047}
1048
1049static void
1050handle_101a01(struct bregs *regs)
1051{
Kevin O'Connore7132042009-05-25 00:10:35 -04001052 SET_BDA(dcc_index, regs->bl);
1053 dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
1054 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001055}
1056
1057static void
1058handle_101aXX(struct bregs *regs)
1059{
1060 debug_stub(regs);
1061}
1062
1063static void
1064handle_101a(struct bregs *regs)
1065{
1066 switch (regs->al) {
1067 case 0x00: handle_101a00(regs); break;
1068 case 0x01: handle_101a01(regs); break;
1069 default: handle_101aXX(regs); break;
1070 }
1071}
1072
1073
Kevin O'Connorefb45232012-01-14 22:55:11 -05001074static u8 static_functionality[0x10] VAR16 = {
1075 /* 0 */ 0xff, // All modes supported #1
1076 /* 1 */ 0xe0, // All modes supported #2
1077 /* 2 */ 0x0f, // All modes supported #3
1078 /* 3 */ 0x00, 0x00, 0x00, 0x00, // reserved
1079 /* 7 */ 0x07, // 200, 350, 400 scan lines
1080 /* 8 */ 0x02, // mamimum number of visible charsets in text mode
1081 /* 9 */ 0x08, // total number of charset blocks in text mode
1082 /* a */ 0xe7, // Change to add new functions
1083 /* b */ 0x0c, // Change to add new functions
1084 /* c */ 0x00, // reserved
1085 /* d */ 0x00, // reserved
1086 /* e */ 0x00, // Change to add new functions
1087 /* f */ 0x00 // reserved
1088};
1089
Kevin O'Connorca668642009-05-21 23:06:08 -04001090struct funcInfo {
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001091 struct segoff_s static_functionality;
Kevin O'Connorca668642009-05-21 23:06:08 -04001092 u8 bda_0x49[30];
1093 u8 bda_0x84[3];
1094 u8 dcc_index;
1095 u8 dcc_alt;
1096 u16 colors;
1097 u8 pages;
1098 u8 scan_lines;
1099 u8 primary_char;
1100 u8 secondar_char;
1101 u8 misc;
1102 u8 non_vga_mode;
1103 u8 reserved_2f[2];
1104 u8 video_mem;
1105 u8 save_flags;
1106 u8 disp_info;
1107 u8 reserved_34[12];
1108};
1109
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001110static void
1111handle_101b(struct bregs *regs)
1112{
Kevin O'Connorca668642009-05-21 23:06:08 -04001113 u16 seg = regs->es;
1114 struct funcInfo *info = (void*)(regs->di+0);
1115 memset_far(seg, info, 0, sizeof(*info));
1116 // Address of static functionality table
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001117 SET_FARVAR(seg, info->static_functionality
1118 , SEGOFF(get_global_seg(), (u32)static_functionality));
Kevin O'Connorca668642009-05-21 23:06:08 -04001119
1120 // Hard coded copy from BIOS area. Should it be cleaner ?
Kevin O'Connor9f985422009-09-09 11:34:39 -04001121 memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49
1122 , sizeof(info->bda_0x49));
1123 memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84
1124 , sizeof(info->bda_0x84));
Kevin O'Connorca668642009-05-21 23:06:08 -04001125
1126 SET_FARVAR(seg, info->dcc_index, GET_BDA(dcc_index));
1127 SET_FARVAR(seg, info->colors, 16);
1128 SET_FARVAR(seg, info->pages, 8);
1129 SET_FARVAR(seg, info->scan_lines, 2);
1130 SET_FARVAR(seg, info->video_mem, 3);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001131 regs->al = 0x1B;
1132}
1133
1134
1135static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001136handle_101c(struct bregs *regs)
1137{
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001138 u16 seg = regs->es;
1139 void *data = (void*)(regs->bx+0);
1140 u16 states = regs->cx;
1141 if (states & ~0x07)
1142 goto fail;
1143 int ret;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001144 switch (regs->al) {
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001145 case 0x00:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001146 ret = vgahw_size_state(states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001147 if (ret < 0)
1148 goto fail;
1149 regs->bx = ret / 64;
1150 break;
1151 case 0x01:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001152 ret = vgahw_save_state(seg, data, states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001153 if (ret)
1154 goto fail;
1155 break;
1156 case 0x02:
Kevin O'Connor2469f892012-02-04 12:40:02 -05001157 ret = vgahw_restore_state(seg, data, states);
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001158 if (ret)
1159 goto fail;
1160 break;
1161 default:
1162 goto fail;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001163 }
Kevin O'Connor9f857fc2012-02-04 11:59:02 -05001164 regs->al = 0x1c;
1165fail:
1166 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001167}
1168
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001169static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001170handle_10XX(struct bregs *regs)
1171{
1172 debug_stub(regs);
1173}
1174
1175// INT 10h Video Support Service Entry Point
1176void VISIBLE16
1177handle_10(struct bregs *regs)
1178{
1179 debug_enter(regs, DEBUG_VGA_10);
1180 switch (regs->ah) {
1181 case 0x00: handle_1000(regs); break;
1182 case 0x01: handle_1001(regs); break;
1183 case 0x02: handle_1002(regs); break;
1184 case 0x03: handle_1003(regs); break;
1185 case 0x04: handle_1004(regs); break;
1186 case 0x05: handle_1005(regs); break;
1187 case 0x06: handle_1006(regs); break;
1188 case 0x07: handle_1007(regs); break;
1189 case 0x08: handle_1008(regs); break;
1190 case 0x09: handle_1009(regs); break;
1191 case 0x0a: handle_100a(regs); break;
1192 case 0x0b: handle_100b(regs); break;
1193 case 0x0c: handle_100c(regs); break;
1194 case 0x0d: handle_100d(regs); break;
1195 case 0x0e: handle_100e(regs); break;
1196 case 0x0f: handle_100f(regs); break;
1197 case 0x10: handle_1010(regs); break;
1198 case 0x11: handle_1011(regs); break;
1199 case 0x12: handle_1012(regs); break;
1200 case 0x13: handle_1013(regs); break;
1201 case 0x1a: handle_101a(regs); break;
1202 case 0x1b: handle_101b(regs); break;
1203 case 0x1c: handle_101c(regs); break;
1204 case 0x4f: handle_104f(regs); break;
1205 default: handle_10XX(regs); break;
1206 }
1207}
1208
1209
1210/****************************************************************
1211 * VGA post
1212 ****************************************************************/
1213
1214static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -05001215init_bios_area(void)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001216{
1217 // init detected hardware BIOS Area
1218 // set 80x25 color (not clear from RBIL but usual)
1219 u16 eqf = GET_BDA(equipment_list_flags);
1220 SET_BDA(equipment_list_flags, (eqf & 0xffcf) | 0x20);
1221
1222 // Just for the first int10 find its children
1223
1224 // the default char height
1225 SET_BDA(char_height, 0x10);
1226
1227 // Clear the screen
1228 SET_BDA(video_ctl, 0x60);
1229
1230 // Set the basic screen we have
1231 SET_BDA(video_switches, 0xf9);
1232
1233 // Set the basic modeset options
1234 SET_BDA(modeset_ctl, 0x51);
1235
1236 // Set the default MSR
1237 SET_BDA(video_msr, 0x09);
1238}
1239
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -05001240int VgaBDF VAR16 = -1;
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001241int HaveRunInit VAR16;
Kevin O'Connor161d2012011-12-31 19:42:21 -05001242
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001243void VISIBLE16
1244vga_post(struct bregs *regs)
1245{
1246 debug_enter(regs, DEBUG_VGA_POST);
1247
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001248 if (CONFIG_VGA_PCI && !GET_GLOBAL(HaveRunInit)) {
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -05001249 u16 bdf = regs->ax;
Kevin O'Connor2af8ba12012-01-29 12:17:33 -05001250 if ((pci_config_readw(bdf, PCI_VENDOR_ID)
1251 == GET_GLOBAL(rom_pci_data.vendor))
1252 && (pci_config_readw(bdf, PCI_DEVICE_ID)
1253 == GET_GLOBAL(rom_pci_data.device)))
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -05001254 SET_VGA(VgaBDF, bdf);
1255 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001256
Kevin O'Connor161d2012011-12-31 19:42:21 -05001257 int ret = vgahw_init();
1258 if (ret) {
1259 dprintf(1, "Failed to initialize VGA hardware. Exiting.\n");
1260 return;
1261 }
Kevin O'Connor4c52fb42011-12-24 00:44:07 -05001262
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001263 if (GET_GLOBAL(HaveRunInit))
1264 return;
1265
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001266 init_bios_area();
1267
Kevin O'Connoraad3b692012-01-14 23:15:40 -05001268 SET_VGA(video_save_pointer_table.videoparam
1269 , SEGOFF(get_global_seg(), (u32)video_param_table));
1270 stdvga_build_video_param();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001271
1272 extern void entry_10(void);
Kevin O'Connor9f985422009-09-09 11:34:39 -04001273 SET_IVT(0x10, SEGOFF(get_global_seg(), (u32)entry_10));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001274
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001275 // XXX - clear screen and display info
1276
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -05001277 SET_VGA(HaveRunInit, 1);
1278
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001279 // Fixup checksum
1280 extern u8 _rom_header_size, _rom_header_checksum;
1281 SET_VGA(_rom_header_checksum, 0);
Kevin O'Connord113a992009-05-16 21:05:02 -04001282 u8 sum = -checksum_far(get_global_seg(), 0, _rom_header_size * 512);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001283 SET_VGA(_rom_header_checksum, sum);
1284}