blob: a75bc81eb296c2e6d396cce4691aa623013f01d3 [file] [log] [blame]
Kevin O'Connorc0c7df62009-05-17 18:11:33 -04001// 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
Kevin O'Connorf98bbf02012-01-27 23:09:02 -05008#include "vgabios.h" // vgafb_scroll
Kevin O'Connorc0c7df62009-05-17 18:11:33 -04009#include "biosvar.h" // GET_BDA
10#include "util.h" // memset_far
Kevin O'Connor160d34a2012-01-16 18:48:26 -050011#include "stdvga.h" // stdvga_planar4_plane
Kevin O'Connorc0c7df62009-05-17 18:11:33 -040012
Kevin O'Connorc0c7df62009-05-17 18:11:33 -040013
14/****************************************************************
15 * Screen scrolling
16 ****************************************************************/
17
Kevin O'Connor2c34f412009-05-31 15:25:14 -040018static inline void *
19memcpy_stride(u16 seg, void *dst, void *src, int copylen, int stride, int lines)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -040020{
Kevin O'Connor2c34f412009-05-31 15:25:14 -040021 for (; lines; lines--, dst+=stride, src+=stride)
22 memcpy_far(seg, dst, seg, src, copylen);
23 return dst;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -040024}
25
Kevin O'Connor2c34f412009-05-31 15:25:14 -040026static inline void
27memset_stride(u16 seg, void *dst, u8 val, int setlen, int stride, int lines)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -040028{
Kevin O'Connor2c34f412009-05-31 15:25:14 -040029 for (; lines; lines--, dst+=stride)
30 memset_far(seg, dst, val, setlen);
31}
32
33static inline void
34memset16_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);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -040038}
39
40static void
Kevin O'Connor217f2bc2009-05-31 00:46:47 -040041scroll_pl4(struct vgamode_s *vmode_g, int nblines, int attr
42 , struct cursorpos ul, struct cursorpos lr)
43{
Kevin O'Connor68ab0412012-01-01 10:54:19 -050044 int cheight = GET_GLOBAL(vmode_g->cheight);
45 int cwidth = 1;
46 int stride = GET_BDA(video_cols) * cwidth;
Kevin O'Connor2c34f412009-05-31 15:25:14 -040047 void *src_far, *dest_far;
48 if (nblines >= 0) {
Kevin O'Connor68ab0412012-01-01 10:54:19 -050049 dest_far = (void*)(ul.y * cheight * stride + ul.x * cwidth);
Kevin O'Connor2c34f412009-05-31 15:25:14 -040050 src_far = dest_far + nblines * cheight * stride;
51 } else {
52 // Scroll down
53 nblines = -nblines;
Kevin O'Connor68ab0412012-01-01 10:54:19 -050054 dest_far = (void*)(lr.y * cheight * stride + ul.x * cwidth);
Kevin O'Connor2c34f412009-05-31 15:25:14 -040055 src_far = dest_far - nblines * cheight * stride;
56 stride = -stride;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -040057 }
Kevin O'Connor2c34f412009-05-31 15:25:14 -040058 if (attr < 0)
59 attr = 0;
Kevin O'Connor160d34a2012-01-16 18:48:26 -050060 int cols = lr.x - ul.x + 1;
61 int rows = lr.y - ul.y + 1;
62 int i;
63 for (i=0; i<4; i++) {
64 stdvga_planar4_plane(i);
65 void *dest = dest_far;
66 if (nblines < rows)
67 dest = memcpy_stride(SEG_GRAPH, dest, src_far, cols * cwidth
68 , stride, (rows - nblines) * cheight);
69 u8 pixels = (attr & (1<<i)) ? 0xff : 0x00;
70 memset_stride(SEG_GRAPH, dest, pixels, cols * cwidth
71 , stride, nblines * cheight);
72 }
73 stdvga_planar4_plane(-1);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -040074}
75
Kevin O'Connor217f2bc2009-05-31 00:46:47 -040076static void
77scroll_cga(struct vgamode_s *vmode_g, int nblines, int attr
78 , struct cursorpos ul, struct cursorpos lr)
79{
Kevin O'Connor68ab0412012-01-01 10:54:19 -050080 int cheight = GET_GLOBAL(vmode_g->cheight) / 2;
Kevin O'Connor80da87d2012-01-02 11:13:14 -050081 int cwidth = GET_GLOBAL(vmode_g->depth);
Kevin O'Connor68ab0412012-01-01 10:54:19 -050082 int stride = GET_BDA(video_cols) * cwidth;
Kevin O'Connor2c34f412009-05-31 15:25:14 -040083 void *src_far, *dest_far;
84 if (nblines >= 0) {
Kevin O'Connor68ab0412012-01-01 10:54:19 -050085 dest_far = (void*)(ul.y * cheight * stride + ul.x * cwidth);
Kevin O'Connor2c34f412009-05-31 15:25:14 -040086 src_far = dest_far + nblines * cheight * stride;
87 } else {
88 // Scroll down
89 nblines = -nblines;
Kevin O'Connor68ab0412012-01-01 10:54:19 -050090 dest_far = (void*)(lr.y * cheight * stride + ul.x * cwidth);
Kevin O'Connor2c34f412009-05-31 15:25:14 -040091 src_far = dest_far - nblines * cheight * stride;
92 stride = -stride;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -040093 }
Kevin O'Connor160d34a2012-01-16 18:48:26 -050094 if (attr < 0)
95 attr = 0;
Kevin O'Connorb8886952012-02-02 20:56:38 -050096 if (cwidth == 1)
97 attr = (attr&1) | ((attr&1)<<1);
98 attr &= 3;
99 attr |= (attr<<2) | (attr<<4) | (attr<<6);
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500100 int cols = lr.x - ul.x + 1;
Kevin O'Connor2c34f412009-05-31 15:25:14 -0400101 int rows = lr.y - ul.y + 1;
102 if (nblines < rows) {
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500103 memcpy_stride(SEG_CTEXT, dest_far+0x2000, src_far+0x2000, cols * cwidth
Kevin O'Connorae6eb8f2012-01-01 10:33:51 -0500104 , stride, (rows - nblines) * cheight);
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500105 dest_far = memcpy_stride(SEG_CTEXT, dest_far, src_far, cols * cwidth
Kevin O'Connorae6eb8f2012-01-01 10:33:51 -0500106 , stride, (rows - nblines) * cheight);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400107 }
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500108 memset_stride(SEG_CTEXT, dest_far + 0x2000, attr, cols * cwidth
Kevin O'Connorae6eb8f2012-01-01 10:33:51 -0500109 , stride, nblines * cheight);
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500110 memset_stride(SEG_CTEXT, dest_far, attr, cols * cwidth
Kevin O'Connorae6eb8f2012-01-01 10:33:51 -0500111 , stride, nblines * cheight);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400112}
113
114static void
Kevin O'Connorb9373972012-01-01 11:04:42 -0500115scroll_lin(struct vgamode_s *vmode_g, int nblines, int attr
116 , struct cursorpos ul, struct cursorpos lr)
117{
118 int cheight = 8;
119 int cwidth = 8;
120 int stride = GET_BDA(video_cols) * cwidth;
121 void *src_far, *dest_far;
122 if (nblines >= 0) {
123 dest_far = (void*)(ul.y * cheight * stride + ul.x * cwidth);
124 src_far = dest_far + nblines * cheight * stride;
125 } else {
126 // Scroll down
127 nblines = -nblines;
128 dest_far = (void*)(lr.y * cheight * stride + ul.x * cwidth);
129 src_far = dest_far - nblines * cheight * stride;
130 stride = -stride;
131 }
Kevin O'Connor160d34a2012-01-16 18:48:26 -0500132 if (attr < 0)
133 attr = 0;
Kevin O'Connorb9373972012-01-01 11:04:42 -0500134 int cols = lr.x - ul.x + 1;
135 int rows = lr.y - ul.y + 1;
136 if (nblines < rows)
137 dest_far = memcpy_stride(SEG_GRAPH, dest_far, src_far, cols * cwidth
138 , stride, (rows - nblines) * cheight);
Kevin O'Connorb9373972012-01-01 11:04:42 -0500139 memset_stride(SEG_GRAPH, dest_far, attr, cols * cwidth
140 , stride, nblines * cheight);
141}
142
143static void
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400144scroll_text(struct vgamode_s *vmode_g, int nblines, int attr
145 , struct cursorpos ul, struct cursorpos lr)
146{
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500147 int cheight = 1;
148 int cwidth = 2;
Kevin O'Connor83047be2012-01-07 18:27:19 -0500149 int stride = GET_BDA(video_cols) * cwidth;
150 void *src_far, *dest_far = (void*)(GET_BDA(video_pagesize) * ul.page);
Kevin O'Connor2c34f412009-05-31 15:25:14 -0400151 if (nblines >= 0) {
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500152 dest_far += ul.y * cheight * stride + ul.x * cwidth;
153 src_far = dest_far + nblines * cheight * stride;
Kevin O'Connor2c34f412009-05-31 15:25:14 -0400154 } else {
155 // Scroll down
156 nblines = -nblines;
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500157 dest_far += lr.y * cheight * stride + ul.x * cwidth;
158 src_far = dest_far - nblines * cheight * stride;
Kevin O'Connor2c34f412009-05-31 15:25:14 -0400159 stride = -stride;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400160 }
Kevin O'Connor160d34a2012-01-16 18:48:26 -0500161 if (attr < 0)
162 attr = 0x07;
163 attr = (attr << 8) | ' ';
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500164 int cols = lr.x - ul.x + 1;
Kevin O'Connor2c34f412009-05-31 15:25:14 -0400165 int rows = lr.y - ul.y + 1;
166 u16 seg = GET_GLOBAL(vmode_g->sstart);
167 if (nblines < rows)
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500168 dest_far = memcpy_stride(seg, dest_far, src_far, cols * cwidth
169 , stride, (rows - nblines) * cheight);
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500170 memset16_stride(seg, dest_far, attr, cols * cwidth
171 , stride, nblines * cheight);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400172}
173
174void
175vgafb_scroll(int nblines, int attr, struct cursorpos ul, struct cursorpos lr)
176{
177 // Get the mode
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500178 struct vgamode_s *vmode_g = get_current_mode();
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400179 if (!vmode_g)
180 return;
181
182 // FIXME gfx mode not complete
183 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500184 case MM_TEXT:
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400185 scroll_text(vmode_g, nblines, attr, ul, lr);
186 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500187 case MM_PLANAR:
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400188 scroll_pl4(vmode_g, nblines, attr, ul, lr);
189 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500190 case MM_CGA:
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400191 scroll_cga(vmode_g, nblines, attr, ul, lr);
192 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500193 case MM_DIRECT:
194 case MM_PACKED:
Kevin O'Connorb9373972012-01-01 11:04:42 -0500195 scroll_lin(vmode_g, nblines, attr, ul, lr);
196 break;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400197 }
198}
199
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400200
201/****************************************************************
202 * Read/write characters to screen
203 ****************************************************************/
204
205static void
Kevin O'Connord3b38152009-05-26 00:05:37 -0400206write_gfx_char_pl4(struct vgamode_s *vmode_g
207 , struct cursorpos cp, struct carattr ca)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400208{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400209 u16 nbcols = GET_BDA(video_cols);
210 if (cp.x >= nbcols)
211 return;
212
Kevin O'Connor87233e92011-12-23 21:40:34 -0500213 u8 cheight = GET_GLOBAL(vmode_g->cheight);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400214 u8 *fdata_g;
215 switch (cheight) {
216 case 14:
217 fdata_g = vgafont14;
218 break;
219 case 16:
220 fdata_g = vgafont16;
221 break;
222 default:
223 fdata_g = vgafont8;
224 }
Kevin O'Connor918b1562009-05-25 11:05:18 -0400225 u16 addr = cp.x + cp.y * cheight * nbcols;
Kevin O'Connor09262412009-05-25 11:44:11 -0400226 u16 src = ca.car * cheight;
Kevin O'Connor160d34a2012-01-16 18:48:26 -0500227 int i;
228 for (i=0; i<4; i++) {
229 stdvga_planar4_plane(i);
230 u8 colors = ((ca.attr & (1<<i)) ? 0xff : 0x00);
231 int j;
232 for (j = 0; j < cheight; j++) {
233 u8 *dest_far = (void*)(addr + j * nbcols);
234 u8 pixels = colors & GET_GLOBAL(fdata_g[src + j]);
235 if (ca.attr & 0x80)
236 pixels ^= GET_FARVAR(SEG_GRAPH, *dest_far);
237 SET_FARVAR(SEG_GRAPH, *dest_far, pixels);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400238 }
239 }
Kevin O'Connor160d34a2012-01-16 18:48:26 -0500240 stdvga_planar4_plane(-1);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400241}
242
243static void
Kevin O'Connord3b38152009-05-26 00:05:37 -0400244write_gfx_char_cga(struct vgamode_s *vmode_g
245 , struct cursorpos cp, struct carattr ca)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400246{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400247 u16 nbcols = GET_BDA(video_cols);
248 if (cp.x >= nbcols)
249 return;
250
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400251 u8 *fdata_g = vgafont8;
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500252 u8 bpp = GET_GLOBAL(vmode_g->depth);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400253 u16 addr = (cp.x * bpp) + cp.y * 320;
Kevin O'Connor09262412009-05-25 11:44:11 -0400254 u16 src = ca.car * 8;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400255 u8 i;
256 for (i = 0; i < 8; i++) {
257 u8 *dest_far = (void*)(addr + (i >> 1) * 80);
258 if (i & 1)
259 dest_far += 0x2000;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400260 if (bpp == 1) {
Kevin O'Connor9a4d0c12012-02-02 20:56:10 -0500261 u8 colors = (ca.attr & 0x01) ? 0xff : 0x00;
262 u8 pixels = colors & GET_GLOBAL(fdata_g[src + i]);
Kevin O'Connor09262412009-05-25 11:44:11 -0400263 if (ca.attr & 0x80)
Kevin O'Connor9a4d0c12012-02-02 20:56:10 -0500264 pixels ^= GET_FARVAR(SEG_GRAPH, *dest_far);
265 SET_FARVAR(SEG_CTEXT, *dest_far, pixels);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400266 } else {
Kevin O'Connor9a4d0c12012-02-02 20:56:10 -0500267 u16 pixels = 0;
268 u8 fontline = GET_GLOBAL(fdata_g[src + i]);
269 int j;
270 for (j = 0; j < 8; j++)
271 if (fontline & (1<<j))
272 pixels |= (ca.attr & 0x03) << (j*2);
273 pixels = htons(pixels);
274 if (ca.attr & 0x80)
275 pixels ^= GET_FARVAR(SEG_GRAPH, *(u16*)dest_far);
276 SET_FARVAR(SEG_CTEXT, *(u16*)dest_far, pixels);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400277 }
278 }
279}
280
281static void
Kevin O'Connord3b38152009-05-26 00:05:37 -0400282write_gfx_char_lin(struct vgamode_s *vmode_g
283 , struct cursorpos cp, struct carattr ca)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400284{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400285 // Get the dimensions
286 u16 nbcols = GET_BDA(video_cols);
287 if (cp.x >= nbcols)
288 return;
289
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400290 u8 *fdata_g = vgafont8;
Kevin O'Connor918b1562009-05-25 11:05:18 -0400291 u16 addr = cp.x * 8 + cp.y * nbcols * 64;
Kevin O'Connor09262412009-05-25 11:44:11 -0400292 u16 src = ca.car * 8;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400293 u8 i;
294 for (i = 0; i < 8; i++) {
295 u8 *dest_far = (void*)(addr + i * nbcols * 8);
Kevin O'Connor9a4d0c12012-02-02 20:56:10 -0500296 u8 fontline = GET_GLOBAL(fdata_g[src + i]);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400297 u8 j;
298 for (j = 0; j < 8; j++) {
Kevin O'Connor9a4d0c12012-02-02 20:56:10 -0500299 u8 pixel = (fontline & (0x80>>j)) ? ca.attr : 0x00;
300 SET_FARVAR(SEG_GRAPH, dest_far[j], pixel);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400301 }
302 }
303}
304
Kevin O'Connord3b38152009-05-26 00:05:37 -0400305static void
306write_text_char(struct vgamode_s *vmode_g
307 , struct cursorpos cp, struct carattr ca)
308{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400309 // Compute the address
Kevin O'Connor83047be2012-01-07 18:27:19 -0500310 u16 nbcols = GET_BDA(video_cols);
311 void *address_far = (void*)(GET_BDA(video_pagesize) * cp.page
Kevin O'Connord3b38152009-05-26 00:05:37 -0400312 + (cp.x + cp.y * nbcols) * 2);
313
314 if (ca.use_attr) {
315 u16 dummy = (ca.attr << 8) | ca.car;
316 SET_FARVAR(GET_GLOBAL(vmode_g->sstart), *(u16*)address_far, dummy);
317 } else {
318 SET_FARVAR(GET_GLOBAL(vmode_g->sstart), *(u8*)address_far, ca.car);
319 }
320}
321
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400322void
Kevin O'Connord3b38152009-05-26 00:05:37 -0400323vgafb_write_char(struct cursorpos cp, struct carattr ca)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400324{
325 // Get the mode
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500326 struct vgamode_s *vmode_g = get_current_mode();
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400327 if (!vmode_g)
328 return;
329
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400330 // FIXME gfx mode not complete
Kevin O'Connord3b38152009-05-26 00:05:37 -0400331 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500332 case MM_TEXT:
Kevin O'Connord3b38152009-05-26 00:05:37 -0400333 write_text_char(vmode_g, cp, ca);
334 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500335 case MM_PLANAR:
Kevin O'Connord3b38152009-05-26 00:05:37 -0400336 write_gfx_char_pl4(vmode_g, cp, ca);
337 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500338 case MM_CGA:
Kevin O'Connord3b38152009-05-26 00:05:37 -0400339 write_gfx_char_cga(vmode_g, cp, ca);
340 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500341 case MM_DIRECT:
342 case MM_PACKED:
Kevin O'Connord3b38152009-05-26 00:05:37 -0400343 write_gfx_char_lin(vmode_g, cp, ca);
344 break;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400345 }
346}
347
Kevin O'Connor09262412009-05-25 11:44:11 -0400348struct carattr
Kevin O'Connord3b38152009-05-26 00:05:37 -0400349vgafb_read_char(struct cursorpos cp)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400350{
351 // Get the mode
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500352 struct vgamode_s *vmode_g = get_current_mode();
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400353 if (!vmode_g)
Kevin O'Connor09262412009-05-25 11:44:11 -0400354 goto fail;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400355
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500356 if (GET_GLOBAL(vmode_g->memmodel) != MM_TEXT) {
Kevin O'Connord3b38152009-05-26 00:05:37 -0400357 // FIXME gfx mode
358 dprintf(1, "Read char in graphics mode\n");
359 goto fail;
360 }
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400361
Kevin O'Connord3b38152009-05-26 00:05:37 -0400362 // Compute the address
Kevin O'Connor83047be2012-01-07 18:27:19 -0500363 u16 nbcols = GET_BDA(video_cols);
364 u16 *address_far = (void*)(GET_BDA(video_pagesize) * cp.page
Kevin O'Connord3b38152009-05-26 00:05:37 -0400365 + (cp.x + cp.y * nbcols) * 2);
366 u16 v = GET_FARVAR(GET_GLOBAL(vmode_g->sstart), *address_far);
367 struct carattr ca = {v, v>>8, 0};
Kevin O'Connor09262412009-05-25 11:44:11 -0400368 return ca;
Kevin O'Connord3b38152009-05-26 00:05:37 -0400369
370fail: ;
371 struct carattr ca2 = {0, 0, 0};
372 return ca2;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400373}
374
375
376/****************************************************************
377 * Read/write pixels
378 ****************************************************************/
379
380void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400381vgafb_write_pixel(u8 color, u16 x, u16 y)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400382{
383 // Get the mode
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500384 struct vgamode_s *vmode_g = get_current_mode();
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400385 if (!vmode_g)
386 return;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400387
Kevin O'Connor160d34a2012-01-16 18:48:26 -0500388 u8 *addr_far, mask, attr, data, i;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400389 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500390 case MM_PLANAR:
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400391 addr_far = (void*)(x / 8 + y * GET_BDA(video_cols));
392 mask = 0x80 >> (x & 0x07);
Kevin O'Connor160d34a2012-01-16 18:48:26 -0500393 for (i=0; i<4; i++) {
394 stdvga_planar4_plane(i);
395 u8 colors = (color & (1<<i)) ? 0xff : 0x00;
396 u8 orig = GET_FARVAR(SEG_GRAPH, *addr_far);
397 if (color & 0x80)
398 colors ^= orig;
399 SET_FARVAR(SEG_GRAPH, *addr_far, (colors & mask) | (orig & ~mask));
400 }
401 stdvga_planar4_plane(-1);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400402 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500403 case MM_CGA:
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500404 if (GET_GLOBAL(vmode_g->depth) == 2)
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400405 addr_far = (void*)((x >> 2) + (y >> 1) * 80);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400406 else
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400407 addr_far = (void*)((x >> 3) + (y >> 1) * 80);
408 if (y & 1)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400409 addr_far += 0x2000;
410 data = GET_FARVAR(SEG_CTEXT, *addr_far);
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500411 if (GET_GLOBAL(vmode_g->depth) == 2) {
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400412 attr = (color & 0x03) << ((3 - (x & 0x03)) * 2);
413 mask = 0x03 << ((3 - (x & 0x03)) * 2);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400414 } else {
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400415 attr = (color & 0x01) << (7 - (x & 0x07));
416 mask = 0x01 << (7 - (x & 0x07));
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400417 }
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400418 if (color & 0x80) {
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400419 data ^= attr;
420 } else {
421 data &= ~mask;
422 data |= attr;
423 }
424 SET_FARVAR(SEG_CTEXT, *addr_far, data);
425 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500426 case MM_DIRECT:
427 case MM_PACKED:
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400428 addr_far = (void*)(x + y * (GET_BDA(video_cols) * 8));
429 SET_FARVAR(SEG_GRAPH, *addr_far, color);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400430 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500431 case MM_TEXT:
432 return;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400433 }
434}
435
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400436u8
437vgafb_read_pixel(u16 x, u16 y)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400438{
439 // Get the mode
Kevin O'Connor4a73f932012-01-21 11:08:35 -0500440 struct vgamode_s *vmode_g = get_current_mode();
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400441 if (!vmode_g)
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400442 return 0;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400443
444 u8 *addr_far, mask, attr=0, data, i;
445 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500446 case MM_PLANAR:
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400447 addr_far = (void*)(x / 8 + y * GET_BDA(video_cols));
448 mask = 0x80 >> (x & 0x07);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400449 attr = 0x00;
450 for (i = 0; i < 4; i++) {
Kevin O'Connor160d34a2012-01-16 18:48:26 -0500451 stdvga_planar4_plane(i);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400452 data = GET_FARVAR(SEG_GRAPH, *addr_far) & mask;
453 if (data > 0)
454 attr |= (0x01 << i);
455 }
Kevin O'Connor160d34a2012-01-16 18:48:26 -0500456 stdvga_planar4_plane(-1);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400457 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500458 case MM_CGA:
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400459 addr_far = (void*)((x >> 2) + (y >> 1) * 80);
460 if (y & 1)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400461 addr_far += 0x2000;
462 data = GET_FARVAR(SEG_CTEXT, *addr_far);
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500463 if (GET_GLOBAL(vmode_g->depth) == 2)
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400464 attr = (data >> ((3 - (x & 0x03)) * 2)) & 0x03;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400465 else
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400466 attr = (data >> (7 - (x & 0x07))) & 0x01;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400467 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500468 case MM_DIRECT:
469 case MM_PACKED:
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400470 addr_far = (void*)(x + y * (GET_BDA(video_cols) * 8));
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400471 attr = GET_FARVAR(SEG_GRAPH, *addr_far);
472 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500473 case MM_TEXT:
474 return 0;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400475 }
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400476 return attr;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400477}