1 // Code for manipulating VGA framebuffers.
3 // Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2001-2008 the LGPL VGABios developers Team
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "vgabios.h" // vgafb_scroll
9 #include "biosvar.h" // GET_BDA
10 #include "util.h" // memset_far
11 #include "byteorder.h" // cpu_to_be16
12 #include "stdvga.h" // stdvga_planar4_plane
15 /****************************************************************
17 ****************************************************************/
20 memcpy_stride(u16 seg, void *dst, void *src, int copylen, int stride, int lines)
22 for (; lines; lines--, dst+=stride, src+=stride)
23 memcpy_far(seg, dst, seg, src, copylen);
28 memset_stride(u16 seg, void *dst, u8 val, int setlen, int stride, int lines)
30 for (; lines; lines--, dst+=stride)
31 memset_far(seg, dst, val, setlen);
35 memset16_stride(u16 seg, void *dst, u16 val, int setlen, int stride, int lines)
37 for (; lines; lines--, dst+=stride)
38 memset16_far(seg, dst, val, setlen);
42 scroll_pl4(struct vgamode_s *vmode_g, int nblines, int attr
43 , struct cursorpos ul, struct cursorpos lr)
45 int cheight = GET_GLOBAL(vmode_g->cheight);
47 int stride = GET_BDA(video_cols) * cwidth;
48 void *src_far, *dest_far;
50 dest_far = (void*)(ul.y * cheight * stride + ul.x * cwidth);
51 src_far = dest_far + nblines * cheight * stride;
55 dest_far = (void*)(lr.y * cheight * stride + ul.x * cwidth);
56 src_far = dest_far - nblines * cheight * stride;
61 int cols = lr.x - ul.x + 1;
62 int rows = lr.y - ul.y + 1;
65 stdvga_planar4_plane(i);
66 void *dest = dest_far;
68 dest = memcpy_stride(SEG_GRAPH, dest, src_far, cols * cwidth
69 , stride, (rows - nblines) * cheight);
70 u8 pixels = (attr & (1<<i)) ? 0xff : 0x00;
71 memset_stride(SEG_GRAPH, dest, pixels, cols * cwidth
72 , stride, nblines * cheight);
74 stdvga_planar4_plane(-1);
78 scroll_cga(struct vgamode_s *vmode_g, int nblines, int attr
79 , struct cursorpos ul, struct cursorpos lr)
81 int cheight = GET_GLOBAL(vmode_g->cheight) / 2;
82 int cwidth = GET_GLOBAL(vmode_g->depth);
83 int stride = GET_BDA(video_cols) * cwidth;
84 void *src_far, *dest_far;
86 dest_far = (void*)(ul.y * cheight * stride + ul.x * cwidth);
87 src_far = dest_far + nblines * cheight * stride;
91 dest_far = (void*)(lr.y * cheight * stride + ul.x * cwidth);
92 src_far = dest_far - nblines * cheight * stride;
98 attr = (attr&1) | ((attr&1)<<1);
100 attr |= (attr<<2) | (attr<<4) | (attr<<6);
101 int cols = lr.x - ul.x + 1;
102 int rows = lr.y - ul.y + 1;
103 if (nblines < rows) {
104 memcpy_stride(SEG_CTEXT, dest_far+0x2000, src_far+0x2000, cols * cwidth
105 , stride, (rows - nblines) * cheight);
106 dest_far = memcpy_stride(SEG_CTEXT, dest_far, src_far, cols * cwidth
107 , stride, (rows - nblines) * cheight);
109 memset_stride(SEG_CTEXT, dest_far + 0x2000, attr, cols * cwidth
110 , stride, nblines * cheight);
111 memset_stride(SEG_CTEXT, dest_far, attr, cols * cwidth
112 , stride, nblines * cheight);
116 scroll_lin(struct vgamode_s *vmode_g, int nblines, int attr
117 , struct cursorpos ul, struct cursorpos lr)
121 int stride = GET_BDA(video_cols) * cwidth;
122 void *src_far, *dest_far;
124 dest_far = (void*)(ul.y * cheight * stride + ul.x * cwidth);
125 src_far = dest_far + nblines * cheight * stride;
129 dest_far = (void*)(lr.y * cheight * stride + ul.x * cwidth);
130 src_far = dest_far - nblines * cheight * stride;
135 int cols = lr.x - ul.x + 1;
136 int rows = lr.y - ul.y + 1;
138 dest_far = memcpy_stride(SEG_GRAPH, dest_far, src_far, cols * cwidth
139 , stride, (rows - nblines) * cheight);
140 memset_stride(SEG_GRAPH, dest_far, attr, cols * cwidth
141 , stride, nblines * cheight);
145 scroll_text(struct vgamode_s *vmode_g, int nblines, int attr
146 , struct cursorpos ul, struct cursorpos lr)
150 int stride = GET_BDA(video_cols) * cwidth;
151 void *src_far, *dest_far = (void*)(GET_BDA(video_pagesize) * ul.page);
153 dest_far += ul.y * cheight * stride + ul.x * cwidth;
154 src_far = dest_far + nblines * cheight * stride;
158 dest_far += lr.y * cheight * stride + ul.x * cwidth;
159 src_far = dest_far - nblines * cheight * stride;
164 attr = (attr << 8) | ' ';
165 int cols = lr.x - ul.x + 1;
166 int rows = lr.y - ul.y + 1;
167 u16 seg = GET_GLOBAL(vmode_g->sstart);
169 dest_far = memcpy_stride(seg, dest_far, src_far, cols * cwidth
170 , stride, (rows - nblines) * cheight);
171 memset16_stride(seg, dest_far, attr, cols * cwidth
172 , stride, nblines * cheight);
176 vgafb_scroll(int nblines, int attr, struct cursorpos ul, struct cursorpos lr)
179 struct vgamode_s *vmode_g = get_current_mode();
183 // FIXME gfx mode not complete
184 switch (GET_GLOBAL(vmode_g->memmodel)) {
186 scroll_text(vmode_g, nblines, attr, ul, lr);
189 scroll_pl4(vmode_g, nblines, attr, ul, lr);
192 scroll_cga(vmode_g, nblines, attr, ul, lr);
196 scroll_lin(vmode_g, nblines, attr, ul, lr);
204 /****************************************************************
205 * Read/write characters to screen
206 ****************************************************************/
209 write_gfx_char_pl4(struct vgamode_s *vmode_g
210 , struct cursorpos cp, struct carattr ca)
212 u16 nbcols = GET_BDA(video_cols);
216 u8 cheight = GET_GLOBAL(vmode_g->cheight);
228 u16 addr = cp.x + cp.y * cheight * nbcols;
229 u16 src = ca.car * cheight;
231 for (i=0; i<4; i++) {
232 stdvga_planar4_plane(i);
233 u8 colors = ((ca.attr & (1<<i)) ? 0xff : 0x00);
235 for (j = 0; j < cheight; j++) {
236 u8 *dest_far = (void*)(addr + j * nbcols);
237 u8 pixels = colors & GET_GLOBAL(fdata_g[src + j]);
239 pixels ^= GET_FARVAR(SEG_GRAPH, *dest_far);
240 SET_FARVAR(SEG_GRAPH, *dest_far, pixels);
243 stdvga_planar4_plane(-1);
247 write_gfx_char_cga(struct vgamode_s *vmode_g
248 , struct cursorpos cp, struct carattr ca)
250 u16 nbcols = GET_BDA(video_cols);
254 u8 *fdata_g = vgafont8;
255 u8 bpp = GET_GLOBAL(vmode_g->depth);
256 u16 addr = (cp.x * bpp) + cp.y * 320;
257 u16 src = ca.car * 8;
259 for (i = 0; i < 8; i++) {
260 u8 *dest_far = (void*)(addr + (i >> 1) * 80);
264 u8 colors = (ca.attr & 0x01) ? 0xff : 0x00;
265 u8 pixels = colors & GET_GLOBAL(fdata_g[src + i]);
267 pixels ^= GET_FARVAR(SEG_GRAPH, *dest_far);
268 SET_FARVAR(SEG_CTEXT, *dest_far, pixels);
271 u8 fontline = GET_GLOBAL(fdata_g[src + i]);
273 for (j = 0; j < 8; j++)
274 if (fontline & (1<<j))
275 pixels |= (ca.attr & 0x03) << (j*2);
276 pixels = cpu_to_be16(pixels);
278 pixels ^= GET_FARVAR(SEG_GRAPH, *(u16*)dest_far);
279 SET_FARVAR(SEG_CTEXT, *(u16*)dest_far, pixels);
285 write_gfx_char_lin(struct vgamode_s *vmode_g
286 , struct cursorpos cp, struct carattr ca)
288 // Get the dimensions
289 u16 nbcols = GET_BDA(video_cols);
293 u8 *fdata_g = vgafont8;
294 u16 addr = cp.x * 8 + cp.y * nbcols * 64;
295 u16 src = ca.car * 8;
297 for (i = 0; i < 8; i++) {
298 u8 *dest_far = (void*)(addr + i * nbcols * 8);
299 u8 fontline = GET_GLOBAL(fdata_g[src + i]);
301 for (j = 0; j < 8; j++) {
302 u8 pixel = (fontline & (0x80>>j)) ? ca.attr : 0x00;
303 SET_FARVAR(SEG_GRAPH, dest_far[j], pixel);
309 write_text_char(struct vgamode_s *vmode_g
310 , struct cursorpos cp, struct carattr ca)
312 // Compute the address
313 u16 nbcols = GET_BDA(video_cols);
314 void *address_far = (void*)(GET_BDA(video_pagesize) * cp.page
315 + (cp.x + cp.y * nbcols) * 2);
318 u16 dummy = (ca.attr << 8) | ca.car;
319 SET_FARVAR(GET_GLOBAL(vmode_g->sstart), *(u16*)address_far, dummy);
321 SET_FARVAR(GET_GLOBAL(vmode_g->sstart), *(u8*)address_far, ca.car);
326 vgafb_write_char(struct cursorpos cp, struct carattr ca)
329 struct vgamode_s *vmode_g = get_current_mode();
333 // FIXME gfx mode not complete
334 switch (GET_GLOBAL(vmode_g->memmodel)) {
336 write_text_char(vmode_g, cp, ca);
339 write_gfx_char_pl4(vmode_g, cp, ca);
342 write_gfx_char_cga(vmode_g, cp, ca);
346 write_gfx_char_lin(vmode_g, cp, ca);
354 vgafb_read_char(struct cursorpos cp)
357 struct vgamode_s *vmode_g = get_current_mode();
361 if (GET_GLOBAL(vmode_g->memmodel) != MM_TEXT) {
363 dprintf(1, "Read char in graphics mode\n");
367 // Compute the address
368 u16 nbcols = GET_BDA(video_cols);
369 u16 *address_far = (void*)(GET_BDA(video_pagesize) * cp.page
370 + (cp.x + cp.y * nbcols) * 2);
371 u16 v = GET_FARVAR(GET_GLOBAL(vmode_g->sstart), *address_far);
372 struct carattr ca = {v, v>>8, 0};
376 struct carattr ca2 = {0, 0, 0};
381 /****************************************************************
383 ****************************************************************/
386 vgafb_write_pixel(u8 color, u16 x, u16 y)
389 struct vgamode_s *vmode_g = get_current_mode();
393 u8 *addr_far, mask, attr, data, i;
394 switch (GET_GLOBAL(vmode_g->memmodel)) {
396 addr_far = (void*)(x / 8 + y * GET_BDA(video_cols));
397 mask = 0x80 >> (x & 0x07);
398 for (i=0; i<4; i++) {
399 stdvga_planar4_plane(i);
400 u8 colors = (color & (1<<i)) ? 0xff : 0x00;
401 u8 orig = GET_FARVAR(SEG_GRAPH, *addr_far);
404 SET_FARVAR(SEG_GRAPH, *addr_far, (colors & mask) | (orig & ~mask));
406 stdvga_planar4_plane(-1);
409 if (GET_GLOBAL(vmode_g->depth) == 2)
410 addr_far = (void*)((x >> 2) + (y >> 1) * 80);
412 addr_far = (void*)((x >> 3) + (y >> 1) * 80);
415 data = GET_FARVAR(SEG_CTEXT, *addr_far);
416 if (GET_GLOBAL(vmode_g->depth) == 2) {
417 attr = (color & 0x03) << ((3 - (x & 0x03)) * 2);
418 mask = 0x03 << ((3 - (x & 0x03)) * 2);
420 attr = (color & 0x01) << (7 - (x & 0x07));
421 mask = 0x01 << (7 - (x & 0x07));
429 SET_FARVAR(SEG_CTEXT, *addr_far, data);
433 addr_far = (void*)(x + y * (GET_BDA(video_cols) * 8));
434 SET_FARVAR(SEG_GRAPH, *addr_far, color);
443 vgafb_read_pixel(u16 x, u16 y)
446 struct vgamode_s *vmode_g = get_current_mode();
450 u8 *addr_far, mask, attr=0, data, i;
451 switch (GET_GLOBAL(vmode_g->memmodel)) {
453 addr_far = (void*)(x / 8 + y * GET_BDA(video_cols));
454 mask = 0x80 >> (x & 0x07);
456 for (i = 0; i < 4; i++) {
457 stdvga_planar4_plane(i);
458 data = GET_FARVAR(SEG_GRAPH, *addr_far) & mask;
462 stdvga_planar4_plane(-1);
465 addr_far = (void*)((x >> 2) + (y >> 1) * 80);
468 data = GET_FARVAR(SEG_CTEXT, *addr_far);
469 if (GET_GLOBAL(vmode_g->depth) == 2)
470 attr = (data >> ((3 - (x & 0x03)) * 2)) & 0x03;
472 attr = (data >> (7 - (x & 0x07))) & 0x01;
476 addr_far = (void*)(x + y * (GET_BDA(video_cols) * 8));
477 attr = GET_FARVAR(SEG_GRAPH, *addr_far);