vgabios: Add scrolling for linear (packed pixel) graphics mode.
[seabios.git] / vgasrc / vgafb.c
1 // Code for manipulating VGA framebuffers.
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 #include "biosvar.h" // GET_BDA
9 #include "util.h" // memset_far
10 #include "vgabios.h" // find_vga_entry
11 #include "stdvga.h" // stdvga_grdc_write
12
13
14 /****************************************************************
15  * Screen scrolling
16  ****************************************************************/
17
18 static inline void *
19 memcpy_stride(u16 seg, void *dst, void *src, int copylen, int stride, int lines)
20 {
21     for (; lines; lines--, dst+=stride, src+=stride)
22         memcpy_far(seg, dst, seg, src, copylen);
23     return dst;
24 }
25
26 static inline void
27 memset_stride(u16 seg, void *dst, u8 val, int setlen, int stride, int lines)
28 {
29     for (; lines; lines--, dst+=stride)
30         memset_far(seg, dst, val, setlen);
31 }
32
33 static inline void
34 memset16_stride(u16 seg, void *dst, u16 val, int setlen, int stride, int lines)
35 {
36     for (; lines; lines--, dst+=stride)
37         memset16_far(seg, dst, val, setlen);
38 }
39
40 static void
41 scroll_pl4(struct vgamode_s *vmode_g, int nblines, int attr
42            , struct cursorpos ul, struct cursorpos lr)
43 {
44     int cheight = GET_GLOBAL(vmode_g->cheight);
45     int cwidth = 1;
46     int stride = GET_BDA(video_cols) * cwidth;
47     void *src_far, *dest_far;
48     if (nblines >= 0) {
49         dest_far = (void*)(ul.y * cheight * stride + ul.x * cwidth);
50         src_far = dest_far + nblines * cheight * stride;
51     } else {
52         // Scroll down
53         nblines = -nblines;
54         dest_far = (void*)(lr.y * cheight * stride + ul.x * cwidth);
55         src_far = dest_far - nblines * cheight * stride;
56         stride = -stride;
57     }
58     int cols = lr.x - ul.x + 1;
59     int rows = lr.y - ul.y + 1;
60     if (nblines < rows) {
61         stdvga_grdc_write(0x05, 0x01);
62         dest_far = memcpy_stride(SEG_GRAPH, dest_far, src_far, cols * cwidth
63                                  , stride, (rows - nblines) * cheight);
64     }
65     if (attr < 0)
66         attr = 0;
67     stdvga_grdc_write(0x05, 0x02);
68     memset_stride(SEG_GRAPH, dest_far, attr, cols * cwidth
69                   , stride, nblines * cheight);
70     stdvga_grdc_write(0x05, 0x00);
71 }
72
73 static void
74 scroll_cga(struct vgamode_s *vmode_g, int nblines, int attr
75             , struct cursorpos ul, struct cursorpos lr)
76 {
77     int cheight = GET_GLOBAL(vmode_g->cheight) / 2;
78     int cwidth = GET_GLOBAL(vmode_g->pixbits);
79     int stride = GET_BDA(video_cols) * cwidth;
80     void *src_far, *dest_far;
81     if (nblines >= 0) {
82         dest_far = (void*)(ul.y * cheight * stride + ul.x * cwidth);
83         src_far = dest_far + nblines * cheight * stride;
84     } else {
85         // Scroll down
86         nblines = -nblines;
87         dest_far = (void*)(lr.y * cheight * stride + ul.x * cwidth);
88         src_far = dest_far - nblines * cheight * stride;
89         stride = -stride;
90     }
91     int cols = lr.x - ul.x + 1;
92     int rows = lr.y - ul.y + 1;
93     if (nblines < rows) {
94         memcpy_stride(SEG_CTEXT, dest_far+0x2000, src_far+0x2000, cols * cwidth
95                       , stride, (rows - nblines) * cheight);
96         dest_far = memcpy_stride(SEG_CTEXT, dest_far, src_far, cols * cwidth
97                                  , stride, (rows - nblines) * cheight);
98     }
99     if (attr < 0)
100         attr = 0;
101     memset_stride(SEG_CTEXT, dest_far + 0x2000, attr, cols * cwidth
102                   , stride, nblines * cheight);
103     memset_stride(SEG_CTEXT, dest_far, attr, cols * cwidth
104                   , stride, nblines * cheight);
105 }
106
107 static void
108 scroll_lin(struct vgamode_s *vmode_g, int nblines, int attr
109            , struct cursorpos ul, struct cursorpos lr)
110 {
111     int cheight = 8;
112     int cwidth = 8;
113     int stride = GET_BDA(video_cols) * cwidth;
114     void *src_far, *dest_far;
115     if (nblines >= 0) {
116         dest_far = (void*)(ul.y * cheight * stride + ul.x * cwidth);
117         src_far = dest_far + nblines * cheight * stride;
118     } else {
119         // Scroll down
120         nblines = -nblines;
121         dest_far = (void*)(lr.y * cheight * stride + ul.x * cwidth);
122         src_far = dest_far - nblines * cheight * stride;
123         stride = -stride;
124     }
125     int cols = lr.x - ul.x + 1;
126     int rows = lr.y - ul.y + 1;
127     if (nblines < rows)
128         dest_far = memcpy_stride(SEG_GRAPH, dest_far, src_far, cols * cwidth
129                                  , stride, (rows - nblines) * cheight);
130     if (attr < 0)
131         attr = 0;
132     memset_stride(SEG_GRAPH, dest_far, attr, cols * cwidth
133                   , stride, nblines * cheight);
134 }
135
136 static void
137 scroll_text(struct vgamode_s *vmode_g, int nblines, int attr
138             , struct cursorpos ul, struct cursorpos lr)
139 {
140     int cheight = 1;
141     int cwidth = 2;
142     u16 nbrows = GET_BDA(video_rows) + 1;
143     u16 nbcols = GET_BDA(video_cols);
144     int stride = nbcols * cwidth;
145     void *src_far, *dest_far = (void*)SCREEN_MEM_START(nbcols, nbrows, ul.page);
146     if (nblines >= 0) {
147         dest_far += ul.y * cheight * stride + ul.x * cwidth;
148         src_far = dest_far + nblines * cheight * stride;
149     } else {
150         // Scroll down
151         nblines = -nblines;
152         dest_far += lr.y * cheight * stride + ul.x * cwidth;
153         src_far = dest_far - nblines * cheight * stride;
154         stride = -stride;
155     }
156     int cols = lr.x - ul.x + 1;
157     int rows = lr.y - ul.y + 1;
158     u16 seg = GET_GLOBAL(vmode_g->sstart);
159     if (nblines < rows)
160         dest_far = memcpy_stride(seg, dest_far, src_far, cols * cwidth
161                                  , stride, (rows - nblines) * cheight);
162     if (attr < 0)
163         attr = 0x07;
164     attr = (attr << 8) | ' ';
165     memset16_stride(seg, dest_far, attr, cols * cwidth
166                     , stride, nblines * cheight);
167 }
168
169 void
170 vgafb_scroll(int nblines, int attr, struct cursorpos ul, struct cursorpos lr)
171 {
172     // Get the mode
173     struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
174     if (!vmode_g)
175         return;
176
177     // FIXME gfx mode not complete
178     switch (GET_GLOBAL(vmode_g->memmodel)) {
179     case CTEXT:
180     case MTEXT:
181         scroll_text(vmode_g, nblines, attr, ul, lr);
182         break;
183     case PLANAR4:
184     case PLANAR1:
185         scroll_pl4(vmode_g, nblines, attr, ul, lr);
186         break;
187     case CGA:
188         scroll_cga(vmode_g, nblines, attr, ul, lr);
189         break;
190     case LINEAR8:
191         scroll_lin(vmode_g, nblines, attr, ul, lr);
192         break;
193     }
194 }
195
196
197 /****************************************************************
198  * Read/write characters to screen
199  ****************************************************************/
200
201 static void
202 write_gfx_char_pl4(struct vgamode_s *vmode_g
203                    , struct cursorpos cp, struct carattr ca)
204 {
205     u16 nbcols = GET_BDA(video_cols);
206     if (cp.x >= nbcols)
207         return;
208
209     u8 cheight = GET_GLOBAL(vmode_g->cheight);
210     u8 *fdata_g;
211     switch (cheight) {
212     case 14:
213         fdata_g = vgafont14;
214         break;
215     case 16:
216         fdata_g = vgafont16;
217         break;
218     default:
219         fdata_g = vgafont8;
220     }
221     u16 addr = cp.x + cp.y * cheight * nbcols;
222     u16 src = ca.car * cheight;
223     stdvga_sequ_write(0x02, 0x0f);
224     stdvga_grdc_write(0x05, 0x02);
225     if (ca.attr & 0x80)
226         stdvga_grdc_write(0x03, 0x18);
227     else
228         stdvga_grdc_write(0x03, 0x00);
229     u8 i;
230     for (i = 0; i < cheight; i++) {
231         u8 *dest_far = (void*)(addr + i * nbcols);
232         u8 j;
233         for (j = 0; j < 8; j++) {
234             u8 mask = 0x80 >> j;
235             stdvga_grdc_write(0x08, mask);
236             GET_FARVAR(SEG_GRAPH, *(volatile u8*)dest_far);
237             if (GET_GLOBAL(fdata_g[src + i]) & mask)
238                 SET_FARVAR(SEG_GRAPH, *dest_far, ca.attr & 0x0f);
239             else
240                 SET_FARVAR(SEG_GRAPH, *dest_far, 0x00);
241         }
242     }
243     stdvga_grdc_write(0x08, 0xff);
244     stdvga_grdc_write(0x05, 0x00);
245     stdvga_grdc_write(0x03, 0x00);
246 }
247
248 static void
249 write_gfx_char_cga(struct vgamode_s *vmode_g
250                    , struct cursorpos cp, struct carattr ca)
251 {
252     u16 nbcols = GET_BDA(video_cols);
253     if (cp.x >= nbcols)
254         return;
255
256     u8 *fdata_g = vgafont8;
257     u8 bpp = GET_GLOBAL(vmode_g->pixbits);
258     u16 addr = (cp.x * bpp) + cp.y * 320;
259     u16 src = ca.car * 8;
260     u8 i;
261     for (i = 0; i < 8; i++) {
262         u8 *dest_far = (void*)(addr + (i >> 1) * 80);
263         if (i & 1)
264             dest_far += 0x2000;
265         u8 mask = 0x80;
266         if (bpp == 1) {
267             u8 data = 0;
268             if (ca.attr & 0x80)
269                 data = GET_FARVAR(SEG_CTEXT, *dest_far);
270             u8 j;
271             for (j = 0; j < 8; j++) {
272                 if (GET_GLOBAL(fdata_g[src + i]) & mask) {
273                     if (ca.attr & 0x80)
274                         data ^= (ca.attr & 0x01) << (7 - j);
275                     else
276                         data |= (ca.attr & 0x01) << (7 - j);
277                 }
278                 mask >>= 1;
279             }
280             SET_FARVAR(SEG_CTEXT, *dest_far, data);
281         } else {
282             while (mask > 0) {
283                 u8 data = 0;
284                 if (ca.attr & 0x80)
285                     data = GET_FARVAR(SEG_CTEXT, *dest_far);
286                 u8 j;
287                 for (j = 0; j < 4; j++) {
288                     if (GET_GLOBAL(fdata_g[src + i]) & mask) {
289                         if (ca.attr & 0x80)
290                             data ^= (ca.attr & 0x03) << ((3 - j) * 2);
291                         else
292                             data |= (ca.attr & 0x03) << ((3 - j) * 2);
293                     }
294                     mask >>= 1;
295                 }
296                 SET_FARVAR(SEG_CTEXT, *dest_far, data);
297                 dest_far += 1;
298             }
299         }
300     }
301 }
302
303 static void
304 write_gfx_char_lin(struct vgamode_s *vmode_g
305                    , struct cursorpos cp, struct carattr ca)
306 {
307     // Get the dimensions
308     u16 nbcols = GET_BDA(video_cols);
309     if (cp.x >= nbcols)
310         return;
311
312     u8 *fdata_g = vgafont8;
313     u16 addr = cp.x * 8 + cp.y * nbcols * 64;
314     u16 src = ca.car * 8;
315     u8 i;
316     for (i = 0; i < 8; i++) {
317         u8 *dest_far = (void*)(addr + i * nbcols * 8);
318         u8 mask = 0x80;
319         u8 j;
320         for (j = 0; j < 8; j++) {
321             u8 data = 0x00;
322             if (GET_GLOBAL(fdata_g[src + i]) & mask)
323                 data = ca.attr;
324             SET_FARVAR(SEG_GRAPH, dest_far[j], data);
325             mask >>= 1;
326         }
327     }
328 }
329
330 static void
331 write_text_char(struct vgamode_s *vmode_g
332                 , struct cursorpos cp, struct carattr ca)
333 {
334     // Get the dimensions
335     u16 nbrows = GET_BDA(video_rows) + 1;
336     u16 nbcols = GET_BDA(video_cols);
337
338     // Compute the address
339     void *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, cp.page)
340                                 + (cp.x + cp.y * nbcols) * 2);
341
342     if (ca.use_attr) {
343         u16 dummy = (ca.attr << 8) | ca.car;
344         SET_FARVAR(GET_GLOBAL(vmode_g->sstart), *(u16*)address_far, dummy);
345     } else {
346         SET_FARVAR(GET_GLOBAL(vmode_g->sstart), *(u8*)address_far, ca.car);
347     }
348 }
349
350 void
351 vgafb_write_char(struct cursorpos cp, struct carattr ca)
352 {
353     // Get the mode
354     struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
355     if (!vmode_g)
356         return;
357
358     // FIXME gfx mode not complete
359     switch (GET_GLOBAL(vmode_g->memmodel)) {
360     case CTEXT:
361     case MTEXT:
362         write_text_char(vmode_g, cp, ca);
363         break;
364     case PLANAR4:
365     case PLANAR1:
366         write_gfx_char_pl4(vmode_g, cp, ca);
367         break;
368     case CGA:
369         write_gfx_char_cga(vmode_g, cp, ca);
370         break;
371     case LINEAR8:
372         write_gfx_char_lin(vmode_g, cp, ca);
373         break;
374     }
375 }
376
377 struct carattr
378 vgafb_read_char(struct cursorpos cp)
379 {
380     // Get the mode
381     struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
382     if (!vmode_g)
383         goto fail;
384
385     if (!(GET_GLOBAL(vmode_g->memmodel) & TEXT)) {
386         // FIXME gfx mode
387         dprintf(1, "Read char in graphics mode\n");
388         goto fail;
389     }
390
391     // Get the dimensions
392     u16 nbrows = GET_BDA(video_rows) + 1;
393     u16 nbcols = GET_BDA(video_cols);
394
395     // Compute the address
396     u16 *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, cp.page)
397                                + (cp.x + cp.y * nbcols) * 2);
398     u16 v = GET_FARVAR(GET_GLOBAL(vmode_g->sstart), *address_far);
399     struct carattr ca = {v, v>>8, 0};
400     return ca;
401
402 fail: ;
403     struct carattr ca2 = {0, 0, 0};
404     return ca2;
405 }
406
407
408 /****************************************************************
409  * Read/write pixels
410  ****************************************************************/
411
412 void
413 vgafb_write_pixel(u8 color, u16 x, u16 y)
414 {
415     // Get the mode
416     struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
417     if (!vmode_g)
418         return;
419     if (GET_GLOBAL(vmode_g->memmodel) & TEXT)
420         return;
421
422     u8 *addr_far, mask, attr, data;
423     switch (GET_GLOBAL(vmode_g->memmodel)) {
424     case PLANAR4:
425     case PLANAR1:
426         addr_far = (void*)(x / 8 + y * GET_BDA(video_cols));
427         mask = 0x80 >> (x & 0x07);
428         stdvga_grdc_write(0x08, mask);
429         stdvga_grdc_write(0x05, 0x02);
430         GET_FARVAR(SEG_GRAPH, *(volatile u8*)addr_far);
431         if (color & 0x80)
432             stdvga_grdc_write(0x03, 0x18);
433         SET_FARVAR(SEG_GRAPH, *addr_far, color);
434         stdvga_grdc_write(0x08, 0xff);
435         stdvga_grdc_write(0x05, 0x00);
436         stdvga_grdc_write(0x03, 0x00);
437         break;
438     case CGA:
439         if (GET_GLOBAL(vmode_g->pixbits) == 2)
440             addr_far = (void*)((x >> 2) + (y >> 1) * 80);
441         else
442             addr_far = (void*)((x >> 3) + (y >> 1) * 80);
443         if (y & 1)
444             addr_far += 0x2000;
445         data = GET_FARVAR(SEG_CTEXT, *addr_far);
446         if (GET_GLOBAL(vmode_g->pixbits) == 2) {
447             attr = (color & 0x03) << ((3 - (x & 0x03)) * 2);
448             mask = 0x03 << ((3 - (x & 0x03)) * 2);
449         } else {
450             attr = (color & 0x01) << (7 - (x & 0x07));
451             mask = 0x01 << (7 - (x & 0x07));
452         }
453         if (color & 0x80) {
454             data ^= attr;
455         } else {
456             data &= ~mask;
457             data |= attr;
458         }
459         SET_FARVAR(SEG_CTEXT, *addr_far, data);
460         break;
461     case LINEAR8:
462         addr_far = (void*)(x + y * (GET_BDA(video_cols) * 8));
463         SET_FARVAR(SEG_GRAPH, *addr_far, color);
464         break;
465     }
466 }
467
468 u8
469 vgafb_read_pixel(u16 x, u16 y)
470 {
471     // Get the mode
472     struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
473     if (!vmode_g)
474         return 0;
475     if (GET_GLOBAL(vmode_g->memmodel) & TEXT)
476         return 0;
477
478     u8 *addr_far, mask, attr=0, data, i;
479     switch (GET_GLOBAL(vmode_g->memmodel)) {
480     case PLANAR4:
481     case PLANAR1:
482         addr_far = (void*)(x / 8 + y * GET_BDA(video_cols));
483         mask = 0x80 >> (x & 0x07);
484         attr = 0x00;
485         for (i = 0; i < 4; i++) {
486             stdvga_grdc_write(0x04, i);
487             data = GET_FARVAR(SEG_GRAPH, *addr_far) & mask;
488             if (data > 0)
489                 attr |= (0x01 << i);
490         }
491         break;
492     case CGA:
493         addr_far = (void*)((x >> 2) + (y >> 1) * 80);
494         if (y & 1)
495             addr_far += 0x2000;
496         data = GET_FARVAR(SEG_CTEXT, *addr_far);
497         if (GET_GLOBAL(vmode_g->pixbits) == 2)
498             attr = (data >> ((3 - (x & 0x03)) * 2)) & 0x03;
499         else
500             attr = (data >> (7 - (x & 0x07))) & 0x01;
501         break;
502     case LINEAR8:
503         addr_far = (void*)(x + y * (GET_BDA(video_cols) * 8));
504         attr = GET_FARVAR(SEG_GRAPH, *addr_far);
505         break;
506     }
507     return attr;
508 }