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