blob: 5083292e5317fa08864b23178dc0ec22a534194c [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'Connore1e000b2011-12-31 03:30:40 -050017#include "vgabios.h" // find_vga_entry
Julian Pidancet7c6509c2011-12-19 05:07:55 +000018#include "optionroms.h" // struct pci_data
19#include "config.h" // CONFIG_*
Kevin O'Connor88ca7412011-12-31 04:24:20 -050020#include "stdvga.h" // stdvga_screen_disable
Kevin O'Connor4c52fb42011-12-24 00:44:07 -050021#include "geodelx.h" // geodelx_init
Kevin O'Connorf1e217d2011-12-31 03:18:18 -050022#include "bochsvga.h" // bochsvga_init
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040023
24// XXX
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040025#define DEBUG_VGA_POST 1
26#define DEBUG_VGA_10 3
27
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040028
Julian Pidancet7c6509c2011-12-19 05:07:55 +000029/****************************************************************
30 * PCI Data
31 ****************************************************************/
32#if CONFIG_VGA_PCI == 1
33struct pci_data rom_pci_data VAR16VISIBLE = {
34 .signature = PCI_ROM_SIGNATURE,
35 .vendor = CONFIG_VGA_VID,
36 .device = CONFIG_VGA_DID,
37 .dlen = 0x18,
38 .class_hi = 0x300,
39 .irevision = 1,
40 .type = PCIROM_CODETYPE_X86,
41 .indicator = 0x80,
42};
43#endif
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040044
45/****************************************************************
46 * Helper functions
47 ****************************************************************/
48
Kevin O'Connor217f2bc2009-05-31 00:46:47 -040049static inline void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040050call16_vgaint(u32 eax, u32 ebx)
51{
52 asm volatile(
53 "int $0x10\n"
54 "cli\n"
55 "cld"
56 :
57 : "a"(eax), "b"(ebx)
58 : "cc", "memory");
59}
60
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040061static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040062perform_gray_scale_summing(u16 start, u16 count)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040063{
Kevin O'Connor88ca7412011-12-31 04:24:20 -050064 stdvga_screen_disable();
Kevin O'Connordd2be772009-05-16 15:41:23 -040065 int i;
66 for (i = start; i < start+count; i++) {
Kevin O'Connora0ecb052009-05-18 23:34:00 -040067 u8 rgb[3];
Kevin O'Connor88ca7412011-12-31 04:24:20 -050068 stdvga_get_dac_regs(GET_SEG(SS), rgb, i, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040069
70 // intensity = ( 0.3 * Red ) + ( 0.59 * Green ) + ( 0.11 * Blue )
Kevin O'Connora0ecb052009-05-18 23:34:00 -040071 u16 intensity = ((77 * rgb[0] + 151 * rgb[1] + 28 * rgb[2]) + 0x80) >> 8;
Kevin O'Connordd2be772009-05-16 15:41:23 -040072 if (intensity > 0x3f)
73 intensity = 0x3f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040074
Kevin O'Connor88ca7412011-12-31 04:24:20 -050075 stdvga_set_dac_regs(GET_SEG(SS), rgb, i, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040076 }
Kevin O'Connor88ca7412011-12-31 04:24:20 -050077 stdvga_screen_enable();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040078}
79
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040080static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040081set_cursor_shape(u8 start, u8 end)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040082{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040083 start &= 0x3f;
84 end &= 0x1f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040085
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040086 u16 curs = (start << 8) + end;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040087 SET_BDA(cursor_type, curs);
88
Kevin O'Connordd2be772009-05-16 15:41:23 -040089 u8 modeset_ctl = GET_BDA(modeset_ctl);
90 u16 cheight = GET_BDA(char_height);
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040091 if ((modeset_ctl & 0x01) && (cheight > 8) && (end < 8) && (start < 0x20)) {
92 if (end != (start + 1))
93 start = ((start + 1) * cheight / 8) - 1;
Kevin O'Connordd2be772009-05-16 15:41:23 -040094 else
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040095 start = ((end + 1) * cheight / 8) - 2;
96 end = ((end + 1) * cheight / 8) - 1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040097 }
Kevin O'Connor88ca7412011-12-31 04:24:20 -050098 stdvga_set_cursor_shape(start, end);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040099}
100
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400101static u16
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400102get_cursor_shape(u8 page)
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400103{
104 if (page > 7)
105 return 0;
106 // FIXME should handle VGA 14/16 lines
107 return GET_BDA(cursor_type);
108}
109
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400110static void
Kevin O'Connor918b1562009-05-25 11:05:18 -0400111set_cursor_pos(struct cursorpos cp)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400112{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400113 // Should not happen...
Kevin O'Connor918b1562009-05-25 11:05:18 -0400114 if (cp.page > 7)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400115 return;
116
117 // Bios cursor pos
Kevin O'Connor918b1562009-05-25 11:05:18 -0400118 SET_BDA(cursor_pos[cp.page], (cp.y << 8) | cp.x);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400119
120 // Set the hardware cursor
Kevin O'Connordd2be772009-05-16 15:41:23 -0400121 u8 current = GET_BDA(video_page);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400122 if (cp.page != current)
Kevin O'Connordd2be772009-05-16 15:41:23 -0400123 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400124
Kevin O'Connordd2be772009-05-16 15:41:23 -0400125 // Get the dimensions
126 u16 nbcols = GET_BDA(video_cols);
127 u16 nbrows = GET_BDA(video_rows) + 1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400128
Kevin O'Connordd2be772009-05-16 15:41:23 -0400129 // Calculate the address knowing nbcols nbrows and page num
Kevin O'Connor918b1562009-05-25 11:05:18 -0400130 u16 address = (SCREEN_IO_START(nbcols, nbrows, cp.page)
131 + cp.x + cp.y * nbcols);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400132
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500133 stdvga_set_cursor_pos(address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400134}
135
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400136static struct cursorpos
Kevin O'Connor918b1562009-05-25 11:05:18 -0400137get_cursor_pos(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400138{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400139 if (page == 0xff)
140 // special case - use current page
141 page = GET_BDA(video_page);
142 if (page > 7) {
143 struct cursorpos cp = { 0, 0, 0xfe };
144 return cp;
145 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400146 // FIXME should handle VGA 14/16 lines
Kevin O'Connor918b1562009-05-25 11:05:18 -0400147 u16 xy = GET_BDA(cursor_pos[page]);
148 struct cursorpos cp = {xy, xy>>8, page};
149 return cp;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400150}
151
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400152static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400153set_active_page(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400154{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400155 if (page > 7)
156 return;
157
158 // Get the mode
Kevin O'Connor5727c292009-05-16 17:29:32 -0400159 struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
160 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400161 return;
162
163 // Get pos curs pos for the right page
Kevin O'Connor918b1562009-05-25 11:05:18 -0400164 struct cursorpos cp = get_cursor_pos(page);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400165
Kevin O'Connordd2be772009-05-16 15:41:23 -0400166 u16 address;
Kevin O'Connore4f220f2009-05-25 23:37:13 -0400167 if (GET_GLOBAL(vmode_g->memmodel) & TEXT) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400168 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -0400169 u16 nbcols = GET_BDA(video_cols);
170 u16 nbrows = GET_BDA(video_rows) + 1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400171
172 // Calculate the address knowing nbcols nbrows and page num
173 address = SCREEN_MEM_START(nbcols, nbrows, page);
174 SET_BDA(video_pagestart, address);
175
176 // Start address
177 address = SCREEN_IO_START(nbcols, nbrows, page);
178 } else {
Kevin O'Connor87233e92011-12-23 21:40:34 -0500179 address = page * GET_GLOBAL(vmode_g->slength);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400180 }
181
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500182 stdvga_set_active_page(address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400183
184 // And change the BIOS page
185 SET_BDA(video_page, page);
186
Kevin O'Connora12c2152009-05-13 22:06:16 -0400187 dprintf(1, "Set active page %02x address %04x\n", page, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400188
189 // Display the cursor, now the page is active
Kevin O'Connor918b1562009-05-25 11:05:18 -0400190 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400191}
192
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400193static void
194set_scan_lines(u8 lines)
195{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500196 stdvga_set_scan_lines(lines);
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400197 if (lines == 8)
198 set_cursor_shape(0x06, 0x07);
199 else
200 set_cursor_shape(lines - 4, lines - 3);
201 SET_BDA(char_height, lines);
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500202 u16 vde = stdvga_get_vde();
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400203 u8 rows = vde / lines;
204 SET_BDA(video_rows, rows - 1);
205 u16 cols = GET_BDA(video_cols);
206 SET_BDA(video_pagesize, rows * cols * 2);
207}
208
209
210/****************************************************************
211 * Character writing
212 ****************************************************************/
213
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400214// Scroll the screen one line. This function is designed to be called
215// tail-recursive to reduce stack usage.
216static void noinline
217scroll_one(u16 nbrows, u16 nbcols, u8 page)
Kevin O'Connor09262412009-05-25 11:44:11 -0400218{
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400219 struct cursorpos ul = {0, 0, page};
220 struct cursorpos lr = {nbcols-1, nbrows-1, page};
221 vgafb_scroll(1, -1, ul, lr);
222}
223
224// Write a character to the screen at a given position. Implement
225// special characters and scroll the screen if necessary.
226static void
227write_teletype(struct cursorpos *pcp, struct carattr ca)
228{
229 struct cursorpos cp = *pcp;
230
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400231 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -0400232 u16 nbrows = GET_BDA(video_rows) + 1;
233 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400234
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400235 switch (ca.car) {
236 case 7:
237 //FIXME should beep
238 break;
239 case 8:
240 if (cp.x > 0)
241 cp.x--;
242 break;
243 case '\r':
244 cp.x = 0;
245 break;
246 case '\n':
247 cp.y++;
248 break;
249 case '\t':
250 do {
251 struct carattr dummyca = {' ', ca.attr, ca.use_attr};
252 vgafb_write_char(cp, dummyca);
253 cp.x++;
254 } while (cp.x < nbcols && cp.x % 8);
255 break;
256 default:
257 vgafb_write_char(cp, ca);
258 cp.x++;
259 }
260
Kevin O'Connor82221b22009-05-26 00:20:40 -0400261 // Do we need to wrap ?
262 if (cp.x == nbcols) {
263 cp.x = 0;
264 cp.y++;
265 }
266 // Do we need to scroll ?
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400267 if (cp.y < nbrows) {
268 *pcp = cp;
269 return;
Kevin O'Connor82221b22009-05-26 00:20:40 -0400270 }
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400271 // Scroll screen
272 cp.y--;
273 *pcp = cp;
274 scroll_one(nbrows, nbcols, cp.page);
Kevin O'Connor82221b22009-05-26 00:20:40 -0400275}
276
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400277// Write out a buffer of alternating characters and attributes.
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400278static void
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400279write_attr_string(struct cursorpos *pcp, u16 count, u16 seg, u8 *offset_far)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400280{
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400281 while (count--) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400282 u8 car = GET_FARVAR(seg, *offset_far);
283 offset_far++;
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400284 u8 attr = GET_FARVAR(seg, *offset_far);
285 offset_far++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400286
Kevin O'Connor09262412009-05-25 11:44:11 -0400287 struct carattr ca = {car, attr, 1};
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400288 write_teletype(pcp, ca);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400289 }
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400290}
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400291
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400292// Write out a buffer of characters.
293static void
294write_string(struct cursorpos *pcp, u8 attr, u16 count, u16 seg, u8 *offset_far)
295{
296 while (count--) {
297 u8 car = GET_FARVAR(seg, *offset_far);
298 offset_far++;
299
300 struct carattr ca = {car, attr, 1};
301 write_teletype(pcp, ca);
302 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400303}
304
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400305
306/****************************************************************
307 * Save and restore bda state
308 ****************************************************************/
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400309
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400310static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400311save_bda_state(u16 seg, struct saveBDAstate *info)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400312{
Kevin O'Connorca668642009-05-21 23:06:08 -0400313 SET_FARVAR(seg, info->video_mode, GET_BDA(video_mode));
314 SET_FARVAR(seg, info->video_cols, GET_BDA(video_cols));
315 SET_FARVAR(seg, info->video_pagesize, GET_BDA(video_pagesize));
316 SET_FARVAR(seg, info->crtc_address, GET_BDA(crtc_address));
317 SET_FARVAR(seg, info->video_rows, GET_BDA(video_rows));
318 SET_FARVAR(seg, info->char_height, GET_BDA(char_height));
319 SET_FARVAR(seg, info->video_ctl, GET_BDA(video_ctl));
320 SET_FARVAR(seg, info->video_switches, GET_BDA(video_switches));
321 SET_FARVAR(seg, info->modeset_ctl, GET_BDA(modeset_ctl));
322 SET_FARVAR(seg, info->cursor_type, GET_BDA(cursor_type));
323 u16 i;
324 for (i=0; i<8; i++)
325 SET_FARVAR(seg, info->cursor_pos[i], GET_BDA(cursor_pos[i]));
326 SET_FARVAR(seg, info->video_pagestart, GET_BDA(video_pagestart));
327 SET_FARVAR(seg, info->video_page, GET_BDA(video_page));
328 /* current font */
Kevin O'Connor9f985422009-09-09 11:34:39 -0400329 SET_FARVAR(seg, info->font0, GET_IVT(0x1f));
330 SET_FARVAR(seg, info->font1, GET_IVT(0x43));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400331}
332
Kevin O'Connorca668642009-05-21 23:06:08 -0400333static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400334restore_bda_state(u16 seg, struct saveBDAstate *info)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400335{
Kevin O'Connorca668642009-05-21 23:06:08 -0400336 SET_BDA(video_mode, GET_FARVAR(seg, info->video_mode));
337 SET_BDA(video_cols, GET_FARVAR(seg, info->video_cols));
338 SET_BDA(video_pagesize, GET_FARVAR(seg, info->video_pagesize));
339 SET_BDA(crtc_address, GET_FARVAR(seg, info->crtc_address));
340 SET_BDA(video_rows, GET_FARVAR(seg, info->video_rows));
341 SET_BDA(char_height, GET_FARVAR(seg, info->char_height));
342 SET_BDA(video_ctl, GET_FARVAR(seg, info->video_ctl));
343 SET_BDA(video_switches, GET_FARVAR(seg, info->video_switches));
344 SET_BDA(modeset_ctl, GET_FARVAR(seg, info->modeset_ctl));
345 SET_BDA(cursor_type, GET_FARVAR(seg, info->cursor_type));
346 u16 i;
347 for (i = 0; i < 8; i++)
348 SET_BDA(cursor_pos[i], GET_FARVAR(seg, info->cursor_pos[i]));
349 SET_BDA(video_pagestart, GET_FARVAR(seg, info->video_pagestart));
350 SET_BDA(video_page, GET_FARVAR(seg, info->video_page));
351 /* current font */
Kevin O'Connor9f985422009-09-09 11:34:39 -0400352 SET_IVT(0x1f, GET_FARVAR(seg, info->font0));
353 SET_IVT(0x43, GET_FARVAR(seg, info->font1));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400354}
355
356
357/****************************************************************
358 * VGA int 10 handler
359 ****************************************************************/
360
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400361// set video mode
Julian Pidancet87879e22011-12-19 05:08:00 +0000362void
363vga_set_mode(u8 mode, u8 noclearmem)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400364{
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400365 // find the entry in the video modes
366 struct vgamode_s *vmode_g = find_vga_entry(mode);
367 dprintf(1, "mode search %02x found %p\n", mode, vmode_g);
368 if (!vmode_g)
369 return;
370
371 // Read the bios mode set control
372 u8 modeset_ctl = GET_BDA(modeset_ctl);
373
374 // Then we know the number of lines
375// FIXME
376
377 // if palette loading (bit 3 of modeset ctl = 0)
378 if ((modeset_ctl & 0x08) == 0) { // Set the PEL mask
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500379 stdvga_set_pel_mask(GET_GLOBAL(vmode_g->pelmask));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400380
381 // From which palette
382 u8 *palette_g = GET_GLOBAL(vmode_g->dac);
383 u16 palsize = GET_GLOBAL(vmode_g->dacsize) / 3;
384
385 // Always 256*3 values
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500386 stdvga_set_dac_regs(get_global_seg(), palette_g, 0, palsize);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400387 u16 i;
388 for (i = palsize; i < 0x0100; i++) {
389 static u8 rgb[3] VAR16;
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500390 stdvga_set_dac_regs(get_global_seg(), rgb, i, 1);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400391 }
392
393 if ((modeset_ctl & 0x02) == 0x02)
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400394 perform_gray_scale_summing(0x00, 0x100);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400395 }
396
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500397 stdvga_set_mode(vmode_g);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400398
399 if (noclearmem == 0x00)
400 clear_screen(vmode_g);
401
402 // Set CRTC address VGA or MDA
403 u16 crtc_addr = VGAREG_VGA_CRTC_ADDRESS;
404 if (GET_GLOBAL(vmode_g->memmodel) == MTEXT)
405 crtc_addr = VGAREG_MDA_CRTC_ADDRESS;
406
407 // Set the BIOS mem
Kevin O'Connor87233e92011-12-23 21:40:34 -0500408 u16 cheight = GET_GLOBAL(vmode_g->cheight);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400409 SET_BDA(video_mode, mode);
Kevin O'Connor87233e92011-12-23 21:40:34 -0500410 SET_BDA(video_cols, GET_GLOBAL(vmode_g->twidth));
411 SET_BDA(video_pagesize, GET_GLOBAL(vmode_g->slength));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400412 SET_BDA(crtc_address, crtc_addr);
Kevin O'Connor87233e92011-12-23 21:40:34 -0500413 SET_BDA(video_rows, GET_GLOBAL(vmode_g->theight)-1);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400414 SET_BDA(char_height, cheight);
415 SET_BDA(video_ctl, (0x60 | noclearmem));
416 SET_BDA(video_switches, 0xF9);
417 SET_BDA(modeset_ctl, GET_BDA(modeset_ctl) & 0x7f);
418
419 // FIXME We nearly have the good tables. to be reworked
420 SET_BDA(dcc_index, 0x08); // 8 is VGA should be ok for now
Kevin O'Connor9f985422009-09-09 11:34:39 -0400421 SET_BDA(video_savetable
Kevin O'Connor815e4472011-12-21 09:05:32 -0500422 , SEGOFF(get_global_seg(), (u32)&video_save_pointer_table));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400423
424 // FIXME
425 SET_BDA(video_msr, 0x00); // Unavailable on vanilla vga, but...
426 SET_BDA(video_pal, 0x00); // Unavailable on vanilla vga, but...
427
428 // Set cursor shape
Kevin O'Connore4f220f2009-05-25 23:37:13 -0400429 if (GET_GLOBAL(vmode_g->memmodel) & TEXT)
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400430 set_cursor_shape(0x06, 0x07);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400431 // Set cursor pos for page 0..7
432 int i;
Kevin O'Connor918b1562009-05-25 11:05:18 -0400433 for (i = 0; i < 8; i++) {
434 struct cursorpos cp = {0, 0, i};
435 set_cursor_pos(cp);
436 }
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400437
438 // Set active page 0
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400439 set_active_page(0x00);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400440
441 // Write the fonts in memory
Kevin O'Connore4f220f2009-05-25 23:37:13 -0400442 if (GET_GLOBAL(vmode_g->memmodel) & TEXT) {
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400443 call16_vgaint(0x1104, 0);
444 call16_vgaint(0x1103, 0);
445 }
446 // Set the ints 0x1F and 0x43
Kevin O'Connor9f985422009-09-09 11:34:39 -0400447 SET_IVT(0x1f, SEGOFF(get_global_seg(), (u32)&vgafont8[128 * 8]));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400448
449 switch (cheight) {
450 case 8:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400451 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont8));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400452 break;
453 case 14:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400454 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont14));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400455 break;
456 case 16:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400457 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont16));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400458 break;
459 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400460}
461
462static void
Julian Pidancet87879e22011-12-19 05:08:00 +0000463handle_1000(struct bregs *regs)
464{
465 u8 noclearmem = regs->al & 0x80;
466 u8 mode = regs->al & 0x7f;
467
468 // Set regs->al
469 if (mode > 7)
470 regs->al = 0x20;
471 else if (mode == 6)
472 regs->al = 0x3f;
473 else
474 regs->al = 0x30;
475
Kevin O'Connore48a5372011-12-20 23:56:14 -0500476 if (CONFIG_VGA_CIRRUS) {
477 int ret = cirrus_set_video_mode(mode, noclearmem);
478 if (ret)
479 return;
480 }
Julian Pidancet87879e22011-12-19 05:08:00 +0000481
Kevin O'Connorf1e217d2011-12-31 03:18:18 -0500482 if (bochsvga_enabled())
483 bochsvga_hires_enable(0);
Julian Pidancet87879e22011-12-19 05:08:00 +0000484
485 vga_set_mode(mode, noclearmem);
486}
487
488static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400489handle_1001(struct bregs *regs)
490{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400491 set_cursor_shape(regs->ch, regs->cl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400492}
493
494static void
495handle_1002(struct bregs *regs)
496{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400497 struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
498 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400499}
500
501static void
502handle_1003(struct bregs *regs)
503{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400504 regs->cx = get_cursor_shape(regs->bh);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400505 struct cursorpos cp = get_cursor_pos(regs->bh);
506 regs->dl = cp.x;
507 regs->dh = cp.y;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400508}
509
510// Read light pen pos (unimplemented)
511static void
512handle_1004(struct bregs *regs)
513{
514 debug_stub(regs);
515 regs->ax = regs->bx = regs->cx = regs->dx = 0;
516}
517
518static void
519handle_1005(struct bregs *regs)
520{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400521 set_active_page(regs->al);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400522}
523
524static void
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400525verify_scroll(struct bregs *regs, int dir)
526{
527 u8 page = GET_BDA(video_page);
528 struct cursorpos ul = {regs->cl, regs->ch, page};
529 struct cursorpos lr = {regs->dl, regs->dh, page};
530
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400531 u16 nbrows = GET_BDA(video_rows) + 1;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400532 if (lr.y >= nbrows)
533 lr.y = nbrows - 1;
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400534 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400535 if (lr.x >= nbcols)
536 lr.x = nbcols - 1;
537
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400538 if (ul.x > lr.x || ul.y > lr.y)
539 return;
540
541 u16 nblines = regs->al;
542 if (!nblines || nblines > lr.y - ul.y + 1)
543 nblines = lr.y - ul.y + 1;
544
545 vgafb_scroll(dir * nblines, regs->bh, ul, lr);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400546}
547
548static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400549handle_1006(struct bregs *regs)
550{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400551 verify_scroll(regs, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400552}
553
554static void
555handle_1007(struct bregs *regs)
556{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400557 verify_scroll(regs, -1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400558}
559
560static void
561handle_1008(struct bregs *regs)
562{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400563 struct carattr ca = vgafb_read_char(get_cursor_pos(regs->bh));
Kevin O'Connor09262412009-05-25 11:44:11 -0400564 regs->al = ca.car;
565 regs->ah = ca.attr;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400566}
567
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400568static void noinline
Kevin O'Connord3b38152009-05-26 00:05:37 -0400569write_chars(u8 page, struct carattr ca, u16 count)
570{
571 struct cursorpos cp = get_cursor_pos(page);
572 while (count--) {
573 vgafb_write_char(cp, ca);
574 cp.x++;
575 }
576}
577
578static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400579handle_1009(struct bregs *regs)
580{
Kevin O'Connor09262412009-05-25 11:44:11 -0400581 struct carattr ca = {regs->al, regs->bl, 1};
Kevin O'Connord3b38152009-05-26 00:05:37 -0400582 write_chars(regs->bh, ca, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400583}
584
585static void
586handle_100a(struct bregs *regs)
587{
Kevin O'Connor09262412009-05-25 11:44:11 -0400588 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connord3b38152009-05-26 00:05:37 -0400589 write_chars(regs->bh, ca, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400590}
591
592
593static void
594handle_100b00(struct bregs *regs)
595{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500596 stdvga_set_border_color(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400597}
598
599static void
600handle_100b01(struct bregs *regs)
601{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500602 stdvga_set_palette(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400603}
604
605static void
606handle_100bXX(struct bregs *regs)
607{
608 debug_stub(regs);
609}
610
611static void
612handle_100b(struct bregs *regs)
613{
614 switch (regs->bh) {
615 case 0x00: handle_100b00(regs); break;
616 case 0x01: handle_100b01(regs); break;
617 default: handle_100bXX(regs); break;
618 }
619}
620
621
622static void
623handle_100c(struct bregs *regs)
624{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400625 // XXX - page (regs->bh) is unused
626 vgafb_write_pixel(regs->al, regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400627}
628
629static void
630handle_100d(struct bregs *regs)
631{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400632 // XXX - page (regs->bh) is unused
633 regs->al = vgafb_read_pixel(regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400634}
635
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400636static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400637handle_100e(struct bregs *regs)
638{
639 // Ralf Brown Interrupt list is WRONG on bh(page)
640 // We do output only on the current page !
Kevin O'Connor09262412009-05-25 11:44:11 -0400641 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connor116a0442009-05-26 00:47:32 -0400642 struct cursorpos cp = get_cursor_pos(0xff);
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400643 write_teletype(&cp, ca);
Kevin O'Connor116a0442009-05-26 00:47:32 -0400644 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400645}
646
647static void
648handle_100f(struct bregs *regs)
649{
Kevin O'Connore7132042009-05-25 00:10:35 -0400650 regs->bh = GET_BDA(video_page);
651 regs->al = GET_BDA(video_mode) | (GET_BDA(video_ctl) & 0x80);
652 regs->ah = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400653}
654
655
656static void
657handle_101000(struct bregs *regs)
658{
659 if (regs->bl > 0x14)
660 return;
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500661 stdvga_set_single_palette_reg(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400662}
663
664static void
665handle_101001(struct bregs *regs)
666{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500667 stdvga_set_overscan_border_color(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400668}
669
670static void
671handle_101002(struct bregs *regs)
672{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500673 stdvga_set_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400674}
675
676static void
677handle_101003(struct bregs *regs)
678{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500679 stdvga_toggle_intensity(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400680}
681
682static void
683handle_101007(struct bregs *regs)
684{
685 if (regs->bl > 0x14)
686 return;
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500687 regs->bh = stdvga_get_single_palette_reg(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400688}
689
690static void
691handle_101008(struct bregs *regs)
692{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500693 regs->bh = stdvga_get_overscan_border_color();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400694}
695
696static void
697handle_101009(struct bregs *regs)
698{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500699 stdvga_get_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400700}
701
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400702static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400703handle_101010(struct bregs *regs)
704{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400705 u8 rgb[3] = {regs->dh, regs->ch, regs->cl};
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500706 stdvga_set_dac_regs(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400707}
708
709static void
710handle_101012(struct bregs *regs)
711{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500712 stdvga_set_dac_regs(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400713}
714
715static void
716handle_101013(struct bregs *regs)
717{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500718 stdvga_select_video_dac_color_page(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400719}
720
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400721static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400722handle_101015(struct bregs *regs)
723{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400724 u8 rgb[3];
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500725 stdvga_get_dac_regs(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400726 regs->dh = rgb[0];
727 regs->ch = rgb[1];
728 regs->cl = rgb[2];
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400729}
730
731static void
732handle_101017(struct bregs *regs)
733{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500734 stdvga_get_dac_regs(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400735}
736
737static void
738handle_101018(struct bregs *regs)
739{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500740 stdvga_set_pel_mask(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400741}
742
743static void
744handle_101019(struct bregs *regs)
745{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500746 regs->bl = stdvga_get_pel_mask();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400747}
748
749static void
750handle_10101a(struct bregs *regs)
751{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500752 stdvga_read_video_dac_state(&regs->bl, &regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400753}
754
755static void
756handle_10101b(struct bregs *regs)
757{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400758 perform_gray_scale_summing(regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400759}
760
761static void
762handle_1010XX(struct bregs *regs)
763{
764 debug_stub(regs);
765}
766
767static void
768handle_1010(struct bregs *regs)
769{
770 switch (regs->al) {
771 case 0x00: handle_101000(regs); break;
772 case 0x01: handle_101001(regs); break;
773 case 0x02: handle_101002(regs); break;
774 case 0x03: handle_101003(regs); break;
775 case 0x07: handle_101007(regs); break;
776 case 0x08: handle_101008(regs); break;
777 case 0x09: handle_101009(regs); break;
778 case 0x10: handle_101010(regs); break;
779 case 0x12: handle_101012(regs); break;
780 case 0x13: handle_101013(regs); break;
781 case 0x15: handle_101015(regs); break;
782 case 0x17: handle_101017(regs); break;
783 case 0x18: handle_101018(regs); break;
784 case 0x19: handle_101019(regs); break;
785 case 0x1a: handle_10101a(regs); break;
786 case 0x1b: handle_10101b(regs); break;
787 default: handle_1010XX(regs); break;
788 }
789}
790
791
792static void
793handle_101100(struct bregs *regs)
794{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500795 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
796 , regs->dx, regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400797}
798
799static void
800handle_101101(struct bregs *regs)
801{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500802 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400803}
804
805static void
806handle_101102(struct bregs *regs)
807{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500808 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400809}
810
811static void
812handle_101103(struct bregs *regs)
813{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500814 stdvga_set_text_block_specifier(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400815}
816
817static void
818handle_101104(struct bregs *regs)
819{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500820 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400821}
822
823static void
824handle_101110(struct bregs *regs)
825{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500826 stdvga_load_font(regs->es, (void*)(regs->bp+0), regs->cx
827 , regs->dx, regs->bl, regs->bh);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400828 set_scan_lines(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400829}
830
831static void
832handle_101111(struct bregs *regs)
833{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500834 stdvga_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400835 set_scan_lines(14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400836}
837
838static void
839handle_101112(struct bregs *regs)
840{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500841 stdvga_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400842 set_scan_lines(8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400843}
844
845static void
846handle_101114(struct bregs *regs)
847{
Kevin O'Connor2bec7d62011-12-31 04:31:16 -0500848 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400849 set_scan_lines(16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400850}
851
852static void
853handle_101130(struct bregs *regs)
854{
Kevin O'Connore7132042009-05-25 00:10:35 -0400855 switch (regs->bh) {
856 case 0x00: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500857 struct segoff_s so = GET_IVT(0x1f);
858 regs->es = so.seg;
859 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400860 break;
861 }
862 case 0x01: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500863 struct segoff_s so = GET_IVT(0x43);
864 regs->es = so.seg;
865 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400866 break;
867 }
868 case 0x02:
869 regs->es = get_global_seg();
870 regs->bp = (u32)vgafont14;
871 break;
872 case 0x03:
873 regs->es = get_global_seg();
874 regs->bp = (u32)vgafont8;
875 break;
876 case 0x04:
877 regs->es = get_global_seg();
878 regs->bp = (u32)vgafont8 + 128 * 8;
879 break;
880 case 0x05:
881 regs->es = get_global_seg();
882 regs->bp = (u32)vgafont14alt;
883 break;
884 case 0x06:
885 regs->es = get_global_seg();
886 regs->bp = (u32)vgafont16;
887 break;
888 case 0x07:
889 regs->es = get_global_seg();
890 regs->bp = (u32)vgafont16alt;
891 break;
892 default:
893 dprintf(1, "Get font info BH(%02x) was discarded\n", regs->bh);
894 return;
895 }
896 // Set byte/char of on screen font
897 regs->cx = GET_BDA(char_height) & 0xff;
898
899 // Set Highest char row
900 regs->dx = GET_BDA(video_rows);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400901}
902
903static void
904handle_1011XX(struct bregs *regs)
905{
906 debug_stub(regs);
907}
908
909static void
910handle_1011(struct bregs *regs)
911{
912 switch (regs->al) {
913 case 0x00: handle_101100(regs); break;
914 case 0x01: handle_101101(regs); break;
915 case 0x02: handle_101102(regs); break;
916 case 0x03: handle_101103(regs); break;
917 case 0x04: handle_101104(regs); break;
918 case 0x10: handle_101110(regs); break;
919 case 0x11: handle_101111(regs); break;
920 case 0x12: handle_101112(regs); break;
921 case 0x14: handle_101114(regs); break;
922 case 0x30: handle_101130(regs); break;
923 default: handle_1011XX(regs); break;
924 }
925}
926
927
928static void
929handle_101210(struct bregs *regs)
930{
Kevin O'Connore7132042009-05-25 00:10:35 -0400931 u16 crtc_addr = GET_BDA(crtc_address);
932 if (crtc_addr == VGAREG_MDA_CRTC_ADDRESS)
933 regs->bx = 0x0103;
934 else
935 regs->bx = 0x0003;
936 regs->cx = GET_BDA(video_switches) & 0x0f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400937}
938
939static void
940handle_101230(struct bregs *regs)
941{
Kevin O'Connore7132042009-05-25 00:10:35 -0400942 u8 mctl = GET_BDA(modeset_ctl);
943 u8 vswt = GET_BDA(video_switches);
944 switch (regs->al) {
945 case 0x00:
946 // 200 lines
947 mctl = (mctl & ~0x10) | 0x80;
948 vswt = (vswt & ~0x0f) | 0x08;
949 break;
950 case 0x01:
951 // 350 lines
952 mctl &= ~0x90;
953 vswt = (vswt & ~0x0f) | 0x09;
954 break;
955 case 0x02:
956 // 400 lines
957 mctl = (mctl & ~0x80) | 0x10;
958 vswt = (vswt & ~0x0f) | 0x09;
959 break;
960 default:
961 dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
962 break;
963 }
964 SET_BDA(modeset_ctl, mctl);
965 SET_BDA(video_switches, vswt);
966 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400967}
968
969static void
970handle_101231(struct bregs *regs)
971{
Kevin O'Connore7132042009-05-25 00:10:35 -0400972 u8 v = (regs->al & 0x01) << 3;
973 u8 mctl = GET_BDA(video_ctl) & ~0x08;
974 SET_BDA(video_ctl, mctl | v);
975 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400976}
977
978static void
979handle_101232(struct bregs *regs)
980{
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500981 stdvga_enable_video_addressing(regs->al);
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400982 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400983}
984
985static void
986handle_101233(struct bregs *regs)
987{
Kevin O'Connore7132042009-05-25 00:10:35 -0400988 u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
989 u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
990 SET_BDA(modeset_ctl, v | v2);
991 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400992}
993
994static void
995handle_101234(struct bregs *regs)
996{
Kevin O'Connore7132042009-05-25 00:10:35 -0400997 u8 v = (regs->al & 0x01) ^ 0x01;
998 u8 v2 = GET_BDA(modeset_ctl) & ~0x01;
999 SET_BDA(modeset_ctl, v | v2);
1000 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001001}
1002
1003static void
1004handle_101235(struct bregs *regs)
1005{
1006 debug_stub(regs);
1007 regs->al = 0x12;
1008}
1009
1010static void
1011handle_101236(struct bregs *regs)
1012{
1013 debug_stub(regs);
1014 regs->al = 0x12;
1015}
1016
1017static void
1018handle_1012XX(struct bregs *regs)
1019{
1020 debug_stub(regs);
1021}
1022
1023static void
1024handle_1012(struct bregs *regs)
1025{
1026 switch (regs->bl) {
1027 case 0x10: handle_101210(regs); break;
1028 case 0x30: handle_101230(regs); break;
1029 case 0x31: handle_101231(regs); break;
1030 case 0x32: handle_101232(regs); break;
1031 case 0x33: handle_101233(regs); break;
1032 case 0x34: handle_101234(regs); break;
1033 case 0x35: handle_101235(regs); break;
1034 case 0x36: handle_101236(regs); break;
1035 default: handle_1012XX(regs); break;
1036 }
1037
1038 // XXX - cirrus has 1280, 1281, 1282, 1285, 129a, 12a0, 12a1, 12a2, 12ae
1039}
1040
1041
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001042// Write string
Kevin O'Connor0ad77f02009-05-31 20:46:43 -04001043static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001044handle_1013(struct bregs *regs)
1045{
Kevin O'Connor918b1562009-05-25 11:05:18 -04001046 struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -04001047 // if row=0xff special case : use current cursor position
1048 if (cp.y == 0xff)
1049 cp = get_cursor_pos(cp.page);
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001050 u8 flag = regs->al;
1051 if (flag & 2)
1052 write_attr_string(&cp, regs->cx, regs->es, (void*)(regs->bp + 0));
1053 else
1054 write_string(&cp, regs->bl, regs->cx, regs->es, (void*)(regs->bp + 0));
1055
1056 if (flag & 1)
1057 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001058}
1059
1060
1061static void
1062handle_101a00(struct bregs *regs)
1063{
Kevin O'Connore7132042009-05-25 00:10:35 -04001064 regs->bx = GET_BDA(dcc_index);
1065 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001066}
1067
1068static void
1069handle_101a01(struct bregs *regs)
1070{
Kevin O'Connore7132042009-05-25 00:10:35 -04001071 SET_BDA(dcc_index, regs->bl);
1072 dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
1073 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001074}
1075
1076static void
1077handle_101aXX(struct bregs *regs)
1078{
1079 debug_stub(regs);
1080}
1081
1082static void
1083handle_101a(struct bregs *regs)
1084{
1085 switch (regs->al) {
1086 case 0x00: handle_101a00(regs); break;
1087 case 0x01: handle_101a01(regs); break;
1088 default: handle_101aXX(regs); break;
1089 }
1090}
1091
1092
Kevin O'Connorca668642009-05-21 23:06:08 -04001093struct funcInfo {
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001094 struct segoff_s static_functionality;
Kevin O'Connorca668642009-05-21 23:06:08 -04001095 u8 bda_0x49[30];
1096 u8 bda_0x84[3];
1097 u8 dcc_index;
1098 u8 dcc_alt;
1099 u16 colors;
1100 u8 pages;
1101 u8 scan_lines;
1102 u8 primary_char;
1103 u8 secondar_char;
1104 u8 misc;
1105 u8 non_vga_mode;
1106 u8 reserved_2f[2];
1107 u8 video_mem;
1108 u8 save_flags;
1109 u8 disp_info;
1110 u8 reserved_34[12];
1111};
1112
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001113static void
1114handle_101b(struct bregs *regs)
1115{
Kevin O'Connorca668642009-05-21 23:06:08 -04001116 u16 seg = regs->es;
1117 struct funcInfo *info = (void*)(regs->di+0);
1118 memset_far(seg, info, 0, sizeof(*info));
1119 // Address of static functionality table
Kevin O'Connor87dfad32011-12-23 21:18:49 -05001120 SET_FARVAR(seg, info->static_functionality
1121 , SEGOFF(get_global_seg(), (u32)static_functionality));
Kevin O'Connorca668642009-05-21 23:06:08 -04001122
1123 // Hard coded copy from BIOS area. Should it be cleaner ?
Kevin O'Connor9f985422009-09-09 11:34:39 -04001124 memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49
1125 , sizeof(info->bda_0x49));
1126 memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84
1127 , sizeof(info->bda_0x84));
Kevin O'Connorca668642009-05-21 23:06:08 -04001128
1129 SET_FARVAR(seg, info->dcc_index, GET_BDA(dcc_index));
1130 SET_FARVAR(seg, info->colors, 16);
1131 SET_FARVAR(seg, info->pages, 8);
1132 SET_FARVAR(seg, info->scan_lines, 2);
1133 SET_FARVAR(seg, info->video_mem, 3);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001134 regs->al = 0x1B;
1135}
1136
1137
1138static void
1139handle_101c00(struct bregs *regs)
1140{
Kevin O'Connorca668642009-05-21 23:06:08 -04001141 u16 flags = regs->cx;
1142 u16 size = 0;
1143 if (flags & 1)
1144 size += sizeof(struct saveVideoHardware);
1145 if (flags & 2)
1146 size += sizeof(struct saveBDAstate);
1147 if (flags & 4)
1148 size += sizeof(struct saveDACcolors);
1149 regs->bx = size;
1150 regs->al = 0x1c;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001151}
1152
1153static void
1154handle_101c01(struct bregs *regs)
1155{
Kevin O'Connorca668642009-05-21 23:06:08 -04001156 u16 flags = regs->cx;
1157 u16 seg = regs->es;
1158 void *data = (void*)(regs->bx+0);
1159 if (flags & 1) {
Kevin O'Connor88ca7412011-12-31 04:24:20 -05001160 stdvga_save_state(seg, data);
Kevin O'Connorca668642009-05-21 23:06:08 -04001161 data += sizeof(struct saveVideoHardware);
1162 }
1163 if (flags & 2) {
Kevin O'Connor227a2bb2009-05-31 22:00:20 -04001164 save_bda_state(seg, data);
Kevin O'Connorca668642009-05-21 23:06:08 -04001165 data += sizeof(struct saveBDAstate);
1166 }
1167 if (flags & 4)
Kevin O'Connor88ca7412011-12-31 04:24:20 -05001168 stdvga_save_dac_state(seg, data);
Kevin O'Connorca668642009-05-21 23:06:08 -04001169 regs->al = 0x1c;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001170}
1171
1172static void
1173handle_101c02(struct bregs *regs)
1174{
Kevin O'Connorca668642009-05-21 23:06:08 -04001175 u16 flags = regs->cx;
1176 u16 seg = regs->es;
1177 void *data = (void*)(regs->bx+0);
1178 if (flags & 1) {
Kevin O'Connor88ca7412011-12-31 04:24:20 -05001179 stdvga_restore_state(seg, data);
Kevin O'Connorca668642009-05-21 23:06:08 -04001180 data += sizeof(struct saveVideoHardware);
1181 }
1182 if (flags & 2) {
Kevin O'Connor227a2bb2009-05-31 22:00:20 -04001183 restore_bda_state(seg, data);
Kevin O'Connorca668642009-05-21 23:06:08 -04001184 data += sizeof(struct saveBDAstate);
1185 }
1186 if (flags & 4)
Kevin O'Connor88ca7412011-12-31 04:24:20 -05001187 stdvga_restore_dac_state(seg, data);
Kevin O'Connorca668642009-05-21 23:06:08 -04001188 regs->al = 0x1c;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001189}
1190
1191static void
1192handle_101cXX(struct bregs *regs)
1193{
1194 debug_stub(regs);
1195}
1196
1197static void
1198handle_101c(struct bregs *regs)
1199{
1200 switch (regs->al) {
1201 case 0x00: handle_101c00(regs); break;
1202 case 0x01: handle_101c01(regs); break;
1203 case 0x02: handle_101c02(regs); break;
1204 default: handle_101cXX(regs); break;
1205 }
1206}
1207
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001208static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001209handle_10XX(struct bregs *regs)
1210{
1211 debug_stub(regs);
1212}
1213
1214// INT 10h Video Support Service Entry Point
1215void VISIBLE16
1216handle_10(struct bregs *regs)
1217{
1218 debug_enter(regs, DEBUG_VGA_10);
1219 switch (regs->ah) {
1220 case 0x00: handle_1000(regs); break;
1221 case 0x01: handle_1001(regs); break;
1222 case 0x02: handle_1002(regs); break;
1223 case 0x03: handle_1003(regs); break;
1224 case 0x04: handle_1004(regs); break;
1225 case 0x05: handle_1005(regs); break;
1226 case 0x06: handle_1006(regs); break;
1227 case 0x07: handle_1007(regs); break;
1228 case 0x08: handle_1008(regs); break;
1229 case 0x09: handle_1009(regs); break;
1230 case 0x0a: handle_100a(regs); break;
1231 case 0x0b: handle_100b(regs); break;
1232 case 0x0c: handle_100c(regs); break;
1233 case 0x0d: handle_100d(regs); break;
1234 case 0x0e: handle_100e(regs); break;
1235 case 0x0f: handle_100f(regs); break;
1236 case 0x10: handle_1010(regs); break;
1237 case 0x11: handle_1011(regs); break;
1238 case 0x12: handle_1012(regs); break;
1239 case 0x13: handle_1013(regs); break;
1240 case 0x1a: handle_101a(regs); break;
1241 case 0x1b: handle_101b(regs); break;
1242 case 0x1c: handle_101c(regs); break;
1243 case 0x4f: handle_104f(regs); break;
1244 default: handle_10XX(regs); break;
1245 }
1246}
1247
1248
1249/****************************************************************
1250 * VGA post
1251 ****************************************************************/
1252
1253static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -05001254init_bios_area(void)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001255{
1256 // init detected hardware BIOS Area
1257 // set 80x25 color (not clear from RBIL but usual)
1258 u16 eqf = GET_BDA(equipment_list_flags);
1259 SET_BDA(equipment_list_flags, (eqf & 0xffcf) | 0x20);
1260
1261 // Just for the first int10 find its children
1262
1263 // the default char height
1264 SET_BDA(char_height, 0x10);
1265
1266 // Clear the screen
1267 SET_BDA(video_ctl, 0x60);
1268
1269 // Set the basic screen we have
1270 SET_BDA(video_switches, 0xf9);
1271
1272 // Set the basic modeset options
1273 SET_BDA(modeset_ctl, 0x51);
1274
1275 // Set the default MSR
1276 SET_BDA(video_msr, 0x09);
1277}
1278
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001279void VISIBLE16
1280vga_post(struct bregs *regs)
1281{
1282 debug_enter(regs, DEBUG_VGA_POST);
1283
Kevin O'Connor88ca7412011-12-31 04:24:20 -05001284 stdvga_init();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001285
Kevin O'Connor4c52fb42011-12-24 00:44:07 -05001286 if (CONFIG_VGA_GEODELX)
1287 geodelx_init();
1288
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001289 init_bios_area();
1290
Kevin O'Connorf1e217d2011-12-31 03:18:18 -05001291 bochsvga_init(regs->ah, regs->al);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001292
1293 extern void entry_10(void);
Kevin O'Connor9f985422009-09-09 11:34:39 -04001294 SET_IVT(0x10, SEGOFF(get_global_seg(), (u32)entry_10));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001295
Julian Pidancet677631f2011-12-19 05:07:53 +00001296 if (CONFIG_VGA_CIRRUS)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001297 cirrus_init();
1298
1299 // XXX - clear screen and display info
1300
Kevin O'Connorf3760372011-12-23 22:41:08 -05001301 build_video_param();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001302
1303 // Fixup checksum
1304 extern u8 _rom_header_size, _rom_header_checksum;
1305 SET_VGA(_rom_header_checksum, 0);
Kevin O'Connord113a992009-05-16 21:05:02 -04001306 u8 sum = -checksum_far(get_global_seg(), 0, _rom_header_size * 512);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001307 SET_VGA(_rom_header_checksum, sum);
1308}