blob: d7a5366da994be1d2d0e3b81c9776d33286e516f [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:
Kevin O'Connor99e08b72009-05-17 00:07:31 -0400800 write_gfx_char_pl4(car, attr, xcurs, ycurs, nbcols, cheight);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400801 break;
802 case CGA:
803 write_gfx_char_cga(car, attr, xcurs, ycurs, nbcols, bpp);
804 break;
805 case LINEAR8:
806 write_gfx_char_lin(car, attr, xcurs, ycurs, nbcols);
807 break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400808 }
Kevin O'Connordd2be772009-05-16 15:41:23 -0400809 xcurs++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400810 }
811}
812
813// -------------------------------------------------------------------
814static void
815biosfn_write_char_only(u8 car, u8 page, u8 attr, u16 count)
816{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400817 // Get the mode
Kevin O'Connor5727c292009-05-16 17:29:32 -0400818 struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
819 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400820 return;
821
822 // Get the cursor pos for the page
Kevin O'Connor0818e1a2009-05-16 18:00:19 -0400823 u16 cursor = biosfn_get_cursor_pos(page);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400824 u8 xcurs = cursor & 0x00ff;
825 u8 ycurs = (cursor & 0xff00) >> 8;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400826
827 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -0400828 u16 nbrows = GET_BDA(video_rows) + 1;
829 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400830
Kevin O'Connor5727c292009-05-16 17:29:32 -0400831 if (GET_GLOBAL(vmode_g->class) == TEXT) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400832 // Compute the address
Kevin O'Connordd2be772009-05-16 15:41:23 -0400833 u8 *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, page)
834 + (xcurs + ycurs * nbcols) * 2);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400835 while (count-- > 0) {
Kevin O'Connor5727c292009-05-16 17:29:32 -0400836 SET_FARVAR(GET_GLOBAL(vmode_g->sstart), *address_far, car);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400837 address_far += 2;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400838 }
Kevin O'Connordd2be772009-05-16 15:41:23 -0400839 return;
840 }
841
842 // FIXME gfx mode not complete
Kevin O'Connor5727c292009-05-16 17:29:32 -0400843 struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
844 u8 cheight = GET_GLOBAL(vparam_g->cheight);
845 u8 bpp = GET_GLOBAL(vmode_g->pixbits);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400846 while ((count-- > 0) && (xcurs < nbcols)) {
Kevin O'Connor5727c292009-05-16 17:29:32 -0400847 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connordd2be772009-05-16 15:41:23 -0400848 case PLANAR4:
849 case PLANAR1:
Kevin O'Connor99e08b72009-05-17 00:07:31 -0400850 write_gfx_char_pl4(car, attr, xcurs, ycurs, nbcols, cheight);
Kevin O'Connordd2be772009-05-16 15:41:23 -0400851 break;
852 case CGA:
853 write_gfx_char_cga(car, attr, xcurs, ycurs, nbcols, bpp);
854 break;
855 case LINEAR8:
856 write_gfx_char_lin(car, attr, xcurs, ycurs, nbcols);
857 break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400858 }
Kevin O'Connordd2be772009-05-16 15:41:23 -0400859 xcurs++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400860 }
861}
862
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400863// -------------------------------------------------------------------
864static void
865biosfn_set_border_color(struct bregs *regs)
866{
867 inb(VGAREG_ACTL_RESET);
868 outb(0x00, VGAREG_ACTL_ADDRESS);
869 u8 al = regs->bl & 0x0f;
870 if (al & 0x08)
871 al += 0x08;
Kevin O'Connord9fc0a02009-05-07 22:00:25 -0400872 outb(al, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400873 u8 bl = regs->bl & 0x10;
874
875 int i;
876 for (i = 1; i < 4; i++) {
877 outb(i, VGAREG_ACTL_ADDRESS);
878
879 al = inb(VGAREG_ACTL_READ_DATA);
880 al &= 0xef;
881 al |= bl;
Kevin O'Connord9fc0a02009-05-07 22:00:25 -0400882 outb(al, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400883 }
884 outb(0x20, VGAREG_ACTL_ADDRESS);
885}
886
887static void
888biosfn_set_palette(struct bregs *regs)
889{
890 inb(VGAREG_ACTL_RESET);
891 u8 bl = regs->bl & 0x01;
892 int i;
893 for (i = 1; i < 4; i++) {
894 outb(i, VGAREG_ACTL_ADDRESS);
895
896 u8 al = inb(VGAREG_ACTL_READ_DATA);
897 al &= 0xfe;
898 al |= bl;
Kevin O'Connord9fc0a02009-05-07 22:00:25 -0400899 outb(al, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400900 }
901 outb(0x20, VGAREG_ACTL_ADDRESS);
902}
903
904// -------------------------------------------------------------------
905static void
906biosfn_write_pixel(u8 BH, u8 AL, u16 CX, u16 DX)
907{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400908 // Get the mode
Kevin O'Connor5727c292009-05-16 17:29:32 -0400909 struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
910 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400911 return;
Kevin O'Connor5727c292009-05-16 17:29:32 -0400912 if (GET_GLOBAL(vmode_g->class) == TEXT)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400913 return;
914
Kevin O'Connordd2be772009-05-16 15:41:23 -0400915 u8 *addr_far, mask, attr, data;
Kevin O'Connor5727c292009-05-16 17:29:32 -0400916 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400917 case PLANAR4:
918 case PLANAR1:
Kevin O'Connordd2be772009-05-16 15:41:23 -0400919 addr_far = (void*)(CX / 8 + DX * GET_BDA(video_cols));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400920 mask = 0x80 >> (CX & 0x07);
921 outw((mask << 8) | 0x08, VGAREG_GRDC_ADDRESS);
922 outw(0x0205, VGAREG_GRDC_ADDRESS);
Kevin O'Connord113a992009-05-16 21:05:02 -0400923 data = GET_FARVAR(SEG_GRAPH, *addr_far);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400924 if (AL & 0x80)
925 outw(0x1803, VGAREG_GRDC_ADDRESS);
Kevin O'Connord113a992009-05-16 21:05:02 -0400926 SET_FARVAR(SEG_GRAPH, *addr_far, AL);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400927 outw(0xff08, VGAREG_GRDC_ADDRESS);
928 outw(0x0005, VGAREG_GRDC_ADDRESS);
929 outw(0x0003, VGAREG_GRDC_ADDRESS);
930 break;
931 case CGA:
Kevin O'Connor5727c292009-05-16 17:29:32 -0400932 if (GET_GLOBAL(vmode_g->pixbits) == 2)
Kevin O'Connordd2be772009-05-16 15:41:23 -0400933 addr_far = (void*)((CX >> 2) + (DX >> 1) * 80);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400934 else
Kevin O'Connordd2be772009-05-16 15:41:23 -0400935 addr_far = (void*)((CX >> 3) + (DX >> 1) * 80);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400936 if (DX & 1)
Kevin O'Connordd2be772009-05-16 15:41:23 -0400937 addr_far += 0x2000;
Kevin O'Connord113a992009-05-16 21:05:02 -0400938 data = GET_FARVAR(SEG_CTEXT, *addr_far);
Kevin O'Connor5727c292009-05-16 17:29:32 -0400939 if (GET_GLOBAL(vmode_g->pixbits) == 2) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400940 attr = (AL & 0x03) << ((3 - (CX & 0x03)) * 2);
941 mask = 0x03 << ((3 - (CX & 0x03)) * 2);
942 } else {
943 attr = (AL & 0x01) << (7 - (CX & 0x07));
944 mask = 0x01 << (7 - (CX & 0x07));
945 }
946 if (AL & 0x80) {
947 data ^= attr;
948 } else {
949 data &= ~mask;
950 data |= attr;
951 }
Kevin O'Connord113a992009-05-16 21:05:02 -0400952 SET_FARVAR(SEG_CTEXT, *addr_far, data);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400953 break;
954 case LINEAR8:
Kevin O'Connordd2be772009-05-16 15:41:23 -0400955 addr_far = (void*)(CX + DX * (GET_BDA(video_cols) * 8));
Kevin O'Connord113a992009-05-16 21:05:02 -0400956 SET_FARVAR(SEG_GRAPH, *addr_far, AL);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400957 break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400958 }
959}
960
961// -------------------------------------------------------------------
962static void
963biosfn_read_pixel(u8 BH, u16 CX, u16 DX, u16 *AX)
964{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400965 // Get the mode
Kevin O'Connor5727c292009-05-16 17:29:32 -0400966 struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
967 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400968 return;
Kevin O'Connor5727c292009-05-16 17:29:32 -0400969 if (GET_GLOBAL(vmode_g->class) == TEXT)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400970 return;
971
Kevin O'Connor5727c292009-05-16 17:29:32 -0400972 u8 *addr_far, mask, attr=0, data, i;
973 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400974 case PLANAR4:
975 case PLANAR1:
Kevin O'Connordd2be772009-05-16 15:41:23 -0400976 addr_far = (void*)(CX / 8 + DX * GET_BDA(video_cols));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400977 mask = 0x80 >> (CX & 0x07);
978 attr = 0x00;
979 for (i = 0; i < 4; i++) {
980 outw((i << 8) | 0x04, VGAREG_GRDC_ADDRESS);
Kevin O'Connord113a992009-05-16 21:05:02 -0400981 data = GET_FARVAR(SEG_GRAPH, *addr_far) & mask;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400982 if (data > 0)
983 attr |= (0x01 << i);
984 }
985 break;
986 case CGA:
Kevin O'Connordd2be772009-05-16 15:41:23 -0400987 addr_far = (void*)((CX >> 2) + (DX >> 1) * 80);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400988 if (DX & 1)
Kevin O'Connordd2be772009-05-16 15:41:23 -0400989 addr_far += 0x2000;
Kevin O'Connord113a992009-05-16 21:05:02 -0400990 data = GET_FARVAR(SEG_CTEXT, *addr_far);
Kevin O'Connor5727c292009-05-16 17:29:32 -0400991 if (GET_GLOBAL(vmode_g->pixbits) == 2)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400992 attr = (data >> ((3 - (CX & 0x03)) * 2)) & 0x03;
993 else
994 attr = (data >> (7 - (CX & 0x07))) & 0x01;
995 break;
996 case LINEAR8:
Kevin O'Connordd2be772009-05-16 15:41:23 -0400997 addr_far = (void*)(CX + DX * (GET_BDA(video_cols) * 8));
Kevin O'Connord113a992009-05-16 21:05:02 -0400998 attr = GET_FARVAR(SEG_GRAPH, *addr_far);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400999 break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001000 }
1001 *AX = (*AX & 0xff00) | attr;
1002}
1003
1004// -------------------------------------------------------------------
1005static void
1006biosfn_write_teletype(u8 car, u8 page, u8 attr, u8 flag)
1007{ // flag = WITH_ATTR / NO_ATTR
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001008 // special case if page is 0xff, use current page
1009 if (page == 0xff)
1010 page = GET_BDA(video_page);
1011
1012 // Get the mode
Kevin O'Connor5727c292009-05-16 17:29:32 -04001013 struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
1014 if (!vmode_g)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001015 return;
1016
1017 // Get the cursor pos for the page
Kevin O'Connor0818e1a2009-05-16 18:00:19 -04001018 u16 cursor = biosfn_get_cursor_pos(page);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001019 u8 xcurs = cursor & 0x00ff;
1020 u8 ycurs = (cursor & 0xff00) >> 8;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001021
1022 // Get the dimensions
Kevin O'Connordd2be772009-05-16 15:41:23 -04001023 u16 nbrows = GET_BDA(video_rows) + 1;
1024 u16 nbcols = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001025
1026 switch (car) {
1027 case 7:
1028 //FIXME should beep
1029 break;
1030
1031 case 8:
1032 if (xcurs > 0)
1033 xcurs--;
1034 break;
1035
1036 case '\r':
1037 xcurs = 0;
1038 break;
1039
1040 case '\n':
1041 ycurs++;
1042 break;
1043
1044 case '\t':
1045 do {
1046 biosfn_write_teletype(' ', page, attr, flag);
Kevin O'Connor0818e1a2009-05-16 18:00:19 -04001047 cursor = biosfn_get_cursor_pos(page);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001048 xcurs = cursor & 0x00ff;
1049 ycurs = (cursor & 0xff00) >> 8;
1050 } while (xcurs % 8 == 0);
1051 break;
1052
1053 default:
1054
Kevin O'Connor5727c292009-05-16 17:29:32 -04001055 if (GET_GLOBAL(vmode_g->class) == TEXT) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001056 // Compute the address
Kevin O'Connordd2be772009-05-16 15:41:23 -04001057 u8 *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, page)
1058 + (xcurs + ycurs * nbcols) * 2);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001059 // Write the char
Kevin O'Connor5727c292009-05-16 17:29:32 -04001060 SET_FARVAR(GET_GLOBAL(vmode_g->sstart), address_far[0], car);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001061 if (flag == WITH_ATTR)
Kevin O'Connor5727c292009-05-16 17:29:32 -04001062 SET_FARVAR(GET_GLOBAL(vmode_g->sstart), address_far[1], attr);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001063 } else {
1064 // FIXME gfx mode not complete
Kevin O'Connor5727c292009-05-16 17:29:32 -04001065 struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
1066 u8 cheight = GET_GLOBAL(vparam_g->cheight);
1067 u8 bpp = GET_GLOBAL(vmode_g->pixbits);
1068 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001069 case PLANAR4:
1070 case PLANAR1:
Kevin O'Connor5727c292009-05-16 17:29:32 -04001071 write_gfx_char_pl4(car, attr, xcurs, ycurs, nbcols, cheight);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001072 break;
1073 case CGA:
1074 write_gfx_char_cga(car, attr, xcurs, ycurs, nbcols, bpp);
1075 break;
1076 case LINEAR8:
1077 write_gfx_char_lin(car, attr, xcurs, ycurs, nbcols);
1078 break;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001079 }
1080 }
1081 xcurs++;
1082 }
1083
1084 // Do we need to wrap ?
1085 if (xcurs == nbcols) {
1086 xcurs = 0;
1087 ycurs++;
1088 }
1089 // Do we need to scroll ?
1090 if (ycurs == nbrows) {
Kevin O'Connor5727c292009-05-16 17:29:32 -04001091 if (GET_GLOBAL(vmode_g->class) == TEXT)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001092 biosfn_scroll(0x01, 0x07, 0, 0, nbrows - 1, nbcols - 1, page,
1093 SCROLL_UP);
1094 else
1095 biosfn_scroll(0x01, 0x00, 0, 0, nbrows - 1, nbcols - 1, page,
1096 SCROLL_UP);
1097 ycurs -= 1;
1098 }
1099 // Set the cursor for the page
1100 cursor = ycurs;
1101 cursor <<= 8;
1102 cursor += xcurs;
1103 biosfn_set_cursor_pos(page, cursor);
1104}
1105
1106// -------------------------------------------------------------------
1107static void
1108biosfn_get_video_mode(struct bregs *regs)
1109{
1110 regs->bh = GET_BDA(video_page);
1111 regs->al = GET_BDA(video_mode) | (GET_BDA(video_ctl) & 0x80);
1112 regs->ah = GET_BDA(video_cols);
1113}
1114
1115// -------------------------------------------------------------------
1116static void
1117biosfn_set_overscan_border_color(struct bregs *regs)
1118{
1119 inb(VGAREG_ACTL_RESET);
1120 outb(0x11, VGAREG_ACTL_ADDRESS);
Kevin O'Connord9fc0a02009-05-07 22:00:25 -04001121 outb(regs->bh, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001122 outb(0x20, VGAREG_ACTL_ADDRESS);
1123}
1124
1125// -------------------------------------------------------------------
1126static void
1127biosfn_set_all_palette_reg(struct bregs *regs)
1128{
1129 inb(VGAREG_ACTL_RESET);
1130
Kevin O'Connordd2be772009-05-16 15:41:23 -04001131 u8 *data_far = (u8*)(regs->dx + 0);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001132 int i;
1133 for (i = 0; i < 0x10; i++) {
1134 outb(i, VGAREG_ACTL_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001135 u8 val = GET_FARVAR(regs->es, *data_far);
Kevin O'Connord9fc0a02009-05-07 22:00:25 -04001136 outb(val, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001137 data_far++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001138 }
1139 outb(0x11, VGAREG_ACTL_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001140 outb(GET_FARVAR(regs->es, *data_far), VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001141 outb(0x20, VGAREG_ACTL_ADDRESS);
1142}
1143
1144// -------------------------------------------------------------------
1145static void
1146biosfn_toggle_intensity(struct bregs *regs)
1147{
1148 inb(VGAREG_ACTL_RESET);
1149 outb(0x10, VGAREG_ACTL_ADDRESS);
1150 u8 val = (inb(VGAREG_ACTL_READ_DATA) & 0x7f) | ((regs->bl & 0x01) << 3);
Kevin O'Connord9fc0a02009-05-07 22:00:25 -04001151 outb(val, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001152 outb(0x20, VGAREG_ACTL_ADDRESS);
1153}
1154
1155// -------------------------------------------------------------------
1156void
1157biosfn_set_single_palette_reg(u8 reg, u8 val)
1158{
1159 inb(VGAREG_ACTL_RESET);
1160 outb(reg, VGAREG_ACTL_ADDRESS);
Kevin O'Connord9fc0a02009-05-07 22:00:25 -04001161 outb(val, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001162 outb(0x20, VGAREG_ACTL_ADDRESS);
1163}
1164
1165// -------------------------------------------------------------------
1166u8
1167biosfn_get_single_palette_reg(u8 reg)
1168{
1169 inb(VGAREG_ACTL_RESET);
1170 outb(reg, VGAREG_ACTL_ADDRESS);
1171 u8 v = inb(VGAREG_ACTL_READ_DATA);
1172 inb(VGAREG_ACTL_RESET);
1173 outb(0x20, VGAREG_ACTL_ADDRESS);
1174 return v;
1175}
1176
1177// -------------------------------------------------------------------
1178static void
1179biosfn_read_overscan_border_color(struct bregs *regs)
1180{
1181 inb(VGAREG_ACTL_RESET);
1182 outb(0x11, VGAREG_ACTL_ADDRESS);
1183 regs->bh = inb(VGAREG_ACTL_READ_DATA);
1184 inb(VGAREG_ACTL_RESET);
1185 outb(0x20, VGAREG_ACTL_ADDRESS);
1186}
1187
1188// -------------------------------------------------------------------
1189static void
1190biosfn_get_all_palette_reg(struct bregs *regs)
1191{
Kevin O'Connordd2be772009-05-16 15:41:23 -04001192 u8 *data_far = (u8*)(regs->dx + 0);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001193 int i;
1194 for (i = 0; i < 0x10; i++) {
1195 inb(VGAREG_ACTL_RESET);
1196 outb(i, VGAREG_ACTL_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001197 SET_FARVAR(regs->es, *data_far, inb(VGAREG_ACTL_READ_DATA));
1198 data_far++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001199 }
1200 inb(VGAREG_ACTL_RESET);
1201 outb(0x11, VGAREG_ACTL_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001202 SET_FARVAR(regs->es, *data_far, inb(VGAREG_ACTL_READ_DATA));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001203 inb(VGAREG_ACTL_RESET);
1204 outb(0x20, VGAREG_ACTL_ADDRESS);
1205}
1206
1207// -------------------------------------------------------------------
1208static void
1209biosfn_set_single_dac_reg(struct bregs *regs)
1210{
1211 outb(regs->bl, VGAREG_DAC_WRITE_ADDRESS);
1212 outb(regs->dh, VGAREG_DAC_DATA);
1213 outb(regs->ch, VGAREG_DAC_DATA);
1214 outb(regs->cl, VGAREG_DAC_DATA);
1215}
1216
1217// -------------------------------------------------------------------
1218static void
1219biosfn_set_all_dac_reg(struct bregs *regs)
1220{
1221 outb(regs->bl, VGAREG_DAC_WRITE_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001222 u8 *data_far = (u8*)(regs->dx + 0);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001223 int count = regs->cx;
1224 while (count) {
Kevin O'Connordd2be772009-05-16 15:41:23 -04001225 outb(GET_FARVAR(regs->es, *data_far), VGAREG_DAC_DATA);
1226 data_far++;
1227 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++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001231 count--;
1232 }
1233}
1234
1235// -------------------------------------------------------------------
1236static void
1237biosfn_select_video_dac_color_page(struct bregs *regs)
1238{
1239 inb(VGAREG_ACTL_RESET);
1240 outb(0x10, VGAREG_ACTL_ADDRESS);
1241 u8 val = inb(VGAREG_ACTL_READ_DATA);
1242 if (!(regs->bl & 0x01)) {
1243 val = (val & 0x7f) | (regs->bh << 7);
Kevin O'Connord9fc0a02009-05-07 22:00:25 -04001244 outb(val, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001245 outb(0x20, VGAREG_ACTL_ADDRESS);
1246 return;
1247 }
1248 inb(VGAREG_ACTL_RESET);
1249 outb(0x14, VGAREG_ACTL_ADDRESS);
1250 u8 bh = regs->bh;
1251 if (!(val & 0x80))
1252 bh <<= 2;
1253 bh &= 0x0f;
Kevin O'Connord9fc0a02009-05-07 22:00:25 -04001254 outb(bh, VGAREG_ACTL_WRITE_DATA);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001255 outb(0x20, VGAREG_ACTL_ADDRESS);
1256}
1257
1258// -------------------------------------------------------------------
1259static void
1260biosfn_read_single_dac_reg(struct bregs *regs)
1261{
1262 outb(regs->bl, VGAREG_DAC_READ_ADDRESS);
1263 regs->dh = inb(VGAREG_DAC_DATA);
1264 regs->ch = inb(VGAREG_DAC_DATA);
1265 regs->cl = inb(VGAREG_DAC_DATA);
1266}
1267
1268// -------------------------------------------------------------------
1269static void
1270biosfn_read_all_dac_reg(struct bregs *regs)
1271{
1272 outb(regs->bl, VGAREG_DAC_READ_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001273 u8 *data_far = (u8*)(regs->dx + 0);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001274 int count = regs->cx;
1275 while (count) {
Kevin O'Connordd2be772009-05-16 15:41:23 -04001276 SET_FARVAR(regs->es, *data_far, inb(VGAREG_DAC_DATA));
1277 data_far++;
1278 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++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001282 count--;
1283 }
1284}
1285
1286// -------------------------------------------------------------------
1287static void
1288biosfn_set_pel_mask(struct bregs *regs)
1289{
1290 outb(regs->bl, VGAREG_PEL_MASK);
1291}
1292
1293// -------------------------------------------------------------------
1294static void
1295biosfn_read_pel_mask(struct bregs *regs)
1296{
1297 regs->bl = inb(VGAREG_PEL_MASK);
1298}
1299
1300// -------------------------------------------------------------------
1301static void
1302biosfn_read_video_dac_state(struct bregs *regs)
1303{
1304 inb(VGAREG_ACTL_RESET);
1305 outb(0x10, VGAREG_ACTL_ADDRESS);
1306 u8 val1 = inb(VGAREG_ACTL_READ_DATA) >> 7;
1307
1308 inb(VGAREG_ACTL_RESET);
1309 outb(0x14, VGAREG_ACTL_ADDRESS);
1310 u8 val2 = inb(VGAREG_ACTL_READ_DATA) & 0x0f;
1311 if (!(val1 & 0x01))
1312 val2 >>= 2;
1313
1314 inb(VGAREG_ACTL_RESET);
1315 outb(0x20, VGAREG_ACTL_ADDRESS);
1316
1317 regs->bl = val1;
1318 regs->bh = val2;
1319}
1320
1321// -------------------------------------------------------------------
1322static void
1323get_font_access()
1324{
1325 outw(0x0100, VGAREG_SEQU_ADDRESS);
1326 outw(0x0402, VGAREG_SEQU_ADDRESS);
1327 outw(0x0704, VGAREG_SEQU_ADDRESS);
1328 outw(0x0300, VGAREG_SEQU_ADDRESS);
1329 outw(0x0204, VGAREG_GRDC_ADDRESS);
1330 outw(0x0005, VGAREG_GRDC_ADDRESS);
1331 outw(0x0406, VGAREG_GRDC_ADDRESS);
1332}
1333
1334static void
1335release_font_access()
1336{
1337 outw(0x0100, VGAREG_SEQU_ADDRESS);
1338 outw(0x0302, VGAREG_SEQU_ADDRESS);
1339 outw(0x0304, VGAREG_SEQU_ADDRESS);
1340 outw(0x0300, VGAREG_SEQU_ADDRESS);
1341 u16 v = inw(VGAREG_READ_MISC_OUTPUT);
1342 v = ((v & 0x01) << 10) | 0x0a06;
1343 outw(v, VGAREG_GRDC_ADDRESS);
1344 outw(0x0004, VGAREG_GRDC_ADDRESS);
1345 outw(0x1005, VGAREG_GRDC_ADDRESS);
1346}
1347
1348static void
1349set_scan_lines(u8 lines)
1350{
Kevin O'Connordd2be772009-05-16 15:41:23 -04001351 u16 crtc_addr = GET_BDA(crtc_address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001352 outb(0x09, crtc_addr);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001353 u8 crtc_r9 = inb(crtc_addr + 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001354 crtc_r9 = (crtc_r9 & 0xe0) | (lines - 1);
1355 outb(crtc_r9, crtc_addr + 1);
1356 if (lines == 8)
1357 biosfn_set_cursor_shape(0x06, 0x07);
1358 else
1359 biosfn_set_cursor_shape(lines - 4, lines - 3);
1360 SET_BDA(char_height, lines);
1361 outb(0x12, crtc_addr);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001362 u16 vde = inb(crtc_addr + 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001363 outb(0x07, crtc_addr);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001364 u8 ovl = inb(crtc_addr + 1);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001365 vde += (((ovl & 0x02) << 7) + ((ovl & 0x40) << 3) + 1);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001366 u8 rows = vde / lines;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001367 SET_BDA(video_rows, rows - 1);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001368 u16 cols = GET_BDA(video_cols);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001369 SET_BDA(video_pagesize, rows * cols * 2);
1370}
1371
1372static void
1373biosfn_load_text_user_pat(u8 AL, u16 ES, u16 BP, u16 CX, u16 DX, u8 BL,
1374 u8 BH)
1375{
1376 get_font_access();
1377 u16 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
1378 u16 i;
1379 for (i = 0; i < CX; i++) {
Kevin O'Connorf94ec432009-05-16 17:37:23 -04001380 void *src_far = (void*)(BP + i * BH);
1381 void *dest_far = (void*)(blockaddr + (DX + i) * 32);
Kevin O'Connord113a992009-05-16 21:05:02 -04001382 memcpy_far(SEG_GRAPH, dest_far, ES, src_far, BH);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001383 }
1384 release_font_access();
1385 if (AL >= 0x10)
1386 set_scan_lines(BH);
1387}
1388
1389static void
1390biosfn_load_text_8_14_pat(u8 AL, u8 BL)
1391{
1392 get_font_access();
1393 u16 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
1394 u16 i;
1395 for (i = 0; i < 0x100; i++) {
1396 u16 src = i * 14;
Kevin O'Connorf94ec432009-05-16 17:37:23 -04001397 void *dest_far = (void*)(blockaddr + i * 32);
Kevin O'Connord113a992009-05-16 21:05:02 -04001398 memcpy_far(SEG_GRAPH, dest_far, get_global_seg(), &vgafont14[src], 14);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001399 }
1400 release_font_access();
1401 if (AL >= 0x10)
1402 set_scan_lines(14);
1403}
1404
1405static void
1406biosfn_load_text_8_8_pat(u8 AL, u8 BL)
1407{
1408 get_font_access();
1409 u16 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
1410 u16 i;
1411 for (i = 0; i < 0x100; i++) {
1412 u16 src = i * 8;
Kevin O'Connorf94ec432009-05-16 17:37:23 -04001413 void *dest_far = (void*)(blockaddr + i * 32);
Kevin O'Connord113a992009-05-16 21:05:02 -04001414 memcpy_far(SEG_GRAPH, dest_far, get_global_seg(), &vgafont8[src], 8);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001415 }
1416 release_font_access();
1417 if (AL >= 0x10)
1418 set_scan_lines(8);
1419}
1420
1421// -------------------------------------------------------------------
1422static void
1423biosfn_set_text_block_specifier(struct bregs *regs)
1424{
1425 outw((regs->bl << 8) | 0x03, VGAREG_SEQU_ADDRESS);
1426}
1427
1428// -------------------------------------------------------------------
1429static void
1430biosfn_load_text_8_16_pat(u8 AL, u8 BL)
1431{
1432 get_font_access();
1433 u16 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
1434 u16 i;
1435 for (i = 0; i < 0x100; i++) {
1436 u16 src = i * 16;
Kevin O'Connorf94ec432009-05-16 17:37:23 -04001437 void *dest_far = (void*)(blockaddr + i * 32);
Kevin O'Connord113a992009-05-16 21:05:02 -04001438 memcpy_far(SEG_GRAPH, dest_far, get_global_seg(), &vgafont16[src], 16);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001439 }
1440 release_font_access();
1441 if (AL >= 0x10)
1442 set_scan_lines(16);
1443}
1444
1445// -------------------------------------------------------------------
1446static void
1447biosfn_get_font_info(u8 BH, u16 *ES, u16 *BP, u16 *CX, u16 *DX)
1448{
1449 switch (BH) {
1450 case 0x00: {
1451 u32 segoff = GET_IVT(0x1f).segoff;
1452 *ES = segoff >> 16;
1453 *BP = segoff;
1454 break;
1455 }
1456 case 0x01: {
1457 u32 segoff = GET_IVT(0x43).segoff;
1458 *ES = segoff >> 16;
1459 *BP = segoff;
1460 break;
1461 }
1462 case 0x02:
Kevin O'Connord113a992009-05-16 21:05:02 -04001463 *ES = get_global_seg();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001464 *BP = (u32)vgafont14;
1465 break;
1466 case 0x03:
Kevin O'Connord113a992009-05-16 21:05:02 -04001467 *ES = get_global_seg();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001468 *BP = (u32)vgafont8;
1469 break;
1470 case 0x04:
Kevin O'Connord113a992009-05-16 21:05:02 -04001471 *ES = get_global_seg();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001472 *BP = (u32)vgafont8 + 128 * 8;
1473 break;
1474 case 0x05:
Kevin O'Connord113a992009-05-16 21:05:02 -04001475 *ES = get_global_seg();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001476 *BP = (u32)vgafont14alt;
1477 break;
1478 case 0x06:
Kevin O'Connord113a992009-05-16 21:05:02 -04001479 *ES = get_global_seg();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001480 *BP = (u32)vgafont16;
1481 break;
1482 case 0x07:
Kevin O'Connord113a992009-05-16 21:05:02 -04001483 *ES = get_global_seg();
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001484 *BP = (u32)vgafont16alt;
1485 break;
1486 default:
Kevin O'Connora12c2152009-05-13 22:06:16 -04001487 dprintf(1, "Get font info BH(%02x) was discarded\n", BH);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001488 return;
1489 }
1490 // Set byte/char of on screen font
1491 *CX = GET_BDA(char_height) & 0xff;
1492
1493 // Set Highest char row
1494 *DX = GET_BDA(video_rows);
1495}
1496
1497// -------------------------------------------------------------------
1498static void
1499biosfn_get_ega_info(struct bregs *regs)
1500{
1501 regs->cx = GET_BDA(video_switches) & 0x0f;
1502 regs->ax = GET_BDA(crtc_address);
1503 if (regs->ax == VGAREG_MDA_CRTC_ADDRESS)
1504 regs->bx = 0x0103;
1505 else
1506 regs->bx = 0x0003;
1507}
1508
1509// -------------------------------------------------------------------
1510static void
1511biosfn_select_vert_res(struct bregs *regs)
1512{
1513 u8 mctl = GET_BDA(modeset_ctl);
1514 u8 vswt = GET_BDA(video_switches);
1515
1516 switch (regs->al) {
1517 case 0x00:
1518 // 200 lines
1519 mctl = (mctl & ~0x10) | 0x80;
1520 vswt = (vswt & ~0x0f) | 0x08;
1521 break;
1522 case 0x01:
1523 // 350 lines
1524 mctl &= ~0x90;
1525 vswt = (vswt & ~0x0f) | 0x09;
1526 break;
1527 case 0x02:
1528 // 400 lines
1529 mctl = (mctl & ~0x80) | 0x10;
1530 vswt = (vswt & ~0x0f) | 0x09;
1531 break;
1532 default:
Kevin O'Connora12c2152009-05-13 22:06:16 -04001533 dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001534 break;
1535 }
1536 SET_BDA(modeset_ctl, mctl);
1537 SET_BDA(video_switches, vswt);
1538 regs->ax = 0x1212;
1539}
1540
1541static void
1542biosfn_enable_default_palette_loading(struct bregs *regs)
1543{
1544 u8 v = (regs->al & 0x01) << 3;
1545 u8 mctl = GET_BDA(video_ctl) & ~0x08;
1546 SET_BDA(video_ctl, mctl | v);
1547 regs->ax = 0x1212;
1548}
1549
1550static void
1551biosfn_enable_video_addressing(struct bregs *regs)
1552{
1553 u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
1554 u8 v2 = inb(VGAREG_READ_MISC_OUTPUT) & ~0x02;
1555 outb(v | v2, VGAREG_WRITE_MISC_OUTPUT);
1556 regs->ax = 0x1212;
1557}
1558
1559
1560static void
1561biosfn_enable_grayscale_summing(struct bregs *regs)
1562{
1563 u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
1564 u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
1565 SET_BDA(modeset_ctl, v | v2);
1566 regs->ax = 0x1212;
1567}
1568
1569static void
1570biosfn_enable_cursor_emulation(struct bregs *regs)
1571{
1572 u8 v = (regs->al & 0x01) ^ 0x01;
1573 u8 v2 = GET_BDA(modeset_ctl) & ~0x01;
1574 SET_BDA(modeset_ctl, v | v2);
1575 regs->ax = 0x1212;
1576}
1577
1578// -------------------------------------------------------------------
1579static void
1580biosfn_write_string(u8 flag, u8 page, u8 attr, u16 count, u8 row, u8 col,
Kevin O'Connordd2be772009-05-16 15:41:23 -04001581 u16 seg, u8 *offset_far)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001582{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001583 // Read curs info for the page
Kevin O'Connor0818e1a2009-05-16 18:00:19 -04001584 u16 oldcurs = biosfn_get_cursor_pos(page);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001585
1586 // if row=0xff special case : use current cursor position
1587 if (row == 0xff) {
1588 col = oldcurs & 0x00ff;
1589 row = (oldcurs & 0xff00) >> 8;
1590 }
1591
Kevin O'Connordd2be772009-05-16 15:41:23 -04001592 u16 newcurs = row;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001593 newcurs <<= 8;
1594 newcurs += col;
1595 biosfn_set_cursor_pos(page, newcurs);
1596
1597 while (count-- != 0) {
Kevin O'Connordd2be772009-05-16 15:41:23 -04001598 u8 car = GET_FARVAR(seg, *offset_far);
1599 offset_far++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001600 if ((flag & 0x02) != 0) {
Kevin O'Connordd2be772009-05-16 15:41:23 -04001601 attr = GET_FARVAR(seg, *offset_far);
1602 offset_far++;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001603 }
1604
1605 biosfn_write_teletype(car, page, attr, WITH_ATTR);
1606 }
1607
1608 // Set back curs pos
1609 if ((flag & 0x01) == 0)
1610 biosfn_set_cursor_pos(page, oldcurs);
1611}
1612
1613// -------------------------------------------------------------------
1614static void
1615biosfn_read_display_code(struct bregs *regs)
1616{
1617 regs->bx = GET_BDA(dcc_index);
1618 regs->al = 0x1a;
1619}
1620
1621static void
1622biosfn_set_display_code(struct bregs *regs)
1623{
1624 SET_BDA(dcc_index, regs->bl);
Kevin O'Connora12c2152009-05-13 22:06:16 -04001625 dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001626 regs->al = 0x1a;
1627}
1628
1629// -------------------------------------------------------------------
1630static void
1631biosfn_read_state_info(u16 BX, u16 ES, u16 DI)
1632{
1633 // Address of static functionality table
1634 SET_FARVAR(ES, *(u16*)(DI + 0x00), (u32)static_functionality);
Kevin O'Connord113a992009-05-16 21:05:02 -04001635 SET_FARVAR(ES, *(u16*)(DI + 0x02), get_global_seg());
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001636
1637 // Hard coded copy from BIOS area. Should it be cleaner ?
1638 memcpy_far(ES, (void*)(DI + 0x04), SEG_BDA, (void*)0x49, 30);
1639 memcpy_far(ES, (void*)(DI + 0x22), SEG_BDA, (void*)0x84, 3);
1640
1641 SET_FARVAR(ES, *(u8*)(DI + 0x25), GET_BDA(dcc_index));
1642 SET_FARVAR(ES, *(u8*)(DI + 0x26), 0);
1643 SET_FARVAR(ES, *(u8*)(DI + 0x27), 16);
1644 SET_FARVAR(ES, *(u8*)(DI + 0x28), 0);
1645 SET_FARVAR(ES, *(u8*)(DI + 0x29), 8);
1646 SET_FARVAR(ES, *(u8*)(DI + 0x2a), 2);
1647 SET_FARVAR(ES, *(u8*)(DI + 0x2b), 0);
1648 SET_FARVAR(ES, *(u8*)(DI + 0x2c), 0);
1649 SET_FARVAR(ES, *(u8*)(DI + 0x31), 3);
1650 SET_FARVAR(ES, *(u8*)(DI + 0x32), 0);
1651
1652 memset_far(ES, (void*)(DI + 0x33), 0, 13);
1653}
1654
1655// -------------------------------------------------------------------
1656// -------------------------------------------------------------------
1657static u16
1658biosfn_read_video_state_size(u16 CX)
1659{
1660 u16 size = 0;
1661 if (CX & 1)
1662 size += 0x46;
1663 if (CX & 2)
1664 size += (5 + 8 + 5) * 2 + 6;
1665 if (CX & 4)
1666 size += 3 + 256 * 3 + 1;
1667 return size;
1668}
1669
1670static u16
1671biosfn_save_video_state(u16 CX, u16 ES, u16 BX)
1672{
Kevin O'Connordd2be772009-05-16 15:41:23 -04001673 u16 crtc_addr = GET_BDA(crtc_address);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001674 if (CX & 1) {
1675 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_SEQU_ADDRESS));
1676 BX++;
1677 SET_FARVAR(ES, *(u8*)(BX+0), inb(crtc_addr));
1678 BX++;
1679 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_GRDC_ADDRESS));
1680 BX++;
1681 inb(VGAREG_ACTL_RESET);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001682 u16 ar_index = inb(VGAREG_ACTL_ADDRESS);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001683 SET_FARVAR(ES, *(u8*)(BX+0), ar_index);
1684 BX++;
1685 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_READ_FEATURE_CTL));
1686 BX++;
1687
Kevin O'Connordd2be772009-05-16 15:41:23 -04001688 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001689 for (i = 1; i <= 4; i++) {
1690 outb(i, VGAREG_SEQU_ADDRESS);
1691 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_SEQU_DATA));
1692 BX++;
1693 }
1694 outb(0, VGAREG_SEQU_ADDRESS);
1695 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_SEQU_DATA));
1696 BX++;
1697
1698 for (i = 0; i <= 0x18; i++) {
1699 outb(i, crtc_addr);
1700 SET_FARVAR(ES, *(u8*)(BX+0), inb(crtc_addr + 1));
1701 BX++;
1702 }
1703
1704 for (i = 0; i <= 0x13; i++) {
1705 inb(VGAREG_ACTL_RESET);
1706 outb(i | (ar_index & 0x20), VGAREG_ACTL_ADDRESS);
1707 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_ACTL_READ_DATA));
1708 BX++;
1709 }
1710 inb(VGAREG_ACTL_RESET);
1711
1712 for (i = 0; i <= 8; i++) {
1713 outb(i, VGAREG_GRDC_ADDRESS);
1714 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_GRDC_DATA));
1715 BX++;
1716 }
1717
1718 SET_FARVAR(ES, *(u16*)(BX+0), crtc_addr);
1719 BX += 2;
1720
1721 /* XXX: read plane latches */
1722 SET_FARVAR(ES, *(u8*)(BX+0), 0);
1723 BX++;
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 }
1731 if (CX & 2) {
1732 SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_mode));
1733 BX++;
1734 SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(video_cols));
1735 BX += 2;
1736 SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(video_pagesize));
1737 BX += 2;
1738 SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(crtc_address));
1739 BX += 2;
1740 SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_rows));
1741 BX++;
1742 SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(char_height));
1743 BX += 2;
1744 SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_ctl));
1745 BX++;
1746 SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_switches));
1747 BX++;
1748 SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(modeset_ctl));
1749 BX++;
1750 SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(cursor_type));
1751 BX += 2;
Kevin O'Connordd2be772009-05-16 15:41:23 -04001752 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001753 for (i = 0; i < 8; i++) {
1754 SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(cursor_pos[i]));
1755 BX += 2;
1756 }
1757 SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(video_pagestart));
1758 BX += 2;
1759 SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_page));
1760 BX++;
1761 /* current font */
Kevin O'Connorf61afb82009-05-16 21:32:27 -04001762 SET_FARVAR(ES, *(u32*)(BX+0), GET_IVT(0x1f).segoff);
1763 BX += 4;
1764 SET_FARVAR(ES, *(u32*)(BX+0), GET_IVT(0x43).segoff);
1765 BX += 4;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001766 }
1767 if (CX & 4) {
1768 /* XXX: check this */
1769 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_DAC_STATE));
1770 BX++; /* read/write mode dac */
1771 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_DAC_WRITE_ADDRESS));
1772 BX++; /* pix address */
1773 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_PEL_MASK));
1774 BX++;
1775 // Set the whole dac always, from 0
1776 outb(0x00, VGAREG_DAC_WRITE_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001777 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001778 for (i = 0; i < 256 * 3; i++) {
1779 SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_DAC_DATA));
1780 BX++;
1781 }
1782 SET_FARVAR(ES, *(u8*)(BX+0), 0);
1783 BX++; /* color select register */
1784 }
1785 return BX;
1786}
1787
1788static u16
1789biosfn_restore_video_state(u16 CX, u16 ES, u16 BX)
1790{
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001791 if (CX & 1) {
1792 // Reset Attribute Ctl flip-flop
1793 inb(VGAREG_ACTL_RESET);
1794
Kevin O'Connordd2be772009-05-16 15:41:23 -04001795 u16 crtc_addr = GET_FARVAR(ES, *(u16*)(BX + 0x40));
1796 u16 addr1 = BX;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001797 BX += 5;
1798
Kevin O'Connordd2be772009-05-16 15:41:23 -04001799 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001800 for (i = 1; i <= 4; i++) {
1801 outb(i, VGAREG_SEQU_ADDRESS);
1802 outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_SEQU_DATA);
1803 BX++;
1804 }
1805 outb(0, VGAREG_SEQU_ADDRESS);
1806 outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_SEQU_DATA);
1807 BX++;
1808
1809 // Disable CRTC write protection
1810 outw(0x0011, crtc_addr);
1811 // Set CRTC regs
1812 for (i = 0; i <= 0x18; i++) {
1813 if (i != 0x11) {
1814 outb(i, crtc_addr);
1815 outb(GET_FARVAR(ES, *(u8*)(BX+0)), crtc_addr + 1);
1816 }
1817 BX++;
1818 }
1819 // select crtc base address
Kevin O'Connordd2be772009-05-16 15:41:23 -04001820 u16 v = inb(VGAREG_READ_MISC_OUTPUT) & ~0x01;
Kevin O'Connord9fc0a02009-05-07 22:00:25 -04001821 if (crtc_addr == VGAREG_VGA_CRTC_ADDRESS)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001822 v |= 0x01;
1823 outb(v, VGAREG_WRITE_MISC_OUTPUT);
1824
1825 // enable write protection if needed
1826 outb(0x11, crtc_addr);
1827 outb(GET_FARVAR(ES, *(u8*)(BX - 0x18 + 0x11)), crtc_addr + 1);
1828
1829 // Set Attribute Ctl
Kevin O'Connordd2be772009-05-16 15:41:23 -04001830 u16 ar_index = GET_FARVAR(ES, *(u8*)(addr1 + 0x03));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001831 inb(VGAREG_ACTL_RESET);
1832 for (i = 0; i <= 0x13; i++) {
1833 outb(i | (ar_index & 0x20), VGAREG_ACTL_ADDRESS);
1834 outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_ACTL_WRITE_DATA);
1835 BX++;
1836 }
1837 outb(ar_index, VGAREG_ACTL_ADDRESS);
1838 inb(VGAREG_ACTL_RESET);
1839
1840 for (i = 0; i <= 8; i++) {
1841 outb(i, VGAREG_GRDC_ADDRESS);
1842 outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_GRDC_DATA);
1843 BX++;
1844 }
1845 BX += 2; /* crtc_addr */
1846 BX += 4; /* plane latches */
1847
1848 outb(GET_FARVAR(ES, *(u8*)(addr1+0)), VGAREG_SEQU_ADDRESS);
1849 addr1++;
1850 outb(GET_FARVAR(ES, *(u8*)(addr1+0)), crtc_addr);
1851 addr1++;
1852 outb(GET_FARVAR(ES, *(u8*)(addr1+0)), VGAREG_GRDC_ADDRESS);
1853 addr1++;
1854 addr1++;
1855 outb(GET_FARVAR(ES, *(u8*)(addr1+0)), crtc_addr - 0x4 + 0xa);
1856 addr1++;
1857 }
1858 if (CX & 2) {
1859 SET_BDA(video_mode, GET_FARVAR(ES, *(u8*)(BX+0)));
1860 BX++;
1861 SET_BDA(video_cols, GET_FARVAR(ES, *(u16*)(BX+0)));
1862 BX += 2;
1863 SET_BDA(video_pagesize, GET_FARVAR(ES, *(u16*)(BX+0)));
1864 BX += 2;
1865 SET_BDA(crtc_address, GET_FARVAR(ES, *(u16*)(BX+0)));
1866 BX += 2;
1867 SET_BDA(video_rows, GET_FARVAR(ES, *(u8*)(BX+0)));
1868 BX++;
1869 SET_BDA(char_height, GET_FARVAR(ES, *(u16*)(BX+0)));
1870 BX += 2;
1871 SET_BDA(video_ctl, GET_FARVAR(ES, *(u8*)(BX+0)));
1872 BX++;
1873 SET_BDA(video_switches, GET_FARVAR(ES, *(u8*)(BX+0)));
1874 BX++;
1875 SET_BDA(modeset_ctl, GET_FARVAR(ES, *(u8*)(BX+0)));
1876 BX++;
1877 SET_BDA(cursor_type, GET_FARVAR(ES, *(u16*)(BX+0)));
1878 BX += 2;
Kevin O'Connordd2be772009-05-16 15:41:23 -04001879 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001880 for (i = 0; i < 8; i++) {
1881 SET_BDA(cursor_pos[i], GET_FARVAR(ES, *(u16*)(BX+0)));
1882 BX += 2;
1883 }
1884 SET_BDA(video_pagestart, GET_FARVAR(ES, *(u16*)(BX+0)));
1885 BX += 2;
1886 SET_BDA(video_page, GET_FARVAR(ES, *(u8*)(BX+0)));
1887 BX++;
1888 /* current font */
1889 SET_IVT(0x1f, GET_FARVAR(ES, *(u16*)(BX+2)), GET_FARVAR(ES, *(u16*)(BX+0)));
1890 BX += 4;
1891 SET_IVT(0x43, GET_FARVAR(ES, *(u16*)(BX+2)), GET_FARVAR(ES, *(u16*)(BX+0)));
1892 BX += 4;
1893 }
1894 if (CX & 4) {
1895 BX++;
Kevin O'Connordd2be772009-05-16 15:41:23 -04001896 u16 v = GET_FARVAR(ES, *(u8*)(BX+0));
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001897 BX++;
1898 outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_PEL_MASK);
1899 BX++;
1900 // Set the whole dac always, from 0
1901 outb(0x00, VGAREG_DAC_WRITE_ADDRESS);
Kevin O'Connordd2be772009-05-16 15:41:23 -04001902 u16 i;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001903 for (i = 0; i < 256 * 3; i++) {
1904 outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_DAC_DATA);
1905 BX++;
1906 }
1907 BX++;
1908 outb(v, VGAREG_DAC_WRITE_ADDRESS);
1909 }
1910 return BX;
1911}
1912
1913
1914/****************************************************************
1915 * VGA int 10 handler
1916 ****************************************************************/
1917
1918static void
1919handle_1000(struct bregs *regs)
1920{
1921 // XXX - inline
1922 biosfn_set_video_mode(regs->al);
1923 switch(regs->al & 0x7F) {
1924 case 6:
1925 regs->al = 0x3F;
1926 break;
1927 case 0:
1928 case 1:
1929 case 2:
1930 case 3:
1931 case 4:
1932 case 5:
1933 case 7:
1934 regs->al = 0x30;
1935 break;
1936 default:
1937 regs->al = 0x20;
1938 }
1939}
1940
1941static void
1942handle_1001(struct bregs *regs)
1943{
1944 biosfn_set_cursor_shape(regs->ch, regs->cl);
1945}
1946
1947static void
1948handle_1002(struct bregs *regs)
1949{
1950 biosfn_set_cursor_pos(regs->bh, regs->dx);
1951}
1952
1953static void
1954handle_1003(struct bregs *regs)
1955{
Kevin O'Connor0818e1a2009-05-16 18:00:19 -04001956 regs->cx = biosfn_get_cursor_shape(regs->bh);
1957 regs->dx = biosfn_get_cursor_pos(regs->bh);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001958}
1959
1960// Read light pen pos (unimplemented)
1961static void
1962handle_1004(struct bregs *regs)
1963{
1964 debug_stub(regs);
1965 regs->ax = regs->bx = regs->cx = regs->dx = 0;
1966}
1967
1968static void
1969handle_1005(struct bregs *regs)
1970{
1971 biosfn_set_active_page(regs->al);
1972}
1973
1974static void
1975handle_1006(struct bregs *regs)
1976{
1977 biosfn_scroll(regs->al, regs->bh, regs->ch, regs->cl, regs->dh, regs->dl
1978 , 0xFF, SCROLL_UP);
1979}
1980
1981static void
1982handle_1007