blob: b7cddee828e75159de0a3bd33cd7bf196684df4f [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// * introduce "struct vregs", or add ebp to struct bregs.
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040011// * define structs for save/restore state
12// * review correctness of converted asm by comparing with RBIL
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040013// * refactor redundant code into sub-functions
14// * See if there is a method to the in/out stuff that can be encapsulated.
15// * remove "biosfn" prefixes
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040016// * verify all funcs static
17//
Kevin O'Connor6ace78f2009-05-14 19:24:49 -040018// * convert vbe/clext code
19//
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040020// * separate code into separate files
21// * extract hw code from bios interfaces
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040022
23#include "bregs.h" // struct bregs
24#include "biosvar.h" // GET_BDA
25#include "util.h" // memset
26#include "vgatables.h" // vga_modes
27
28// XXX
29#define CONFIG_VBE 0
30#define CONFIG_CIRRUS 0
31
32// XXX
33#define DEBUG_VGA_POST 1
34#define DEBUG_VGA_10 3
35
Kevin O'Connord113a992009-05-16 21:05:02 -040036#define SET_VGA(var, val) SET_FARVAR(get_global_seg(), (var), (val))
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040037
38
39// ===================================================================
40//
41// Video Utils
42//
43// ===================================================================
44
45// -------------------------------------------------------------------
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040046inline void
47call16_vgaint(u32 eax, u32 ebx)
48{
49 asm volatile(
50 "int $0x10\n"
51 "cli\n"
52 "cld"
53 :
54 : "a"(eax), "b"(ebx)
55 : "cc", "memory");
56}
57
58// XXX
59inline void
60memcpy16_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
61{
62 memcpy_far(d_seg, d_far, s_seg, s_far, len);
63}
64
65
66// ===================================================================
67//
68// BIOS functions
69//
70// ===================================================================
71
72// -------------------------------------------------------------------
73static void
74biosfn_perform_gray_scale_summing(u16 start, u16 count)
75{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040076 inb(VGAREG_ACTL_RESET);
77 outb(0x00, VGAREG_ACTL_ADDRESS);
78
Kevin O'Connordd2be772009-05-16 15:41:23 -040079 int i;
80 for (i = start; i < start+count; i++) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040081 // set read address and switch to read mode
Kevin O'Connordd2be772009-05-16 15:41:23 -040082 outb(i, VGAREG_DAC_READ_ADDRESS);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040083 // get 6-bit wide RGB data values
Kevin O'Connordd2be772009-05-16 15:41:23 -040084 u8 r = inb(VGAREG_DAC_DATA);
85 u8 g = inb(VGAREG_DAC_DATA);
86 u8 b = inb(VGAREG_DAC_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040087
88 // intensity = ( 0.3 * Red ) + ( 0.59 * Green ) + ( 0.11 * Blue )
Kevin O'Connordd2be772009-05-16 15:41:23 -040089 u16 intensity = ((77 * r + 151 * g + 28 * b) + 0x80) >> 8;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040090
Kevin O'Connordd2be772009-05-16 15:41:23 -040091 if (intensity > 0x3f)
92 intensity = 0x3f;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040093
94 // set write address and switch to write mode
Kevin O'Connordd2be772009-05-16 15:41:23 -040095 outb(i, VGAREG_DAC_WRITE_ADDRESS);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040096 // write new intensity value
Kevin O'Connordd2be772009-05-16 15:41:23 -040097 outb(intensity & 0xff, VGAREG_DAC_DATA);
98 outb(intensity & 0xff, VGAREG_DAC_DATA);
99 outb(intensity & 0xff, VGAREG_DAC_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400100 }
101 inb(VGAREG_ACTL_RESET);
102 outb(0x20, VGAREG_ACTL_ADDRESS);
103}
104
105// -------------------------------------------------------------------
106static void
107biosfn_set_cursor_shape(u8 CH, u8 CL)
108{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400109 CH &= 0x3f;
110 CL &= 0x1f;
111
Kevin O'Connordd2be772009-05-16 15:41:23 -0400112 u16 curs = (CH << 8) + CL;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400113 SET_BDA(cursor_type, curs);
114
Kevin O'Connordd2be772009-05-16 15:41:23 -0400115 u8 modeset_ctl = GET_BDA(modeset_ctl);
116 u16 cheight = GET_BDA(char_height);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400117 if ((modeset_ctl & 0x01) && (cheight > 8) && (CL < 8) && (CH < 0x20)) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400118 if (CL != (CH + 1))
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400119 CH = ((CH + 1) * cheight / 8) - 1;
Kevin O'Connordd2be772009-05-16 15:41:23 -0400120 else
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400121 CH = ((CL + 1) * cheight / 8) - 2;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400122 CL = ((CL + 1) * cheight / 8) - 1;
123 }
124 // CTRC regs 0x0a and 0x0b
Kevin O'Connordd2be772009-05-16 15:41:23 -0400125 u16 crtc_addr = GET_BDA(crtc_address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400126 outb(0x0a, crtc_addr);
127 outb(CH, crtc_addr + 1);
128 outb(0x0b, crtc_addr);
129 outb(CL, crtc_addr + 1);
130}
131
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400132static u16
133biosfn_get_cursor_shape(u8 page)
134{
135 if (page > 7)
136 return 0;
137 // FIXME should handle VGA 14/16 lines
138 return GET_BDA(cursor_type);
139}
140
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400141// -------------------------------------------------------------------
142static void
143biosfn_set_cursor_pos(u8 page, u16 cursor)
144{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400145 // Should not happen...
146 if (page > 7)
147 return;
148
149 // Bios cursor pos
150 SET_BDA(cursor_pos[page], cursor);
151
152 // Set the hardware cursor
Kevin O'Connordd2be772009-05-16 15:41:23 -0400153 u8 current = GET_BDA(video_page);
154 if (page != current)
155 return;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400156
Kevin O'Connordd2be772009-05-16 15:41:23 -0400157 // Get the dimensions
158 u16 nbcols = GET_BDA(video_cols);
159 u16 nbrows = GET_BDA(video_rows) + 1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400160
Kevin O'Connordd2be772009-05-16 15:41:23 -0400161 u8 xcurs = cursor & 0x00ff;
162 u8 ycurs = (cursor & 0xff00) >> 8;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400163
Kevin O'Connordd2be772009-05-16 15:41:23 -0400164 // Calculate the address knowing nbcols nbrows and page num
165 u16 address = SCREEN_IO_START(nbcols, nbrows, page) + xcurs + ycurs * nbcols;
166
167 // CRTC regs 0x0e and 0x0f
168 u16 crtc_addr = GET_BDA(crtc_address);
169 outb(0x0e, crtc_addr);
170 outb((address & 0xff00) >> 8, crtc_addr + 1);
171 outb(0x0f, crtc_addr);
172 outb(address & 0x00ff, crtc_addr + 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400173}
174
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400175static u16
176biosfn_get_cursor_pos(u8 page)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400177{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400178 if (page > 7)
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400179 return 0;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400180 // FIXME should handle VGA 14/16 lines
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400181 return GET_BDA(cursor_pos[page]);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400182}
183
184// -------------------------------------------------------------------
185static void
186biosfn_set_active_page(u8 page)
187{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400188 if (page > 7)
189 return;
190
191 // Get the mode
Kevin O'Connor5727c292009-05-16 17:29:32 -0400192 struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
193 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400194 return;
195
196 // Get pos curs pos for the right page
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400197 u16 cursor = biosfn_get_cursor_pos(page);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400198
Kevin O'Connordd2be772009-05-16 15:41:23 -0400199 u16 address;
Kevin O'Connor5727c292009-05-16 17:29:32 -0400200 if (GET_GLOBAL(vmode_g->class) == TEXT) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400201 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -0400202 u16 nbcols = GET_BDA(video_cols);
203 u16 nbrows = GET_BDA(video_rows) + 1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400204
205 // Calculate the address knowing nbcols nbrows and page num
206 address = SCREEN_MEM_START(nbcols, nbrows, page);
207 SET_BDA(video_pagestart, address);
208
209 // Start address
210 address = SCREEN_IO_START(nbcols, nbrows, page);
211 } else {
Kevin O'Connor5727c292009-05-16 17:29:32 -0400212 struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
213 address = page * GET_GLOBAL(vparam_g->slength);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400214 }
215
216 // CRTC regs 0x0c and 0x0d
Kevin O'Connordd2be772009-05-16 15:41:23 -0400217 u16 crtc_addr = GET_BDA(crtc_address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400218 outb(0x0c, crtc_addr);
219 outb((address & 0xff00) >> 8, crtc_addr + 1);
220 outb(0x0d, crtc_addr);
221 outb(address & 0x00ff, crtc_addr + 1);
222
223 // And change the BIOS page
224 SET_BDA(video_page, page);
225
Kevin O'Connora12c2152009-05-13 22:06:16 -0400226 dprintf(1, "Set active page %02x address %04x\n", page, address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400227
228 // Display the cursor, now the page is active
229 biosfn_set_cursor_pos(page, cursor);
230}
231
232static void
233biosfn_set_video_mode(u8 mode)
234{ // mode: Bit 7 is 1 if no clear screen
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400235 if (CONFIG_CIRRUS)
236 cirrus_set_video_mode(mode);
237
Kevin O'Connor21079f42009-05-16 21:30:10 -0400238 if (CONFIG_VBE)
239 if (vbe_has_vbe_display())
240 dispi_set_enable(VBE_DISPI_DISABLED);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400241
242 // The real mode
Kevin O'Connordd2be772009-05-16 15:41:23 -0400243 u8 noclearmem = mode & 0x80;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400244 mode = mode & 0x7f;
245
246 // find the entry in the video modes
Kevin O'Connor5727c292009-05-16 17:29:32 -0400247 struct vgamode_s *vmode_g = find_vga_entry(mode);
248 dprintf(1, "mode search %02x found %p\n", mode, vmode_g);
249 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400250 return;
251
Kevin O'Connor5727c292009-05-16 17:29:32 -0400252 struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
253 u16 twidth = GET_GLOBAL(vparam_g->twidth);
254 u16 theightm1 = GET_GLOBAL(vparam_g->theightm1);
255 u16 cheight = GET_GLOBAL(vparam_g->cheight);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400256
257 // Read the bios mode set control
Kevin O'Connordd2be772009-05-16 15:41:23 -0400258 u8 modeset_ctl = GET_BDA(modeset_ctl);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400259
260 // Then we know the number of lines
261// FIXME
262
263 // if palette loading (bit 3 of modeset ctl = 0)
264 if ((modeset_ctl & 0x08) == 0) { // Set the PEL mask
Kevin O'Connor5727c292009-05-16 17:29:32 -0400265 outb(GET_GLOBAL(vmode_g->pelmask), VGAREG_PEL_MASK);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400266
267 // Set the whole dac always, from 0
268 outb(0x00, VGAREG_DAC_WRITE_ADDRESS);
269
270 // From which palette
Kevin O'Connor5727c292009-05-16 17:29:32 -0400271 u8 *palette_g = GET_GLOBAL(vmode_g->dac);
272 u16 palsize = GET_GLOBAL(vmode_g->dacsize);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400273 // Always 256*3 values
Kevin O'Connordd2be772009-05-16 15:41:23 -0400274 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400275 for (i = 0; i < 0x0100; i++) {
Kevin O'Connor5727c292009-05-16 17:29:32 -0400276 if (i <= palsize) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400277 outb(GET_GLOBAL(palette_g[(i * 3) + 0]), VGAREG_DAC_DATA);
278 outb(GET_GLOBAL(palette_g[(i * 3) + 1]), VGAREG_DAC_DATA);
279 outb(GET_GLOBAL(palette_g[(i * 3) + 2]), VGAREG_DAC_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400280 } else {
281 outb(0, VGAREG_DAC_DATA);
282 outb(0, VGAREG_DAC_DATA);
283 outb(0, VGAREG_DAC_DATA);
284 }
285 }
286 if ((modeset_ctl & 0x02) == 0x02)
287 biosfn_perform_gray_scale_summing(0x00, 0x100);
288 }
289 // Reset Attribute Ctl flip-flop
290 inb(VGAREG_ACTL_RESET);
291
292 // Set Attribute Ctl
Kevin O'Connordd2be772009-05-16 15:41:23 -0400293 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400294 for (i = 0; i <= 0x13; i++) {
295 outb(i, VGAREG_ACTL_ADDRESS);
Kevin O'Connor5727c292009-05-16 17:29:32 -0400296 outb(GET_GLOBAL(vparam_g->actl_regs[i]), VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400297 }
298 outb(0x14, VGAREG_ACTL_ADDRESS);
299 outb(0x00, VGAREG_ACTL_WRITE_DATA);
300
301 // Set Sequencer Ctl
302 outb(0, VGAREG_SEQU_ADDRESS);
303 outb(0x03, VGAREG_SEQU_DATA);
304 for (i = 1; i <= 4; i++) {
305 outb(i, VGAREG_SEQU_ADDRESS);
Kevin O'Connor5727c292009-05-16 17:29:32 -0400306 outb(GET_GLOBAL(vparam_g->sequ_regs[i - 1]), VGAREG_SEQU_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400307 }
308
309 // Set Grafx Ctl
310 for (i = 0; i <= 8; i++) {
311 outb(i, VGAREG_GRDC_ADDRESS);
Kevin O'Connor5727c292009-05-16 17:29:32 -0400312 outb(GET_GLOBAL(vparam_g->grdc_regs[i]), VGAREG_GRDC_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400313 }
314
315 // Set CRTC address VGA or MDA
316 u16 crtc_addr = VGAREG_VGA_CRTC_ADDRESS;
Kevin O'Connor5727c292009-05-16 17:29:32 -0400317 if (GET_GLOBAL(vmode_g->memmodel) == MTEXT)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400318 crtc_addr = VGAREG_MDA_CRTC_ADDRESS;
319
320 // Disable CRTC write protection
321 outw(0x0011, crtc_addr);
322 // Set CRTC regs
323 for (i = 0; i <= 0x18; i++) {
324 outb(i, crtc_addr);
Kevin O'Connor5727c292009-05-16 17:29:32 -0400325 outb(GET_GLOBAL(vparam_g->crtc_regs[i]), crtc_addr + 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400326 }
327
328 // Set the misc register
Kevin O'Connor5727c292009-05-16 17:29:32 -0400329 outb(GET_GLOBAL(vparam_g->miscreg), VGAREG_WRITE_MISC_OUTPUT);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400330
331 // Enable video
332 outb(0x20, VGAREG_ACTL_ADDRESS);
333 inb(VGAREG_ACTL_RESET);
334
335 if (noclearmem == 0x00) {
Kevin O'Connor5727c292009-05-16 17:29:32 -0400336 if (GET_GLOBAL(vmode_g->class) == TEXT) {
337 memset16_far(GET_GLOBAL(vmode_g->sstart), 0, 0x0720, 32*1024);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400338 } else {
339 if (mode < 0x0d) {
Kevin O'Connor5727c292009-05-16 17:29:32 -0400340 memset16_far(GET_GLOBAL(vmode_g->sstart), 0, 0x0000, 32*1024);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400341 } else {
342 outb(0x02, VGAREG_SEQU_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400343 u8 mmask = inb(VGAREG_SEQU_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400344 outb(0x0f, VGAREG_SEQU_DATA); // all planes
Kevin O'Connor5727c292009-05-16 17:29:32 -0400345 memset16_far(GET_GLOBAL(vmode_g->sstart), 0, 0x0000, 64*1024);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400346 outb(mmask, VGAREG_SEQU_DATA);
347 }
348 }
349 }
350 // Set the BIOS mem
351 SET_BDA(video_mode, mode);
352 SET_BDA(video_cols, twidth);
Kevin O'Connor5727c292009-05-16 17:29:32 -0400353 SET_BDA(video_pagesize, GET_GLOBAL(vparam_g->slength));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400354 SET_BDA(crtc_address, crtc_addr);
355 SET_BDA(video_rows, theightm1);
356 SET_BDA(char_height, cheight);
357 SET_BDA(video_ctl, (0x60 | noclearmem));
358 SET_BDA(video_switches, 0xF9);
359 SET_BDA(modeset_ctl, GET_BDA(modeset_ctl) & 0x7f);
360
361 // FIXME We nearly have the good tables. to be reworked
362 SET_BDA(dcc_index, 0x08); // 8 is VGA should be ok for now
363 SET_BDA(video_savetable_ptr, (u32)video_save_pointer_table);
Kevin O'Connord113a992009-05-16 21:05:02 -0400364 SET_BDA(video_savetable_seg, get_global_seg());
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400365
366 // FIXME
367 SET_BDA(video_msr, 0x00); // Unavailable on vanilla vga, but...
368 SET_BDA(video_pal, 0x00); // Unavailable on vanilla vga, but...
369
370 // Set cursor shape
Kevin O'Connor5727c292009-05-16 17:29:32 -0400371 if (GET_GLOBAL(vmode_g->class) == TEXT)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400372 biosfn_set_cursor_shape(0x06, 0x07);
373 // Set cursor pos for page 0..7
374 for (i = 0; i < 8; i++)
375 biosfn_set_cursor_pos(i, 0x0000);
376
377 // Set active page 0
378 biosfn_set_active_page(0x00);
379
380 // Write the fonts in memory
Kevin O'Connor5727c292009-05-16 17:29:32 -0400381 if (GET_GLOBAL(vmode_g->class) == TEXT) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400382 call16_vgaint(0x1104, 0);
383 call16_vgaint(0x1103, 0);
384 }
385 // Set the ints 0x1F and 0x43
Kevin O'Connord113a992009-05-16 21:05:02 -0400386 SET_IVT(0x1f, get_global_seg(), (u32)&vgafont8[128 * 8]);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400387
388 switch (cheight) {
389 case 8:
Kevin O'Connord113a992009-05-16 21:05:02 -0400390 SET_IVT(0x43, get_global_seg(), (u32)vgafont8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400391 break;
392 case 14:
Kevin O'Connord113a992009-05-16 21:05:02 -0400393 SET_IVT(0x43, get_global_seg(), (u32)vgafont14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400394 break;
395 case 16:
Kevin O'Connord113a992009-05-16 21:05:02 -0400396 SET_IVT(0x43, get_global_seg(), (u32)vgafont16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400397 break;
398 }
399}
400
401// -------------------------------------------------------------------
402static void
403vgamem_copy_pl4(u8 xstart, u8 ysrc, u8 ydest, u8 cols, u8 nbcols,
404 u8 cheight)
405{
406 u16 src = ysrc * cheight * nbcols + xstart;
407 u16 dest = ydest * cheight * nbcols + xstart;
408 outw(0x0105, VGAREG_GRDC_ADDRESS);
409 u8 i;
410 for (i = 0; i < cheight; i++)
Kevin O'Connord113a992009-05-16 21:05:02 -0400411 memcpy_far(SEG_GRAPH, (void*)(dest + i * nbcols)
412 , SEG_GRAPH, (void*)(src + i * nbcols), cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400413 outw(0x0005, VGAREG_GRDC_ADDRESS);
414}
415
416// -------------------------------------------------------------------
417static void
418vgamem_fill_pl4(u8 xstart, u8 ystart, u8 cols, u8 nbcols, u8 cheight,
419 u8 attr)
420{
421 u16 dest = ystart * cheight * nbcols + xstart;
422 outw(0x0205, VGAREG_GRDC_ADDRESS);
423 u8 i;
424 for (i = 0; i < cheight; i++)
Kevin O'Connord113a992009-05-16 21:05:02 -0400425 memset_far(SEG_GRAPH, (void*)(dest + i * nbcols), attr, cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400426 outw(0x0005, VGAREG_GRDC_ADDRESS);
427}
428
429// -------------------------------------------------------------------
430static void
431vgamem_copy_cga(u8 xstart, u8 ysrc, u8 ydest, u8 cols, u8 nbcols,
432 u8 cheight)
433{
434 u16 src = ((ysrc * cheight * nbcols) >> 1) + xstart;
435 u16 dest = ((ydest * cheight * nbcols) >> 1) + xstart;
436 u8 i;
437 for (i = 0; i < cheight; i++)
438 if (i & 1)
Kevin O'Connord113a992009-05-16 21:05:02 -0400439 memcpy_far(SEG_CTEXT, (void*)(0x2000 + dest + (i >> 1) * nbcols)
440 , SEG_CTEXT, (void*)(0x2000 + src + (i >> 1) * nbcols)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400441 , cols);
442 else
Kevin O'Connord113a992009-05-16 21:05:02 -0400443 memcpy_far(SEG_CTEXT, (void*)(dest + (i >> 1) * nbcols)
444 , SEG_CTEXT, (void*)(src + (i >> 1) * nbcols), cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400445}
446
447// -------------------------------------------------------------------
448static void
449vgamem_fill_cga(u8 xstart, u8 ystart, u8 cols, u8 nbcols, u8 cheight,
450 u8 attr)
451{
452 u16 dest = ((ystart * cheight * nbcols) >> 1) + xstart;
453 u8 i;
454 for (i = 0; i < cheight; i++)
455 if (i & 1)
Kevin O'Connord113a992009-05-16 21:05:02 -0400456 memset_far(SEG_CTEXT, (void*)(0x2000 + dest + (i >> 1) * nbcols)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400457 , attr, cols);
458 else
Kevin O'Connord113a992009-05-16 21:05:02 -0400459 memset_far(SEG_CTEXT, (void*)(dest + (i >> 1) * nbcols), attr, cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400460}
461
462// -------------------------------------------------------------------
463static void
464biosfn_scroll(u8 nblines, u8 attr, u8 rul, u8 cul, u8 rlr, u8 clr, u8 page,
465 u8 dir)
466{
467 // page == 0xFF if current
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400468 if (rul > rlr)
469 return;
470 if (cul > clr)
471 return;
472
473 // Get the mode
Kevin O'Connor5727c292009-05-16 17:29:32 -0400474 struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
475 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400476 return;
477
478 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -0400479 u16 nbrows = GET_BDA(video_rows) + 1;
480 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400481
482 // Get the current page
483 if (page == 0xFF)
484 page = GET_BDA(video_page);
485
486 if (rlr >= nbrows)
487 rlr = nbrows - 1;
488 if (clr >= nbcols)
489 clr = nbcols - 1;
490 if (nblines > nbrows)
491 nblines = 0;
Kevin O'Connordd2be772009-05-16 15:41:23 -0400492 u8 cols = clr - cul + 1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400493
Kevin O'Connor5727c292009-05-16 17:29:32 -0400494 if (GET_GLOBAL(vmode_g->class) == TEXT) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400495 // Compute the address
Kevin O'Connorf94ec432009-05-16 17:37:23 -0400496 void *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, page));
Kevin O'Connora5288ff2009-05-13 22:25:24 -0400497 dprintf(3, "Scroll, address %p (%d %d %02x)\n"
Kevin O'Connorf94ec432009-05-16 17:37:23 -0400498 , address_far, nbrows, nbcols, page);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400499
500 if (nblines == 0 && rul == 0 && cul == 0 && rlr == nbrows - 1
501 && clr == nbcols - 1) {
Kevin O'Connorf94ec432009-05-16 17:37:23 -0400502 memset16_far(GET_GLOBAL(vmode_g->sstart), address_far
Kevin O'Connora5288ff2009-05-13 22:25:24 -0400503 , (u16)attr * 0x100 + ' ', nbrows * nbcols * 2);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400504 } else { // if Scroll up
505 if (dir == SCROLL_UP) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400506 u16 i;
507 for (i = rul; i <= rlr; i++)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400508 if ((i + nblines > rlr) || (nblines == 0))
Kevin O'Connor5727c292009-05-16 17:29:32 -0400509 memset16_far(GET_GLOBAL(vmode_g->sstart)
Kevin O'Connorf94ec432009-05-16 17:37:23 -0400510 , address_far + (i * nbcols + cul) * 2
Kevin O'Connora5288ff2009-05-13 22:25:24 -0400511 , (u16)attr * 0x100 + ' ', cols * 2);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400512 else
Kevin O'Connor5727c292009-05-16 17:29:32 -0400513 memcpy16_far(GET_GLOBAL(vmode_g->sstart)
Kevin O'Connorf94ec432009-05-16 17:37:23 -0400514 , address_far + (i * nbcols + cul) * 2
Kevin O'Connor5727c292009-05-16 17:29:32 -0400515 , GET_GLOBAL(vmode_g->sstart)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400516 , (void*)(((i + nblines) * nbcols + cul) * 2)
Kevin O'Connora5288ff2009-05-13 22:25:24 -0400517 , cols * 2);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400518 } else {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400519 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400520 for (i = rlr; i >= rul; i--) {
521 if ((i < rul + nblines) || (nblines == 0))
Kevin O'Connor5727c292009-05-16 17:29:32 -0400522 memset16_far(GET_GLOBAL(vmode_g->sstart)
Kevin O'Connorf94ec432009-05-16 17:37:23 -0400523 , address_far + (i * nbcols + cul) * 2
Kevin O'Connora5288ff2009-05-13 22:25:24 -0400524 , (u16)attr * 0x100 + ' ', cols * 2);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400525 else
Kevin O'Connor5727c292009-05-16 17:29:32 -0400526 memcpy16_far(GET_GLOBAL(vmode_g->sstart)
Kevin O'Connorf94ec432009-05-16 17:37:23 -0400527 , address_far + (i * nbcols + cul) * 2
Kevin O'Connor5727c292009-05-16 17:29:32 -0400528 , GET_GLOBAL(vmode_g->sstart)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400529 , (void*)(((i - nblines) * nbcols + cul) * 2)
Kevin O'Connora5288ff2009-05-13 22:25:24 -0400530 , cols * 2);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400531 if (i > rlr)
532 break;
533 }
534 }
535 }
Kevin O'Connordd2be772009-05-16 15:41:23 -0400536 return;
537 }
538
539 // FIXME gfx mode not complete
Kevin O'Connor5727c292009-05-16 17:29:32 -0400540 struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
541 u8 cheight = GET_GLOBAL(vparam_g->cheight);
542 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400543 case PLANAR4:
544 case PLANAR1:
545 if (nblines == 0 && rul == 0 && cul == 0 && rlr == nbrows - 1
546 && clr == nbcols - 1) {
547 outw(0x0205, VGAREG_GRDC_ADDRESS);
Kevin O'Connor5727c292009-05-16 17:29:32 -0400548 memset_far(GET_GLOBAL(vmode_g->sstart), 0, attr,
Kevin O'Connordd2be772009-05-16 15:41:23 -0400549 nbrows * nbcols * cheight);
550 outw(0x0005, VGAREG_GRDC_ADDRESS);
551 } else { // if Scroll up
552 if (dir == SCROLL_UP) {
553 u16 i;
554 for (i = rul; i <= rlr; i++)
555 if ((i + nblines > rlr) || (nblines == 0))
556 vgamem_fill_pl4(cul, i, cols, nbcols, cheight,
557 attr);
558 else
559 vgamem_copy_pl4(cul, i + nblines, i, cols,
560 nbcols, cheight);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400561 } else {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400562 u16 i;
563 for (i = rlr; i >= rul; i--) {
564 if ((i < rul + nblines) || (nblines == 0))
565 vgamem_fill_pl4(cul, i, cols, nbcols, cheight,
566 attr);
567 else
568 vgamem_copy_pl4(cul, i, i - nblines, cols,
569 nbcols, cheight);
570 if (i > rlr)
571 break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400572 }
573 }
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400574 }
Kevin O'Connordd2be772009-05-16 15:41:23 -0400575 break;
576 case CGA: {
Kevin O'Connor5727c292009-05-16 17:29:32 -0400577 u8 bpp = GET_GLOBAL(vmode_g->pixbits);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400578 if (nblines == 0 && rul == 0 && cul == 0 && rlr == nbrows - 1
579 && clr == nbcols - 1) {
Kevin O'Connor5727c292009-05-16 17:29:32 -0400580 memset_far(GET_GLOBAL(vmode_g->sstart), 0, attr,
Kevin O'Connordd2be772009-05-16 15:41:23 -0400581 nbrows * nbcols * cheight * bpp);
582 } else {
583 if (bpp == 2) {
584 cul <<= 1;
585 cols <<= 1;
586 nbcols <<= 1;
587 }
588 // if Scroll up
589 if (dir == SCROLL_UP) {
590 u16 i;
591 for (i = rul; i <= rlr; i++)
592 if ((i + nblines > rlr) || (nblines == 0))
593 vgamem_fill_cga(cul, i, cols, nbcols, cheight,
594 attr);
595 else
596 vgamem_copy_cga(cul, i + nblines, i, cols,
597 nbcols, cheight);
598 } else {
599 u16 i;
600 for (i = rlr; i >= rul; i--) {
601 if ((i < rul + nblines) || (nblines == 0))
602 vgamem_fill_cga(cul, i, cols, nbcols, cheight,
603 attr);
604 else
605 vgamem_copy_cga(cul, i, i - nblines, cols,
606 nbcols, cheight);
607 if (i > rlr)
608 break;
609 }
610 }
611 }
612 break;
613 }
614 default:
615 dprintf(1, "Scroll in graphics mode\n");
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400616 }
617}
618
619// -------------------------------------------------------------------
620static void
621biosfn_read_char_attr(u8 page, u16 *car)
622{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400623 // Get the mode
Kevin O'Connor5727c292009-05-16 17:29:32 -0400624 struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
625 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400626 return;
627
628 // Get the cursor pos for the page
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400629 u16 cursor = biosfn_get_cursor_pos(page);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400630 u8 xcurs = cursor & 0x00ff;
631 u8 ycurs = (cursor & 0xff00) >> 8;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400632
633 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -0400634 u16 nbrows = GET_BDA(video_rows) + 1;
635 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400636
Kevin O'Connor5727c292009-05-16 17:29:32 -0400637 if (GET_GLOBAL(vmode_g->class) == TEXT) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400638 // Compute the address
Kevin O'Connordd2be772009-05-16 15:41:23 -0400639 u16 *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, page)
640 + (xcurs + ycurs * nbcols) * 2);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400641
Kevin O'Connor5727c292009-05-16 17:29:32 -0400642 *car = GET_FARVAR(GET_GLOBAL(vmode_g->sstart), *address_far);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400643 } else {
644 // FIXME gfx mode
Kevin O'Connor6ace78f2009-05-14 19:24:49 -0400645 dprintf(1, "Read char in graphics mode\n");
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400646 }
647}
648
649// -------------------------------------------------------------------
650static void
651write_gfx_char_pl4(u8 car, u8 attr, u8 xcurs, u8 ycurs, u8 nbcols,
652 u8 cheight)
653{
Kevin O'Connordd2be772009-05-16 15:41:23 -0400654 u8 *fdata_g;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400655 switch (cheight) {
656 case 14:
Kevin O'Connordd2be772009-05-16 15:41:23 -0400657 fdata_g = vgafont14;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400658 break;
659 case 16:
Kevin O'Connordd2be772009-05-16 15:41:23 -0400660 fdata_g = vgafont16;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400661 break;
662 default:
Kevin O'Connordd2be772009-05-16 15:41:23 -0400663 fdata_g = vgafont8;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400664 }
Kevin O'Connordd2be772009-05-16 15:41:23 -0400665 u16 addr = xcurs + ycurs * cheight * nbcols;
666 u16 src = car * cheight;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400667 outw(0x0f02, VGAREG_SEQU_ADDRESS);
668 outw(0x0205, VGAREG_GRDC_ADDRESS);
669 if (attr & 0x80)
670 outw(0x1803, VGAREG_GRDC_ADDRESS);
671 else
672 outw(0x0003, VGAREG_GRDC_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400673 u8 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400674 for (i = 0; i < cheight; i++) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400675 u8 *dest_far = (void*)(addr + i * nbcols);
676 u8 j;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400677 for (j = 0; j < 8; j++) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400678 u8 mask = 0x80 >> j;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400679 outw((mask << 8) | 0x08, VGAREG_GRDC_ADDRESS);
Kevin O'Connord113a992009-05-16 21:05:02 -0400680 GET_FARVAR(SEG_GRAPH, *dest_far);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400681 if (GET_GLOBAL(fdata_g[src + i]) & mask)
Kevin O'Connord113a992009-05-16 21:05:02 -0400682 SET_FARVAR(SEG_GRAPH, *dest_far, attr & 0x0f);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400683 else
Kevin O'Connord113a992009-05-16 21:05:02 -0400684 SET_FARVAR(SEG_GRAPH, *dest_far, 0x00);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400685 }
686 }
687 outw(0xff08, VGAREG_GRDC_ADDRESS);
688 outw(0x0005, VGAREG_GRDC_ADDRESS);
689 outw(0x0003, VGAREG_GRDC_ADDRESS);
690}
691
692// -------------------------------------------------------------------
693static void
694write_gfx_char_cga(u8 car, u8 attr, u8 xcurs, u8 ycurs, u8 nbcols, u8 bpp)
695{
Kevin O'Connordd2be772009-05-16 15:41:23 -0400696 u8 *fdata_g = vgafont8;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400697 u16 addr = (xcurs * bpp) + ycurs * 320;
698 u16 src = car * 8;
699 u8 i;
700 for (i = 0; i < 8; i++) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400701 u8 *dest_far = (void*)(addr + (i >> 1) * 80);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400702 if (i & 1)
Kevin O'Connordd2be772009-05-16 15:41:23 -0400703 dest_far += 0x2000;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400704 u8 mask = 0x80;
705 if (bpp == 1) {
706 u8 data = 0;
707 if (attr & 0x80)
Kevin O'Connord113a992009-05-16 21:05:02 -0400708 data = GET_FARVAR(SEG_CTEXT, *dest_far);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400709 u8 j;
710 for (j = 0; j < 8; j++) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400711 if (GET_GLOBAL(fdata_g[src + i]) & mask) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400712 if (attr & 0x80)
713 data ^= (attr & 0x01) << (7 - j);
714 else
715 data |= (attr & 0x01) << (7 - j);
716 }
717 mask >>= 1;
718 }
Kevin O'Connord113a992009-05-16 21:05:02 -0400719 SET_FARVAR(SEG_CTEXT, *dest_far, data);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400720 } else {
721 while (mask > 0) {
722 u8 data = 0;
723 if (attr & 0x80)
Kevin O'Connord113a992009-05-16 21:05:02 -0400724 data = GET_FARVAR(SEG_CTEXT, *dest_far);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400725 u8 j;
726 for (j = 0; j < 4; j++) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400727 if (GET_GLOBAL(fdata_g[src + i]) & mask) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400728 if (attr & 0x80)
729 data ^= (attr & 0x03) << ((3 - j) * 2);
730 else
731 data |= (attr & 0x03) << ((3 - j) * 2);
732 }
733 mask >>= 1;
734 }
Kevin O'Connord113a992009-05-16 21:05:02 -0400735 SET_FARVAR(SEG_CTEXT, *dest_far, data);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400736 dest_far += 1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400737 }
738 }
739 }
740}
741
742// -------------------------------------------------------------------
743static void
744write_gfx_char_lin(u8 car, u8 attr, u8 xcurs, u8 ycurs, u8 nbcols)
745{
Kevin O'Connordd2be772009-05-16 15:41:23 -0400746 u8 *fdata_g = vgafont8;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400747 u16 addr = xcurs * 8 + ycurs * nbcols * 64;
748 u16 src = car * 8;
749 u8 i;
750 for (i = 0; i < 8; i++) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400751 u8 *dest_far = (void*)(addr + i * nbcols * 8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400752 u8 mask = 0x80;
753 u8 j;
754 for (j = 0; j < 8; j++) {
755 u8 data = 0x00;
Kevin O'Connordd2be772009-05-16 15:41:23 -0400756 if (GET_GLOBAL(fdata_g[src + i]) & mask)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400757 data = attr;
Kevin O'Connord113a992009-05-16 21:05:02 -0400758 SET_FARVAR(SEG_GRAPH, dest_far[j], data);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400759 mask >>= 1;
760 }
761 }
762}
763
764// -------------------------------------------------------------------
765static void
766biosfn_write_char_attr(u8 car, u8 page, u8 attr, u16 count)
767{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400768 // Get the mode
Kevin O'Connor5727c292009-05-16 17:29:32 -0400769 struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
770 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400771 return;
772
773 // Get the cursor pos for the page
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400774 u16 cursor = biosfn_get_cursor_pos(page);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400775 u8 xcurs = cursor & 0x00ff;
776 u8 ycurs = (cursor & 0xff00) >> 8;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400777
778 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -0400779 u16 nbrows = GET_BDA(video_rows) + 1;
780 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400781
Kevin O'Connor5727c292009-05-16 17:29:32 -0400782 if (GET_GLOBAL(vmode_g->class) == TEXT) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400783 // Compute the address
Kevin O'Connorf94ec432009-05-16 17:37:23 -0400784 void *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, page)
785 + (xcurs + ycurs * nbcols) * 2);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400786
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400787 u16 dummy = ((u16)attr << 8) + car;
Kevin O'Connorf94ec432009-05-16 17:37:23 -0400788 memset16_far(GET_GLOBAL(vmode_g->sstart), address_far, dummy, count * 2);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400789 return;
790 }
791
792 // FIXME gfx mode not complete
Kevin O'Connor5727c292009-05-16 17:29:32 -0400793 struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
794 u8 cheight = GET_GLOBAL(vparam_g->cheight);
795 u8 bpp = GET_GLOBAL(vmode_g->pixbits);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400796 while ((count-- > 0) && (xcurs < nbcols)) {
Kevin O'Connor5727c292009-05-16 17:29:32 -0400797 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400798 case PLANAR4:
799 case PLANAR1:
800 write_gfx_char_pl4(car, attr, xcurs, ycurs, nbcols,
801 cheight);
802 break;
803 case CGA:
804 write_gfx_char_cga(car, attr, xcurs, ycurs, nbcols, bpp);
805 break;
806 case LINEAR8:
807 write_gfx_char_lin(car, attr, xcurs, ycurs, nbcols);
808 break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400809 }
Kevin O'Connordd2be772009-05-16 15:41:23 -0400810 xcurs++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400811 }
812}
813
814// -------------------------------------------------------------------
815static void
816biosfn_write_char_only(u8 car, u8 page, u8 attr, u16 count)
817{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400818 // Get the mode
Kevin O'Connor5727c292009-05-16 17:29:32 -0400819 struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
820 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400821 return;
822
823 // Get the cursor pos for the page
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400824 u16 cursor = biosfn_get_cursor_pos(page);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400825 u8 xcurs = cursor & 0x00ff;
826 u8 ycurs = (cursor & 0xff00) >> 8;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400827
828 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -0400829 u16 nbrows = GET_BDA(video_rows) + 1;
830 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400831
Kevin O'Connor5727c292009-05-16 17:29:32 -0400832 if (GET_GLOBAL(vmode_g->class) == TEXT) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400833 // Compute the address
Kevin O'Connordd2be772009-05-16 15:41:23 -0400834 u8 *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, page)
835 + (xcurs + ycurs * nbcols) * 2);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400836 while (count-- > 0) {
Kevin O'Connor5727c292009-05-16 17:29:32 -0400837 SET_FARVAR(GET_GLOBAL(vmode_g->sstart), *address_far, car);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400838 address_far += 2;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400839 }
Kevin O'Connordd2be772009-05-16 15:41:23 -0400840 return;
841 }
842
843 // FIXME gfx mode not complete
Kevin O'Connor5727c292009-05-16 17:29:32 -0400844 struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
845 u8 cheight = GET_GLOBAL(vparam_g->cheight);
846 u8 bpp = GET_GLOBAL(vmode_g->pixbits);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400847 while ((count-- > 0) && (xcurs < nbcols)) {
Kevin O'Connor5727c292009-05-16 17:29:32 -0400848 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400849 case PLANAR4:
850 case PLANAR1:
851 write_gfx_char_pl4(car, attr, xcurs, ycurs, nbcols,
852 cheight);
853 break;
854 case CGA:
855 write_gfx_char_cga(car, attr, xcurs, ycurs, nbcols, bpp);
856 break;
857 case LINEAR8:
858 write_gfx_char_lin(car, attr, xcurs, ycurs, nbcols);
859 break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400860 }
Kevin O'Connordd2be772009-05-16 15:41:23 -0400861 xcurs++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400862 }
863}
864
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400865// -------------------------------------------------------------------
866static void
867biosfn_set_border_color(struct bregs *regs)
868{
869 inb(VGAREG_ACTL_RESET);
870 outb(0x00, VGAREG_ACTL_ADDRESS);
871 u8 al = regs->bl & 0x0f;
872 if (al & 0x08)
873 al += 0x08;
Kevin O'Connord9fc0a02009-05-07 22:00:25 -0400874 outb(al, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400875 u8 bl = regs->bl & 0x10;
876
877 int i;
878 for (i = 1; i < 4; i++) {
879 outb(i, VGAREG_ACTL_ADDRESS);
880
881 al = inb(VGAREG_ACTL_READ_DATA);
882 al &= 0xef;
883 al |= bl;
Kevin O'Connord9fc0a02009-05-07 22:00:25 -0400884 outb(al, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400885 }
886 outb(0x20, VGAREG_ACTL_ADDRESS);
887}
888
889static void
890biosfn_set_palette(struct bregs *regs)
891{
892 inb(VGAREG_ACTL_RESET);
893 u8 bl = regs->bl & 0x01;
894 int i;
895 for (i = 1; i < 4; i++) {
896 outb(i, VGAREG_ACTL_ADDRESS);
897
898 u8 al = inb(VGAREG_ACTL_READ_DATA);
899 al &= 0xfe;
900 al |= bl;
Kevin O'Connord9fc0a02009-05-07 22:00:25 -0400901 outb(al, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400902 }
903 outb(0x20, VGAREG_ACTL_ADDRESS);
904}
905
906// -------------------------------------------------------------------
907static void
908biosfn_write_pixel(u8 BH, u8 AL, u16 CX, u16 DX)
909{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400910 // Get the mode
Kevin O'Connor5727c292009-05-16 17:29:32 -0400911 struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
912 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400913 return;
Kevin O'Connor5727c292009-05-16 17:29:32 -0400914 if (GET_GLOBAL(vmode_g->class) == TEXT)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400915 return;
916
Kevin O'Connordd2be772009-05-16 15:41:23 -0400917 u8 *addr_far, mask, attr, data;
Kevin O'Connor5727c292009-05-16 17:29:32 -0400918 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400919 case PLANAR4:
920 case PLANAR1:
Kevin O'Connordd2be772009-05-16 15:41:23 -0400921 addr_far = (void*)(CX / 8 + DX * GET_BDA(video_cols));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400922 mask = 0x80 >> (CX & 0x07);
923 outw((mask << 8) | 0x08, VGAREG_GRDC_ADDRESS);
924 outw(0x0205, VGAREG_GRDC_ADDRESS);
Kevin O'Connord113a992009-05-16 21:05:02 -0400925 data = GET_FARVAR(SEG_GRAPH, *addr_far);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400926 if (AL & 0x80)
927 outw(0x1803, VGAREG_GRDC_ADDRESS);
Kevin O'Connord113a992009-05-16 21:05:02 -0400928 SET_FARVAR(SEG_GRAPH, *addr_far, AL);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400929 outw(0xff08, VGAREG_GRDC_ADDRESS);
930 outw(0x0005, VGAREG_GRDC_ADDRESS);
931 outw(0x0003, VGAREG_GRDC_ADDRESS);
932 break;
933 case CGA:
Kevin O'Connor5727c292009-05-16 17:29:32 -0400934 if (GET_GLOBAL(vmode_g->pixbits) == 2)
Kevin O'Connordd2be772009-05-16 15:41:23 -0400935 addr_far = (void*)((CX >> 2) + (DX >> 1) * 80);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400936 else
Kevin O'Connordd2be772009-05-16 15:41:23 -0400937 addr_far = (void*)((CX >> 3) + (DX >> 1) * 80);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400938 if (DX & 1)
Kevin O'Connordd2be772009-05-16 15:41:23 -0400939 addr_far += 0x2000;
Kevin O'Connord113a992009-05-16 21:05:02 -0400940 data = GET_FARVAR(SEG_CTEXT, *addr_far);
Kevin O'Connor5727c292009-05-16 17:29:32 -0400941 if (GET_GLOBAL(vmode_g->pixbits) == 2) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400942 attr = (AL & 0x03) << ((3 - (CX & 0x03)) * 2);
943 mask = 0x03 << ((3 - (CX & 0x03)) * 2);
944 } else {
945 attr = (AL & 0x01) << (7 - (CX & 0x07));
946 mask = 0x01 << (7 - (CX & 0x07));
947 }
948 if (AL & 0x80) {
949 data ^= attr;
950 } else {
951 data &= ~mask;
952 data |= attr;
953 }
Kevin O'Connord113a992009-05-16 21:05:02 -0400954 SET_FARVAR(SEG_CTEXT, *addr_far, data);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400955 break;
956 case LINEAR8:
Kevin O'Connordd2be772009-05-16 15:41:23 -0400957 addr_far = (void*)(CX + DX * (GET_BDA(video_cols) * 8));
Kevin O'Connord113a992009-05-16 21:05:02 -0400958 SET_FARVAR(SEG_GRAPH, *addr_far, AL);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400959 break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400960 }
961}
962
963// -------------------------------------------------------------------
964static void
965biosfn_read_pixel(u8 BH, u16 CX, u16 DX, u16 *AX)
966{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400967 // Get the mode
Kevin O'Connor5727c292009-05-16 17:29:32 -0400968 struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
969 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400970 return;
Kevin O'Connor5727c292009-05-16 17:29:32 -0400971 if (GET_GLOBAL(vmode_g->class) == TEXT)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400972 return;
973
Kevin O'Connor5727c292009-05-16 17:29:32 -0400974 u8 *addr_far, mask, attr=0, data, i;
975 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400976 case PLANAR4:
977 case PLANAR1:
Kevin O'Connordd2be772009-05-16 15:41:23 -0400978 addr_far = (void*)(CX / 8 + DX * GET_BDA(video_cols));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400979 mask = 0x80 >> (CX & 0x07);
980 attr = 0x00;
981 for (i = 0; i < 4; i++) {
982 outw((i << 8) | 0x04, VGAREG_GRDC_ADDRESS);
Kevin O'Connord113a992009-05-16 21:05:02 -0400983 data = GET_FARVAR(SEG_GRAPH, *addr_far) & mask;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400984 if (data > 0)
985 attr |= (0x01 << i);
986 }
987 break;
988 case CGA:
Kevin O'Connordd2be772009-05-16 15:41:23 -0400989 addr_far = (void*)((CX >> 2) + (DX >> 1) * 80);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400990 if (DX & 1)
Kevin O'Connordd2be772009-05-16 15:41:23 -0400991 addr_far += 0x2000;
Kevin O'Connord113a992009-05-16 21:05:02 -0400992 data = GET_FARVAR(SEG_CTEXT, *addr_far);
Kevin O'Connor5727c292009-05-16 17:29:32 -0400993 if (GET_GLOBAL(vmode_g->pixbits) == 2)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400994 attr = (data >> ((3 - (CX & 0x03)) * 2)) & 0x03;
995 else
996 attr = (data >> (7 - (CX & 0x07))) & 0x01;
997 break;
998 case LINEAR8:
Kevin O'Connordd2be772009-05-16 15:41:23 -0400999 addr_far = (void*)(CX + DX * (GET_BDA(video_cols) * 8));
Kevin O'Connord113a992009-05-16 21:05:02 -04001000 attr = GET_FARVAR(SEG_GRAPH, *addr_far);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001001 break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001002 }
1003 *AX = (*AX & 0xff00) | attr;
1004}
1005
1006// -------------------------------------------------------------------
1007static void
1008biosfn_write_teletype(u8 car, u8 page, u8 attr, u8 flag)
1009{ // flag = WITH_ATTR / NO_ATTR
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001010 // special case if page is 0xff, use current page
1011 if (page == 0xff)
1012 page = GET_BDA(video_page);
1013
1014 // Get the mode
Kevin O'Connor5727c292009-05-16 17:29:32 -04001015 struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
1016 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001017 return;
1018
1019 // Get the cursor pos for the page
Kevin O'Connor0818e1a2009-05-16 18:00:19 -04001020 u16 cursor = biosfn_get_cursor_pos(page);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001021 u8 xcurs = cursor & 0x00ff;
1022 u8 ycurs = (cursor & 0xff00) >> 8;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001023
1024 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -04001025 u16 nbrows = GET_BDA(video_rows) + 1;
1026 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001027
1028 switch (car) {
1029 case 7:
1030 //FIXME should beep
1031 break;
1032
1033 case 8:
1034 if (xcurs > 0)
1035 xcurs--;
1036 break;
1037
1038 case '\r':
1039 xcurs = 0;
1040 break;
1041
1042 case '\n':
1043 ycurs++;
1044 break;
1045
1046 case '\t':
1047 do {
1048 biosfn_write_teletype(' ', page, attr, flag);
Kevin O'Connor0818e1a2009-05-16 18:00:19 -04001049 cursor = biosfn_get_cursor_pos(page);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001050 xcurs = cursor & 0x00ff;
1051 ycurs = (cursor & 0xff00) >> 8;
1052 } while (xcurs % 8 == 0);
1053 break;
1054
1055 default:
1056
Kevin O'Connor5727c292009-05-16 17:29:32 -04001057 if (GET_GLOBAL(vmode_g->class) == TEXT) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001058 // Compute the address
Kevin O'Connordd2be772009-05-16 15:41:23 -04001059 u8 *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, page)
1060 + (xcurs + ycurs * nbcols) * 2);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001061 // Write the char
Kevin O'Connor5727c292009-05-16 17:29:32 -04001062 SET_FARVAR(GET_GLOBAL(vmode_g->sstart), address_far[0], car);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001063 if (flag == WITH_ATTR)
Kevin O'Connor5727c292009-05-16 17:29:32 -04001064 SET_FARVAR(GET_GLOBAL(vmode_g->sstart), address_far[1], attr);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001065 } else {
1066 // FIXME gfx mode not complete
Kevin O'Connor5727c292009-05-16 17:29:32 -04001067 struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
1068 u8 cheight = GET_GLOBAL(vparam_g->cheight);
1069 u8 bpp = GET_GLOBAL(vmode_g->pixbits);
1070 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001071 case PLANAR4:
1072 case PLANAR1:
Kevin O'Connor5727c292009-05-16 17:29:32 -04001073 write_gfx_char_pl4(car, attr, xcurs, ycurs, nbcols, cheight);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001074 break;
1075 case CGA:
1076 write_gfx_char_cga(car, attr, xcurs, ycurs, nbcols, bpp);
1077 break;
1078 case LINEAR8:
1079 write_gfx_char_lin(car, attr, xcurs, ycurs, nbcols);
1080 break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001081 }
1082 }
1083 xcurs++;
1084 }
1085
1086 // Do we need to wrap ?
1087 if (xcurs == nbcols) {
1088 xcurs = 0;
1089 ycurs++;
1090 }
1091 // Do we need to scroll ?
1092 if (ycurs == nbrows) {
Kevin O'Connor5727c292009-05-16 17:29:32 -04001093 if (GET_GLOBAL(vmode_g->class) == TEXT)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001094 biosfn_scroll(0x01, 0x07, 0, 0, nbrows - 1, nbcols - 1, page,
1095 SCROLL_UP);
1096 else
1097 biosfn_scroll(0x01, 0x00, 0, 0, nbrows - 1, nbcols - 1, page,
1098 SCROLL_UP);
1099 ycurs -= 1;
1100 }
1101 // Set the cursor for the page
1102 cursor = ycurs;
1103 cursor <<= 8;
1104 cursor += xcurs;
1105 biosfn_set_cursor_pos(page, cursor);
1106}
1107
1108// -------------------------------------------------------------------
1109static void
1110biosfn_get_video_mode(struct bregs *regs)
1111{
1112 regs->bh = GET_BDA(video_page);
1113 regs->al = GET_BDA(video_mode) | (GET_BDA(video_ctl) & 0x80);
1114 regs->ah = GET_BDA(video_cols);
1115}
1116
1117// -------------------------------------------------------------------
1118static void
1119biosfn_set_overscan_border_color(struct bregs *regs)
1120{
1121 inb(VGAREG_ACTL_RESET);
1122 outb(0x11, VGAREG_ACTL_ADDRESS);
Kevin O'Connord9fc0a02009-05-07 22:00:25 -04001123 outb(regs->bh, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001124 outb(0x20, VGAREG_ACTL_ADDRESS);
1125}
1126
1127// -------------------------------------------------------------------
1128static void
1129biosfn_set_all_palette_reg(struct bregs *regs)
1130{
1131 inb(VGAREG_ACTL_RESET);
1132
Kevin O'Connordd2be772009-05-16 15:41:23 -04001133 u8 *data_far = (u8*)(regs->dx + 0);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001134 int i;
1135 for (i = 0; i < 0x10; i++) {
1136 outb(i, VGAREG_ACTL_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001137 u8 val = GET_FARVAR(regs->es, *data_far);
Kevin O'Connord9fc0a02009-05-07 22:00:25 -04001138 outb(val, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001139 data_far++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001140 }
1141 outb(0x11, VGAREG_ACTL_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001142 outb(GET_FARVAR(regs->es, *data_far), VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001143 outb(0x20, VGAREG_ACTL_ADDRESS);
1144}
1145
1146// -------------------------------------------------------------------
1147static void
1148biosfn_toggle_intensity(struct bregs *regs)
1149{
1150 inb(VGAREG_ACTL_RESET);
1151 outb(0x10, VGAREG_ACTL_ADDRESS);
1152 u8 val = (inb(VGAREG_ACTL_READ_DATA) & 0x7f) | ((regs->bl & 0x01) << 3);
Kevin O'Connord9fc0a02009-05-07 22:00:25 -04001153 outb(val, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001154 outb(0x20, VGAREG_ACTL_ADDRESS);
1155}
1156
1157// -------------------------------------------------------------------
1158void
1159biosfn_set_single_palette_reg(u8 reg, u8 val)
1160{
1161 inb(VGAREG_ACTL_RESET);
1162 outb(reg, VGAREG_ACTL_ADDRESS);
Kevin O'Connord9fc0a02009-05-07 22:00:25 -04001163 outb(val, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001164 outb(0x20, VGAREG_ACTL_ADDRESS);
1165}
1166
1167// -------------------------------------------------------------------
1168u8
1169biosfn_get_single_palette_reg(u8 reg)
1170{
1171 inb(VGAREG_ACTL_RESET);
1172 outb(reg, VGAREG_ACTL_ADDRESS);
1173 u8 v = inb(VGAREG_ACTL_READ_DATA);
1174 inb(VGAREG_ACTL_RESET);
1175 outb(0x20, VGAREG_ACTL_ADDRESS);
1176 return v;
1177}
1178
1179// -------------------------------------------------------------------
1180static void
1181biosfn_read_overscan_border_color(struct bregs *regs)
1182{
1183 inb(VGAREG_ACTL_RESET);
1184 outb(0x11, VGAREG_ACTL_ADDRESS);
1185 regs->bh = inb(VGAREG_ACTL_READ_DATA);
1186 inb(VGAREG_ACTL_RESET);
1187 outb(0x20, VGAREG_ACTL_ADDRESS);
1188}
1189
1190// -------------------------------------------------------------------
1191static void
1192biosfn_get_all_palette_reg(struct bregs *regs)
1193{
Kevin O'Connordd2be772009-05-16 15:41:23 -04001194 u8 *data_far = (u8*)(regs->dx + 0);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001195 int i;
1196 for (i = 0; i < 0x10; i++) {
1197 inb(VGAREG_ACTL_RESET);
1198 outb(i, VGAREG_ACTL_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001199 SET_FARVAR(regs->es, *data_far, inb(VGAREG_ACTL_READ_DATA));
1200 data_far++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001201 }
1202 inb(VGAREG_ACTL_RESET);
1203 outb(0x11, VGAREG_ACTL_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001204 SET_FARVAR(regs->es, *data_far, inb(VGAREG_ACTL_READ_DATA));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001205 inb(VGAREG_ACTL_RESET);
1206 outb(0x20, VGAREG_ACTL_ADDRESS);
1207}
1208
1209// -------------------------------------------------------------------
1210static void
1211biosfn_set_single_dac_reg(struct bregs *regs)
1212{
1213 outb(regs->bl, VGAREG_DAC_WRITE_ADDRESS);
1214 outb(regs->dh, VGAREG_DAC_DATA);
1215 outb(regs->ch, VGAREG_DAC_DATA);
1216 outb(regs->cl, VGAREG_DAC_DATA);
1217}
1218
1219// -------------------------------------------------------------------
1220static void
1221biosfn_set_all_dac_reg(struct bregs *regs)
1222{
1223 outb(regs->bl, VGAREG_DAC_WRITE_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001224 u8 *data_far = (u8*)(regs->dx + 0);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001225 int count = regs->cx;
1226 while (count) {
Kevin O'Connordd2be772009-05-16 15:41:23 -04001227 outb(GET_FARVAR(regs->es, *data_far), VGAREG_DAC_DATA);
1228 data_far++;
1229 outb(GET_FARVAR(regs->es, *data_far), VGAREG_DAC_DATA);
1230 data_far++;
1231 outb(GET_FARVAR(regs->es, *data_far), VGAREG_DAC_DATA);
1232 data_far++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001233 count--;
1234 }
1235}
1236
1237// -------------------------------------------------------------------
1238static void
1239biosfn_select_video_dac_color_page(struct bregs *regs)
1240{
1241 inb(VGAREG_ACTL_RESET);
1242 outb(0x10, VGAREG_ACTL_ADDRESS);
1243 u8 val = inb(VGAREG_ACTL_READ_DATA);
1244 if (!(regs->bl & 0x01)) {
1245 val = (val & 0x7f) | (regs->bh << 7);
Kevin O'Connord9fc0a02009-05-07 22:00:25 -04001246 outb(val, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001247 outb(0x20, VGAREG_ACTL_ADDRESS);
1248 return;
1249 }
1250 inb(VGAREG_ACTL_RESET);
1251 outb(0x14, VGAREG_ACTL_ADDRESS);
1252 u8 bh = regs->bh;
1253 if (!(val & 0x80))
1254 bh <<= 2;
1255 bh &= 0x0f;
Kevin O'Connord9fc0a02009-05-07 22:00:25 -04001256 outb(bh, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001257 outb(0x20, VGAREG_ACTL_ADDRESS);
1258}
1259
1260// -------------------------------------------------------------------
1261static void
1262biosfn_read_single_dac_reg(struct bregs *regs)
1263{
1264 outb(regs->bl, VGAREG_DAC_READ_ADDRESS);
1265 regs->dh = inb(VGAREG_DAC_DATA);
1266 regs->ch = inb(VGAREG_DAC_DATA);
1267 regs->cl = inb(VGAREG_DAC_DATA);
1268}
1269
1270// -------------------------------------------------------------------
1271static void
1272biosfn_read_all_dac_reg(struct bregs *regs)
1273{
1274 outb(regs->bl, VGAREG_DAC_READ_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001275 u8 *data_far = (u8*)(regs->dx + 0);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001276 int count = regs->cx;
1277 while (count) {
Kevin O'Connordd2be772009-05-16 15:41:23 -04001278 SET_FARVAR(regs->es, *data_far, inb(VGAREG_DAC_DATA));
1279 data_far++;
1280 SET_FARVAR(regs->es, *data_far, inb(VGAREG_DAC_DATA));
1281 data_far++;
1282 SET_FARVAR(regs->es, *data_far, inb(VGAREG_DAC_DATA));
1283 data_far++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001284 count--;
1285 }
1286}
1287
1288// -------------------------------------------------------------------
1289static void
1290biosfn_set_pel_mask(struct bregs *regs)
1291{
1292 outb(regs->bl, VGAREG_PEL_MASK);
1293}
1294
1295// -------------------------------------------------------------------
1296static void
1297biosfn_read_pel_mask(struct bregs *regs)
1298{
1299 regs->bl = inb(VGAREG_PEL_MASK);
1300}
1301
1302// -------------------------------------------------------------------
1303static void
1304biosfn_read_video_dac_state(struct bregs *regs)
1305{
1306 inb(VGAREG_ACTL_RESET);
1307 outb(0x10, VGAREG_ACTL_ADDRESS);
1308 u8 val1 = inb(VGAREG_ACTL_READ_DATA) >> 7;
1309
1310 inb(VGAREG_ACTL_RESET);
1311 outb(0x14, VGAREG_ACTL_ADDRESS);
1312 u8 val2 = inb(VGAREG_ACTL_READ_DATA) & 0x0f;
1313 if (!(val1 & 0x01))
1314 val2 >>= 2;
1315
1316 inb(VGAREG_ACTL_RESET);
1317 outb(0x20, VGAREG_ACTL_ADDRESS);
1318
1319 regs->bl = val1;
1320 regs->bh = val2;
1321}
1322
1323// -------------------------------------------------------------------
1324static void
1325get_font_access()
1326{
1327 outw(0x0100, VGAREG_SEQU_ADDRESS);
1328 outw(0x0402, VGAREG_SEQU_ADDRESS);
1329 outw(0x0704, VGAREG_SEQU_ADDRESS);
1330 outw(0x0300, VGAREG_SEQU_ADDRESS);
1331 outw(0x0204, VGAREG_GRDC_ADDRESS);
1332 outw(0x0005, VGAREG_GRDC_ADDRESS);
1333 outw(0x0406, VGAREG_GRDC_ADDRESS);
1334}
1335
1336static void
1337release_font_access()
1338{
1339 outw(0x0100, VGAREG_SEQU_ADDRESS);
1340 outw(0x0302, VGAREG_SEQU_ADDRESS);
1341 outw(0x0304, VGAREG_SEQU_ADDRESS);
1342 outw(0x0300, VGAREG_SEQU_ADDRESS);
1343 u16 v = inw(VGAREG_READ_MISC_OUTPUT);
1344 v = ((v & 0x01) << 10) | 0x0a06;
1345 outw(v, VGAREG_GRDC_ADDRESS);
1346 outw(0x0004, VGAREG_GRDC_ADDRESS);
1347 outw(0x1005, VGAREG_GRDC_ADDRESS);
1348}
1349
1350static void
1351set_scan_lines(u8 lines)
1352{
Kevin O'Connordd2be772009-05-16 15:41:23 -04001353 u16 crtc_addr = GET_BDA(crtc_address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001354 outb(0x09, crtc_addr);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001355 u8 crtc_r9 = inb(crtc_addr + 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001356 crtc_r9 = (crtc_r9 & 0xe0) | (lines - 1);
1357 outb(crtc_r9, crtc_addr + 1);
1358 if (lines == 8)
1359 biosfn_set_cursor_shape(0x06, 0x07);
1360 else
1361 biosfn_set_cursor_shape(lines - 4, lines - 3);
1362 SET_BDA(char_height, lines);
1363 outb(0x12, crtc_addr);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001364 u16 vde = inb(crtc_addr + 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001365 outb(0x07, crtc_addr);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001366 u8 ovl = inb(crtc_addr + 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001367 vde += (((ovl & 0x02) << 7) + ((ovl & 0x40) << 3) + 1);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001368 u8 rows = vde / lines;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001369 SET_BDA(video_rows, rows - 1);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001370 u16 cols = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001371 SET_BDA(video_pagesize, rows * cols * 2);
1372}
1373
1374static void
1375biosfn_load_text_user_pat(u8 AL, u16 ES, u16 BP, u16 CX, u16 DX, u8 BL,
1376 u8 BH)
1377{
1378 get_font_access();
1379 u16 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
1380 u16 i;
1381 for (i = 0; i < CX; i++) {
Kevin O'Connorf94ec432009-05-16 17:37:23 -04001382 void *src_far = (void*)(BP + i * BH);
1383 void *dest_far = (void*)(blockaddr + (DX + i) * 32);
Kevin O'Connord113a992009-05-16 21:05:02 -04001384 memcpy_far(SEG_GRAPH, dest_far, ES, src_far, BH);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001385 }
1386 release_font_access();
1387 if (AL >= 0x10)
1388 set_scan_lines(BH);
1389}
1390
1391static void
1392biosfn_load_text_8_14_pat(u8 AL, u8 BL)
1393{
1394 get_font_access();
1395 u16 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
1396 u16 i;
1397 for (i = 0; i < 0x100; i++) {
1398 u16 src = i * 14;
Kevin O'Connorf94ec432009-05-16 17:37:23 -04001399 void *dest_far = (void*)(blockaddr + i * 32);
Kevin O'Connord113a992009-05-16 21:05:02 -04001400 memcpy_far(SEG_GRAPH, dest_far, get_global_seg(), &vgafont14[src], 14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001401 }
1402 release_font_access();
1403 if (AL >= 0x10)
1404 set_scan_lines(14);
1405}
1406
1407static void
1408biosfn_load_text_8_8_pat(u8 AL, u8 BL)
1409{
1410 get_font_access();
1411 u16 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
1412 u16 i;
1413 for (i = 0; i < 0x100; i++) {
1414 u16 src = i * 8;
Kevin O'Connorf94ec432009-05-16 17:37:23 -04001415 void *dest_far = (void*)(blockaddr + i * 32);
Kevin O'Connord113a992009-05-16 21:05:02 -04001416 memcpy_far(SEG_GRAPH, dest_far, get_global_seg(), &vgafont8[src], 8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001417 }
1418 release_font_access();
1419 if (AL >= 0x10)
1420 set_scan_lines(8);
1421}
1422
1423// -------------------------------------------------------------------
1424static void
1425biosfn_set_text_block_specifier(struct bregs *regs)
1426{
1427 outw((regs->bl << 8) | 0x03, VGAREG_SEQU_ADDRESS);
1428}
1429
1430// -------------------------------------------------------------------
1431static void
1432biosfn_load_text_8_16_pat(u8 AL, u8 BL)
1433{
1434 get_font_access();
1435 u16 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
1436 u16 i;
1437 for (i = 0; i < 0x100; i++) {
1438 u16 src = i * 16;
Kevin O'Connorf94ec432009-05-16 17:37:23 -04001439 void *dest_far = (void*)(blockaddr + i * 32);
Kevin O'Connord113a992009-05-16 21:05:02 -04001440 memcpy_far(SEG_GRAPH, dest_far, get_global_seg(), &vgafont16[src], 16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001441 }
1442 release_font_access();
1443 if (AL >= 0x10)
1444 set_scan_lines(16);
1445}
1446
1447// -------------------------------------------------------------------
1448static void
1449biosfn_get_font_info(u8 BH, u16 *ES, u16 *BP, u16 *CX, u16 *DX)
1450{
1451 switch (BH) {
1452 case 0x00: {
1453 u32 segoff = GET_IVT(0x1f).segoff;
1454 *ES = segoff >> 16;
1455 *BP = segoff;
1456 break;
1457 }
1458 case 0x01: {
1459 u32 segoff = GET_IVT(0x43).segoff;
1460 *ES = segoff >> 16;
1461 *BP = segoff;
1462 break;
1463 }
1464 case 0x02:
Kevin O'Connord113a992009-05-16 21:05:02 -04001465 *ES = get_global_seg();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001466 *BP = (u32)vgafont14;
1467 break;
1468 case 0x03:
Kevin O'Connord113a992009-05-16 21:05:02 -04001469 *ES = get_global_seg();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001470 *BP = (u32)vgafont8;
1471 break;
1472 case 0x04:
Kevin O'Connord113a992009-05-16 21:05:02 -04001473 *ES = get_global_seg();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001474 *BP = (u32)vgafont8 + 128 * 8;
1475 break;
1476 case 0x05:
Kevin O'Connord113a992009-05-16 21:05:02 -04001477 *ES = get_global_seg();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001478 *BP = (u32)vgafont14alt;
1479 break;
1480 case 0x06:
Kevin O'Connord113a992009-05-16 21:05:02 -04001481 *ES = get_global_seg();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001482 *BP = (u32)vgafont16;
1483 break;
1484 case 0x07:
Kevin O'Connord113a992009-05-16 21:05:02 -04001485 *ES = get_global_seg();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001486 *BP = (u32)vgafont16alt;
1487 break;
1488 default:
Kevin O'Connora12c2152009-05-13 22:06:16 -04001489 dprintf(1, "Get font info BH(%02x) was discarded\n", BH);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001490 return;
1491 }
1492 // Set byte/char of on screen font
1493 *CX = GET_BDA(char_height) & 0xff;
1494
1495 // Set Highest char row
1496 *DX = GET_BDA(video_rows);
1497}
1498
1499// -------------------------------------------------------------------
1500static void
1501biosfn_get_ega_info(struct bregs *regs)
1502{
1503 regs->cx = GET_BDA(video_switches) & 0x0f;
1504 regs->ax = GET_BDA(crtc_address);
1505 if (regs->ax == VGAREG_MDA_CRTC_ADDRESS)
1506 regs->bx = 0x0103;
1507 else
1508 regs->bx = 0x0003;
1509}
1510
1511// -------------------------------------------------------------------
1512static void
1513biosfn_select_vert_res(struct bregs *regs)
1514{
1515 u8 mctl = GET_BDA(modeset_ctl);
1516 u8 vswt = GET_BDA(video_switches);
1517
1518 switch (regs->al) {
1519 case 0x00:
1520 // 200 lines
1521 mctl = (mctl & ~0x10) | 0x80;
1522 vswt = (vswt & ~0x0f) | 0x08;
1523 break;
1524 case 0x01:
1525 // 350 lines
1526 mctl &= ~0x90;
1527 vswt = (vswt & ~0x0f) | 0x09;
1528 break;
1529 case 0x02:
1530 // 400 lines
1531 mctl = (mctl & ~0x80) | 0x10;
1532 vswt = (vswt & ~0x0f) | 0x09;
1533 break;
1534 default:
Kevin O'Connora12c2152009-05-13 22:06:16 -04001535 dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001536 break;
1537 }
1538 SET_BDA(modeset_ctl, mctl);
1539 SET_BDA(video_switches, vswt);
1540 regs->ax = 0x1212;
1541}
1542
1543static void
1544biosfn_enable_default_palette_loading(struct bregs *regs)
1545{
1546 u8 v = (regs->al & 0x01) << 3;
1547 u8 mctl = GET_BDA(video_ctl) & ~0x08;
1548 SET_BDA(video_ctl, mctl | v);
1549 regs->ax = 0x1212;
1550}
1551
1552static void
1553biosfn_enable_video_addressing(struct bregs *regs)
1554{
1555 u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
1556 u8 v2 = inb(VGAREG_READ_MISC_OUTPUT) & ~0x02;
1557 outb(v | v2, VGAREG_WRITE_MISC_OUTPUT);
1558 regs->ax = 0x1212;
1559}
1560
1561
1562static void
1563biosfn_enable_grayscale_summing(struct bregs *regs)
1564{
1565 u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
1566 u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
1567 SET_BDA(modeset_ctl, v | v2);
1568 regs->ax = 0x1212;
1569}
1570
1571static void
1572biosfn_enable_cursor_emulation(struct bregs *regs)
1573{
1574 u8 v = (regs->al & 0x01) ^ 0x01;
1575 u8 v2 = GET_BDA(modeset_ctl) & ~0x01;
1576 SET_BDA(modeset_ctl, v | v2);
1577 regs->ax = 0x1212;
1578}
1579
1580// -------------------------------------------------------------------
1581static void
1582biosfn_write_string(u8 flag, u8 page, u8 attr, u16 count, u8 row, u8 col,
Kevin O'Connordd2be772009-05-16 15:41:23 -04001583 u16 seg, u8 *offset_far)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001584{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001585 // Read curs info for the page
Kevin O'Connor0818e1a2009-05-16 18:00:19 -04001586 u16 oldcurs = biosfn_get_cursor_pos(page);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001587
1588 // if row=0xff special case : use current cursor position
1589 if (row == 0xff) {
1590 col = oldcurs & 0x00ff;
1591 row = (oldcurs & 0xff00) >> 8;
1592 }
1593
Kevin O'Connordd2be772009-05-16 15:41:23 -04001594 u16 newcurs = row;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001595 newcurs <<= 8;
1596 newcurs += col;
1597 biosfn_set_cursor_pos(page, newcurs);
1598
1599 while (count-- != 0) {
Kevin O'Connordd2be772009-05-16 15:41:23 -04001600 u8 car = GET_FARVAR(seg, *offset_far);
1601 offset_far++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001602 if ((flag & 0x02) != 0) {
Kevin O'Connordd2be772009-05-16 15:41:23 -04001603 attr = GET_FARVAR(seg, *offset_far);
1604 offset_far++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001605 }
1606
1607 biosfn_write_teletype(car, page, attr, WITH_ATTR);
1608 }
1609
1610 // Set back curs pos
1611 if ((flag & 0x01) == 0)
1612 biosfn_set_cursor_pos(page, oldcurs);
1613}
1614
1615// -------------------------------------------------------------------
1616static void
1617biosfn_read_display_code(struct bregs *regs)
1618{
1619 regs->bx = GET_BDA(dcc_index);
1620 regs->al = 0x1a;
1621}
1622
1623static void
1624biosfn_set_display_code(struct bregs *regs)
1625{
1626 SET_BDA(dcc_index, regs->bl);
Kevin O'Connora12c2152009-05-13 22:06:16 -04001627 dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001628 regs->al = 0x1a;
1629}
1630
1631// -------------------------------------------------------------------
1632static void
1633biosfn_read_state_info(u16 BX, u16 ES, u16 DI)
1634{
1635 // Address of static functionality table
1636 SET_FARVAR(ES, *(u16*)(DI + 0x00), (u32)static_functionality);
Kevin O'Connord113a992009-05-16 21:05:02 -04001637 SET_FARVAR(ES, *(u16*)(DI + 0x02), get_global_seg());
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001638
1639 // Hard coded copy from BIOS area. Should it be cleaner ?
1640 memcpy_far(ES, (void*)(DI + 0x04), SEG_BDA, (void*)0x49, 30);
1641 memcpy_far(ES, (void*)(DI + 0x22), SEG_BDA, (void*)0x84, 3);
1642
1643 SET_FARVAR(ES, *(u8*)(DI + 0x25), GET_BDA(dcc_index));
1644 SET_FARVAR(ES, *(u8*)(DI + 0x26), 0);
1645 SET_FARVAR(ES, *(u8*)(DI + 0x27), 16);
1646 SET_FARVAR(ES, *(u8*)(DI + 0x28), 0);
1647 SET_FARVAR(ES, *(u8*)(DI + 0x29), 8);
1648 SET_FARVAR(ES, *(u8*)(DI + 0x2a), 2);
1649 SET_FARVAR(ES, *(u8*)(DI + 0x2b), 0);
1650 SET_FARVAR(ES, *(u8*)(DI + 0x2c), 0);
1651 SET_FARVAR(ES, *(u8*)(DI + 0x31), 3);
1652 SET_FARVAR(ES, *(u8*)(DI + 0x32), 0);
1653
1654 memset_far(ES, (void*)(DI + 0x33), 0, 13);
1655}
1656
1657// -------------------------------------------------------------------
1658// -------------------------------------------------------------------
1659static u16
1660biosfn_read_video_state_size(u16 CX)
1661{
1662 u16 size = 0;
1663 if (CX & 1)
1664 size += 0x46;
1665 if (CX & 2)
1666 size += (5 + 8 + 5) * 2 + 6;
1667 if (CX & 4)
1668 size += 3 + 256 * 3 + 1;
1669 return size;
1670}
1671
1672static u16
1673biosfn_save_video_state(u16 CX, u16 ES, u16 BX)
1674{
Kevin O'Connordd2be772009-05-16 15:41:23 -04001675 u16 crtc_addr = GET_BDA(crtc_address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001676 if (CX & 1) {
1677 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_SEQU_ADDRESS));
1678 BX++;
1679 SET_FARVAR(ES, *(u8*)(BX+0), inb(crtc_addr));
1680 BX++;
1681 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_GRDC_ADDRESS));
1682 BX++;
1683 inb(VGAREG_ACTL_RESET);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001684 u16 ar_index = inb(VGAREG_ACTL_ADDRESS);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001685 SET_FARVAR(ES, *(u8*)(BX+0), ar_index);
1686 BX++;
1687 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_READ_FEATURE_CTL));
1688 BX++;
1689
Kevin O'Connordd2be772009-05-16 15:41:23 -04001690 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001691 for (i = 1; i <= 4; i++) {
1692 outb(i, VGAREG_SEQU_ADDRESS);
1693 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_SEQU_DATA));
1694 BX++;
1695 }
1696 outb(0, VGAREG_SEQU_ADDRESS);
1697 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_SEQU_DATA));
1698 BX++;
1699
1700 for (i = 0; i <= 0x18; i++) {
1701 outb(i, crtc_addr);
1702 SET_FARVAR(ES, *(u8*)(BX+0), inb(crtc_addr + 1));
1703 BX++;
1704 }
1705
1706 for (i = 0; i <= 0x13; i++) {
1707 inb(VGAREG_ACTL_RESET);
1708 outb(i | (ar_index & 0x20), VGAREG_ACTL_ADDRESS);
1709 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_ACTL_READ_DATA));
1710 BX++;
1711 }
1712 inb(VGAREG_ACTL_RESET);
1713
1714 for (i = 0; i <= 8; i++) {
1715 outb(i, VGAREG_GRDC_ADDRESS);
1716 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_GRDC_DATA));
1717 BX++;
1718 }
1719
1720 SET_FARVAR(ES, *(u16*)(BX+0), crtc_addr);
1721 BX += 2;
1722
1723 /* XXX: read plane latches */
1724 SET_FARVAR(ES, *(u8*)(BX+0), 0);
1725 BX++;
1726 SET_FARVAR(ES, *(u8*)(BX+0), 0);
1727 BX++;
1728 SET_FARVAR(ES, *(u8*)(BX+0), 0);
1729 BX++;
1730 SET_FARVAR(ES, *(u8*)(BX+0), 0);
1731 BX++;
1732 }
1733 if (CX & 2) {
1734 SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_mode));
1735 BX++;
1736 SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(video_cols));
1737 BX += 2;
1738 SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(video_pagesize));
1739 BX += 2;
1740 SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(crtc_address));
1741 BX += 2;
1742 SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_rows));
1743 BX++;
1744 SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(char_height));
1745 BX += 2;
1746 SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_ctl));
1747 BX++;
1748 SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_switches));
1749 BX++;
1750 SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(modeset_ctl));
1751 BX++;
1752 SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(cursor_type));
1753 BX += 2;
Kevin O'Connordd2be772009-05-16 15:41:23 -04001754 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001755 for (i = 0; i < 8; i++) {
1756 SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(cursor_pos[i]));
1757 BX += 2;
1758 }
1759 SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(video_pagestart));
1760 BX += 2;
1761 SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_page));
1762 BX++;
1763 /* current font */
Kevin O'Connorf61afb82009-05-16 21:32:27 -04001764 SET_FARVAR(ES, *(u32*)(BX+0), GET_IVT(0x1f).segoff);
1765 BX += 4;
1766 SET_FARVAR(ES, *(u32*)(BX+0), GET_IVT(0x43).segoff);
1767 BX += 4;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001768 }
1769 if (CX & 4) {
1770 /* XXX: check this */
1771 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_DAC_STATE));
1772 BX++; /* read/write mode dac */
1773 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_DAC_WRITE_ADDRESS));
1774 BX++; /* pix address */
1775 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_PEL_MASK));
1776 BX++;
1777 // Set the whole dac always, from 0
1778 outb(0x00, VGAREG_DAC_WRITE_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001779 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001780 for (i = 0; i < 256 * 3; i++) {
1781 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_DAC_DATA));
1782 BX++;
1783 }
1784 SET_FARVAR(ES, *(u8*)(BX+0), 0);
1785 BX++; /* color select register */
1786 }
1787 return BX;
1788}
1789
1790static u16
1791biosfn_restore_video_state(u16 CX, u16 ES, u16 BX)
1792{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001793 if (CX & 1) {
1794 // Reset Attribute Ctl flip-flop
1795 inb(VGAREG_ACTL_RESET);
1796
Kevin O'Connordd2be772009-05-16 15:41:23 -04001797 u16 crtc_addr = GET_FARVAR(ES, *(u16*)(BX + 0x40));
1798 u16 addr1 = BX;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001799 BX += 5;
1800
Kevin O'Connordd2be772009-05-16 15:41:23 -04001801 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001802 for (i = 1; i <= 4; i++) {
1803 outb(i, VGAREG_SEQU_ADDRESS);
1804 outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_SEQU_DATA);
1805 BX++;
1806 }
1807 outb(0, VGAREG_SEQU_ADDRESS);
1808 outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_SEQU_DATA);
1809 BX++;
1810
1811 // Disable CRTC write protection
1812 outw(0x0011, crtc_addr);
1813 // Set CRTC regs
1814 for (i = 0; i <= 0x18; i++) {
1815 if (i != 0x11) {
1816 outb(i, crtc_addr);
1817 outb(GET_FARVAR(ES, *(u8*)(BX+0)), crtc_addr + 1);
1818 }
1819 BX++;
1820 }
1821 // select crtc base address
Kevin O'Connordd2be772009-05-16 15:41:23 -04001822 u16 v = inb(VGAREG_READ_MISC_OUTPUT) & ~0x01;
Kevin O'Connord9fc0a02009-05-07 22:00:25 -04001823 if (crtc_addr == VGAREG_VGA_CRTC_ADDRESS)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001824 v |= 0x01;
1825 outb(v, VGAREG_WRITE_MISC_OUTPUT);
1826
1827 // enable write protection if needed
1828 outb(0x11, crtc_addr);
1829 outb(GET_FARVAR(ES, *(u8*)(BX - 0x18 + 0x11)), crtc_addr + 1);
1830
1831 // Set Attribute Ctl
Kevin O'Connordd2be772009-05-16 15:41:23 -04001832 u16 ar_index = GET_FARVAR(ES, *(u8*)(addr1 + 0x03));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001833 inb(VGAREG_ACTL_RESET);
1834 for (i = 0; i <= 0x13; i++) {
1835 outb(i | (ar_index & 0x20), VGAREG_ACTL_ADDRESS);
1836 outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_ACTL_WRITE_DATA);
1837 BX++;
1838 }
1839 outb(ar_index, VGAREG_ACTL_ADDRESS);
1840 inb(VGAREG_ACTL_RESET);
1841
1842 for (i = 0; i <= 8; i++) {
1843 outb(i, VGAREG_GRDC_ADDRESS);
1844 outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_GRDC_DATA);
1845 BX++;
1846 }
1847 BX += 2; /* crtc_addr */
1848 BX += 4; /* plane latches */
1849
1850 outb(GET_FARVAR(ES, *(u8*)(addr1+0)), VGAREG_SEQU_ADDRESS);
1851 addr1++;
1852 outb(GET_FARVAR(ES, *(u8*)(addr1+0)), crtc_addr);
1853 addr1++;
1854 outb(GET_FARVAR(ES, *(u8*)(addr1+0)), VGAREG_GRDC_ADDRESS);
1855 addr1++;
1856 addr1++;
1857 outb(GET_FARVAR(ES, *(u8*)(addr1+0)), crtc_addr - 0x4 + 0xa);
1858 addr1++;
1859 }
1860 if (CX & 2) {
1861 SET_BDA(video_mode, GET_FARVAR(ES, *(u8*)(BX+0)));
1862 BX++;
1863 SET_BDA(video_cols, GET_FARVAR(ES, *(u16*)(BX+0)));
1864 BX += 2;
1865 SET_BDA(video_pagesize, GET_FARVAR(ES, *(u16*)(BX+0)));
1866 BX += 2;
1867 SET_BDA(crtc_address, GET_FARVAR(ES, *(u16*)(BX+0)));
1868 BX += 2;
1869 SET_BDA(video_rows, GET_FARVAR(ES, *(u8*)(BX+0)));
1870 BX++;
1871 SET_BDA(char_height, GET_FARVAR(ES, *(u16*)(BX+0)));
1872 BX += 2;
1873 SET_BDA(video_ctl, GET_FARVAR(ES, *(u8*)(BX+0)));
1874 BX++;
1875 SET_BDA(video_switches, GET_FARVAR(ES, *(u8*)(BX+0)));
1876 BX++;
1877 SET_BDA(modeset_ctl, GET_FARVAR(ES, *(u8*)(BX+0)));
1878 BX++;
1879 SET_BDA(cursor_type, GET_FARVAR(ES, *(u16*)(BX+0)));
1880 BX += 2;
Kevin O'Connordd2be772009-05-16 15:41:23 -04001881 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001882 for (i = 0; i < 8; i++) {
1883 SET_BDA(cursor_pos[i], GET_FARVAR(ES, *(u16*)(BX+0)));
1884 BX += 2;
1885 }
1886 SET_BDA(video_pagestart, GET_FARVAR(ES, *(u16*)(BX+0)));
1887 BX += 2;
1888 SET_BDA(video_page, GET_FARVAR(ES, *(u8*)(BX+0)));
1889 BX++;
1890 /* current font */
1891 SET_IVT(0x1f, GET_FARVAR(ES, *(u16*)(BX+2)), GET_FARVAR(ES, *(u16*)(BX+0)));
1892 BX += 4;
1893 SET_IVT(0x43, GET_FARVAR(ES, *(u16*)(BX+2)), GET_FARVAR(ES, *(u16*)(BX+0)));
1894 BX += 4;
1895 }
1896 if (CX & 4) {
1897 BX++;
Kevin O'Connordd2be772009-05-16 15:41:23 -04001898 u16 v = GET_FARVAR(ES, *(u8*)(BX+0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001899 BX++;
1900 outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_PEL_MASK);
1901 BX++;
1902 // Set the whole dac always, from 0
1903 outb(0x00, VGAREG_DAC_WRITE_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001904 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001905 for (i = 0; i < 256 * 3; i++) {
1906 outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_DAC_DATA);
1907 BX++;
1908 }
1909 BX++;
1910 outb(v, VGAREG_DAC_WRITE_ADDRESS);
1911 }
1912 return BX;
1913}
1914
1915
1916/****************************************************************
1917 * VGA int 10 handler
1918 ****************************************************************/
1919
1920static void
1921handle_1000(struct bregs *regs)
1922{
1923 // XXX - inline
1924 biosfn_set_video_mode(regs->al);
1925 switch(regs->al & 0x7F) {
1926 case 6:
1927 regs->al = 0x3F;
1928 break;
1929 case 0:
1930 case 1:
1931 case 2:
1932 case 3:
1933 case 4:
1934 case 5:
1935 case 7:
1936 regs->al = 0x30;
1937 break;
1938 default:
1939 regs->al = 0x20;
1940 }
1941}
1942
1943static void
1944handle_1001(struct bregs *regs)
1945{
1946 biosfn_set_cursor_shape(regs->ch, regs->cl);
1947}
1948
1949static void
1950handle_1002(struct bregs *regs)
1951{
1952 biosfn_set_cursor_pos(regs->bh, regs->dx);
1953}
1954
1955static void
1956handle_1003(struct bregs *regs)
1957{
Kevin O'Connor0818e1a2009-05-16 18:00:19 -04001958 regs->cx = biosfn_get_cursor_shape(regs->bh);
1959 regs->dx = biosfn_get_cursor_pos(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001960}
1961
1962// Read light pen pos (unimplemented)
1963static void
1964handle_1004(struct bregs *regs)
1965{
1966 debug_stub(regs);
1967 regs->ax = regs->bx = regs->cx = regs->dx = 0;
1968}
1969
1970static void
1971handle_1005(struct bregs *regs)
1972{
1973 biosfn_set_active_page(regs->al);
1974}
1975
1976static void
1977handle_1006(struct bregs *regs)
1978{
1979 biosfn_scroll(regs->al, regs->bh, regs->ch, regs->cl, regs->dh, regs->dl
1980 , 0xFF, SCROLL_UP);
1981}
1982
1983static void
1984handle_1007(struct bregs *regs)
1985{
1986 biosfn_scroll(regs->al, regs->bh, regs->ch, regs->cl, regs->dh, regs->dl