Deleted Added
full compact
vt_vga.c (271121) vt_vga.c (271128)
1/*-
2 * Copyright (c) 2005 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Copyright (c) 2009 The FreeBSD Foundation
6 * All rights reserved.
7 *
8 * Portions of this software were developed by Ed Schouten

--- 17 unchanged lines hidden (view full) ---

26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2005 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Copyright (c) 2009 The FreeBSD Foundation
6 * All rights reserved.
7 *
8 * Portions of this software were developed by Ed Schouten

--- 17 unchanged lines hidden (view full) ---

26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: stable/10/sys/dev/vt/hw/vga/vt_vga.c 271121 2014-09-04 19:22:01Z emaste $");
34__FBSDID("$FreeBSD: stable/10/sys/dev/vt/hw/vga/vt_vga.c 271128 2014-09-04 20:18:08Z emaste $");
35
36#include <sys/param.h>
37#include <sys/kernel.h>
38#include <sys/systm.h>
39
40#include <dev/vt/vt.h>
41#include <dev/vt/hw/vga/vt_vga_reg.h>
42

--- 6 unchanged lines hidden (view full) ---

49#include <machine/vmparam.h>
50#endif /* __amd64__ || __i386__ */
51
52struct vga_softc {
53 bus_space_tag_t vga_fb_tag;
54 bus_space_handle_t vga_fb_handle;
55 bus_space_tag_t vga_reg_tag;
56 bus_space_handle_t vga_reg_handle;
35
36#include <sys/param.h>
37#include <sys/kernel.h>
38#include <sys/systm.h>
39
40#include <dev/vt/vt.h>
41#include <dev/vt/hw/vga/vt_vga_reg.h>
42

--- 6 unchanged lines hidden (view full) ---

49#include <machine/vmparam.h>
50#endif /* __amd64__ || __i386__ */
51
52struct vga_softc {
53 bus_space_tag_t vga_fb_tag;
54 bus_space_handle_t vga_fb_handle;
55 bus_space_tag_t vga_reg_tag;
56 bus_space_handle_t vga_reg_handle;
57 int vga_curcolor;
57 int vga_wmode;
58 term_color_t vga_curfg, vga_curbg;
58};
59
60/* Convenience macros. */
61#define MEM_READ1(sc, ofs) \
62 bus_space_read_1(sc->vga_fb_tag, sc->vga_fb_handle, ofs)
63#define MEM_WRITE1(sc, ofs, val) \
64 bus_space_write_1(sc->vga_fb_tag, sc->vga_fb_handle, ofs, val)
65#define REG_READ1(sc, reg) \
66 bus_space_read_1(sc->vga_reg_tag, sc->vga_reg_handle, reg)
67#define REG_WRITE1(sc, reg, val) \
68 bus_space_write_1(sc->vga_reg_tag, sc->vga_reg_handle, reg, val)
69
70#define VT_VGA_WIDTH 640
71#define VT_VGA_HEIGHT 480
72#define VT_VGA_MEMSIZE (VT_VGA_WIDTH * VT_VGA_HEIGHT / 8)
73
59};
60
61/* Convenience macros. */
62#define MEM_READ1(sc, ofs) \
63 bus_space_read_1(sc->vga_fb_tag, sc->vga_fb_handle, ofs)
64#define MEM_WRITE1(sc, ofs, val) \
65 bus_space_write_1(sc->vga_fb_tag, sc->vga_fb_handle, ofs, val)
66#define REG_READ1(sc, reg) \
67 bus_space_read_1(sc->vga_reg_tag, sc->vga_reg_handle, reg)
68#define REG_WRITE1(sc, reg, val) \
69 bus_space_write_1(sc->vga_reg_tag, sc->vga_reg_handle, reg, val)
70
71#define VT_VGA_WIDTH 640
72#define VT_VGA_HEIGHT 480
73#define VT_VGA_MEMSIZE (VT_VGA_WIDTH * VT_VGA_HEIGHT / 8)
74
75/*
76 * VGA is designed to handle 8 pixels at a time (8 pixels in one byte of
77 * memory).
78 */
79#define VT_VGA_PIXELS_BLOCK 8
80
81/*
82 * We use an off-screen addresses to:
83 * o store the background color;
84 * o store pixels pattern.
85 * Those addresses are then loaded in the latches once.
86 */
87#define VT_VGA_BGCOLOR_OFFSET VT_VGA_MEMSIZE
88
74static vd_probe_t vga_probe;
75static vd_init_t vga_init;
76static vd_blank_t vga_blank;
89static vd_probe_t vga_probe;
90static vd_init_t vga_init;
91static vd_blank_t vga_blank;
77static vd_bitbltchr_t vga_bitbltchr;
92static vd_bitblt_text_t vga_bitblt_text;
93static vd_bitblt_bmp_t vga_bitblt_bitmap;
78static vd_drawrect_t vga_drawrect;
79static vd_setpixel_t vga_setpixel;
94static vd_drawrect_t vga_drawrect;
95static vd_setpixel_t vga_setpixel;
80static vd_putchar_t vga_putchar;
81static vd_postswitch_t vga_postswitch;
82
83static const struct vt_driver vt_vga_driver = {
84 .vd_name = "vga",
85 .vd_probe = vga_probe,
86 .vd_init = vga_init,
87 .vd_blank = vga_blank,
96static vd_postswitch_t vga_postswitch;
97
98static const struct vt_driver vt_vga_driver = {
99 .vd_name = "vga",
100 .vd_probe = vga_probe,
101 .vd_init = vga_init,
102 .vd_blank = vga_blank,
88 .vd_bitbltchr = vga_bitbltchr,
103 .vd_bitblt_text = vga_bitblt_text,
104 .vd_bitblt_bmp = vga_bitblt_bitmap,
89 .vd_drawrect = vga_drawrect,
90 .vd_setpixel = vga_setpixel,
105 .vd_drawrect = vga_drawrect,
106 .vd_setpixel = vga_setpixel,
91 .vd_putchar = vga_putchar,
92 .vd_postswitch = vga_postswitch,
93 .vd_priority = VD_PRIORITY_GENERIC,
94};
95
96/*
97 * Driver supports both text mode and graphics mode. Make sure the
98 * buffer is always big enough to support both.
99 */
100static struct vga_softc vga_conssoftc;
101VT_DRIVER_DECLARE(vt_vga, vt_vga_driver);
102
103static inline void
107 .vd_postswitch = vga_postswitch,
108 .vd_priority = VD_PRIORITY_GENERIC,
109};
110
111/*
112 * Driver supports both text mode and graphics mode. Make sure the
113 * buffer is always big enough to support both.
114 */
115static struct vga_softc vga_conssoftc;
116VT_DRIVER_DECLARE(vt_vga, vt_vga_driver);
117
118static inline void
104vga_setcolor(struct vt_device *vd, term_color_t color)
119vga_setwmode(struct vt_device *vd, int wmode)
105{
106 struct vga_softc *sc = vd->vd_softc;
107
120{
121 struct vga_softc *sc = vd->vd_softc;
122
108 if (sc->vga_curcolor != color) {
109 REG_WRITE1(sc, VGA_GC_ADDRESS, VGA_GC_SET_RESET);
110 REG_WRITE1(sc, VGA_GC_DATA, color);
111 sc->vga_curcolor = color;
112 }
113}
123 if (sc->vga_wmode == wmode)
124 return;
114
125
115static void
116vga_blank(struct vt_device *vd, term_color_t color)
117{
118 struct vga_softc *sc = vd->vd_softc;
119 u_int ofs;
126 REG_WRITE1(sc, VGA_GC_ADDRESS, VGA_GC_MODE);
127 REG_WRITE1(sc, VGA_GC_DATA, wmode);
128 sc->vga_wmode = wmode;
120
129
121 vga_setcolor(vd, color);
122 for (ofs = 0; ofs < VT_VGA_MEMSIZE; ofs++)
123 MEM_WRITE1(sc, ofs, 0xff);
130 switch (wmode) {
131 case 3:
132 /* Re-enable all plans. */
133 REG_WRITE1(sc, VGA_SEQ_ADDRESS, VGA_SEQ_MAP_MASK);
134 REG_WRITE1(sc, VGA_SEQ_DATA, VGA_SEQ_MM_EM3 | VGA_SEQ_MM_EM2 |
135 VGA_SEQ_MM_EM1 | VGA_SEQ_MM_EM0);
136 break;
137 }
124}
125
126static inline void
138}
139
140static inline void
127vga_bitblt_put(struct vt_device *vd, u_long dst, term_color_t color,
128 uint8_t v)
141vga_setfg(struct vt_device *vd, term_color_t color)
129{
130 struct vga_softc *sc = vd->vd_softc;
131
142{
143 struct vga_softc *sc = vd->vd_softc;
144
132 /* Skip empty writes, in order to avoid palette changes. */
133 if (v != 0x00) {
134 vga_setcolor(vd, color);
135 /*
136 * When this MEM_READ1() gets disabled, all sorts of
137 * artifacts occur. This is because this read loads the
138 * set of 8 pixels that are about to be changed. There
139 * is one scenario where we can avoid the read, namely
140 * if all pixels are about to be overwritten anyway.
141 */
142 if (v != 0xff)
143 MEM_READ1(sc, dst);
144 MEM_WRITE1(sc, dst, v);
145 }
146}
145 vga_setwmode(vd, 3);
147
146
148static void
149vga_setpixel(struct vt_device *vd, int x, int y, term_color_t color)
150{
147 if (sc->vga_curfg == color)
148 return;
151
149
152 vga_bitblt_put(vd, (y * VT_VGA_WIDTH / 8) + (x / 8), color,
153 0x80 >> (x % 8));
150 REG_WRITE1(sc, VGA_GC_ADDRESS, VGA_GC_SET_RESET);
151 REG_WRITE1(sc, VGA_GC_DATA, color);
152 sc->vga_curfg = color;
154}
155
153}
154
156static void
157vga_drawrect(struct vt_device *vd, int x1, int y1, int x2, int y2, int fill,
158 term_color_t color)
159{
160 int x, y;
161
162 for (y = y1; y <= y2; y++) {
163 if (fill || (y == y1) || (y == y2)) {
164 for (x = x1; x <= x2; x++)
165 vga_setpixel(vd, x, y, color);
166 } else {
167 vga_setpixel(vd, x1, y, color);
168 vga_setpixel(vd, x2, y, color);
169 }
170 }
171}
172
173static inline void
155static inline void
174vga_bitblt_draw(struct vt_device *vd, const uint8_t *src,
175 u_long ldst, uint8_t shift, unsigned int width, unsigned int height,
176 term_color_t color, int negate)
156vga_setbg(struct vt_device *vd, term_color_t color)
177{
157{
178 u_long dst;
179 int w;
180 uint8_t b, r, out;
158 struct vga_softc *sc = vd->vd_softc;
181
159
182 for (; height > 0; height--) {
183 dst = ldst;
184 ldst += VT_VGA_WIDTH / 8;
185 r = 0;
186 for (w = width; w > 0; w -= 8) {
187 b = *src++;
188 if (negate) {
189 b = ~b;
190 /* Don't go too far. */
191 if (w < 8)
192 b &= 0xff << (8 - w);
193 }
194 /* Reintroduce bits from previous column. */
195 out = (b >> shift) | r;
196 r = b << (8 - shift);
197 vga_bitblt_put(vd, dst++, color, out);
198 }
199 /* Print the remainder. */
200 vga_bitblt_put(vd, dst, color, r);
201 }
202}
160 vga_setwmode(vd, 3);
203
161
204static void
205vga_bitbltchr(struct vt_device *vd, const uint8_t *src, const uint8_t *mask,
206 int bpl, vt_axis_t top, vt_axis_t left, unsigned int width,
207 unsigned int height, term_color_t fg, term_color_t bg)
208{
209 u_long dst, ldst;
210 int w;
211
212 /* Don't try to put off screen pixels */
213 if (((left + width) > VT_VGA_WIDTH) || ((top + height) >
214 VT_VGA_HEIGHT))
162 if (sc->vga_curbg == color)
215 return;
216
163 return;
164
217 dst = (VT_VGA_WIDTH * top + left) / 8;
165 REG_WRITE1(sc, VGA_GC_ADDRESS, VGA_GC_SET_RESET);
166 REG_WRITE1(sc, VGA_GC_DATA, color);
218
167
219 for (; height > 0; height--) {
220 ldst = dst;
221 for (w = width; w > 0; w -= 8) {
222 vga_bitblt_put(vd, ldst, fg, *src);
223 vga_bitblt_put(vd, ldst, bg, ~*src);
224 ldst++;
225 src++;
226 }
227 dst += VT_VGA_WIDTH / 8;
228 }
229}
168 /*
169 * Write 8 pixels using the background color to an off-screen
170 * byte in the video memory.
171 */
172 MEM_WRITE1(sc, VT_VGA_BGCOLOR_OFFSET, 0xff);
230
173
231/* Bitblt with mask support. Slow. */
232static void
233vga_maskbitbltchr(struct vt_device *vd, const uint8_t *src, const uint8_t *mask,
234 int bpl, vt_axis_t top, vt_axis_t left, unsigned int width,
235 unsigned int height, term_color_t fg, term_color_t bg)
236{
237 struct vga_softc *sc = vd->vd_softc;
238 u_long dst;
239 uint8_t shift;
174 /*
175 * Read those 8 pixels back to load the background color in the
176 * latches register.
177 */
178 MEM_READ1(sc, VT_VGA_BGCOLOR_OFFSET);
240
179
241 dst = (VT_VGA_WIDTH * top + left) / 8;
242 shift = left % 8;
180 sc->vga_curbg = color;
243
181
244 /* Don't try to put off screen pixels */
245 if (((left + width) > VT_VGA_WIDTH) || ((top + height) >
246 VT_VGA_HEIGHT))
247 return;
248
249 if (sc->vga_curcolor == fg) {
250 vga_bitblt_draw(vd, src, dst, shift, width, height, fg, 0);
251 vga_bitblt_draw(vd, src, dst, shift, width, height, bg, 1);
252 } else {
253 vga_bitblt_draw(vd, src, dst, shift, width, height, bg, 1);
254 vga_bitblt_draw(vd, src, dst, shift, width, height, fg, 0);
255 }
182 /*
183 * The Set/Reset register doesn't contain the fg color anymore,
184 * store an invalid color.
185 */
186 sc->vga_curfg = 0xff;
256}
257
258/*
259 * Binary searchable table for Unicode to CP437 conversion.
260 */
261
262struct unicp437 {
263 uint16_t unicode_base;

--- 112 unchanged lines hidden (view full) ---

376 return (c - cp437table[mid].unicode_base +
377 cp437table[mid].cp437_base);
378 }
379
380 return '?';
381}
382
383static void
187}
188
189/*
190 * Binary searchable table for Unicode to CP437 conversion.
191 */
192
193struct unicp437 {
194 uint16_t unicode_base;

--- 112 unchanged lines hidden (view full) ---

307 return (c - cp437table[mid].unicode_base +
308 cp437table[mid].cp437_base);
309 }
310
311 return '?';
312}
313
314static void
384vga_putchar(struct vt_device *vd, term_char_t c,
385 vt_axis_t top, vt_axis_t left, term_color_t fg, term_color_t bg)
315vga_blank(struct vt_device *vd, term_color_t color)
386{
387 struct vga_softc *sc = vd->vd_softc;
316{
317 struct vga_softc *sc = vd->vd_softc;
388 uint8_t ch, attr;
318 u_int ofs;
389
319
320 vga_setfg(vd, color);
321 for (ofs = 0; ofs < VT_VGA_MEMSIZE; ofs++)
322 MEM_WRITE1(sc, ofs, 0xff);
323}
324
325static inline void
326vga_bitblt_put(struct vt_device *vd, u_long dst, term_color_t color,
327 uint8_t v)
328{
329 struct vga_softc *sc = vd->vd_softc;
330
331 /* Skip empty writes, in order to avoid palette changes. */
332 if (v != 0x00) {
333 vga_setfg(vd, color);
334 /*
335 * When this MEM_READ1() gets disabled, all sorts of
336 * artifacts occur. This is because this read loads the
337 * set of 8 pixels that are about to be changed. There
338 * is one scenario where we can avoid the read, namely
339 * if all pixels are about to be overwritten anyway.
340 */
341 if (v != 0xff) {
342 MEM_READ1(sc, dst);
343
344 /* The bg color was trashed by the reads. */
345 sc->vga_curbg = 0xff;
346 }
347 MEM_WRITE1(sc, dst, v);
348 }
349}
350
351static void
352vga_setpixel(struct vt_device *vd, int x, int y, term_color_t color)
353{
354
355 vga_bitblt_put(vd, (y * VT_VGA_WIDTH / 8) + (x / 8), color,
356 0x80 >> (x % 8));
357}
358
359static void
360vga_drawrect(struct vt_device *vd, int x1, int y1, int x2, int y2, int fill,
361 term_color_t color)
362{
363 int x, y;
364
365 for (y = y1; y <= y2; y++) {
366 if (fill || (y == y1) || (y == y2)) {
367 for (x = x1; x <= x2; x++)
368 vga_setpixel(vd, x, y, color);
369 } else {
370 vga_setpixel(vd, x1, y, color);
371 vga_setpixel(vd, x2, y, color);
372 }
373 }
374}
375
376static void
377vga_compute_shifted_pattern(const uint8_t *src, unsigned int bytes,
378 unsigned int src_x, unsigned int x_count, unsigned int dst_x,
379 uint8_t *pattern, uint8_t *mask)
380{
381 unsigned int n;
382
383 n = src_x / 8;
384
390 /*
385 /*
391 * Convert character to CP437, which is the character set used
392 * by the VGA hardware by default.
386 * This mask has bits set, where a pixel (ether 0 or 1)
387 * comes from the source bitmap.
393 */
388 */
394 ch = vga_get_cp437(c);
389 if (mask != NULL) {
390 *mask = (0xff
391 >> (8 - x_count))
392 << (8 - x_count - dst_x);
393 }
395
394
395 if (n == (src_x + x_count - 1) / 8) {
396 /* All the pixels we want are in the same byte. */
397 *pattern = src[n];
398 if (dst_x >= src_x)
399 *pattern >>= (dst_x - src_x % 8);
400 else
401 *pattern <<= (src_x % 8 - dst_x);
402 } else {
403 /* The pixels we want are split into two bytes. */
404 if (dst_x >= src_x % 8) {
405 *pattern =
406 src[n] << (8 - dst_x - src_x % 8) |
407 src[n + 1] >> (dst_x - src_x % 8);
408 } else {
409 *pattern =
410 src[n] << (src_x % 8 - dst_x) |
411 src[n + 1] >> (8 - src_x % 8 - dst_x);
412 }
413 }
414}
415
416static void
417vga_copy_bitmap_portion(uint8_t *pattern_2colors, uint8_t *pattern_ncolors,
418 const uint8_t *src, const uint8_t *src_mask, unsigned int src_width,
419 unsigned int src_x, unsigned int dst_x, unsigned int x_count,
420 unsigned int src_y, unsigned int dst_y, unsigned int y_count,
421 term_color_t fg, term_color_t bg, int overwrite)
422{
423 unsigned int i, bytes;
424 uint8_t pattern, relevant_bits, mask;
425
426 bytes = (src_width + 7) / 8;
427
428 for (i = 0; i < y_count; ++i) {
429 vga_compute_shifted_pattern(src + (src_y + i) * bytes,
430 bytes, src_x, x_count, dst_x, &pattern, &relevant_bits);
431
432 if (src_mask == NULL) {
433 /*
434 * No src mask. Consider that all wanted bits
435 * from the source are "authoritative".
436 */
437 mask = relevant_bits;
438 } else {
439 /*
440 * There's an src mask. We shift it the same way
441 * we shifted the source pattern.
442 */
443 vga_compute_shifted_pattern(
444 src_mask + (src_y + i) * bytes,
445 bytes, src_x, x_count, dst_x,
446 &mask, NULL);
447
448 /* Now, only keep the wanted bits among them. */
449 mask &= relevant_bits;
450 }
451
452 /*
453 * Clear bits from the pattern which must be
454 * transparent, according to the source mask.
455 */
456 pattern &= mask;
457
458 /* Set the bits in the 2-colors array. */
459 if (overwrite)
460 pattern_2colors[dst_y + i] &= ~mask;
461 pattern_2colors[dst_y + i] |= pattern;
462
463 if (pattern_ncolors == NULL)
464 continue;
465
466 /*
467 * Set the same bits in the n-colors array. This one
468 * supports transparency, when a given bit is cleared in
469 * all colors.
470 */
471 if (overwrite) {
472 /*
473 * Ensure that the pixels used by this bitmap are
474 * cleared in other colors.
475 */
476 for (int j = 0; j < 16; ++j)
477 pattern_ncolors[(dst_y + i) * 16 + j] &=
478 ~mask;
479 }
480 pattern_ncolors[(dst_y + i) * 16 + fg] |= pattern;
481 pattern_ncolors[(dst_y + i) * 16 + bg] |= (~pattern & mask);
482 }
483}
484
485static void
486vga_bitblt_pixels_block_2colors(struct vt_device *vd, const uint8_t *masks,
487 term_color_t fg, term_color_t bg,
488 unsigned int x, unsigned int y, unsigned int height)
489{
490 unsigned int i, offset;
491 struct vga_softc *sc;
492
396 /*
493 /*
397 * Convert colors to VGA attributes.
494 * The great advantage of Write Mode 3 is that we just need
495 * to load the foreground in the Set/Reset register, load the
496 * background color in the latches register (this is done
497 * through a write in offscreen memory followed by a read of
498 * that data), then write the pattern to video memory. This
499 * pattern indicates if the pixel should use the foreground
500 * color (bit set) or the background color (bit cleared).
398 */
501 */
399 attr = bg << 4 | fg;
400
502
401 MEM_WRITE1(sc, 0x18000 + (top * 80 + left) * 2 + 0, ch);
402 MEM_WRITE1(sc, 0x18000 + (top * 80 + left) * 2 + 1, attr);
503 vga_setbg(vd, bg);
504 vga_setfg(vd, fg);
505
506 sc = vd->vd_softc;
507 offset = (VT_VGA_WIDTH * y + x) / 8;
508
509 for (i = 0; i < height; ++i, offset += VT_VGA_WIDTH / 8) {
510 MEM_WRITE1(sc, offset, masks[i]);
511 }
403}
404
405static void
512}
513
514static void
515vga_bitblt_pixels_block_ncolors(struct vt_device *vd, const uint8_t *masks,
516 unsigned int x, unsigned int y, unsigned int height)
517{
518 unsigned int i, j, plan, color, offset;
519 struct vga_softc *sc;
520 uint8_t mask, plans[height * 4];
521
522 sc = vd->vd_softc;
523
524 memset(plans, 0, sizeof(plans));
525
526 /*
527 * To write a group of pixels using 3 or more colors, we select
528 * Write Mode 0 and write one byte to each plan separately.
529 */
530
531 /*
532 * We first compute each byte: each plan contains one bit of the
533 * color code for each of the 8 pixels.
534 *
535 * For example, if the 8 pixels are like this:
536 * GBBBBBBY
537 * where:
538 * G (gray) = 0b0111
539 * B (black) = 0b0000
540 * Y (yellow) = 0b0011
541 *
542 * The corresponding for bytes are:
543 * GBBBBBBY
544 * Plan 0: 10000001 = 0x81
545 * Plan 1: 10000001 = 0x81
546 * Plan 2: 10000000 = 0x80
547 * Plan 3: 00000000 = 0x00
548 * | | |
549 * | | +-> 0b0011 (Y)
550 * | +-----> 0b0000 (B)
551 * +--------> 0b0111 (G)
552 */
553
554 for (i = 0; i < height; ++i) {
555 for (color = 0; color < 16; ++color) {
556 mask = masks[i * 16 + color];
557 if (mask == 0x00)
558 continue;
559
560 for (j = 0; j < 8; ++j) {
561 if (!((mask >> (7 - j)) & 0x1))
562 continue;
563
564 /* The pixel "j" uses color "color". */
565 for (plan = 0; plan < 4; ++plan)
566 plans[i * 4 + plan] |=
567 ((color >> plan) & 0x1) << (7 - j);
568 }
569 }
570 }
571
572 /*
573 * The bytes are ready: we now switch to Write Mode 0 and write
574 * all bytes, one plan at a time.
575 */
576 vga_setwmode(vd, 0);
577
578 REG_WRITE1(sc, VGA_SEQ_ADDRESS, VGA_SEQ_MAP_MASK);
579 for (plan = 0; plan < 4; ++plan) {
580 /* Select plan. */
581 REG_WRITE1(sc, VGA_SEQ_DATA, 1 << plan);
582
583 /* Write all bytes for this plan, from Y to Y+height. */
584 for (i = 0; i < height; ++i) {
585 offset = (VT_VGA_WIDTH * (y + i) + x) / 8;
586 MEM_WRITE1(sc, offset, plans[i * 4 + plan]);
587 }
588 }
589}
590
591static void
592vga_bitblt_one_text_pixels_block(struct vt_device *vd,
593 const struct vt_window *vw, unsigned int x, unsigned int y)
594{
595 const struct vt_buf *vb;
596 const struct vt_font *vf;
597 unsigned int i, col, row, src_x, x_count;
598 unsigned int used_colors_list[16], used_colors;
599 uint8_t pattern_2colors[vw->vw_font->vf_height];
600 uint8_t pattern_ncolors[vw->vw_font->vf_height * 16];
601 term_char_t c;
602 term_color_t fg, bg;
603 const uint8_t *src;
604
605 vb = &vw->vw_buf;
606 vf = vw->vw_font;
607
608 /*
609 * The current pixels block.
610 *
611 * We fill it with portions of characters, because both "grids"
612 * may not match.
613 *
614 * i is the index in this pixels block.
615 */
616
617 i = x;
618 used_colors = 0;
619 memset(used_colors_list, 0, sizeof(used_colors_list));
620 memset(pattern_2colors, 0, sizeof(pattern_2colors));
621 memset(pattern_ncolors, 0, sizeof(pattern_ncolors));
622
623 if (i < vw->vw_draw_area.tr_begin.tp_col) {
624 /*
625 * i is in the margin used to center the text area on
626 * the screen.
627 */
628
629 i = vw->vw_draw_area.tr_begin.tp_col;
630 }
631
632 while (i < x + VT_VGA_PIXELS_BLOCK &&
633 i < vw->vw_draw_area.tr_end.tp_col) {
634 /*
635 * Find which character is drawn on this pixel in the
636 * pixels block.
637 *
638 * While here, record what colors it uses.
639 */
640
641 col = (i - vw->vw_draw_area.tr_begin.tp_col) / vf->vf_width;
642 row = (y - vw->vw_draw_area.tr_begin.tp_row) / vf->vf_height;
643
644 c = VTBUF_GET_FIELD(vb, row, col);
645 src = vtfont_lookup(vf, c);
646
647 vt_determine_colors(c, VTBUF_ISCURSOR(vb, row, col), &fg, &bg);
648 if ((used_colors_list[fg] & 0x1) != 0x1)
649 used_colors++;
650 if ((used_colors_list[bg] & 0x2) != 0x2)
651 used_colors++;
652 used_colors_list[fg] |= 0x1;
653 used_colors_list[bg] |= 0x2;
654
655 /*
656 * Compute the portion of the character we want to draw,
657 * because the pixels block may start in the middle of a
658 * character.
659 *
660 * The first pixel to draw in the character is
661 * the current position -
662 * the start position of the character
663 *
664 * The last pixel to draw is either
665 * - the last pixel of the character, or
666 * - the pixel of the character matching the end of
667 * the pixels block
668 * whichever comes first. This position is then
669 * changed to be relative to the start position of the
670 * character.
671 */
672
673 src_x = i -
674 (col * vf->vf_width + vw->vw_draw_area.tr_begin.tp_col);
675 x_count = min(min(
676 (col + 1) * vf->vf_width +
677 vw->vw_draw_area.tr_begin.tp_col,
678 x + VT_VGA_PIXELS_BLOCK),
679 vw->vw_draw_area.tr_end.tp_col);
680 x_count -= col * vf->vf_width +
681 vw->vw_draw_area.tr_begin.tp_col;
682 x_count -= src_x;
683
684 /* Copy a portion of the character. */
685 vga_copy_bitmap_portion(pattern_2colors, pattern_ncolors,
686 src, NULL, vf->vf_width,
687 src_x, i % VT_VGA_PIXELS_BLOCK, x_count,
688 0, 0, vf->vf_height, fg, bg, 0);
689
690 /* We move to the next portion. */
691 i += x_count;
692 }
693
694#ifndef SC_NO_CUTPASTE
695 /*
696 * Copy the mouse pointer bitmap if it's over the current pixels
697 * block.
698 *
699 * We use the saved cursor position (saved in vt_flush()), because
700 * the current position could be different than the one used
701 * to mark the area dirty.
702 */
703 term_rect_t drawn_area;
704
705 drawn_area.tr_begin.tp_col = x;
706 drawn_area.tr_begin.tp_row = y;
707 drawn_area.tr_end.tp_col = x + VT_VGA_PIXELS_BLOCK;
708 drawn_area.tr_end.tp_row = y + vf->vf_height;
709 if (vd->vd_mshown && vt_is_cursor_in_area(vd, &drawn_area)) {
710 struct vt_mouse_cursor *cursor;
711 unsigned int mx, my;
712 unsigned int dst_x, src_y, dst_y, y_count;
713
714 cursor = vd->vd_mcursor;
715 mx = vd->vd_mx_drawn + vw->vw_draw_area.tr_begin.tp_col;
716 my = vd->vd_my_drawn + vw->vw_draw_area.tr_begin.tp_row;
717
718 /* Compute the portion of the cursor we want to copy. */
719 src_x = x > mx ? x - mx : 0;
720 dst_x = mx > x ? mx - x : 0;
721 x_count = min(min(min(
722 cursor->width - src_x,
723 x + VT_VGA_PIXELS_BLOCK - mx),
724 vw->vw_draw_area.tr_end.tp_col - mx),
725 VT_VGA_PIXELS_BLOCK);
726
727 /*
728 * The cursor isn't aligned on the Y-axis with
729 * characters, so we need to compute the vertical
730 * start/count.
731 */
732 src_y = y > my ? y - my : 0;
733 dst_y = my > y ? my - y : 0;
734 y_count = min(
735 min(cursor->height - src_y, y + vf->vf_height - my),
736 vf->vf_height);
737
738 /* Copy the cursor portion. */
739 vga_copy_bitmap_portion(pattern_2colors, pattern_ncolors,
740 cursor->map, cursor->mask, cursor->width,
741 src_x, dst_x, x_count, src_y, dst_y, y_count,
742 vd->vd_mcursor_fg, vd->vd_mcursor_bg, 1);
743
744 if ((used_colors_list[vd->vd_mcursor_fg] & 0x1) != 0x1)
745 used_colors++;
746 if ((used_colors_list[vd->vd_mcursor_bg] & 0x2) != 0x2)
747 used_colors++;
748 }
749#endif
750
751 /*
752 * The pixels block is completed, we can now draw it on the
753 * screen.
754 */
755 if (used_colors == 2)
756 vga_bitblt_pixels_block_2colors(vd, pattern_2colors, fg, bg,
757 x, y, vf->vf_height);
758 else
759 vga_bitblt_pixels_block_ncolors(vd, pattern_ncolors,
760 x, y, vf->vf_height);
761}
762
763static void
764vga_bitblt_text_gfxmode(struct vt_device *vd, const struct vt_window *vw,
765 const term_rect_t *area)
766{
767 const struct vt_font *vf;
768 unsigned int col, row;
769 unsigned int x1, y1, x2, y2, x, y;
770
771 vf = vw->vw_font;
772
773 /*
774 * Compute the top-left pixel position aligned with the video
775 * adapter pixels block size.
776 *
777 * This is calculated from the top-left column of te dirty area:
778 *
779 * 1. Compute the top-left pixel of the character:
780 * col * font width + x offset
781 *
782 * NOTE: x offset is used to center the text area on the
783 * screen. It's expressed in pixels, not in characters
784 * col/row!
785 *
786 * 2. Find the pixel further on the left marking the start of
787 * an aligned pixels block (eg. chunk of 8 pixels):
788 * character's x / blocksize * blocksize
789 *
790 * The division, being made on integers, achieves the
791 * alignment.
792 *
793 * For the Y-axis, we need to compute the character's y
794 * coordinate, but we don't need to align it.
795 */
796
797 col = area->tr_begin.tp_col;
798 row = area->tr_begin.tp_row;
799 x1 = (int)((col * vf->vf_width + vw->vw_draw_area.tr_begin.tp_col)
800 / VT_VGA_PIXELS_BLOCK)
801 * VT_VGA_PIXELS_BLOCK;
802 y1 = row * vf->vf_height + vw->vw_draw_area.tr_begin.tp_row;
803
804 /*
805 * Compute the bottom right pixel position, again, aligned with
806 * the pixels block size.
807 *
808 * The same rules apply, we just add 1 to base the computation
809 * on the "right border" of the dirty area.
810 */
811
812 col = area->tr_end.tp_col;
813 row = area->tr_end.tp_row;
814 x2 = (int)((col * vf->vf_width + vw->vw_draw_area.tr_begin.tp_col
815 + VT_VGA_PIXELS_BLOCK - 1)
816 / VT_VGA_PIXELS_BLOCK)
817 * VT_VGA_PIXELS_BLOCK;
818 y2 = row * vf->vf_height + vw->vw_draw_area.tr_begin.tp_row;
819
820 /* Clip the area to the screen size. */
821 x2 = min(x2, vw->vw_draw_area.tr_end.tp_col);
822 y2 = min(y2, vw->vw_draw_area.tr_end.tp_row);
823
824 /*
825 * Now, we take care of N pixels line at a time (the first for
826 * loop, N = font height), and for these lines, draw one pixels
827 * block at a time (the second for loop), not a character at a
828 * time.
829 *
830 * Therefore, on the X-axis, characters my be drawn partially if
831 * they are not aligned on 8-pixels boundary.
832 *
833 * However, the operation is repeated for the full height of the
834 * font before moving to the next character, because it allows
835 * to keep the color settings and write mode, before perhaps
836 * changing them with the next one.
837 */
838
839 for (y = y1; y < y2; y += vf->vf_height) {
840 for (x = x1; x < x2; x += VT_VGA_PIXELS_BLOCK) {
841 vga_bitblt_one_text_pixels_block(vd, vw, x, y);
842 }
843 }
844}
845
846static void
847vga_bitblt_text_txtmode(struct vt_device *vd, const struct vt_window *vw,
848 const term_rect_t *area)
849{
850 struct vga_softc *sc;
851 const struct vt_buf *vb;
852 unsigned int col, row;
853 term_char_t c;
854 term_color_t fg, bg;
855 uint8_t ch, attr;
856
857 sc = vd->vd_softc;
858 vb = &vw->vw_buf;
859
860 for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) {
861 for (col = area->tr_begin.tp_col;
862 col < area->tr_end.tp_col;
863 ++col) {
864 /*
865 * Get next character and its associated fg/bg
866 * colors.
867 */
868 c = VTBUF_GET_FIELD(vb, row, col);
869 vt_determine_colors(c, VTBUF_ISCURSOR(vb, row, col),
870 &fg, &bg);
871
872 /*
873 * Convert character to CP437, which is the
874 * character set used by the VGA hardware by
875 * default.
876 */
877 ch = vga_get_cp437(TCHAR_CHARACTER(c));
878
879 /* Convert colors to VGA attributes. */
880 attr = bg << 4 | fg;
881
882 MEM_WRITE1(sc, 0x18000 + (row * 80 + col) * 2 + 0,
883 ch);
884 MEM_WRITE1(sc, 0x18000 + (row * 80 + col) * 2 + 1,
885 attr);
886 }
887 }
888}
889
890static void
891vga_bitblt_text(struct vt_device *vd, const struct vt_window *vw,
892 const term_rect_t *area)
893{
894
895 if (!(vd->vd_flags & VDF_TEXTMODE)) {
896 vga_bitblt_text_gfxmode(vd, vw, area);
897 } else {
898 vga_bitblt_text_txtmode(vd, vw, area);
899 }
900}
901
902static void
903vga_bitblt_bitmap(struct vt_device *vd, const struct vt_window *vw,
904 const uint8_t *pattern, const uint8_t *mask,
905 unsigned int width, unsigned int height,
906 unsigned int x, unsigned int y, term_color_t fg, term_color_t bg)
907{
908 unsigned int x1, y1, x2, y2, i, j, src_x, dst_x, x_count;
909 uint8_t pattern_2colors;
910
911 /* Align coordinates with the 8-pxels grid. */
912 x1 = x / VT_VGA_PIXELS_BLOCK * VT_VGA_PIXELS_BLOCK;
913 y1 = y;
914
915 x2 = (x + width + VT_VGA_PIXELS_BLOCK - 1) /
916 VT_VGA_PIXELS_BLOCK * VT_VGA_PIXELS_BLOCK;
917 y2 = y + height;
918 x2 = min(x2, vd->vd_width - 1);
919 y2 = min(y2, vd->vd_height - 1);
920
921 for (j = y1; j < y2; ++j) {
922 src_x = 0;
923 dst_x = x - x1;
924 x_count = VT_VGA_PIXELS_BLOCK - dst_x;
925
926 for (i = x1; i < x2; i += VT_VGA_PIXELS_BLOCK) {
927 pattern_2colors = 0;
928
929 vga_copy_bitmap_portion(
930 &pattern_2colors, NULL,
931 pattern, mask, width,
932 src_x, dst_x, x_count,
933 j - y1, 0, 1, fg, bg, 0);
934
935 vga_bitblt_pixels_block_2colors(vd,
936 &pattern_2colors, fg, bg,
937 i, j, 1);
938
939 src_x += x_count;
940 dst_x = (dst_x + x_count) % VT_VGA_PIXELS_BLOCK;
941 x_count = min(width - src_x, VT_VGA_PIXELS_BLOCK);
942 }
943 }
944}
945
946static void
406vga_initialize_graphics(struct vt_device *vd)
407{
408 struct vga_softc *sc = vd->vd_softc;
409
410 /* Clock select. */
411 REG_WRITE1(sc, VGA_GEN_MISC_OUTPUT_W, VGA_GEN_MO_VSP | VGA_GEN_MO_HSP |
412 VGA_GEN_MO_PB | VGA_GEN_MO_ER | VGA_GEN_MO_IOA);
413 /* Set sequencer clocking and memory mode. */

--- 206 unchanged lines hidden (view full) ---

620 REG_WRITE1(sc, VGA_CRTC_ADDRESS, VGA_CRTC_MODE_CONTROL);
621 x = REG_READ1(sc, VGA_CRTC_DATA);
622 REG_WRITE1(sc, VGA_CRTC_DATA, x | VGA_CRTC_MC_HR);
623
624 if (!textmode) {
625 /* Switch to write mode 3, because we'll mainly do bitblt. */
626 REG_WRITE1(sc, VGA_GC_ADDRESS, VGA_GC_MODE);
627 REG_WRITE1(sc, VGA_GC_DATA, 3);
947vga_initialize_graphics(struct vt_device *vd)
948{
949 struct vga_softc *sc = vd->vd_softc;
950
951 /* Clock select. */
952 REG_WRITE1(sc, VGA_GEN_MISC_OUTPUT_W, VGA_GEN_MO_VSP | VGA_GEN_MO_HSP |
953 VGA_GEN_MO_PB | VGA_GEN_MO_ER | VGA_GEN_MO_IOA);
954 /* Set sequencer clocking and memory mode. */

--- 206 unchanged lines hidden (view full) ---

1161 REG_WRITE1(sc, VGA_CRTC_ADDRESS, VGA_CRTC_MODE_CONTROL);
1162 x = REG_READ1(sc, VGA_CRTC_DATA);
1163 REG_WRITE1(sc, VGA_CRTC_DATA, x | VGA_CRTC_MC_HR);
1164
1165 if (!textmode) {
1166 /* Switch to write mode 3, because we'll mainly do bitblt. */
1167 REG_WRITE1(sc, VGA_GC_ADDRESS, VGA_GC_MODE);
1168 REG_WRITE1(sc, VGA_GC_DATA, 3);
1169 sc->vga_wmode = 3;
1170
1171 /*
1172 * In Write Mode 3, Enable Set/Reset is ignored, but we
1173 * use Write Mode 0 to write a group of 8 pixels using
1174 * 3 or more colors. In this case, we want to disable
1175 * Set/Reset: set Enable Set/Reset to 0.
1176 */
628 REG_WRITE1(sc, VGA_GC_ADDRESS, VGA_GC_ENABLE_SET_RESET);
1177 REG_WRITE1(sc, VGA_GC_ADDRESS, VGA_GC_ENABLE_SET_RESET);
629 REG_WRITE1(sc, VGA_GC_DATA, 0x0f);
1178 REG_WRITE1(sc, VGA_GC_DATA, 0x00);
1179
1180 /*
1181 * Clear the colors we think are loaded into Set/Reset or
1182 * the latches.
1183 */
1184 sc->vga_curfg = sc->vga_curbg = 0xff;
630 }
631}
632
633static int
634vga_probe(struct vt_device *vd)
635{
636
637 return (CN_INTERNAL);

--- 50 unchanged lines hidden ---
1185 }
1186}
1187
1188static int
1189vga_probe(struct vt_device *vd)
1190{
1191
1192 return (CN_INTERNAL);

--- 50 unchanged lines hidden ---