0f337209aaef9fd21c84efa33269f0ce3a5754c8
[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 MM_TEXT:
180         scroll_text(vmode_g, nblines, attr, ul, lr);
181         break;
182     case MM_PLANAR:
183         scroll_pl4(vmode_g, nblines, attr, ul, lr);
184         break;
185     case MM_CGA:
186         scroll_cga(vmode_g, nblines, attr, ul, lr);
187         break;
188     case MM_DIRECT:
189     case MM_PACKED:
190         scroll_lin(vmode_g, nblines, attr, ul, lr);
191         break;
192     }
193 }
194
195
196 /****************************************************************
197  * Read/write characters to screen
198  ****************************************************************/
199
200 static void
201 write_gfx_char_pl4(struct vgamode_s *vmode_g
202                    , struct cursorpos cp, struct carattr ca)
203 {
204     u16 nbcols = GET_BDA(video_cols);
205     if (cp.x >= nbcols)
206         return;
207
208     u8 cheight = GET_GLOBAL(vmode_g->cheight);
209     u8 *fdata_g;
210     switch (cheight) {
211     case 14:
212         fdata_g = vgafont14;
213         break;
214     case 16:
215         fdata_g = vgafont16;
216         break;
217     default:
218         fdata_g = vgafont8;
219     }
220     u16 addr = cp.x + cp.y * cheight * nbcols;
221     u16 src = ca.car * cheight;
222     stdvga_sequ_write(0x02, 0x0f);
223     stdvga_grdc_write(0x05, 0x02);
224     if (ca.attr & 0x80)
225         stdvga_grdc_write(0x03, 0x18);
226     else
227         stdvga_grdc_write(0x03, 0x00);
228     u8 i;
229     for (i = 0; i < cheight; i++) {
230         u8 *dest_far = (void*)(addr + i * nbcols);
231         u8 j;
232         for (j = 0; j < 8; j++) {
233             u8 mask = 0x80 >> j;
234             stdvga_grdc_write(0x08, mask);
235             GET_FARVAR(SEG_GRAPH, *(volatile u8*)dest_far);
236             if (GET_GLOBAL(fdata_g[src + i]) & mask)
237                 SET_FARVAR(SEG_GRAPH, *dest_far, ca.attr & 0x0f);
238             else
239                 SET_FARVAR(SEG_GRAPH, *dest_far, 0x00);
240         }
241     }
242     stdvga_grdc_write(0x08, 0xff);
243     stdvga_grdc_write(0x05, 0x00);
244     stdvga_grdc_write(0x03, 0x00);
245 }
246
247 static void
248 write_gfx_char_cga(struct vgamode_s *vmode_g
249                    , struct cursorpos cp, struct carattr ca)
250 {
251     u16 nbcols = GET_BDA(video_cols);
252     if (cp.x >= nbcols)
253         return;
254
255     u8 *fdata_g = vgafont8;
256     u8 bpp = GET_GLOBAL(vmode_g->pixbits);
257     u16 addr = (cp.x * bpp) + cp.y * 320;
258     u16 src = ca.car * 8;
259     u8 i;
260     for (i = 0; i < 8; i++) {
261         u8 *dest_far = (void*)(addr + (i >> 1) * 80);
262         if (i & 1)
263             dest_far += 0x2000;
264         u8 mask = 0x80;
265         if (bpp == 1) {
266             u8 data = 0;
267             if (ca.attr & 0x80)
268                 data = GET_FARVAR(SEG_CTEXT, *dest_far);
269             u8 j;
270             for (j = 0; j < 8; j++) {
271                 if (GET_GLOBAL(fdata_g[src + i]) & mask) {
272                     if (ca.attr & 0x80)
273                         data ^= (ca.attr & 0x01) << (7 - j);
274                     else
275                         data |= (ca.attr & 0x01) << (7 - j);
276                 }
277                 mask >>= 1;
278             }
279             SET_FARVAR(SEG_CTEXT, *dest_far, data);
280         } else {
281             while (mask > 0) {
282                 u8 data = 0;
283                 if (ca.attr & 0x80)
284                     data = GET_FARVAR(SEG_CTEXT, *dest_far);
285                 u8 j;
286                 for (j = 0; j < 4; j++) {
287                     if (GET_GLOBAL(fdata_g[src + i]) & mask) {
288                         if (ca.attr & 0x80)
289                             data ^= (ca.attr & 0x03) << ((3 - j) * 2);
290                         else
291                             data |= (ca.attr & 0x03) << ((3 - j) * 2);
292                     }
293                     mask >>= 1;
294                 }
295                 SET_FARVAR(SEG_CTEXT, *dest_far, data);
296                 dest_far += 1;
297             }
298         }
299     }
300 }
301
302 static void
303 write_gfx_char_lin(struct vgamode_s *vmode_g
304                    , struct cursorpos cp, struct carattr ca)
305 {
306     // Get the dimensions
307     u16 nbcols = GET_BDA(video_cols);
308     if (cp.x >= nbcols)
309         return;
310
311     u8 *fdata_g = vgafont8;
312     u16 addr = cp.x * 8 + cp.y * nbcols * 64;
313     u16 src = ca.car * 8;
314     u8 i;
315     for (i = 0; i < 8; i++) {
316         u8 *dest_far = (void*)(addr + i * nbcols * 8);
317         u8 mask = 0x80;
318         u8 j;
319         for (j = 0; j < 8; j++) {
320             u8 data = 0x00;
321             if (GET_GLOBAL(fdata_g[src + i]) & mask)
322                 data = ca.attr;
323             SET_FARVAR(SEG_GRAPH, dest_far[j], data);
324             mask >>= 1;
325         }
326     }
327 }
328
329 static void
330 write_text_char(struct vgamode_s *vmode_g
331                 , struct cursorpos cp, struct carattr ca)
332 {
333     // Get the dimensions
334     u16 nbrows = GET_BDA(video_rows) + 1;
335     u16 nbcols = GET_BDA(video_cols);
336
337     // Compute the address
338     void *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, cp.page)
339                                 + (cp.x + cp.y * nbcols) * 2);
340
341     if (ca.use_attr) {
342         u16 dummy = (ca.attr << 8) | ca.car;
343         SET_FARVAR(GET_GLOBAL(vmode_g->sstart), *(u16*)address_far, dummy);
344     } else {
345         SET_FARVAR(GET_GLOBAL(vmode_g->sstart), *(u8*)address_far, ca.car);
346     }
347 }
348
349 void
350 vgafb_write_char(struct cursorpos cp, struct carattr ca)
351 {
352     // Get the mode
353     struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
354     if (!vmode_g)
355         return;
356
357     // FIXME gfx mode not complete
358     switch (GET_GLOBAL(vmode_g->memmodel)) {
359     case MM_TEXT:
360         write_text_char(vmode_g, cp, ca);
361         break;
362     case MM_PLANAR:
363         write_gfx_char_pl4(vmode_g, cp, ca);
364         break;
365     case MM_CGA:
366         write_gfx_char_cga(vmode_g, cp, ca);
367         break;
368     case MM_DIRECT:
369     case MM_PACKED:
370         write_gfx_char_lin(vmode_g, cp, ca);
371         break;
372     }
373 }
374
375 struct carattr
376 vgafb_read_char(struct cursorpos cp)
377 {
378     // Get the mode
379     struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
380     if (!vmode_g)
381         goto fail;
382
383     if (GET_GLOBAL(vmode_g->memmodel) != MM_TEXT) {
384         // FIXME gfx mode
385         dprintf(1, "Read char in graphics mode\n");
386         goto fail;
387     }
388
389     // Get the dimensions
390     u16 nbrows = GET_BDA(video_rows) + 1;
391     u16 nbcols = GET_BDA(video_cols);
392
393     // Compute the address
394     u16 *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, cp.page)
395                                + (cp.x + cp.y * nbcols) * 2);
396     u16 v = GET_FARVAR(GET_GLOBAL(vmode_g->sstart), *address_far);
397     struct carattr ca = {v, v>>8, 0};
398     return ca;
399
400 fail: ;
401     struct carattr ca2 = {0, 0, 0};
402     return ca2;
403 }
404
405
406 /****************************************************************
407  * Read/write pixels
408  ****************************************************************/
409
410 void
411 vgafb_write_pixel(u8 color, u16 x, u16 y)
412 {
413     // Get the mode
414     struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
415     if (!vmode_g)
416         return;
417
418     u8 *addr_far, mask, attr, data;
419     switch (GET_GLOBAL(vmode_g->memmodel)) {
420     case MM_PLANAR:
421         addr_far = (void*)(x / 8 + y * GET_BDA(video_cols));
422         mask = 0x80 >> (x & 0x07);
423         stdvga_grdc_write(0x08, mask);
424         stdvga_grdc_write(0x05, 0x02);
425         GET_FARVAR(SEG_GRAPH, *(volatile u8*)addr_far);
426         if (color & 0x80)
427             stdvga_grdc_write(0x03, 0x18);
428         SET_FARVAR(SEG_GRAPH, *addr_far, color);
429         stdvga_grdc_write(0x08, 0xff);
430         stdvga_grdc_write(0x05, 0x00);
431         stdvga_grdc_write(0x03, 0x00);
432         break;
433     case MM_CGA:
434         if (GET_GLOBAL(vmode_g->pixbits) == 2)
435             addr_far = (void*)((x >> 2) + (y >> 1) * 80);
436         else
437             addr_far = (void*)((x >> 3) + (y >> 1) * 80);
438         if (y & 1)
439             addr_far += 0x2000;
440         data = GET_FARVAR(SEG_CTEXT, *addr_far);
441         if (GET_GLOBAL(vmode_g->pixbits) == 2) {
442             attr = (color & 0x03) << ((3 - (x & 0x03)) * 2);
443             mask = 0x03 << ((3 - (x & 0x03)) * 2);
444         } else {
445             attr = (color & 0x01) << (7 - (x & 0x07));
446             mask = 0x01 << (7 - (x & 0x07));
447         }
448         if (color & 0x80) {
449             data ^= attr;
450         } else {
451             data &= ~mask;
452             data |= attr;
453         }
454         SET_FARVAR(SEG_CTEXT, *addr_far, data);
455         break;
456     case MM_DIRECT:
457     case MM_PACKED:
458         addr_far = (void*)(x + y * (GET_BDA(video_cols) * 8));
459         SET_FARVAR(SEG_GRAPH, *addr_far, color);
460         break;
461     case MM_TEXT:
462         return;
463     }
464 }
465
466 u8
467 vgafb_read_pixel(u16 x, u16 y)
468 {
469     // Get the mode
470     struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
471     if (!vmode_g)
472         return 0;
473
474     u8 *addr_far, mask, attr=0, data, i;
475     switch (GET_GLOBAL(vmode_g->memmodel)) {
476     case MM_PLANAR:
477         addr_far = (void*)(x / 8 + y * GET_BDA(video_cols));
478         mask = 0x80 >> (x & 0x07);
479         attr = 0x00;
480         for (i = 0; i < 4; i++) {
481             stdvga_grdc_write(0x04, i);
482             data = GET_FARVAR(SEG_GRAPH, *addr_far) & mask;
483             if (data > 0)
484                 attr |= (0x01 << i);
485         }
486         break;
487     case MM_CGA:
488         addr_far = (void*)((x >> 2) + (y >> 1) * 80);
489         if (y & 1)
490             addr_far += 0x2000;
491         data = GET_FARVAR(SEG_CTEXT, *addr_far);
492         if (GET_GLOBAL(vmode_g->pixbits) == 2)
493             attr = (data >> ((3 - (x & 0x03)) * 2)) & 0x03;
494         else
495             attr = (data >> (7 - (x & 0x07))) & 0x01;
496         break;
497     case MM_DIRECT:
498     case MM_PACKED:
499         addr_far = (void*)(x + y * (GET_BDA(video_cols) * 8));
500         attr = GET_FARVAR(SEG_GRAPH, *addr_far);
501         break;
502     case MM_TEXT:
503         return 0;
504     }
505     return attr;
506 }