1 // Standard VGA driver code
3 // Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2001-2008 the LGPL VGABios developers Team
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "stdvga.h" // stdvga_init
9 #include "ioport.h" // outb
10 #include "farptr.h" // SET_FARVAR
11 #include "biosvar.h" // GET_GLOBAL
12 #include "util.h" // memcpy_far
13 #include "vbe.h" // VBE_RETURN_STATUS_FAILED
16 /****************************************************************
18 ****************************************************************/
21 stdvga_set_border_color(u8 color)
26 stdvga_attr_write(0x00, v1);
29 for (i = 1; i < 4; i++)
30 stdvga_attr_mask(i, 0x10, color & 0x10);
34 stdvga_set_overscan_border_color(u8 color)
36 stdvga_attr_write(0x11, color);
40 stdvga_get_overscan_border_color(void)
42 return stdvga_attr_read(0x11);
46 stdvga_set_palette(u8 palid)
49 for (i = 1; i < 4; i++)
50 stdvga_attr_mask(i, 0x01, palid & 0x01);
54 stdvga_set_all_palette_reg(u16 seg, u8 *data_far)
57 for (i = 0; i < 0x10; i++) {
58 stdvga_attr_write(i, GET_FARVAR(seg, *data_far));
61 stdvga_attr_write(0x11, GET_FARVAR(seg, *data_far));
65 stdvga_get_all_palette_reg(u16 seg, u8 *data_far)
68 for (i = 0; i < 0x10; i++) {
69 SET_FARVAR(seg, *data_far, stdvga_attr_read(i));
72 SET_FARVAR(seg, *data_far, stdvga_attr_read(0x11));
76 stdvga_toggle_intensity(u8 flag)
78 stdvga_attr_mask(0x10, 0x08, (flag & 0x01) << 3);
82 stdvga_select_video_dac_color_page(u8 flag, u8 data)
86 stdvga_attr_mask(0x10, 0x80, data << 7);
90 u8 val = stdvga_attr_read(0x10);
94 stdvga_attr_write(0x14, data);
98 stdvga_read_video_dac_state(u8 *pmode, u8 *curpage)
100 u8 val1 = stdvga_attr_read(0x10) >> 7;
101 u8 val2 = stdvga_attr_read(0x14) & 0x0f;
109 /****************************************************************
111 ****************************************************************/
114 stdvga_save_dac_state(u16 seg, struct saveDACcolors *info)
116 /* XXX: check this */
117 SET_FARVAR(seg, info->rwmode, inb(VGAREG_DAC_STATE));
118 SET_FARVAR(seg, info->peladdr, inb(VGAREG_DAC_WRITE_ADDRESS));
119 SET_FARVAR(seg, info->pelmask, stdvga_pelmask_read());
120 stdvga_dac_read(seg, info->dac, 0, 256);
121 SET_FARVAR(seg, info->color_select, 0);
125 stdvga_restore_dac_state(u16 seg, struct saveDACcolors *info)
127 stdvga_pelmask_write(GET_FARVAR(seg, info->pelmask));
128 stdvga_dac_write(seg, info->dac, 0, 256);
129 outb(GET_FARVAR(seg, info->peladdr), VGAREG_DAC_WRITE_ADDRESS);
133 stdvga_perform_gray_scale_summing(u16 start, u16 count)
135 stdvga_attrindex_write(0x00);
137 for (i = start; i < start+count; i++) {
139 stdvga_dac_read(GET_SEG(SS), rgb, i, 1);
141 // intensity = ( 0.3 * Red ) + ( 0.59 * Green ) + ( 0.11 * Blue )
142 u16 intensity = ((77 * rgb[0] + 151 * rgb[1] + 28 * rgb[2]) + 0x80) >> 8;
143 if (intensity > 0x3f)
146 stdvga_dac_write(GET_SEG(SS), rgb, i, 1);
148 stdvga_attrindex_write(0x20);
152 /****************************************************************
154 ****************************************************************/
157 stdvga_set_text_block_specifier(u8 spec)
159 stdvga_sequ_write(0x03, spec);
163 /****************************************************************
165 ****************************************************************/
168 get_font_access(void)
170 stdvga_sequ_write(0x00, 0x01);
171 stdvga_sequ_write(0x02, 0x04);
172 stdvga_sequ_write(0x04, 0x07);
173 stdvga_sequ_write(0x00, 0x03);
174 stdvga_grdc_write(0x04, 0x02);
175 stdvga_grdc_write(0x05, 0x00);
176 stdvga_grdc_write(0x06, 0x04);
180 release_font_access(void)
182 stdvga_sequ_write(0x00, 0x01);
183 stdvga_sequ_write(0x02, 0x03);
184 stdvga_sequ_write(0x04, 0x03);
185 stdvga_sequ_write(0x00, 0x03);
186 u16 v = (stdvga_misc_read() & 0x01) ? 0x0e : 0x0a;
187 stdvga_grdc_write(0x06, v);
188 stdvga_grdc_write(0x04, 0x00);
189 stdvga_grdc_write(0x05, 0x10);
193 stdvga_load_font(u16 seg, void *src_far, u16 count
194 , u16 start, u8 destflags, u8 fontsize)
197 u16 blockaddr = ((destflags & 0x03) << 14) + ((destflags & 0x04) << 11);
198 void *dest_far = (void*)(blockaddr + start*32);
200 for (i = 0; i < count; i++)
201 memcpy_far(SEG_GRAPH, dest_far + i*32
202 , seg, src_far + i*fontsize, fontsize);
203 release_font_access();
207 /****************************************************************
209 ****************************************************************/
212 stdvga_get_crtc(void)
214 if (stdvga_misc_read() & 1)
215 return VGAREG_VGA_CRTC_ADDRESS;
216 return VGAREG_MDA_CRTC_ADDRESS;
220 stdvga_set_cursor_shape(u8 start, u8 end)
222 u16 crtc_addr = stdvga_get_crtc();
223 stdvga_crtc_write(crtc_addr, 0x0a, start);
224 stdvga_crtc_write(crtc_addr, 0x0b, end);
228 stdvga_set_active_page(u16 address)
230 u16 crtc_addr = stdvga_get_crtc();
231 stdvga_crtc_write(crtc_addr, 0x0c, address >> 8);
232 stdvga_crtc_write(crtc_addr, 0x0d, address);
236 stdvga_set_cursor_pos(u16 address)
238 u16 crtc_addr = stdvga_get_crtc();
239 stdvga_crtc_write(crtc_addr, 0x0e, address >> 8);
240 stdvga_crtc_write(crtc_addr, 0x0f, address);
244 stdvga_set_scan_lines(u8 lines)
246 stdvga_crtc_mask(stdvga_get_crtc(), 0x09, 0x1f, lines - 1);
249 // Get vertical display end
253 u16 crtc_addr = stdvga_get_crtc();
254 u16 vde = stdvga_crtc_read(crtc_addr, 0x12);
255 u8 ovl = stdvga_crtc_read(crtc_addr, 0x07);
256 vde += (((ovl & 0x02) << 7) + ((ovl & 0x40) << 3) + 1);
261 /****************************************************************
262 * Save/Restore/Set state
263 ****************************************************************/
266 stdvga_save_state(u16 seg, struct saveVideoHardware *info)
268 u16 crtc_addr = stdvga_get_crtc();
269 SET_FARVAR(seg, info->sequ_index, inb(VGAREG_SEQU_ADDRESS));
270 SET_FARVAR(seg, info->crtc_index, inb(crtc_addr));
271 SET_FARVAR(seg, info->grdc_index, inb(VGAREG_GRDC_ADDRESS));
272 SET_FARVAR(seg, info->actl_index, stdvga_attrindex_read());
273 SET_FARVAR(seg, info->feature, inb(VGAREG_READ_FEATURE_CTL));
277 SET_FARVAR(seg, info->sequ_regs[i], stdvga_sequ_read(i+1));
278 SET_FARVAR(seg, info->sequ0, stdvga_sequ_read(0));
281 SET_FARVAR(seg, info->crtc_regs[i], stdvga_crtc_read(crtc_addr, i));
284 SET_FARVAR(seg, info->actl_regs[i], stdvga_attr_read(i));
287 SET_FARVAR(seg, info->grdc_regs[i], stdvga_grdc_read(i));
289 SET_FARVAR(seg, info->crtc_addr, crtc_addr);
291 /* XXX: read plane latches */
293 SET_FARVAR(seg, info->plane_latch[i], 0);
297 stdvga_restore_state(u16 seg, struct saveVideoHardware *info)
301 stdvga_sequ_write(i+1, GET_FARVAR(seg, info->sequ_regs[i]));
302 stdvga_sequ_write(0x00, GET_FARVAR(seg, info->sequ0));
304 // Disable CRTC write protection
305 u16 crtc_addr = GET_FARVAR(seg, info->crtc_addr);
306 stdvga_crtc_write(crtc_addr, 0x11, 0x00);
310 stdvga_crtc_write(crtc_addr, i, GET_FARVAR(seg, info->crtc_regs[i]));
311 // select crtc base address
312 stdvga_misc_mask(0x01, crtc_addr == VGAREG_VGA_CRTC_ADDRESS ? 0x01 : 0x00);
314 // enable write protection if needed
315 stdvga_crtc_write(crtc_addr, 0x11, GET_FARVAR(seg, info->crtc_regs[0x11]));
319 stdvga_attr_write(i, GET_FARVAR(seg, info->actl_regs[i]));
320 stdvga_attrindex_write(GET_FARVAR(seg, info->actl_index));
323 stdvga_grdc_write(i, GET_FARVAR(seg, info->grdc_regs[i]));
325 outb(GET_FARVAR(seg, info->sequ_index), VGAREG_SEQU_ADDRESS);
326 outb(GET_FARVAR(seg, info->crtc_index), crtc_addr);
327 outb(GET_FARVAR(seg, info->grdc_index), VGAREG_GRDC_ADDRESS);
328 outb(GET_FARVAR(seg, info->feature), crtc_addr - 0x4 + 0xa);
332 clear_screen(struct vgamode_s *vmode_g)
334 switch (GET_GLOBAL(vmode_g->memmodel)) {
336 memset16_far(GET_GLOBAL(vmode_g->sstart), 0, 0x0720, 32*1024);
339 memset16_far(GET_GLOBAL(vmode_g->sstart), 0, 0x0000, 32*1024);
342 // XXX - old code gets/sets/restores sequ register 2 to 0xf -
343 // but it should always be 0xf anyway.
344 memset16_far(GET_GLOBAL(vmode_g->sstart), 0, 0x0000, 64*1024);
349 stdvga_set_mode(int mode, int flags)
351 // find the entry in the video modes
352 struct vgamode_s *vmode_g = stdvga_find_mode(mode);
353 dprintf(1, "mode search %02x found %p\n", mode, vmode_g);
355 return VBE_RETURN_STATUS_FAILED;
356 struct stdvga_mode_s *stdmode_g = container_of(
357 vmode_g, struct stdvga_mode_s, info);
359 // if palette loading (bit 3 of modeset ctl = 0)
360 if (!(flags & MF_NOPALETTE)) { // Set the PEL mask
361 stdvga_pelmask_write(GET_GLOBAL(stdmode_g->pelmask));
363 // From which palette
364 u8 *palette_g = GET_GLOBAL(stdmode_g->dac);
365 u16 palsize = GET_GLOBAL(stdmode_g->dacsize) / 3;
367 // Always 256*3 values
368 stdvga_dac_write(get_global_seg(), palette_g, 0, palsize);
370 for (i = palsize; i < 0x0100; i++) {
371 static u8 rgb[3] VAR16;
372 stdvga_dac_write(get_global_seg(), rgb, i, 1);
375 if (flags & MF_GRAYSUM)
376 stdvga_perform_gray_scale_summing(0x00, 0x100);
380 u8 *regs = GET_GLOBAL(stdmode_g->actl_regs);
382 for (i = 0; i <= 0x13; i++)
383 stdvga_attr_write(i, GET_GLOBAL(regs[i]));
384 stdvga_attr_write(0x14, 0x00);
387 stdvga_sequ_write(0x00, 0x03);
388 regs = GET_GLOBAL(stdmode_g->sequ_regs);
389 for (i = 1; i <= 4; i++)
390 stdvga_sequ_write(i, GET_GLOBAL(regs[i - 1]));
393 regs = GET_GLOBAL(stdmode_g->grdc_regs);
394 for (i = 0; i <= 8; i++)
395 stdvga_grdc_write(i, GET_GLOBAL(regs[i]));
397 // Set CRTC address VGA or MDA
398 u8 miscreg = GET_GLOBAL(stdmode_g->miscreg);
399 u16 crtc_addr = VGAREG_VGA_CRTC_ADDRESS;
401 crtc_addr = VGAREG_MDA_CRTC_ADDRESS;
403 // Disable CRTC write protection
404 stdvga_crtc_write(crtc_addr, 0x11, 0x00);
406 regs = GET_GLOBAL(stdmode_g->crtc_regs);
407 for (i = 0; i <= 0x18; i++)
408 stdvga_crtc_write(crtc_addr, i, GET_GLOBAL(regs[i]));
410 // Set the misc register
411 stdvga_misc_write(miscreg);
414 stdvga_attrindex_write(0x20);
417 if (!(flags & MF_NOCLEARMEM))
418 clear_screen(vmode_g);
420 // Write the fonts in memory
421 u8 memmodel = GET_GLOBAL(vmode_g->memmodel);
422 if (memmodel == MM_TEXT)
423 stdvga_load_font(get_global_seg(), vgafont16, 0x100, 0, 0, 16);
425 // Setup BDA variables
426 modeswitch_set_bda(mode, flags, vmode_g);
432 /****************************************************************
434 ****************************************************************/
437 stdvga_list_modes(u16 seg, u16 *dest, u16 *last)
439 SET_FARVAR(seg, *dest, 0xffff);
443 stdvga_enable_video_addressing(u8 disable)
445 u8 v = (disable & 1) ? 0x00 : 0x02;
446 stdvga_misc_mask(0x02, v);
452 // switch to color mode and enable CPU access 480 lines
453 stdvga_misc_write(0xc3);
454 // more than 64k 3C4/04
455 stdvga_sequ_write(0x04, 0x02);