blob: b6daba651ca26544caacae489610ab38571f1fcc [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
8#include "biosvar.h" // GET_BDA
9#include "util.h" // memset_far
Kevin O'Connor10dff3d2012-01-09 19:19:44 -050010#include "vgahw.h" // vgahw_find_mode
Kevin O'Connor88ca7412011-12-31 04:24:20 -050011#include "stdvga.h" // stdvga_grdc_write
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 int cols = lr.x - ul.x + 1;
59 int rows = lr.y - ul.y + 1;
60 if (nblines < rows) {
Kevin O'Connor88ca7412011-12-31 04:24:20 -050061 stdvga_grdc_write(0x05, 0x01);
Kevin O'Connor68ab0412012-01-01 10:54:19 -050062 dest_far = memcpy_stride(SEG_GRAPH, dest_far, src_far, cols * cwidth
63 , stride, (rows - nblines) * cheight);
Kevin O'Connor2c34f412009-05-31 15:25:14 -040064 }
65 if (attr < 0)
66 attr = 0;
Kevin O'Connor88ca7412011-12-31 04:24:20 -050067 stdvga_grdc_write(0x05, 0x02);
Kevin O'Connor68ab0412012-01-01 10:54:19 -050068 memset_stride(SEG_GRAPH, dest_far, attr, cols * cwidth
69 , stride, nblines * cheight);
Kevin O'Connor88ca7412011-12-31 04:24:20 -050070 stdvga_grdc_write(0x05, 0x00);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -040071}
72
Kevin O'Connor217f2bc2009-05-31 00:46:47 -040073static void
74scroll_cga(struct vgamode_s *vmode_g, int nblines, int attr
75 , struct cursorpos ul, struct cursorpos lr)
76{
Kevin O'Connor68ab0412012-01-01 10:54:19 -050077 int cheight = GET_GLOBAL(vmode_g->cheight) / 2;
Kevin O'Connor80da87d2012-01-02 11:13:14 -050078 int cwidth = GET_GLOBAL(vmode_g->depth);
Kevin O'Connor68ab0412012-01-01 10:54:19 -050079 int stride = GET_BDA(video_cols) * cwidth;
Kevin O'Connor2c34f412009-05-31 15:25:14 -040080 void *src_far, *dest_far;
81 if (nblines >= 0) {
Kevin O'Connor68ab0412012-01-01 10:54:19 -050082 dest_far = (void*)(ul.y * cheight * stride + ul.x * cwidth);
Kevin O'Connor2c34f412009-05-31 15:25:14 -040083 src_far = dest_far + nblines * cheight * stride;
84 } else {
85 // Scroll down
86 nblines = -nblines;
Kevin O'Connor68ab0412012-01-01 10:54:19 -050087 dest_far = (void*)(lr.y * cheight * stride + ul.x * cwidth);
Kevin O'Connor2c34f412009-05-31 15:25:14 -040088 src_far = dest_far - nblines * cheight * stride;
89 stride = -stride;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -040090 }
Kevin O'Connor68ab0412012-01-01 10:54:19 -050091 int cols = lr.x - ul.x + 1;
Kevin O'Connor2c34f412009-05-31 15:25:14 -040092 int rows = lr.y - ul.y + 1;
93 if (nblines < rows) {
Kevin O'Connor68ab0412012-01-01 10:54:19 -050094 memcpy_stride(SEG_CTEXT, dest_far+0x2000, src_far+0x2000, cols * cwidth
Kevin O'Connorae6eb8f2012-01-01 10:33:51 -050095 , stride, (rows - nblines) * cheight);
Kevin O'Connor68ab0412012-01-01 10:54:19 -050096 dest_far = memcpy_stride(SEG_CTEXT, dest_far, src_far, cols * cwidth
Kevin O'Connorae6eb8f2012-01-01 10:33:51 -050097 , stride, (rows - nblines) * cheight);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -040098 }
Kevin O'Connor2c34f412009-05-31 15:25:14 -040099 if (attr < 0)
100 attr = 0;
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500101 memset_stride(SEG_CTEXT, dest_far + 0x2000, attr, cols * cwidth
Kevin O'Connorae6eb8f2012-01-01 10:33:51 -0500102 , stride, nblines * cheight);
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500103 memset_stride(SEG_CTEXT, dest_far, attr, cols * cwidth
Kevin O'Connorae6eb8f2012-01-01 10:33:51 -0500104 , stride, nblines * cheight);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400105}
106
107static void
Kevin O'Connorb9373972012-01-01 11:04:42 -0500108scroll_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
136static void
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400137scroll_text(struct vgamode_s *vmode_g, int nblines, int attr
138 , struct cursorpos ul, struct cursorpos lr)
139{
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500140 int cheight = 1;
141 int cwidth = 2;
Kevin O'Connor83047be2012-01-07 18:27:19 -0500142 int stride = GET_BDA(video_cols) * cwidth;
143 void *src_far, *dest_far = (void*)(GET_BDA(video_pagesize) * ul.page);
Kevin O'Connor2c34f412009-05-31 15:25:14 -0400144 if (nblines >= 0) {
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500145 dest_far += ul.y * cheight * stride + ul.x * cwidth;
146 src_far = dest_far + nblines * cheight * stride;
Kevin O'Connor2c34f412009-05-31 15:25:14 -0400147 } else {
148 // Scroll down
149 nblines = -nblines;
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500150 dest_far += lr.y * cheight * stride + ul.x * cwidth;
151 src_far = dest_far - nblines * cheight * stride;
Kevin O'Connor2c34f412009-05-31 15:25:14 -0400152 stride = -stride;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400153 }
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500154 int cols = lr.x - ul.x + 1;
Kevin O'Connor2c34f412009-05-31 15:25:14 -0400155 int rows = lr.y - ul.y + 1;
156 u16 seg = GET_GLOBAL(vmode_g->sstart);
157 if (nblines < rows)
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500158 dest_far = memcpy_stride(seg, dest_far, src_far, cols * cwidth
159 , stride, (rows - nblines) * cheight);
Kevin O'Connor2c34f412009-05-31 15:25:14 -0400160 if (attr < 0)
161 attr = 0x07;
162 attr = (attr << 8) | ' ';
Kevin O'Connor68ab0412012-01-01 10:54:19 -0500163 memset16_stride(seg, dest_far, attr, cols * cwidth
164 , stride, nblines * cheight);
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400165}
166
167void
168vgafb_scroll(int nblines, int attr, struct cursorpos ul, struct cursorpos lr)
169{
170 // Get the mode
Kevin O'Connor10dff3d2012-01-09 19:19:44 -0500171 struct vgamode_s *vmode_g = vgahw_find_mode(GET_BDA(video_mode));
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400172 if (!vmode_g)
173 return;
174
175 // FIXME gfx mode not complete
176 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500177 case MM_TEXT:
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400178 scroll_text(vmode_g, nblines, attr, ul, lr);
179 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500180 case MM_PLANAR:
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400181 scroll_pl4(vmode_g, nblines, attr, ul, lr);
182 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500183 case MM_CGA:
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400184 scroll_cga(vmode_g, nblines, attr, ul, lr);
185 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500186 case MM_DIRECT:
187 case MM_PACKED:
Kevin O'Connorb9373972012-01-01 11:04:42 -0500188 scroll_lin(vmode_g, nblines, attr, ul, lr);
189 break;
Kevin O'Connor217f2bc2009-05-31 00:46:47 -0400190 }
191}
192
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400193
194/****************************************************************
195 * Read/write characters to screen
196 ****************************************************************/
197
198static void
Kevin O'Connord3b38152009-05-26 00:05:37 -0400199write_gfx_char_pl4(struct vgamode_s *vmode_g
200 , struct cursorpos cp, struct carattr ca)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400201{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400202 u16 nbcols = GET_BDA(video_cols);
203 if (cp.x >= nbcols)
204 return;
205
Kevin O'Connor87233e92011-12-23 21:40:34 -0500206 u8 cheight = GET_GLOBAL(vmode_g->cheight);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400207 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 }
Kevin O'Connor918b1562009-05-25 11:05:18 -0400218 u16 addr = cp.x + cp.y * cheight * nbcols;
Kevin O'Connor09262412009-05-25 11:44:11 -0400219 u16 src = ca.car * cheight;
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500220 stdvga_sequ_write(0x02, 0x0f);
221 stdvga_grdc_write(0x05, 0x02);
Kevin O'Connor09262412009-05-25 11:44:11 -0400222 if (ca.attr & 0x80)
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500223 stdvga_grdc_write(0x03, 0x18);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400224 else
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500225 stdvga_grdc_write(0x03, 0x00);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400226 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;
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500232 stdvga_grdc_write(0x08, mask);
Kevin O'Connorae6eb8f2012-01-01 10:33:51 -0500233 GET_FARVAR(SEG_GRAPH, *(volatile u8*)dest_far);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400234 if (GET_GLOBAL(fdata_g[src + i]) & mask)
Kevin O'Connor09262412009-05-25 11:44:11 -0400235 SET_FARVAR(SEG_GRAPH, *dest_far, ca.attr & 0x0f);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400236 else
237 SET_FARVAR(SEG_GRAPH, *dest_far, 0x00);
238 }
239 }
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500240 stdvga_grdc_write(0x08, 0xff);
241 stdvga_grdc_write(0x05, 0x00);
242 stdvga_grdc_write(0x03, 0x00);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400243}
244
245static void
Kevin O'Connord3b38152009-05-26 00:05:37 -0400246write_gfx_char_cga(struct vgamode_s *vmode_g
247 , struct cursorpos cp, struct carattr ca)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400248{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400249 u16 nbcols = GET_BDA(video_cols);
250 if (cp.x >= nbcols)
251 return;
252
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400253 u8 *fdata_g = vgafont8;
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500254 u8 bpp = GET_GLOBAL(vmode_g->depth);
Kevin O'Connor918b1562009-05-25 11:05:18 -0400255 u16 addr = (cp.x * bpp) + cp.y * 320;
Kevin O'Connor09262412009-05-25 11:44:11 -0400256 u16 src = ca.car * 8;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400257 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;
Kevin O'Connor09262412009-05-25 11:44:11 -0400265 if (ca.attr & 0x80)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400266 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) {
Kevin O'Connor09262412009-05-25 11:44:11 -0400270 if (ca.attr & 0x80)
271 data ^= (ca.attr & 0x01) << (7 - j);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400272 else
Kevin O'Connor09262412009-05-25 11:44:11 -0400273 data |= (ca.attr & 0x01) << (7 - j);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400274 }
275 mask >>= 1;
276 }
277 SET_FARVAR(SEG_CTEXT, *dest_far, data);
278 } else {
279 while (mask > 0) {
280 u8 data = 0;
Kevin O'Connor09262412009-05-25 11:44:11 -0400281 if (ca.attr & 0x80)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400282 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) {
Kevin O'Connor09262412009-05-25 11:44:11 -0400286 if (ca.attr & 0x80)
287 data ^= (ca.attr & 0x03) << ((3 - j) * 2);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400288 else
Kevin O'Connor09262412009-05-25 11:44:11 -0400289 data |= (ca.attr & 0x03) << ((3 - j) * 2);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400290 }
291 mask >>= 1;
292 }
293 SET_FARVAR(SEG_CTEXT, *dest_far, data);
294 dest_far += 1;
295 }
296 }
297 }
298}
299
300static void
Kevin O'Connord3b38152009-05-26 00:05:37 -0400301write_gfx_char_lin(struct vgamode_s *vmode_g
302 , struct cursorpos cp, struct carattr ca)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400303{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400304 // Get the dimensions
305 u16 nbcols = GET_BDA(video_cols);
306 if (cp.x >= nbcols)
307 return;
308
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400309 u8 *fdata_g = vgafont8;
Kevin O'Connor918b1562009-05-25 11:05:18 -0400310 u16 addr = cp.x * 8 + cp.y * nbcols * 64;
Kevin O'Connor09262412009-05-25 11:44:11 -0400311 u16 src = ca.car * 8;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400312 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)
Kevin O'Connor09262412009-05-25 11:44:11 -0400320 data = ca.attr;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400321 SET_FARVAR(SEG_GRAPH, dest_far[j], data);
322 mask >>= 1;
323 }
324 }
325}
326
Kevin O'Connord3b38152009-05-26 00:05:37 -0400327static void
328write_text_char(struct vgamode_s *vmode_g
329 , struct cursorpos cp, struct carattr ca)
330{
Kevin O'Connord3b38152009-05-26 00:05:37 -0400331 // Compute the address
Kevin O'Connor83047be2012-01-07 18:27:19 -0500332 u16 nbcols = GET_BDA(video_cols);
333 void *address_far = (void*)(GET_BDA(video_pagesize) * cp.page
Kevin O'Connord3b38152009-05-26 00:05:37 -0400334 + (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
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400344void
Kevin O'Connord3b38152009-05-26 00:05:37 -0400345vgafb_write_char(struct cursorpos cp, struct carattr ca)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400346{
347 // Get the mode
Kevin O'Connor10dff3d2012-01-09 19:19:44 -0500348 struct vgamode_s *vmode_g = vgahw_find_mode(GET_BDA(video_mode));
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400349 if (!vmode_g)
350 return;
351
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400352 // FIXME gfx mode not complete
Kevin O'Connord3b38152009-05-26 00:05:37 -0400353 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500354 case MM_TEXT:
Kevin O'Connord3b38152009-05-26 00:05:37 -0400355 write_text_char(vmode_g, cp, ca);
356 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500357 case MM_PLANAR:
Kevin O'Connord3b38152009-05-26 00:05:37 -0400358 write_gfx_char_pl4(vmode_g, cp, ca);
359 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500360 case MM_CGA:
Kevin O'Connord3b38152009-05-26 00:05:37 -0400361 write_gfx_char_cga(vmode_g, cp, ca);
362 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500363 case MM_DIRECT:
364 case MM_PACKED:
Kevin O'Connord3b38152009-05-26 00:05:37 -0400365 write_gfx_char_lin(vmode_g, cp, ca);
366 break;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400367 }
368}
369
Kevin O'Connor09262412009-05-25 11:44:11 -0400370struct carattr
Kevin O'Connord3b38152009-05-26 00:05:37 -0400371vgafb_read_char(struct cursorpos cp)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400372{
373 // Get the mode
Kevin O'Connor10dff3d2012-01-09 19:19:44 -0500374 struct vgamode_s *vmode_g = vgahw_find_mode(GET_BDA(video_mode));
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400375 if (!vmode_g)
Kevin O'Connor09262412009-05-25 11:44:11 -0400376 goto fail;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400377
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500378 if (GET_GLOBAL(vmode_g->memmodel) != MM_TEXT) {
Kevin O'Connord3b38152009-05-26 00:05:37 -0400379 // FIXME gfx mode
380 dprintf(1, "Read char in graphics mode\n");
381 goto fail;
382 }
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400383
Kevin O'Connord3b38152009-05-26 00:05:37 -0400384 // Compute the address
Kevin O'Connor83047be2012-01-07 18:27:19 -0500385 u16 nbcols = GET_BDA(video_cols);
386 u16 *address_far = (void*)(GET_BDA(video_pagesize) * cp.page
Kevin O'Connord3b38152009-05-26 00:05:37 -0400387 + (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};
Kevin O'Connor09262412009-05-25 11:44:11 -0400390 return ca;
Kevin O'Connord3b38152009-05-26 00:05:37 -0400391
392fail: ;
393 struct carattr ca2 = {0, 0, 0};
394 return ca2;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400395}
396
397
398/****************************************************************
399 * Read/write pixels
400 ****************************************************************/
401
402void
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400403vgafb_write_pixel(u8 color, u16 x, u16 y)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400404{
405 // Get the mode
Kevin O'Connor10dff3d2012-01-09 19:19:44 -0500406 struct vgamode_s *vmode_g = vgahw_find_mode(GET_BDA(video_mode));
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400407 if (!vmode_g)
408 return;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400409
410 u8 *addr_far, mask, attr, data;
411 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500412 case MM_PLANAR:
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400413 addr_far = (void*)(x / 8 + y * GET_BDA(video_cols));
414 mask = 0x80 >> (x & 0x07);
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500415 stdvga_grdc_write(0x08, mask);
416 stdvga_grdc_write(0x05, 0x02);
Kevin O'Connorae6eb8f2012-01-01 10:33:51 -0500417 GET_FARVAR(SEG_GRAPH, *(volatile u8*)addr_far);
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400418 if (color & 0x80)
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500419 stdvga_grdc_write(0x03, 0x18);
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400420 SET_FARVAR(SEG_GRAPH, *addr_far, color);
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500421 stdvga_grdc_write(0x08, 0xff);
422 stdvga_grdc_write(0x05, 0x00);
423 stdvga_grdc_write(0x03, 0x00);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400424 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500425 case MM_CGA:
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500426 if (GET_GLOBAL(vmode_g->depth) == 2)
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400427 addr_far = (void*)((x >> 2) + (y >> 1) * 80);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400428 else
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400429 addr_far = (void*)((x >> 3) + (y >> 1) * 80);
430 if (y & 1)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400431 addr_far += 0x2000;
432 data = GET_FARVAR(SEG_CTEXT, *addr_far);
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500433 if (GET_GLOBAL(vmode_g->depth) == 2) {
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400434 attr = (color & 0x03) << ((3 - (x & 0x03)) * 2);
435 mask = 0x03 << ((3 - (x & 0x03)) * 2);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400436 } else {
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400437 attr = (color & 0x01) << (7 - (x & 0x07));
438 mask = 0x01 << (7 - (x & 0x07));
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400439 }
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400440 if (color & 0x80) {
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400441 data ^= attr;
442 } else {
443 data &= ~mask;
444 data |= attr;
445 }
446 SET_FARVAR(SEG_CTEXT, *addr_far, data);
447 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500448 case MM_DIRECT:
449 case MM_PACKED:
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400450 addr_far = (void*)(x + y * (GET_BDA(video_cols) * 8));
451 SET_FARVAR(SEG_GRAPH, *addr_far, color);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400452 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500453 case MM_TEXT:
454 return;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400455 }
456}
457
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400458u8
459vgafb_read_pixel(u16 x, u16 y)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400460{
461 // Get the mode
Kevin O'Connor10dff3d2012-01-09 19:19:44 -0500462 struct vgamode_s *vmode_g = vgahw_find_mode(GET_BDA(video_mode));
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400463 if (!vmode_g)
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400464 return 0;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400465
466 u8 *addr_far, mask, attr=0, data, i;
467 switch (GET_GLOBAL(vmode_g->memmodel)) {
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500468 case MM_PLANAR:
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400469 addr_far = (void*)(x / 8 + y * GET_BDA(video_cols));
470 mask = 0x80 >> (x & 0x07);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400471 attr = 0x00;
472 for (i = 0; i < 4; i++) {
Kevin O'Connor88ca7412011-12-31 04:24:20 -0500473 stdvga_grdc_write(0x04, i);
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400474 data = GET_FARVAR(SEG_GRAPH, *addr_far) & mask;
475 if (data > 0)
476 attr |= (0x01 << i);
477 }
478 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500479 case MM_CGA:
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400480 addr_far = (void*)((x >> 2) + (y >> 1) * 80);
481 if (y & 1)
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400482 addr_far += 0x2000;
483 data = GET_FARVAR(SEG_CTEXT, *addr_far);
Kevin O'Connor80da87d2012-01-02 11:13:14 -0500484 if (GET_GLOBAL(vmode_g->depth) == 2)
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400485 attr = (data >> ((3 - (x & 0x03)) * 2)) & 0x03;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400486 else
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400487 attr = (data >> (7 - (x & 0x07))) & 0x01;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400488 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500489 case MM_DIRECT:
490 case MM_PACKED:
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400491 addr_far = (void*)(x + y * (GET_BDA(video_cols) * 8));
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400492 attr = GET_FARVAR(SEG_GRAPH, *addr_far);
493 break;
Kevin O'Connord4398ad2012-01-01 12:32:53 -0500494 case MM_TEXT:
495 return 0;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400496 }
Kevin O'Connor227a2bb2009-05-31 22:00:20 -0400497 return attr;
Kevin O'Connorc0c7df62009-05-17 18:11:33 -0400498}