2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * This code is derived from software contributed to The DragonFly Project
6 * by Sascha Wildner <saw@online.de>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer as
13 * the first lines of this file unmodified.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_syscons.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/consio.h>
44 #include <machine/bus.h>
46 #include <dev/fb/fbreg.h>
47 #include <dev/fb/vgareg.h>
48 #include <dev/syscons/syscons.h>
50 #include <isa/isareg.h>
52 #ifndef SC_RENDER_DEBUG
53 #define SC_RENDER_DEBUG 0
56 static vr_clear_t vga_txtclear;
57 static vr_draw_border_t vga_txtborder;
58 static vr_draw_t vga_txtdraw;
59 static vr_set_cursor_t vga_txtcursor_shape;
60 static vr_draw_cursor_t vga_txtcursor;
61 static vr_blink_cursor_t vga_txtblink;
62 #ifndef SC_NO_CUTPASTE
63 static vr_draw_mouse_t vga_txtmouse;
65 #define vga_txtmouse (vr_draw_mouse_t *)vga_nop
69 static vr_init_t vga_rndrinit;
70 static vr_clear_t vga_pxlclear_direct;
71 static vr_clear_t vga_pxlclear_planar;
72 static vr_draw_border_t vga_pxlborder_direct;
73 static vr_draw_border_t vga_pxlborder_planar;
74 static vr_draw_t vga_egadraw;
75 static vr_draw_t vga_vgadraw_direct;
76 static vr_draw_t vga_vgadraw_planar;
77 static vr_set_cursor_t vga_pxlcursor_shape;
78 static vr_draw_cursor_t vga_pxlcursor_direct;
79 static vr_draw_cursor_t vga_pxlcursor_planar;
80 static vr_blink_cursor_t vga_pxlblink_direct;
81 static vr_blink_cursor_t vga_pxlblink_planar;
82 #ifndef SC_NO_CUTPASTE
83 static vr_draw_mouse_t vga_pxlmouse_direct;
84 static vr_draw_mouse_t vga_pxlmouse_planar;
86 #define vga_pxlmouse_direct (vr_draw_mouse_t *)vga_nop
87 #define vga_pxlmouse_planar (vr_draw_mouse_t *)vga_nop
89 #endif /* SC_PIXEL_MODE */
91 #ifndef SC_NO_MODE_CHANGE
92 static vr_draw_border_t vga_grborder;
95 static void vga_nop(scr_stat *scp);
97 static sc_rndr_sw_t txtrndrsw = {
105 (vr_set_mouse_t *)vga_nop,
108 RENDERER(mda, 0, txtrndrsw, vga_set);
109 RENDERER(cga, 0, txtrndrsw, vga_set);
110 RENDERER(ega, 0, txtrndrsw, vga_set);
111 RENDERER(vga, 0, txtrndrsw, vga_set);
114 static sc_rndr_sw_t egarndrsw = {
115 (vr_init_t *)vga_nop,
117 vga_pxlborder_planar,
120 vga_pxlcursor_planar,
122 (vr_set_mouse_t *)vga_nop,
125 RENDERER(ega, PIXEL_MODE, egarndrsw, vga_set);
127 static sc_rndr_sw_t vgarndrsw = {
129 (vr_clear_t *)vga_nop,
130 (vr_draw_border_t *)vga_nop,
131 (vr_draw_t *)vga_nop,
133 (vr_draw_cursor_t *)vga_nop,
134 (vr_blink_cursor_t *)vga_nop,
135 (vr_set_mouse_t *)vga_nop,
136 (vr_draw_mouse_t *)vga_nop,
138 RENDERER(vga, PIXEL_MODE, vgarndrsw, vga_set);
139 #endif /* SC_PIXEL_MODE */
141 #ifndef SC_NO_MODE_CHANGE
142 static sc_rndr_sw_t grrndrsw = {
143 (vr_init_t *)vga_nop,
144 (vr_clear_t *)vga_nop,
146 (vr_draw_t *)vga_nop,
147 (vr_set_cursor_t *)vga_nop,
148 (vr_draw_cursor_t *)vga_nop,
149 (vr_blink_cursor_t *)vga_nop,
150 (vr_set_mouse_t *)vga_nop,
151 (vr_draw_mouse_t *)vga_nop,
153 RENDERER(cga, GRAPHICS_MODE, grrndrsw, vga_set);
154 RENDERER(ega, GRAPHICS_MODE, grrndrsw, vga_set);
155 RENDERER(vga, GRAPHICS_MODE, grrndrsw, vga_set);
156 #endif /* SC_NO_MODE_CHANGE */
158 RENDERER_MODULE(vga, vga_set);
160 #ifndef SC_NO_CUTPASTE
161 #if !defined(SC_ALT_MOUSE_IMAGE) || defined(SC_PIXEL_MODE)
163 u_short md_border[16];
164 u_short md_interior[16];
169 static const struct mousedata mouse9x13 = { {
170 0xc000, 0xa000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x9780,
171 0xf200, 0x1200, 0x1900, 0x0900, 0x0f00, 0x0000, 0x0000, 0x0000, }, {
172 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7c00, 0x7e00, 0x6800,
173 0x0c00, 0x0c00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000, },
177 static const struct mousedata mouse10x16 = { {
178 0xc000, 0xa000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8080,
179 0x8040, 0x83c0, 0x9200, 0xa900, 0xc900, 0x0480, 0x0480, 0x0300, }, {
180 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7c00, 0x7e00, 0x7f00,
181 0x7f80, 0x7c00, 0x6c00, 0x4600, 0x0600, 0x0300, 0x0300, 0x0000, },
188 #define GET_PIXEL(scp, pos, x, w) \
190 (scp)->sc->adp->va_window + \
191 (x) * (scp)->xoff + \
192 (scp)->yoff * (scp)->font_size * (w) + \
193 (x) * ((pos) % (scp)->xsize) + \
194 (scp)->font_size * (w) * ((pos) / (scp)->xsize); \
197 #define DRAW_PIXEL(scp, pos, color) do { \
198 switch ((scp)->sc->adp->va_info.vi_depth) { \
200 writel((pos), vga_palette32[color]); \
203 if (((pos) & 1) == 0) { \
204 writew((pos), vga_palette32[color]); \
205 writeb((pos) + 2, vga_palette32[color] >> 16); \
207 writeb((pos), vga_palette32[color]); \
208 writew((pos) + 1, vga_palette32[color] >> 8); \
212 if ((scp)->sc->adp->va_info.vi_pixel_fsizes[1] == 5) \
213 writew((pos), vga_palette15[color]); \
215 writew((pos), vga_palette16[color]); \
218 writew((pos), vga_palette15[color]); \
221 writeb((pos), (uint8_t)(color)); \
225 static uint32_t vga_palette32[16] = {
226 0x000000, 0x0000ad, 0x00ad00, 0x00adad,
227 0xad0000, 0xad00ad, 0xad5200, 0xadadad,
228 0x525252, 0x5252ff, 0x52ff52, 0x52ffff,
229 0xff5252, 0xff52ff, 0xffff52, 0xffffff
232 static uint16_t vga_palette16[16] = {
233 0x0000, 0x0016, 0x0560, 0x0576, 0xb000, 0xb016, 0xb2a0, 0xb576,
234 0x52aa, 0x52bf, 0x57ea, 0x57ff, 0xfaaa, 0xfabf, 0xffea, 0xffff
237 static uint16_t vga_palette15[16] = {
238 0x0000, 0x0016, 0x02c0, 0x02d6, 0x5800, 0x5816, 0x5940, 0x5ad6,
239 0x294a, 0x295f, 0x2bea, 0x2bff, 0x7d4a, 0x7d5f, 0x7fea, 0x7fff
242 #ifndef SC_NO_CUTPASTE
243 static uint32_t mouse_buf32[256];
244 static uint16_t mouse_buf16[256];
245 static uint8_t mouse_buf8[256];
250 vga_nop(scr_stat *scp)
254 /* text mode renderer */
257 vga_txtclear(scr_stat *scp, int c, int attr)
259 sc_vtb_clear(&scp->scr, c, attr);
263 vga_txtborder(scr_stat *scp, int color)
265 vidd_set_border(scp->sc->adp, color);
269 vga_txtdraw(scr_stat *scp, int from, int count, int flip)
275 if (from + count > scp->xsize*scp->ysize)
276 count = scp->xsize*scp->ysize - from;
279 for (p = sc_vtb_pointer(&scp->scr, from); count-- > 0; ++from) {
280 c = sc_vtb_getc(&scp->vtb, from);
281 a = sc_vtb_geta(&scp->vtb, from);
282 a = (a & 0x8800) | ((a & 0x7000) >> 4)
283 | ((a & 0x0700) << 4);
284 p = sc_vtb_putchar(&scp->scr, p, c, a);
287 sc_vtb_copy(&scp->vtb, from, &scp->scr, from, count);
292 vga_txtcursor_shape(scr_stat *scp, int base, int height, int blink)
294 if (base < 0 || base >= scp->font_size)
296 /* the caller may set height <= 0 in order to disable the cursor */
298 scp->curs_attr.base = base;
299 scp->curs_attr.height = height;
301 vidd_set_hw_cursor_shape(scp->sc->adp, base, height,
302 scp->font_size, blink);
306 draw_txtcharcursor(scr_stat *scp, int at, u_short c, u_short a, int flip)
312 #ifndef SC_NO_FONT_LOADING
313 if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
318 if (scp->font_size < 14) {
321 } else if (scp->font_size >= 16) {
328 if (scp->curs_attr.base >= h)
332 | ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
333 bcopy(font + c*h, font + sc->cursor_char*h, h);
334 font = font + sc->cursor_char*h;
335 for (i = imax(h - scp->curs_attr.base - scp->curs_attr.height, 0);
336 i < h - scp->curs_attr.base; ++i) {
340 vidd_load_font(sc->adp, 0, h, 8, font, sc->cursor_char, 1);
341 sc_vtb_putc(&scp->scr, at, sc->cursor_char, a);
343 #endif /* SC_NO_FONT_LOADING */
345 if ((a & 0x7000) == 0x7000) {
347 if ((a & 0x0700) == 0)
351 if ((a & 0x0700) == 0x0700)
356 | ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
357 sc_vtb_putc(&scp->scr, at, c, a);
362 vga_txtcursor(scr_stat *scp, int at, int blink, int on, int flip)
364 video_adapter_t *adp;
367 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
372 scp->status |= VR_CURSOR_BLINK;
374 scp->status |= VR_CURSOR_ON;
375 vidd_set_hw_cursor(adp, at%scp->xsize,
378 if (scp->status & VR_CURSOR_ON)
379 vidd_set_hw_cursor(adp, -1, -1);
380 scp->status &= ~VR_CURSOR_ON;
383 scp->status &= ~VR_CURSOR_BLINK;
385 scp->status |= VR_CURSOR_ON;
386 draw_txtcharcursor(scp, at,
387 sc_vtb_getc(&scp->vtb, at),
388 sc_vtb_geta(&scp->vtb, at),
391 cursor_attr = sc_vtb_geta(&scp->vtb, at);
393 cursor_attr = (cursor_attr & 0x8800)
394 | ((cursor_attr & 0x7000) >> 4)
395 | ((cursor_attr & 0x0700) << 4);
396 if (scp->status & VR_CURSOR_ON)
397 sc_vtb_putc(&scp->scr, at,
398 sc_vtb_getc(&scp->vtb, at),
400 scp->status &= ~VR_CURSOR_ON;
406 vga_txtblink(scr_stat *scp, int at, int flip)
410 int sc_txtmouse_no_retrace_wait;
412 #ifndef SC_NO_CUTPASTE
415 draw_txtmouse(scr_stat *scp, int x, int y)
417 #ifndef SC_ALT_MOUSE_IMAGE
418 if (ISMOUSEAVAIL(scp->sc->adp->va_flags)) {
419 const struct mousedata *mdp;
420 u_char font_buf[128];
424 int xoffset, yoffset;
430 /* prepare mousepointer char's bitmaps */
431 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
432 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos)*scp->font_size,
433 &font_buf[0], scp->font_size);
434 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos + 1)*scp->font_size,
435 &font_buf[32], scp->font_size);
437 + sc_vtb_getc(&scp->scr, pos + scp->xsize)*scp->font_size,
438 &font_buf[64], scp->font_size);
440 + sc_vtb_getc(&scp->scr, pos + scp->xsize + 1)*scp->font_size,
441 &font_buf[96], scp->font_size);
442 for (i = 0; i < scp->font_size; ++i) {
443 cursor[i] = font_buf[i]<<8 | font_buf[i+32];
444 cursor[i + scp->font_size] = font_buf[i+64]<<8 | font_buf[i+96];
447 /* now and-or in the mousepointer image */
449 yoffset = y%scp->font_size;
450 for (i = 0; i < 16; ++i) {
451 cursor[i + yoffset] =
452 (cursor[i + yoffset] & ~(mdp->md_border[i] >> xoffset))
453 | (mdp->md_interior[i] >> xoffset);
455 for (i = 0; i < scp->font_size; ++i) {
456 font_buf[i] = (cursor[i] & 0xff00) >> 8;
457 font_buf[i + 32] = cursor[i] & 0xff;
458 font_buf[i + 64] = (cursor[i + scp->font_size] & 0xff00) >> 8;
459 font_buf[i + 96] = cursor[i + scp->font_size] & 0xff;
463 /* wait for vertical retrace to avoid jitter on some videocards */
464 crtc_addr = scp->sc->adp->va_crtc_addr;
465 while (!sc_txtmouse_no_retrace_wait &&
466 !(inb(crtc_addr + 6) & 0x08))
469 c = scp->sc->mouse_char;
470 vidd_load_font(scp->sc->adp, 0, 32, 8, font_buf, c, 4);
472 sc_vtb_putc(&scp->scr, pos, c, sc_vtb_geta(&scp->scr, pos));
473 /* FIXME: may be out of range! */
474 sc_vtb_putc(&scp->scr, pos + scp->xsize, c + 2,
475 sc_vtb_geta(&scp->scr, pos + scp->xsize));
476 if (x < (scp->xsize - 1)*8) {
477 sc_vtb_putc(&scp->scr, pos + 1, c + 1,
478 sc_vtb_geta(&scp->scr, pos + 1));
479 sc_vtb_putc(&scp->scr, pos + scp->xsize + 1, c + 3,
480 sc_vtb_geta(&scp->scr, pos + scp->xsize + 1));
483 #endif /* SC_ALT_MOUSE_IMAGE */
485 /* Red, magenta and brown are mapped to green to to keep it readable */
486 static const int col_conv[16] = {
487 6, 6, 6, 6, 2, 2, 2, 6, 14, 14, 14, 14, 10, 10, 10, 14
493 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
494 a = sc_vtb_geta(&scp->scr, pos);
495 if (scp->sc->adp->va_flags & V_ADP_COLOR)
496 color = (col_conv[(a & 0xf000) >> 12] << 12)
497 | ((a & 0x0f00) | 0x0800);
499 color = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
500 sc_vtb_putc(&scp->scr, pos, sc_vtb_getc(&scp->scr, pos), color);
505 remove_txtmouse(scr_stat *scp, int x, int y)
510 vga_txtmouse(scr_stat *scp, int x, int y, int on)
513 draw_txtmouse(scp, x, y);
515 remove_txtmouse(scp, x, y);
518 #endif /* SC_NO_CUTPASTE */
522 /* pixel (raster text) mode renderer */
525 vga_rndrinit(scr_stat *scp)
527 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PLANAR) {
528 scp->rndr->clear = vga_pxlclear_planar;
529 scp->rndr->draw_border = vga_pxlborder_planar;
530 scp->rndr->draw = vga_vgadraw_planar;
531 scp->rndr->draw_cursor = vga_pxlcursor_planar;
532 scp->rndr->blink_cursor = vga_pxlblink_planar;
533 scp->rndr->draw_mouse = vga_pxlmouse_planar;
535 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT ||
536 scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PACKED) {
537 scp->rndr->clear = vga_pxlclear_direct;
538 scp->rndr->draw_border = vga_pxlborder_direct;
539 scp->rndr->draw = vga_vgadraw_direct;
540 scp->rndr->draw_cursor = vga_pxlcursor_direct;
541 scp->rndr->blink_cursor = vga_pxlblink_direct;
542 scp->rndr->draw_mouse = vga_pxlmouse_direct;
547 vga_pxlclear_direct(scr_stat *scp, int c, int attr)
555 line_width = scp->sc->adp->va_line_width;
556 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
557 lines = scp->ysize * scp->font_size;
558 p = scp->sc->adp->va_window +
559 line_width * scp->yoff * scp->font_size +
560 scp->xoff * 8 * pixel_size;
562 for (i = 0; i < lines; ++i) {
563 bzero_io((void *)p, scp->xsize * 8 * pixel_size);
569 vga_pxlclear_planar(scr_stat *scp, int c, int attr)
576 /* XXX: we are just filling the screen with the background color... */
577 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
578 outw(GDCIDX, 0x0003); /* data rotate/function select */
579 outw(GDCIDX, 0x0f01); /* set/reset enable */
580 outw(GDCIDX, 0xff08); /* bit mask */
581 outw(GDCIDX, ((attr & 0xf000) >> 4) | 0x00); /* set/reset */
582 line_width = scp->sc->adp->va_line_width;
583 lines = scp->ysize*scp->font_size;
584 p = scp->sc->adp->va_window + line_width*scp->yoff*scp->font_size
586 for (i = 0; i < lines; ++i) {
587 bzero_io((void *)p, scp->xsize);
590 outw(GDCIDX, 0x0000); /* set/reset */
591 outw(GDCIDX, 0x0001); /* set/reset enable */
595 vga_pxlborder_direct(scr_stat *scp, int color)
606 line_width = scp->sc->adp->va_line_width;
607 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
610 s = scp->sc->adp->va_window;
611 e = s + line_width * scp->yoff * scp->font_size;
613 for (f = s; f < e; f += pixel_size)
614 DRAW_PIXEL(scp, f, color);
617 y = (scp->yoff + scp->ysize) * scp->font_size;
619 if (scp->ypixel > y) {
620 s = scp->sc->adp->va_window + line_width * y;
621 e = s + line_width * (scp->ypixel - y);
623 for (f = s; f < e; f += pixel_size)
624 DRAW_PIXEL(scp, f, color);
627 y = scp->yoff * scp->font_size;
628 x = scp->xpixel / 8 - scp->xoff - scp->xsize;
630 for (i = 0; i < scp->ysize * scp->font_size; ++i) {
632 s = scp->sc->adp->va_window + line_width * (y + i);
633 e = s + scp->xoff * 8 * pixel_size;
635 for (f = s; f < e; f += pixel_size)
636 DRAW_PIXEL(scp, f, color);
640 s = scp->sc->adp->va_window + line_width * (y + i) +
641 scp->xoff * 8 * pixel_size +
642 scp->xsize * 8 * pixel_size;
643 e = s + x * 8 * pixel_size;
645 for (f = s; f < e; f += pixel_size)
646 DRAW_PIXEL(scp, f, color);
652 vga_pxlborder_planar(scr_stat *scp, int color)
660 vidd_set_border(scp->sc->adp, color);
662 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
663 outw(GDCIDX, 0x0003); /* data rotate/function select */
664 outw(GDCIDX, 0x0f01); /* set/reset enable */
665 outw(GDCIDX, 0xff08); /* bit mask */
666 outw(GDCIDX, (color << 8) | 0x00); /* set/reset */
667 line_width = scp->sc->adp->va_line_width;
668 p = scp->sc->adp->va_window;
670 bzero_io((void *)p, line_width*scp->yoff*scp->font_size);
671 y = (scp->yoff + scp->ysize)*scp->font_size;
673 bzero_io((void *)(p + line_width*y), line_width*(scp->ypixel - y));
674 y = scp->yoff*scp->font_size;
675 x = scp->xpixel/8 - scp->xoff - scp->xsize;
676 for (i = 0; i < scp->ysize*scp->font_size; ++i) {
678 bzero_io((void *)(p + line_width*(y + i)), scp->xoff);
680 bzero_io((void *)(p + line_width*(y + i)
681 + scp->xoff + scp->xsize), x);
683 outw(GDCIDX, 0x0000); /* set/reset */
684 outw(GDCIDX, 0x0001); /* set/reset enable */
688 vga_egadraw(scr_stat *scp, int from, int count, int flip)
700 line_width = scp->sc->adp->va_line_width;
702 d = GET_PIXEL(scp, from, 1, line_width);
704 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
705 outw(GDCIDX, 0x0003); /* data rotate/function select */
706 outw(GDCIDX, 0x0f01); /* set/reset enable */
708 if (from + count > scp->xsize*scp->ysize)
709 count = scp->xsize*scp->ysize - from;
710 for (i = from; count-- > 0; ++i) {
711 a = sc_vtb_geta(&scp->vtb, i);
713 col1 = ((a & 0x7000) >> 4) | (a & 0x0800);
714 col2 = ((a & 0x8000) >> 4) | (a & 0x0700);
717 col2 = (a & 0xf000) >> 4;
719 /* set background color in EGA/VGA latch */
722 outw(GDCIDX, bg | 0x00); /* set/reset */
723 outw(GDCIDX, 0xff08); /* bit mask */
725 c = readb(d); /* set bg color in the latch */
727 /* foreground color */
728 outw(GDCIDX, col1 | 0x00); /* set/reset */
730 f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
731 for (j = 0; j < scp->font_size; ++j, ++f) {
732 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
737 if ((i % scp->xsize) == scp->xsize - 1)
738 d += scp->font_size * line_width - scp->xsize;
740 outw(GDCIDX, 0x0000); /* set/reset */
741 outw(GDCIDX, 0x0001); /* set/reset enable */
742 outw(GDCIDX, 0xff08); /* bit mask */
746 vga_vgadraw_direct(scr_stat *scp, int from, int count, int flip)
751 u_short col1, col2, color;
752 int line_width, pixel_size;
756 line_width = scp->sc->adp->va_line_width;
757 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
759 d = GET_PIXEL(scp, from, 8 * pixel_size, line_width);
761 if (from + count > scp->xsize * scp->ysize)
762 count = scp->xsize * scp->ysize - from;
764 for (i = from; count-- > 0; ++i) {
765 a = sc_vtb_geta(&scp->vtb, i);
768 col1 = (((a & 0x7000) >> 4) | (a & 0x0800)) >> 8;
769 col2 = (((a & 0x8000) >> 4) | (a & 0x0700)) >> 8;
771 col1 = (a & 0x0f00) >> 8;
772 col2 = (a & 0xf000) >> 12;
776 f = &(scp->font[sc_vtb_getc(&scp->vtb, i) * scp->font_size]);
778 for (j = 0; j < scp->font_size; ++j, ++f) {
779 for (k = 0; k < 8; ++k) {
780 color = *f & (1 << (7 - k)) ? col1 : col2;
781 DRAW_PIXEL(scp, e + pixel_size * k, color);
789 if ((i % scp->xsize) == scp->xsize - 1)
790 d += scp->font_size * line_width -
791 scp->xsize * 8 * pixel_size;
796 vga_vgadraw_planar(scr_stat *scp, int from, int count, int flip)
808 line_width = scp->sc->adp->va_line_width;
810 d = GET_PIXEL(scp, from, 1, line_width);
812 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
813 outw(GDCIDX, 0x0003); /* data rotate/function select */
814 outw(GDCIDX, 0x0f01); /* set/reset enable */
815 outw(GDCIDX, 0xff08); /* bit mask */
817 if (from + count > scp->xsize*scp->ysize)
818 count = scp->xsize*scp->ysize - from;
819 for (i = from; count-- > 0; ++i) {
820 a = sc_vtb_geta(&scp->vtb, i);
822 col1 = ((a & 0x7000) >> 4) | (a & 0x0800);
823 col2 = ((a & 0x8000) >> 4) | (a & 0x0700);
826 col2 = (a & 0xf000) >> 4;
828 /* set background color in EGA/VGA latch */
831 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
832 outw(GDCIDX, bg | 0x00); /* set/reset */
834 c = readb(d); /* set bg color in the latch */
835 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
837 /* foreground color */
838 outw(GDCIDX, col1 | 0x00); /* set/reset */
840 f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
841 for (j = 0; j < scp->font_size; ++j, ++f) {
846 if ((i % scp->xsize) == scp->xsize - 1)
847 d += scp->font_size * line_width - scp->xsize;
849 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
850 outw(GDCIDX, 0x0000); /* set/reset */
851 outw(GDCIDX, 0x0001); /* set/reset enable */
855 vga_pxlcursor_shape(scr_stat *scp, int base, int height, int blink)
857 if (base < 0 || base >= scp->font_size)
859 /* the caller may set height <= 0 in order to disable the cursor */
861 scp->curs_attr.base = base;
862 scp->curs_attr.height = height;
867 draw_pxlcursor_direct(scr_stat *scp, int at, int on, int flip)
871 int line_width, pixel_size;
873 int col1, col2, color;
877 line_width = scp->sc->adp->va_line_width;
878 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
880 d = GET_PIXEL(scp, at, 8 * pixel_size, line_width) +
881 (scp->font_size - scp->curs_attr.base - 1) * line_width;
883 a = sc_vtb_geta(&scp->vtb, at);
886 col1 = ((on) ? (a & 0x0f00) : ((a & 0xf000) >> 4)) >> 8;
887 col2 = ((on) ? ((a & 0xf000) >> 4) : (a & 0x0f00)) >> 8;
889 col1 = ((on) ? ((a & 0xf000) >> 4) : (a & 0x0f00)) >> 8;
890 col2 = ((on) ? (a & 0x0f00) : ((a & 0xf000) >> 4)) >> 8;
893 f = &(scp->font[sc_vtb_getc(&scp->vtb, at) * scp->font_size +
894 scp->font_size - scp->curs_attr.base - 1]);
896 height = imin(scp->curs_attr.height, scp->font_size);
898 for (i = 0; i < height; ++i, --f) {
899 for (j = 0; j < 8; ++j) {
900 color = *f & (1 << (7 - j)) ? col1 : col2;
901 DRAW_PIXEL(scp, d + pixel_size * j, color);
909 draw_pxlcursor_planar(scr_stat *scp, int at, int on, int flip)
920 line_width = scp->sc->adp->va_line_width;
922 d = GET_PIXEL(scp, at, 1, line_width) +
923 (scp->font_size - scp->curs_attr.base - 1) * line_width;
925 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
926 outw(GDCIDX, 0x0003); /* data rotate/function select */
927 outw(GDCIDX, 0x0f01); /* set/reset enable */
928 /* set background color in EGA/VGA latch */
929 a = sc_vtb_geta(&scp->vtb, at);
931 col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
933 col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
934 outw(GDCIDX, col | 0x00); /* set/reset */
935 outw(GDCIDX, 0xff08); /* bit mask */
937 c = readb(d); /* set bg color in the latch */
938 /* foreground color */
940 col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
942 col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
943 outw(GDCIDX, col | 0x00); /* set/reset */
944 f = &(scp->font[sc_vtb_getc(&scp->vtb, at)*scp->font_size
945 + scp->font_size - scp->curs_attr.base - 1]);
946 height = imin(scp->curs_attr.height, scp->font_size);
947 for (i = 0; i < height; ++i, --f) {
948 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
952 outw(GDCIDX, 0x0000); /* set/reset */
953 outw(GDCIDX, 0x0001); /* set/reset enable */
954 outw(GDCIDX, 0xff08); /* bit mask */
957 static int pxlblinkrate = 0;
960 vga_pxlcursor_direct(scr_stat *scp, int at, int blink, int on, int flip)
962 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
967 scp->status |= VR_CURSOR_ON;
968 draw_pxlcursor_direct(scp, at, on, flip);
969 } else if (++pxlblinkrate & 4) {
971 scp->status ^= VR_CURSOR_ON;
972 draw_pxlcursor_direct(scp, at,
973 scp->status & VR_CURSOR_ON,
977 if (scp->status & VR_CURSOR_ON)
978 draw_pxlcursor_direct(scp, at, on, flip);
979 scp->status &= ~VR_CURSOR_ON;
982 scp->status |= VR_CURSOR_BLINK;
984 scp->status &= ~VR_CURSOR_BLINK;
988 vga_pxlcursor_planar(scr_stat *scp, int at, int blink, int on, int flip)
990 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
995 scp->status |= VR_CURSOR_ON;
996 draw_pxlcursor_planar(scp, at, on, flip);
997 } else if (++pxlblinkrate & 4) {
999 scp->status ^= VR_CURSOR_ON;
1000 draw_pxlcursor_planar(scp, at,
1001 scp->status & VR_CURSOR_ON,
1005 if (scp->status & VR_CURSOR_ON)
1006 draw_pxlcursor_planar(scp, at, on, flip);
1007 scp->status &= ~VR_CURSOR_ON;
1010 scp->status |= VR_CURSOR_BLINK;
1012 scp->status &= ~VR_CURSOR_BLINK;
1016 vga_pxlblink_direct(scr_stat *scp, int at, int flip)
1018 if (!(scp->status & VR_CURSOR_BLINK))
1020 if (!(++pxlblinkrate & 4))
1023 scp->status ^= VR_CURSOR_ON;
1024 draw_pxlcursor_direct(scp, at, scp->status & VR_CURSOR_ON, flip);
1028 vga_pxlblink_planar(scr_stat *scp, int at, int flip)
1030 if (!(scp->status & VR_CURSOR_BLINK))
1032 if (!(++pxlblinkrate & 4))
1035 scp->status ^= VR_CURSOR_ON;
1036 draw_pxlcursor_planar(scp, at, scp->status & VR_CURSOR_ON, flip);
1039 #ifndef SC_NO_CUTPASTE
1042 draw_pxlmouse_planar(scr_stat *scp, int x, int y)
1044 const struct mousedata *mdp;
1053 mdp = (scp->font_size < 14) ? &mouse9x13 : &mouse10x16;
1054 line_width = scp->sc->adp->va_line_width;
1055 xoff = (x - scp->xoff*8)%8;
1056 yoff = y - rounddown(y, line_width);
1057 ymax = imin(y + mdp->md_height, scp->ypixel);
1059 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1060 outw(GDCIDX, 0x0001); /* set/reset enable */
1061 outw(GDCIDX, 0xff08); /* bit mask */
1062 outw(GDCIDX, 0x0803); /* data rotate/function select (and) */
1063 p = scp->sc->adp->va_window + line_width*y + x/8;
1064 for (i = y, j = 0; i < ymax; ++i, ++j) {
1065 m = ~(mdp->md_border[j] << 8 >> xoff);
1066 for (k = 0; k < 3; ++k) {
1067 m1 = m >> (8 * (2 - k));
1068 if (m1 != 0xff && x + 8 * k < scp->xpixel) {
1075 outw(GDCIDX, 0x1003); /* data rotate/function select (or) */
1076 p = scp->sc->adp->va_window + line_width*y + x/8;
1077 for (i = y, j = 0; i < ymax; ++i, ++j) {
1078 m = mdp->md_interior[j] << 8 >> xoff;
1079 for (k = 0; k < 3; ++k) {
1080 m1 = m >> (8 * (2 - k));
1081 if (m1 != 0 && x + 8 * k < scp->xpixel) {
1088 outw(GDCIDX, 0x0003); /* data rotate/function select */
1092 remove_pxlmouse_planar(scr_stat *scp, int x, int y)
1094 const struct mousedata *mdp;
1096 int bx, by, i, line_width, xend, xoff, yend, yoff;
1098 mdp = (scp->font_size < 14) ? &mouse9x13 : &mouse10x16;
1101 * It is only necessary to remove the mouse image where it overlaps
1102 * the border. Determine the overlap, and do nothing if it is empty.
1104 bx = (scp->xoff + scp->xsize) * 8;
1105 by = (scp->yoff + scp->ysize) * scp->font_size;
1106 xend = imin(x + mdp->md_width, scp->xpixel);
1107 yend = imin(y + mdp->md_height, scp->ypixel);
1108 if (xend <= bx && yend <= by)
1111 /* Repaint the non-empty overlap. */
1112 line_width = scp->sc->adp->va_line_width;
1113 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1114 outw(GDCIDX, 0x0003); /* data rotate/function select */
1115 outw(GDCIDX, 0x0f01); /* set/reset enable */
1116 outw(GDCIDX, 0xff08); /* bit mask */
1117 outw(GDCIDX, (scp->border << 8) | 0x00); /* set/reset */
1118 for (i = x / 8, xoff = i * 8; xoff < xend; ++i, xoff += 8) {
1119 yoff = (xoff >= bx) ? y : by;
1120 p = scp->sc->adp->va_window + yoff * line_width + i;
1121 for (; yoff < yend; ++yoff, p += line_width)
1124 outw(GDCIDX, 0x0000); /* set/reset */
1125 outw(GDCIDX, 0x0001); /* set/reset enable */
1129 vga_pxlmouse_direct(scr_stat *scp, int x, int y, int on)
1131 const struct mousedata *mdp;
1133 int line_width, pixel_size;
1135 static int x_old = 0, xend_old = 0;
1136 static int y_old = 0, yend_old = 0;
1143 mdp = (scp->font_size < 14) ? &mouse9x13 : &mouse10x16;
1146 * Determine overlap with the border and then if removing, do nothing
1147 * if the overlap is empty.
1149 xend = imin(x + mdp->md_width, scp->xpixel);
1150 yend = imin(y + mdp->md_height, scp->ypixel);
1151 if (!on && xend <= (scp->xoff + scp->xsize) * 8 &&
1152 yend <= (scp->yoff + scp->ysize) * scp->font_size)
1155 bpp = scp->sc->adp->va_info.vi_depth;
1157 if ((bpp == 16) && (scp->sc->adp->va_info.vi_pixel_fsizes[1] == 5))
1160 line_width = scp->sc->adp->va_line_width;
1161 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1167 * Repaint overlap with the border and nearby. Unlike in the planar
1168 * case, we kept track of everything under the cursor so can restore
1169 * it all, but we don't completely trust the saved state to be still
1170 * relevant, so do nothing if it is obviously stale.
1172 if (x != x_old || y != y_old || xend != xend_old || yend != yend_old)
1175 p = scp->sc->adp->va_window + y_old * line_width + x_old * pixel_size;
1177 for (i = 0; i < (yend_old - y_old); i++) {
1178 for (j = (xend_old - x_old - 1); j >= 0; j--) {
1181 u32 = (uint32_t*)(p + j * pixel_size);
1182 writel(u32, mouse_buf32[i * 16 + j]);
1187 u16 = (uint16_t*)(p + j * pixel_size);
1188 writew(u16, mouse_buf16[i * 16 + j]);
1191 u8 = (uint8_t*)(p + j * pixel_size);
1192 writeb(u8, mouse_buf8[i * 16 + j]);
1202 p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1204 for (i = 0; i < (yend - y); i++) {
1205 for (j = (xend - x - 1); j >= 0; j--) {
1208 u32 = (uint32_t*)(p + j * pixel_size);
1209 mouse_buf32[i * 16 + j] = *u32;
1210 if (mdp->md_interior[i] & (1 << (15 - j)))
1211 writel(u32, vga_palette32[15]);
1212 else if (mdp->md_border[i] & (1 << (15 - j)))
1216 u16 = (uint16_t*)(p + j * pixel_size);
1217 mouse_buf16[i * 16 + j] = *u16;
1218 if (mdp->md_interior[i] & (1 << (15 - j)))
1219 writew(u16, vga_palette16[15]);
1220 else if (mdp->md_border[i] & (1 << (15 - j)))
1224 u16 = (uint16_t*)(p + j * pixel_size);
1225 mouse_buf16[i * 16 + j] = *u16;
1226 if (mdp->md_interior[i] & (1 << (15 - j)))
1227 writew(u16, vga_palette15[15]);
1228 else if (mdp->md_border[i] & (1 << (15 - j)))
1232 u8 = (uint8_t*)(p + j * pixel_size);
1233 mouse_buf8[i * 16 + j] = *u8;
1234 if (mdp->md_interior[i] & (1 << (15 - j)))
1236 else if (mdp->md_border[i] & (1 << (15 - j)))
1252 vga_pxlmouse_planar(scr_stat *scp, int x, int y, int on)
1255 draw_pxlmouse_planar(scp, x, y);
1257 remove_pxlmouse_planar(scp, x, y);
1260 #endif /* SC_NO_CUTPASTE */
1261 #endif /* SC_PIXEL_MODE */
1263 #ifndef SC_NO_MODE_CHANGE
1265 /* graphics mode renderer */
1268 vga_grborder(scr_stat *scp, int color)
1270 vidd_set_border(scp->sc->adp, color);