blob: 3988da9c9a58b451ace2829cd433a76e9d90b1f4 [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'Connorc0c7df62009-05-17 18:11:33 -040017#include "vgatables.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'Connor1f2c3072009-05-06 23:35:59 -040020
21// XXX
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040022#define DEBUG_VGA_POST 1
23#define DEBUG_VGA_10 3
24
Kevin O'Connord113a992009-05-16 21:05:02 -040025#define SET_VGA(var, val) SET_FARVAR(get_global_seg(), (var), (val))
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040026
Julian Pidancet7c6509c2011-12-19 05:07:55 +000027/****************************************************************
28 * PCI Data
29 ****************************************************************/
30#if CONFIG_VGA_PCI == 1
31struct pci_data rom_pci_data VAR16VISIBLE = {
32 .signature = PCI_ROM_SIGNATURE,
33 .vendor = CONFIG_VGA_VID,
34 .device = CONFIG_VGA_DID,
35 .dlen = 0x18,
36 .class_hi = 0x300,
37 .irevision = 1,
38 .type = PCIROM_CODETYPE_X86,
39 .indicator = 0x80,
40};
41#endif
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040042
43/****************************************************************
44 * Helper functions
45 ****************************************************************/
46
Kevin O'Connor217f2bc2009-05-31 00:46:47 -040047static inline void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040048call16_vgaint(u32 eax, u32 ebx)
49{
50 asm volatile(
51 "int $0x10\n"
52 "cli\n"
53 "cld"
54 :
55 : "a"(eax), "b"(ebx)
56 : "cc", "memory");
57}
58
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040059static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040060perform_gray_scale_summing(u16 start, u16 count)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040061{
Kevin O'Connora0ecb052009-05-18 23:34:00 -040062 vgahw_screen_disable();
Kevin O'Connordd2be772009-05-16 15:41:23 -040063 int i;
64 for (i = start; i < start+count; i++) {
Kevin O'Connora0ecb052009-05-18 23:34:00 -040065 u8 rgb[3];
66 vgahw_get_dac_regs(GET_SEG(SS), rgb, i, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040067
68 // intensity = ( 0.3 * Red ) + ( 0.59 * Green ) + ( 0.11 * Blue )
Kevin O'Connora0ecb052009-05-18 23:34:00 -040069 u16 intensity = ((77 * rgb[0] + 151 * rgb[1] + 28 * rgb[2]) + 0x80) >> 8;
Kevin O'Connordd2be772009-05-16 15:41:23 -040070 if (intensity > 0x3f)
71 intensity = 0x3f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040072
Kevin O'Connora0ecb052009-05-18 23:34:00 -040073 vgahw_set_dac_regs(GET_SEG(SS), rgb, i, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040074 }
Kevin O'Connora0ecb052009-05-18 23:34:00 -040075 vgahw_screen_enable();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040076}
77
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040078static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040079set_cursor_shape(u8 start, u8 end)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040080{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040081 start &= 0x3f;
82 end &= 0x1f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040083
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040084 u16 curs = (start << 8) + end;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040085 SET_BDA(cursor_type, curs);
86
Kevin O'Connordd2be772009-05-16 15:41:23 -040087 u8 modeset_ctl = GET_BDA(modeset_ctl);
88 u16 cheight = GET_BDA(char_height);
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040089 if ((modeset_ctl & 0x01) && (cheight > 8) && (end < 8) && (start < 0x20)) {
90 if (end != (start + 1))
91 start = ((start + 1) * cheight / 8) - 1;
Kevin O'Connordd2be772009-05-16 15:41:23 -040092 else
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040093 start = ((end + 1) * cheight / 8) - 2;
94 end = ((end + 1) * cheight / 8) - 1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040095 }
Kevin O'Connor227a2bb2009-05-31 22:00:20 -040096 vgahw_set_cursor_shape(start, end);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040097}
98
Kevin O'Connor0818e1a2009-05-16 18:00:19 -040099static u16
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400100get_cursor_shape(u8 page)
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400101{
102 if (page > 7)
103 return 0;
104 // FIXME should handle VGA 14/16 lines
105 return GET_BDA(cursor_type);
106}
107
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400108static void
Kevin O'Connor918b1562009-05-25 11:05:18 -0400109set_cursor_pos(struct cursorpos cp)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400110{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400111 // Should not happen...
Kevin O'Connor918b1562009-05-25 11:05:18 -0400112 if (cp.page > 7)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400113 return;
114
115 // Bios cursor pos
Kevin O'Connor918b1562009-05-25 11:05:18 -0400116 SET_BDA(cursor_pos[cp.page], (cp.y << 8) | cp.x);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400117
118 // Set the hardware cursor
Kevin O'Connordd2be772009-05-16 15:41:23 -0400119 u8 current = GET_BDA(video_page);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400120 if (cp.page != current)
Kevin O'Connordd2be772009-05-16 15:41:23 -0400121 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400122
Kevin O'Connordd2be772009-05-16 15:41:23 -0400123 // Get the dimensions
124 u16 nbcols = GET_BDA(video_cols);
125 u16 nbrows = GET_BDA(video_rows) + 1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400126
Kevin O'Connordd2be772009-05-16 15:41:23 -0400127 // Calculate the address knowing nbcols nbrows and page num
Kevin O'Connor918b1562009-05-25 11:05:18 -0400128 u16 address = (SCREEN_IO_START(nbcols, nbrows, cp.page)
129 + cp.x + cp.y * nbcols);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400130
Kevin O'Connora0ecb052009-05-18 23:34:00 -0400131 vgahw_set_cursor_pos(address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400132}
133
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400134static struct cursorpos
Kevin O'Connor918b1562009-05-25 11:05:18 -0400135get_cursor_pos(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400136{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400137 if (page == 0xff)
138 // special case - use current page
139 page = GET_BDA(video_page);
140 if (page > 7) {
141 struct cursorpos cp = { 0, 0, 0xfe };
142 return cp;
143 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400144 // FIXME should handle VGA 14/16 lines
Kevin O'Connor918b1562009-05-25 11:05:18 -0400145 u16 xy = GET_BDA(cursor_pos[page]);
146 struct cursorpos cp = {xy, xy>>8, page};
147 return cp;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400148}
149
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400150static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400151set_active_page(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400152{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400153 if (page > 7)
154 return;
155
156 // Get the mode
Kevin O'Connor5727c292009-05-16 17:29:32 -0400157 struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
158 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400159 return;
160
161 // Get pos curs pos for the right page
Kevin O'Connor918b1562009-05-25 11:05:18 -0400162 struct cursorpos cp = get_cursor_pos(page);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400163
Kevin O'Connordd2be772009-05-16 15:41:23 -0400164 u16 address;
Kevin O'Connore4f220f2009-05-25 23:37:13 -0400165 if (GET_GLOBAL(vmode_g->memmodel) & TEXT) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400166 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -0400167 u16 nbcols = GET_BDA(video_cols);
168 u16 nbrows = GET_BDA(video_rows) + 1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400169
170 // Calculate the address knowing nbcols nbrows and page num
171 address = SCREEN_MEM_START(nbcols, nbrows, page);
172 SET_BDA(video_pagestart, address);
173
174 // Start address
175 address = SCREEN_IO_START(nbcols, nbrows, page);
176 } else {
Kevin O'Connor5727c292009-05-16 17:29:32 -0400177 struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
178 address = page * GET_GLOBAL(vparam_g->slength);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400179 }
180
Kevin O'Connora0ecb052009-05-18 23:34:00 -0400181 vgahw_set_active_page(address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400182
183 // And change the BIOS page
184 SET_BDA(video_page, page);
185
Kevin O'Connora12c2152009-05-13 22:06:16 -0400186 dprintf(1, "Set active page %02x address %04x\n", page, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400187
188 // Display the cursor, now the page is active
Kevin O'Connor918b1562009-05-25 11:05:18 -0400189 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400190}
191
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400192static void
193set_scan_lines(u8 lines)
194{
195 vgahw_set_scan_lines(lines);
196 if (lines == 8)
197 set_cursor_shape(0x06, 0x07);
198 else
199 set_cursor_shape(lines - 4, lines - 3);
200 SET_BDA(char_height, lines);
201 u16 vde = vgahw_get_vde();
202 u8 rows = vde / lines;
203 SET_BDA(video_rows, rows - 1);
204 u16 cols = GET_BDA(video_cols);
205 SET_BDA(video_pagesize, rows * cols * 2);
206}
207
208
209/****************************************************************
210 * Character writing
211 ****************************************************************/
212
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400213// Scroll the screen one line. This function is designed to be called
214// tail-recursive to reduce stack usage.
215static void noinline
216scroll_one(u16 nbrows, u16 nbcols, u8 page)
Kevin O'Connor09262412009-05-25 11:44:11 -0400217{
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400218 struct cursorpos ul = {0, 0, page};
219 struct cursorpos lr = {nbcols-1, nbrows-1, page};
220 vgafb_scroll(1, -1, ul, lr);
221}
222
223// Write a character to the screen at a given position. Implement
224// special characters and scroll the screen if necessary.
225static void
226write_teletype(struct cursorpos *pcp, struct carattr ca)
227{
228 struct cursorpos cp = *pcp;
229
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400230 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -0400231 u16 nbrows = GET_BDA(video_rows) + 1;
232 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400233
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400234 switch (ca.car) {
235 case 7:
236 //FIXME should beep
237 break;
238 case 8:
239 if (cp.x > 0)
240 cp.x--;
241 break;
242 case '\r':
243 cp.x = 0;
244 break;
245 case '\n':
246 cp.y++;
247 break;
248 case '\t':
249 do {
250 struct carattr dummyca = {' ', ca.attr, ca.use_attr};
251 vgafb_write_char(cp, dummyca);
252 cp.x++;
253 } while (cp.x < nbcols && cp.x % 8);
254 break;
255 default:
256 vgafb_write_char(cp, ca);
257 cp.x++;
258 }
259
Kevin O'Connor82221b22009-05-26 00:20:40 -0400260 // Do we need to wrap ?
261 if (cp.x == nbcols) {
262 cp.x = 0;
263 cp.y++;
264 }
265 // Do we need to scroll ?
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400266 if (cp.y < nbrows) {
267 *pcp = cp;
268 return;
Kevin O'Connor82221b22009-05-26 00:20:40 -0400269 }
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400270 // Scroll screen
271 cp.y--;
272 *pcp = cp;
273 scroll_one(nbrows, nbcols, cp.page);
Kevin O'Connor82221b22009-05-26 00:20:40 -0400274}
275
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400276// Write out a buffer of alternating characters and attributes.
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400277static void
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400278write_attr_string(struct cursorpos *pcp, u16 count, u16 seg, u8 *offset_far)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400279{
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -0400280 while (count--) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400281 u8 car = GET_FARVAR(seg, *offset_far);
282 offset_far++;
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400283 u8 attr = GET_FARVAR(seg, *offset_far);
284 offset_far++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400285
Kevin O'Connor09262412009-05-25 11:44:11 -0400286 struct carattr ca = {car, attr, 1};
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400287 write_teletype(pcp, ca);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400288 }
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400289}
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400290
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400291// Write out a buffer of characters.
292static void
293write_string(struct cursorpos *pcp, u8 attr, u16 count, u16 seg, u8 *offset_far)
294{
295 while (count--) {
296 u8 car = GET_FARVAR(seg, *offset_far);
297 offset_far++;
298
299 struct carattr ca = {car, attr, 1};
300 write_teletype(pcp, ca);
301 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400302}
303
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400304
305/****************************************************************
306 * Save and restore bda state
307 ****************************************************************/
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400308
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400309static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400310save_bda_state(u16 seg, struct saveBDAstate *info)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400311{
Kevin O'Connorca668642009-05-21 23:06:08 -0400312 SET_FARVAR(seg, info->video_mode, GET_BDA(video_mode));
313 SET_FARVAR(seg, info->video_cols, GET_BDA(video_cols));
314 SET_FARVAR(seg, info->video_pagesize, GET_BDA(video_pagesize));
315 SET_FARVAR(seg, info->crtc_address, GET_BDA(crtc_address));
316 SET_FARVAR(seg, info->video_rows, GET_BDA(video_rows));
317 SET_FARVAR(seg, info->char_height, GET_BDA(char_height));
318 SET_FARVAR(seg, info->video_ctl, GET_BDA(video_ctl));
319 SET_FARVAR(seg, info->video_switches, GET_BDA(video_switches));
320 SET_FARVAR(seg, info->modeset_ctl, GET_BDA(modeset_ctl));
321 SET_FARVAR(seg, info->cursor_type, GET_BDA(cursor_type));
322 u16 i;
323 for (i=0; i<8; i++)
324 SET_FARVAR(seg, info->cursor_pos[i], GET_BDA(cursor_pos[i]));
325 SET_FARVAR(seg, info->video_pagestart, GET_BDA(video_pagestart));
326 SET_FARVAR(seg, info->video_page, GET_BDA(video_page));
327 /* current font */
Kevin O'Connor9f985422009-09-09 11:34:39 -0400328 SET_FARVAR(seg, info->font0, GET_IVT(0x1f));
329 SET_FARVAR(seg, info->font1, GET_IVT(0x43));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400330}
331
Kevin O'Connorca668642009-05-21 23:06:08 -0400332static void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400333restore_bda_state(u16 seg, struct saveBDAstate *info)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400334{
Kevin O'Connorca668642009-05-21 23:06:08 -0400335 SET_BDA(video_mode, GET_FARVAR(seg, info->video_mode));
336 SET_BDA(video_cols, GET_FARVAR(seg, info->video_cols));
337 SET_BDA(video_pagesize, GET_FARVAR(seg, info->video_pagesize));
338 SET_BDA(crtc_address, GET_FARVAR(seg, info->crtc_address));
339 SET_BDA(video_rows, GET_FARVAR(seg, info->video_rows));
340 SET_BDA(char_height, GET_FARVAR(seg, info->char_height));
341 SET_BDA(video_ctl, GET_FARVAR(seg, info->video_ctl));
342 SET_BDA(video_switches, GET_FARVAR(seg, info->video_switches));
343 SET_BDA(modeset_ctl, GET_FARVAR(seg, info->modeset_ctl));
344 SET_BDA(cursor_type, GET_FARVAR(seg, info->cursor_type));
345 u16 i;
346 for (i = 0; i < 8; i++)
347 SET_BDA(cursor_pos[i], GET_FARVAR(seg, info->cursor_pos[i]));
348 SET_BDA(video_pagestart, GET_FARVAR(seg, info->video_pagestart));
349 SET_BDA(video_page, GET_FARVAR(seg, info->video_page));
350 /* current font */
Kevin O'Connor9f985422009-09-09 11:34:39 -0400351 SET_IVT(0x1f, GET_FARVAR(seg, info->font0));
352 SET_IVT(0x43, GET_FARVAR(seg, info->font1));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400353}
354
355
356/****************************************************************
357 * VGA int 10 handler
358 ****************************************************************/
359
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400360// set video mode
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400361static void
362handle_1000(struct bregs *regs)
363{
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400364 u8 noclearmem = regs->al & 0x80;
365 u8 mode = regs->al & 0x7f;
366
Kevin O'Connor918b1562009-05-25 11:05:18 -0400367 // Set regs->al
368 if (mode > 7)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400369 regs->al = 0x20;
Kevin O'Connor918b1562009-05-25 11:05:18 -0400370 else if (mode == 6)
371 regs->al = 0x3f;
372 else
373 regs->al = 0x30;
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400374
Julian Pidancet677631f2011-12-19 05:07:53 +0000375 if (CONFIG_VGA_CIRRUS)
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400376 cirrus_set_video_mode(mode);
377
Julian Pidancet677631f2011-12-19 05:07:53 +0000378 if (CONFIG_VGA_BOCHS)
379 if (bochs_has_vbe_display())
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400380 dispi_set_enable(VBE_DISPI_DISABLED);
381
382 // find the entry in the video modes
383 struct vgamode_s *vmode_g = find_vga_entry(mode);
384 dprintf(1, "mode search %02x found %p\n", mode, vmode_g);
385 if (!vmode_g)
386 return;
387
388 // Read the bios mode set control
389 u8 modeset_ctl = GET_BDA(modeset_ctl);
390
391 // Then we know the number of lines
392// FIXME
393
394 // if palette loading (bit 3 of modeset ctl = 0)
395 if ((modeset_ctl & 0x08) == 0) { // Set the PEL mask
396 vgahw_set_pel_mask(GET_GLOBAL(vmode_g->pelmask));
397
398 // From which palette
399 u8 *palette_g = GET_GLOBAL(vmode_g->dac);
400 u16 palsize = GET_GLOBAL(vmode_g->dacsize) / 3;
401
402 // Always 256*3 values
403 vgahw_set_dac_regs(get_global_seg(), palette_g, 0, palsize);
404 u16 i;
405 for (i = palsize; i < 0x0100; i++) {
406 static u8 rgb[3] VAR16;
407 vgahw_set_dac_regs(get_global_seg(), rgb, i, 1);
408 }
409
410 if ((modeset_ctl & 0x02) == 0x02)
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400411 perform_gray_scale_summing(0x00, 0x100);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400412 }
413
414 struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
415 vgahw_set_mode(vparam_g);
416
417 if (noclearmem == 0x00)
418 clear_screen(vmode_g);
419
420 // Set CRTC address VGA or MDA
421 u16 crtc_addr = VGAREG_VGA_CRTC_ADDRESS;
422 if (GET_GLOBAL(vmode_g->memmodel) == MTEXT)
423 crtc_addr = VGAREG_MDA_CRTC_ADDRESS;
424
425 // Set the BIOS mem
426 u16 cheight = GET_GLOBAL(vparam_g->cheight);
427 SET_BDA(video_mode, mode);
428 SET_BDA(video_cols, GET_GLOBAL(vparam_g->twidth));
429 SET_BDA(video_pagesize, GET_GLOBAL(vparam_g->slength));
430 SET_BDA(crtc_address, crtc_addr);
431 SET_BDA(video_rows, GET_GLOBAL(vparam_g->theightm1));
432 SET_BDA(char_height, cheight);
433 SET_BDA(video_ctl, (0x60 | noclearmem));
434 SET_BDA(video_switches, 0xF9);
435 SET_BDA(modeset_ctl, GET_BDA(modeset_ctl) & 0x7f);
436
437 // FIXME We nearly have the good tables. to be reworked
438 SET_BDA(dcc_index, 0x08); // 8 is VGA should be ok for now
Kevin O'Connor9f985422009-09-09 11:34:39 -0400439 SET_BDA(video_savetable
440 , SEGOFF(get_global_seg(), (u32)video_save_pointer_table));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400441
442 // FIXME
443 SET_BDA(video_msr, 0x00); // Unavailable on vanilla vga, but...
444 SET_BDA(video_pal, 0x00); // Unavailable on vanilla vga, but...
445
446 // Set cursor shape
Kevin O'Connore4f220f2009-05-25 23:37:13 -0400447 if (GET_GLOBAL(vmode_g->memmodel) & TEXT)
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400448 set_cursor_shape(0x06, 0x07);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400449 // Set cursor pos for page 0..7
450 int i;
Kevin O'Connor918b1562009-05-25 11:05:18 -0400451 for (i = 0; i < 8; i++) {
452 struct cursorpos cp = {0, 0, i};
453 set_cursor_pos(cp);
454 }
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400455
456 // Set active page 0
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400457 set_active_page(0x00);
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400458
459 // Write the fonts in memory
Kevin O'Connore4f220f2009-05-25 23:37:13 -0400460 if (GET_GLOBAL(vmode_g->memmodel) & TEXT) {
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400461 call16_vgaint(0x1104, 0);
462 call16_vgaint(0x1103, 0);
463 }
464 // Set the ints 0x1F and 0x43
Kevin O'Connor9f985422009-09-09 11:34:39 -0400465 SET_IVT(0x1f, SEGOFF(get_global_seg(), (u32)&vgafont8[128 * 8]));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400466
467 switch (cheight) {
468 case 8:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400469 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont8));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400470 break;
471 case 14:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400472 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont14));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400473 break;
474 case 16:
Kevin O'Connor9f985422009-09-09 11:34:39 -0400475 SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont16));
Kevin O'Connor85ea07e2009-05-25 09:41:07 -0400476 break;
477 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400478}
479
480static void
481handle_1001(struct bregs *regs)
482{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400483 set_cursor_shape(regs->ch, regs->cl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400484}
485
486static void
487handle_1002(struct bregs *regs)
488{
Kevin O'Connor918b1562009-05-25 11:05:18 -0400489 struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
490 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400491}
492
493static void
494handle_1003(struct bregs *regs)
495{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400496 regs->cx = get_cursor_shape(regs->bh);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400497 struct cursorpos cp = get_cursor_pos(regs->bh);
498 regs->dl = cp.x;
499 regs->dh = cp.y;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400500}
501
502// Read light pen pos (unimplemented)
503static void
504handle_1004(struct bregs *regs)
505{
506 debug_stub(regs);
507 regs->ax = regs->bx = regs->cx = regs->dx = 0;
508}
509
510static void
511handle_1005(struct bregs *regs)
512{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400513 set_active_page(regs->al);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400514}
515
516static void
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400517verify_scroll(struct bregs *regs, int dir)
518{
519 u8 page = GET_BDA(video_page);
520 struct cursorpos ul = {regs->cl, regs->ch, page};
521 struct cursorpos lr = {regs->dl, regs->dh, page};
522
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400523 u16 nbrows = GET_BDA(video_rows) + 1;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400524 if (lr.y >= nbrows)
525 lr.y = nbrows - 1;
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400526 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400527 if (lr.x >= nbcols)
528 lr.x = nbcols - 1;
529
Kevin O'Connorc3e15872009-05-31 01:37:54 -0400530 if (ul.x > lr.x || ul.y > lr.y)
531 return;
532
533 u16 nblines = regs->al;
534 if (!nblines || nblines > lr.y - ul.y + 1)
535 nblines = lr.y - ul.y + 1;
536
537 vgafb_scroll(dir * nblines, regs->bh, ul, lr);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400538}
539
540static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400541handle_1006(struct bregs *regs)
542{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400543 verify_scroll(regs, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400544}
545
546static void
547handle_1007(struct bregs *regs)
548{
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400549 verify_scroll(regs, -1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400550}
551
552static void
553handle_1008(struct bregs *regs)
554{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400555 struct carattr ca = vgafb_read_char(get_cursor_pos(regs->bh));
Kevin O'Connor09262412009-05-25 11:44:11 -0400556 regs->al = ca.car;
557 regs->ah = ca.attr;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400558}
559
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400560static void noinline
Kevin O'Connord3b38152009-05-26 00:05:37 -0400561write_chars(u8 page, struct carattr ca, u16 count)
562{
563 struct cursorpos cp = get_cursor_pos(page);
564 while (count--) {
565 vgafb_write_char(cp, ca);
566 cp.x++;
567 }
568}
569
570static void
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400571handle_1009(struct bregs *regs)
572{
Kevin O'Connor09262412009-05-25 11:44:11 -0400573 struct carattr ca = {regs->al, regs->bl, 1};
Kevin O'Connord3b38152009-05-26 00:05:37 -0400574 write_chars(regs->bh, ca, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400575}
576
577static void
578handle_100a(struct bregs *regs)
579{
Kevin O'Connor09262412009-05-25 11:44:11 -0400580 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connord3b38152009-05-26 00:05:37 -0400581 write_chars(regs->bh, ca, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400582}
583
584
585static void
586handle_100b00(struct bregs *regs)
587{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400588 vgahw_set_border_color(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400589}
590
591static void
592handle_100b01(struct bregs *regs)
593{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400594 vgahw_set_palette(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400595}
596
597static void
598handle_100bXX(struct bregs *regs)
599{
600 debug_stub(regs);
601}
602
603static void
604handle_100b(struct bregs *regs)
605{
606 switch (regs->bh) {
607 case 0x00: handle_100b00(regs); break;
608 case 0x01: handle_100b01(regs); break;
609 default: handle_100bXX(regs); break;
610 }
611}
612
613
614static void
615handle_100c(struct bregs *regs)
616{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400617 // XXX - page (regs->bh) is unused
618 vgafb_write_pixel(regs->al, regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400619}
620
621static void
622handle_100d(struct bregs *regs)
623{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400624 // XXX - page (regs->bh) is unused
625 regs->al = vgafb_read_pixel(regs->cx, regs->dx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400626}
627
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400628static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400629handle_100e(struct bregs *regs)
630{
631 // Ralf Brown Interrupt list is WRONG on bh(page)
632 // We do output only on the current page !
Kevin O'Connor09262412009-05-25 11:44:11 -0400633 struct carattr ca = {regs->al, regs->bl, 0};
Kevin O'Connor116a0442009-05-26 00:47:32 -0400634 struct cursorpos cp = get_cursor_pos(0xff);
Kevin O'Connorafb287d2009-05-31 21:15:33 -0400635 write_teletype(&cp, ca);
Kevin O'Connor116a0442009-05-26 00:47:32 -0400636 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400637}
638
639static void
640handle_100f(struct bregs *regs)
641{
Kevin O'Connore7132042009-05-25 00:10:35 -0400642 regs->bh = GET_BDA(video_page);
643 regs->al = GET_BDA(video_mode) | (GET_BDA(video_ctl) & 0x80);
644 regs->ah = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400645}
646
647
648static void
649handle_101000(struct bregs *regs)
650{
651 if (regs->bl > 0x14)
652 return;
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400653 vgahw_set_single_palette_reg(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400654}
655
656static void
657handle_101001(struct bregs *regs)
658{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400659 vgahw_set_overscan_border_color(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400660}
661
662static void
663handle_101002(struct bregs *regs)
664{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400665 vgahw_set_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400666}
667
668static void
669handle_101003(struct bregs *regs)
670{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400671 vgahw_toggle_intensity(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400672}
673
674static void
675handle_101007(struct bregs *regs)
676{
677 if (regs->bl > 0x14)
678 return;
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400679 regs->bh = vgahw_get_single_palette_reg(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400680}
681
682static void
683handle_101008(struct bregs *regs)
684{
Kevin O'Connor3862b2d2010-01-03 17:53:58 -0500685 regs->bh = vgahw_get_overscan_border_color();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400686}
687
688static void
689handle_101009(struct bregs *regs)
690{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400691 vgahw_get_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400692}
693
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400694static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400695handle_101010(struct bregs *regs)
696{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400697 u8 rgb[3] = {regs->dh, regs->ch, regs->cl};
698 vgahw_set_dac_regs(GET_SEG(SS), rgb, regs->bx, 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400699}
700
701static void
702handle_101012(struct bregs *regs)
703{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400704 vgahw_set_dac_regs(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400705}
706
707static void
708handle_101013(struct bregs *regs)
709{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400710 vgahw_select_video_dac_color_page(regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400711}
712
Kevin O'Connor0ad77f02009-05-31 20:46:43 -0400713static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400714handle_101015(struct bregs *regs)
715{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400716 u8 rgb[3];
717 vgahw_get_dac_regs(GET_SEG(SS), rgb, regs->bx, 1);
718 regs->dh = rgb[0];
719 regs->ch = rgb[1];
720 regs->cl = rgb[2];
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400721}
722
723static void
724handle_101017(struct bregs *regs)
725{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400726 vgahw_get_dac_regs(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400727}
728
729static void
730handle_101018(struct bregs *regs)
731{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400732 vgahw_set_pel_mask(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400733}
734
735static void
736handle_101019(struct bregs *regs)
737{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400738 regs->bl = vgahw_get_pel_mask();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400739}
740
741static void
742handle_10101a(struct bregs *regs)
743{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400744 vgahw_read_video_dac_state(&regs->bl, &regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400745}
746
747static void
748handle_10101b(struct bregs *regs)
749{
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400750 perform_gray_scale_summing(regs->bx, regs->cx);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400751}
752
753static void
754handle_1010XX(struct bregs *regs)
755{
756 debug_stub(regs);
757}
758
759static void
760handle_1010(struct bregs *regs)
761{
762 switch (regs->al) {
763 case 0x00: handle_101000(regs); break;
764 case 0x01: handle_101001(regs); break;
765 case 0x02: handle_101002(regs); break;
766 case 0x03: handle_101003(regs); break;
767 case 0x07: handle_101007(regs); break;
768 case 0x08: handle_101008(regs); break;
769 case 0x09: handle_101009(regs); break;
770 case 0x10: handle_101010(regs); break;
771 case 0x12: handle_101012(regs); break;
772 case 0x13: handle_101013(regs); break;
773 case 0x15: handle_101015(regs); break;
774 case 0x17: handle_101017(regs); break;
775 case 0x18: handle_101018(regs); break;
776 case 0x19: handle_101019(regs); break;
777 case 0x1a: handle_10101a(regs); break;
778 case 0x1b: handle_10101b(regs); break;
779 default: handle_1010XX(regs); break;
780 }
781}
782
783
784static void
785handle_101100(struct bregs *regs)
786{
Kevin O'Connordeb9cb92009-05-25 09:06:50 -0400787 vgafb_load_font(regs->es, (void*)(regs->bp+0), regs->cx
788 , regs->dx, regs->bl, regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400789}
790
791static void
792handle_101101(struct bregs *regs)
793{
Kevin O'Connordeb9cb92009-05-25 09:06:50 -0400794 vgafb_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400795}
796
797static void
798handle_101102(struct bregs *regs)
799{
Kevin O'Connordeb9cb92009-05-25 09:06:50 -0400800 vgafb_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400801}
802
803static void
804handle_101103(struct bregs *regs)
805{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400806 vgahw_set_text_block_specifier(regs->bl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400807}
808
809static void
810handle_101104(struct bregs *regs)
811{
Kevin O'Connordeb9cb92009-05-25 09:06:50 -0400812 vgafb_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400813}
814
815static void
816handle_101110(struct bregs *regs)
817{
Kevin O'Connordeb9cb92009-05-25 09:06:50 -0400818 vgafb_load_font(regs->es, (void*)(regs->bp+0), regs->cx
819 , regs->dx, regs->bl, regs->bh);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400820 set_scan_lines(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400821}
822
823static void
824handle_101111(struct bregs *regs)
825{
Kevin O'Connordeb9cb92009-05-25 09:06:50 -0400826 vgafb_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400827 set_scan_lines(14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400828}
829
830static void
831handle_101112(struct bregs *regs)
832{
Kevin O'Connordeb9cb92009-05-25 09:06:50 -0400833 vgafb_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400834 set_scan_lines(8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400835}
836
837static void
838handle_101114(struct bregs *regs)
839{
Kevin O'Connordeb9cb92009-05-25 09:06:50 -0400840 vgafb_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400841 set_scan_lines(16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400842}
843
844static void
845handle_101130(struct bregs *regs)
846{
Kevin O'Connore7132042009-05-25 00:10:35 -0400847 switch (regs->bh) {
848 case 0x00: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500849 struct segoff_s so = GET_IVT(0x1f);
850 regs->es = so.seg;
851 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400852 break;
853 }
854 case 0x01: {
Kevin O'Connorc9d3c2d2010-01-01 12:53:32 -0500855 struct segoff_s so = GET_IVT(0x43);
856 regs->es = so.seg;
857 regs->bp = so.offset;
Kevin O'Connore7132042009-05-25 00:10:35 -0400858 break;
859 }
860 case 0x02:
861 regs->es = get_global_seg();
862 regs->bp = (u32)vgafont14;
863 break;
864 case 0x03:
865 regs->es = get_global_seg();
866 regs->bp = (u32)vgafont8;
867 break;
868 case 0x04:
869 regs->es = get_global_seg();
870 regs->bp = (u32)vgafont8 + 128 * 8;
871 break;
872 case 0x05:
873 regs->es = get_global_seg();
874 regs->bp = (u32)vgafont14alt;
875 break;
876 case 0x06:
877 regs->es = get_global_seg();
878 regs->bp = (u32)vgafont16;
879 break;
880 case 0x07:
881 regs->es = get_global_seg();
882 regs->bp = (u32)vgafont16alt;
883 break;
884 default:
885 dprintf(1, "Get font info BH(%02x) was discarded\n", regs->bh);
886 return;
887 }
888 // Set byte/char of on screen font
889 regs->cx = GET_BDA(char_height) & 0xff;
890
891 // Set Highest char row
892 regs->dx = GET_BDA(video_rows);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400893}
894
895static void
896handle_1011XX(struct bregs *regs)
897{
898 debug_stub(regs);
899}
900
901static void
902handle_1011(struct bregs *regs)
903{
904 switch (regs->al) {
905 case 0x00: handle_101100(regs); break;
906 case 0x01: handle_101101(regs); break;
907 case 0x02: handle_101102(regs); break;
908 case 0x03: handle_101103(regs); break;
909 case 0x04: handle_101104(regs); break;
910 case 0x10: handle_101110(regs); break;
911 case 0x11: handle_101111(regs); break;
912 case 0x12: handle_101112(regs); break;
913 case 0x14: handle_101114(regs); break;
914 case 0x30: handle_101130(regs); break;
915 default: handle_1011XX(regs); break;
916 }
917}
918
919
920static void
921handle_101210(struct bregs *regs)
922{
Kevin O'Connore7132042009-05-25 00:10:35 -0400923 u16 crtc_addr = GET_BDA(crtc_address);
924 if (crtc_addr == VGAREG_MDA_CRTC_ADDRESS)
925 regs->bx = 0x0103;
926 else
927 regs->bx = 0x0003;
928 regs->cx = GET_BDA(video_switches) & 0x0f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400929}
930
931static void
932handle_101230(struct bregs *regs)
933{
Kevin O'Connore7132042009-05-25 00:10:35 -0400934 u8 mctl = GET_BDA(modeset_ctl);
935 u8 vswt = GET_BDA(video_switches);
936 switch (regs->al) {
937 case 0x00:
938 // 200 lines
939 mctl = (mctl & ~0x10) | 0x80;
940 vswt = (vswt & ~0x0f) | 0x08;
941 break;
942 case 0x01:
943 // 350 lines
944 mctl &= ~0x90;
945 vswt = (vswt & ~0x0f) | 0x09;
946 break;
947 case 0x02:
948 // 400 lines
949 mctl = (mctl & ~0x80) | 0x10;
950 vswt = (vswt & ~0x0f) | 0x09;
951 break;
952 default:
953 dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
954 break;
955 }
956 SET_BDA(modeset_ctl, mctl);
957 SET_BDA(video_switches, vswt);
958 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400959}
960
961static void
962handle_101231(struct bregs *regs)
963{
Kevin O'Connore7132042009-05-25 00:10:35 -0400964 u8 v = (regs->al & 0x01) << 3;
965 u8 mctl = GET_BDA(video_ctl) & ~0x08;
966 SET_BDA(video_ctl, mctl | v);
967 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400968}
969
970static void
971handle_101232(struct bregs *regs)
972{
Kevin O'Connor8bc059e2009-05-17 21:19:36 -0400973 vgahw_enable_video_addressing(regs->al);
974 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400975}
976
977static void
978handle_101233(struct bregs *regs)
979{
Kevin O'Connore7132042009-05-25 00:10:35 -0400980 u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
981 u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
982 SET_BDA(modeset_ctl, v | v2);
983 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400984}
985
986static void
987handle_101234(struct bregs *regs)
988{
Kevin O'Connore7132042009-05-25 00:10:35 -0400989 u8 v = (regs->al & 0x01) ^ 0x01;
990 u8 v2 = GET_BDA(modeset_ctl) & ~0x01;
991 SET_BDA(modeset_ctl, v | v2);
992 regs->al = 0x12;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400993}
994
995static void
996handle_101235(struct bregs *regs)
997{
998 debug_stub(regs);
999 regs->al = 0x12;
1000}
1001
1002static void
1003handle_101236(struct bregs *regs)
1004{
1005 debug_stub(regs);
1006 regs->al = 0x12;
1007}
1008
1009static void
1010handle_1012XX(struct bregs *regs)
1011{
1012 debug_stub(regs);
1013}
1014
1015static void
1016handle_1012(struct bregs *regs)
1017{
1018 switch (regs->bl) {
1019 case 0x10: handle_101210(regs); break;
1020 case 0x30: handle_101230(regs); break;
1021 case 0x31: handle_101231(regs); break;
1022 case 0x32: handle_101232(regs); break;
1023 case 0x33: handle_101233(regs); break;
1024 case 0x34: handle_101234(regs); break;
1025 case 0x35: handle_101235(regs); break;
1026 case 0x36: handle_101236(regs); break;
1027 default: handle_1012XX(regs); break;
1028 }
1029
1030 // XXX - cirrus has 1280, 1281, 1282, 1285, 129a, 12a0, 12a1, 12a2, 12ae
1031}
1032
1033
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001034// Write string
Kevin O'Connor0ad77f02009-05-31 20:46:43 -04001035static void noinline
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001036handle_1013(struct bregs *regs)
1037{
Kevin O'Connor918b1562009-05-25 11:05:18 -04001038 struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
Kevin O'Connor2e86c6a2009-05-31 20:43:06 -04001039 // if row=0xff special case : use current cursor position
1040 if (cp.y == 0xff)
1041 cp = get_cursor_pos(cp.page);
Kevin O'Connorafb287d2009-05-31 21:15:33 -04001042 u8 flag = regs->al;
1043 if (flag & 2)
1044 write_attr_string(&cp, regs->cx, regs->es, (void*)(regs->bp + 0));
1045 else
1046 write_string(&cp, regs->bl, regs->cx, regs->es, (void*)(regs->bp + 0));
1047
1048 if (flag & 1)
1049 set_cursor_pos(cp);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001050}
1051
1052
1053static void
1054handle_101a00(struct bregs *regs)
1055{
Kevin O'Connore7132042009-05-25 00:10:35 -04001056 regs->bx = GET_BDA(dcc_index);
1057 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001058}
1059
1060static void
1061handle_101a01(struct bregs *regs)
1062{
Kevin O'Connore7132042009-05-25 00:10:35 -04001063 SET_BDA(dcc_index, regs->bl);
1064 dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
1065 regs->al = 0x1a;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001066}
1067
1068static void
1069handle_101aXX(struct bregs *regs)
1070{
1071 debug_stub(regs);
1072}
1073
1074static void
1075handle_101a(struct bregs *regs)
1076{
1077 switch (regs->al) {
1078 case 0x00: handle_101a00(regs); break;
1079 case 0x01: handle_101a01(regs); break;
1080 default: handle_101aXX(regs); break;
1081 }
1082}
1083
1084
Kevin O'Connorca668642009-05-21 23:06:08 -04001085struct funcInfo {
1086 u16 static_functionality_off;
1087 u16 static_functionality_seg;
1088 u8 bda_0x49[30];
1089 u8 bda_0x84[3];
1090 u8 dcc_index;
1091 u8 dcc_alt;
1092 u16 colors;
1093 u8 pages;
1094 u8 scan_lines;
1095 u8 primary_char;
1096 u8 secondar_char;
1097 u8 misc;
1098 u8 non_vga_mode;
1099 u8 reserved_2f[2];
1100 u8 video_mem;
1101 u8 save_flags;
1102 u8 disp_info;
1103 u8 reserved_34[12];
1104};
1105
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001106static void
1107handle_101b(struct bregs *regs)
1108{
Kevin O'Connorca668642009-05-21 23:06:08 -04001109 u16 seg = regs->es;
1110 struct funcInfo *info = (void*)(regs->di+0);
1111 memset_far(seg, info, 0, sizeof(*info));
1112 // Address of static functionality table
1113 SET_FARVAR(seg, info->static_functionality_off, (u32)static_functionality);
1114 SET_FARVAR(seg, info->static_functionality_seg, get_global_seg());
1115
1116 // Hard coded copy from BIOS area. Should it be cleaner ?
Kevin O'Connor9f985422009-09-09 11:34:39 -04001117 memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49
1118 , sizeof(info->bda_0x49));
1119 memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84
1120 , sizeof(info->bda_0x84));
Kevin O'Connorca668642009-05-21 23:06:08 -04001121
1122 SET_FARVAR(seg, info->dcc_index, GET_BDA(dcc_index));
1123 SET_FARVAR(seg, info->colors, 16);
1124 SET_FARVAR(seg, info->pages, 8);
1125 SET_FARVAR(seg, info->scan_lines, 2);
1126 SET_FARVAR(seg, info->video_mem, 3);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001127 regs->al = 0x1B;
1128}
1129
1130
1131static void
1132handle_101c00(struct bregs *regs)
1133{
Kevin O'Connorca668642009-05-21 23:06:08 -04001134 u16 flags = regs->cx;
1135 u16 size = 0;
1136 if (flags & 1)
1137 size += sizeof(struct saveVideoHardware);
1138 if (flags & 2)
1139 size += sizeof(struct saveBDAstate);
1140 if (flags & 4)
1141 size += sizeof(struct saveDACcolors);
1142 regs->bx = size;
1143 regs->al = 0x1c;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001144}
1145
1146static void
1147handle_101c01(struct bregs *regs)
1148{
Kevin O'Connorca668642009-05-21 23:06:08 -04001149 u16 flags = regs->cx;
1150 u16 seg = regs->es;
1151 void *data = (void*)(regs->bx+0);
1152 if (flags & 1) {
1153 vgahw_save_state(seg, data);
1154 data += sizeof(struct saveVideoHardware);
1155 }
1156 if (flags & 2) {
Kevin O'Connor227a2bb2009-05-31 22:00:20 -04001157 save_bda_state(seg, data);
Kevin O'Connorca668642009-05-21 23:06:08 -04001158 data += sizeof(struct saveBDAstate);
1159 }
1160 if (flags & 4)
1161 vgahw_save_dac_state(seg, data);
1162 regs->al = 0x1c;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001163}
1164
1165static void
1166handle_101c02(struct bregs *regs)
1167{
Kevin O'Connorca668642009-05-21 23:06:08 -04001168 u16 flags = regs->cx;
1169 u16 seg = regs->es;
1170 void *data = (void*)(regs->bx+0);
1171 if (flags & 1) {
1172 vgahw_restore_state(seg, data);
1173 data += sizeof(struct saveVideoHardware);
1174 }
1175 if (flags & 2) {
Kevin O'Connor227a2bb2009-05-31 22:00:20 -04001176 restore_bda_state(seg, data);
Kevin O'Connorca668642009-05-21 23:06:08 -04001177 data += sizeof(struct saveBDAstate);
1178 }
1179 if (flags & 4)
1180 vgahw_restore_dac_state(seg, data);
1181 regs->al = 0x1c;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001182}
1183
1184static void
1185handle_101cXX(struct bregs *regs)
1186{
1187 debug_stub(regs);
1188}
1189
1190static void
1191handle_101c(struct bregs *regs)
1192{
1193 switch (regs->al) {
1194 case 0x00: handle_101c00(regs); break;
1195 case 0x01: handle_101c01(regs); break;
1196 case 0x02: handle_101c02(regs); break;
1197 default: handle_101cXX(regs); break;
1198 }
1199}
1200
1201
1202static void
1203handle_104f00(struct bregs *regs)
1204{
1205 // XXX - vbe_biosfn_return_controller_information(&AX,ES,DI);
1206 // XXX - OR cirrus_vesa_00h
1207}
1208
1209static void
1210handle_104f01(struct bregs *regs)
1211{
1212 // XXX - vbe_biosfn_return_mode_information(&AX,CX,ES,DI);
1213 // XXX - OR cirrus_vesa_01h
1214}
1215
1216static void
1217handle_104f02(struct bregs *regs)
1218{
1219 // XXX - vbe_biosfn_set_mode(&AX,BX,ES,DI);
1220 // XXX - OR cirrus_vesa_02h
1221}
1222
1223static void
1224handle_104f03(struct bregs *regs)
1225{
1226 // XXX - vbe_biosfn_return_current_mode
1227 // XXX - OR cirrus_vesa_03h
1228}
1229
1230static void
1231handle_104f04(struct bregs *regs)
1232{
1233 // XXX - vbe_biosfn_save_restore_state(&AX, CX, DX, ES, &BX);
1234}
1235
1236static void
1237handle_104f05(struct bregs *regs)
1238{
1239 // XXX - vbe_biosfn_display_window_control
1240 // XXX - OR cirrus_vesa_05h
1241}
1242
1243static void
1244handle_104f06(struct bregs *regs)
1245{
1246 // XXX - vbe_biosfn_set_get_logical_scan_line_length
1247 // XXX - OR cirrus_vesa_06h
1248}
1249
1250static void
1251handle_104f07(struct bregs *regs)
1252{
1253 // XXX - vbe_biosfn_set_get_display_start
1254 // XXX - OR cirrus_vesa_07h
1255}
1256
1257static void
1258handle_104f08(struct bregs *regs)
1259{
1260 // XXX - vbe_biosfn_set_get_dac_palette_format
1261}
1262
1263static void
1264handle_104f0a(struct bregs *regs)
1265{
1266 // XXX - vbe_biosfn_return_protected_mode_interface
1267}
1268
1269static void
1270handle_104fXX(struct bregs *regs)
1271{
1272 debug_stub(regs);
1273 regs->ax = 0x0100;
1274}
1275
1276static void
1277handle_104f(struct bregs *regs)
1278{
Julian Pidancet677631f2011-12-19 05:07:53 +00001279 if (! CONFIG_VGA_BOCHS || !bochs_has_vbe_display()) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001280 handle_104fXX(regs);
1281 return;
1282 }
1283
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001284 switch (regs->al) {
1285 case 0x00: handle_104f00(regs); break;
1286 case 0x01: handle_104f01(regs); break;
1287 case 0x02: handle_104f02(regs); break;
1288 case 0x03: handle_104f03(regs); break;
1289 case 0x04: handle_104f04(regs); break;
1290 case 0x05: handle_104f05(regs); break;
1291 case 0x06: handle_104f06(regs); break;
1292 case 0x07: handle_104f07(regs); break;
1293 case 0x08: handle_104f08(regs); break;
1294 case 0x0a: handle_104f0a(regs); break;
1295 default: handle_104fXX(regs); break;
1296 }
1297}
1298
1299
1300static void
1301handle_10XX(struct bregs *regs)
1302{
1303 debug_stub(regs);
1304}
1305
1306// INT 10h Video Support Service Entry Point
1307void VISIBLE16
1308handle_10(struct bregs *regs)
1309{
1310 debug_enter(regs, DEBUG_VGA_10);
1311 switch (regs->ah) {
1312 case 0x00: handle_1000(regs); break;
1313 case 0x01: handle_1001(regs); break;
1314 case 0x02: handle_1002(regs); break;
1315 case 0x03: handle_1003(regs); break;
1316 case 0x04: handle_1004(regs); break;
1317 case 0x05: handle_1005(regs); break;
1318 case 0x06: handle_1006(regs); break;
1319 case 0x07: handle_1007(regs); break;
1320 case 0x08: handle_1008(regs); break;
1321 case 0x09: handle_1009(regs); break;
1322 case 0x0a: handle_100a(regs); break;
1323 case 0x0b: handle_100b(regs); break;
1324 case 0x0c: handle_100c(regs); break;
1325 case 0x0d: handle_100d(regs); break;
1326 case 0x0e: handle_100e(regs); break;
1327 case 0x0f: handle_100f(regs); break;
1328 case 0x10: handle_1010(regs); break;
1329 case 0x11: handle_1011(regs); break;
1330 case 0x12: handle_1012(regs); break;
1331 case 0x13: handle_1013(regs); break;
1332 case 0x1a: handle_101a(regs); break;
1333 case 0x1b: handle_101b(regs); break;
1334 case 0x1c: handle_101c(regs); break;
1335 case 0x4f: handle_104f(regs); break;
1336 default: handle_10XX(regs); break;
1337 }
1338}
1339
1340
1341/****************************************************************
1342 * VGA post
1343 ****************************************************************/
1344
1345static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -05001346init_bios_area(void)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001347{
1348 // init detected hardware BIOS Area
1349 // set 80x25 color (not clear from RBIL but usual)
1350 u16 eqf = GET_BDA(equipment_list_flags);
1351 SET_BDA(equipment_list_flags, (eqf & 0xffcf) | 0x20);
1352
1353 // Just for the first int10 find its children
1354
1355 // the default char height
1356 SET_BDA(char_height, 0x10);
1357
1358 // Clear the screen
1359 SET_BDA(video_ctl, 0x60);
1360
1361 // Set the basic screen we have
1362 SET_BDA(video_switches, 0xf9);
1363
1364 // Set the basic modeset options
1365 SET_BDA(modeset_ctl, 0x51);
1366
1367 // Set the default MSR
1368 SET_BDA(video_msr, 0x09);
1369}
1370
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001371void VISIBLE16
1372vga_post(struct bregs *regs)
1373{
1374 debug_enter(regs, DEBUG_VGA_POST);
1375
Kevin O'Connor8bc059e2009-05-17 21:19:36 -04001376 vgahw_init();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001377
1378 init_bios_area();
1379
Julian Pidancet677631f2011-12-19 05:07:53 +00001380 if (CONFIG_VGA_BOCHS)
1381 bochs_init();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001382
1383 extern void entry_10(void);
Kevin O'Connor9f985422009-09-09 11:34:39 -04001384 SET_IVT(0x10, SEGOFF(get_global_seg(), (u32)entry_10));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001385
Julian Pidancet677631f2011-12-19 05:07:53 +00001386 if (CONFIG_VGA_CIRRUS)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001387 cirrus_init();
1388
1389 // XXX - clear screen and display info
1390
1391 // XXX: fill it
1392 SET_VGA(video_save_pointer_table[0], (u32)video_param_table);
Kevin O'Connord113a992009-05-16 21:05:02 -04001393 SET_VGA(video_save_pointer_table[1], get_global_seg());
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001394
1395 // Fixup checksum
1396 extern u8 _rom_header_size, _rom_header_checksum;
1397 SET_VGA(_rom_header_checksum, 0);
Kevin O'Connord113a992009-05-16 21:05:02 -04001398 u8 sum = -checksum_far(get_global_seg(), 0, _rom_header_size * 512);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001399 SET_VGA(_rom_header_checksum, sum);
1400}