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)
162 static u_short mouse_and_mask[16] = {
163 0xc000, 0xe000, 0xf000, 0xf800, 0xfc00, 0xfe00, 0xff00, 0xff80,
164 0xfe00, 0x1e00, 0x1f00, 0x0f00, 0x0f00, 0x0000, 0x0000, 0x0000
166 static u_short mouse_or_mask[16] = {
167 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7c00, 0x7e00, 0x6800,
168 0x0c00, 0x0c00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000
174 #define GET_PIXEL(scp, pos, x, w) \
176 (scp)->sc->adp->va_window + \
177 (x) * (scp)->xoff + \
178 (scp)->yoff * (scp)->font_size * (w) + \
179 (x) * ((pos) % (scp)->xsize) + \
180 (scp)->font_size * (w) * ((pos) / (scp)->xsize); \
183 #define DRAW_PIXEL(scp, pos, color) do { \
184 switch ((scp)->sc->adp->va_info.vi_depth) { \
186 writel((pos), vga_palette32[color]); \
189 if (((pos) & 1) == 0) { \
190 writew((pos), vga_palette32[color]); \
191 writeb((pos) + 2, vga_palette32[color] >> 16); \
193 writeb((pos), vga_palette32[color]); \
194 writew((pos) + 1, vga_palette32[color] >> 8); \
198 if ((scp)->sc->adp->va_info.vi_pixel_fsizes[1] == 5) \
199 writew((pos), vga_palette15[color]); \
201 writew((pos), vga_palette16[color]); \
204 writew((pos), vga_palette15[color]); \
207 writeb((pos), (uint8_t)(color)); \
211 static uint32_t vga_palette32[16] = {
212 0x000000, 0x0000ad, 0x00ad00, 0x00adad,
213 0xad0000, 0xad00ad, 0xad5200, 0xadadad,
214 0x525252, 0x5252ff, 0x52ff52, 0x52ffff,
215 0xff5252, 0xff52ff, 0xffff52, 0xffffff
218 static uint16_t vga_palette16[16] = {
219 0x0000, 0x0016, 0x0560, 0x0576, 0xb000, 0xb016, 0xb2a0, 0xb576,
220 0x52aa, 0x52bf, 0x57ea, 0x57ff, 0xfaaa, 0xfabf, 0xffea, 0xffff
223 static uint16_t vga_palette15[16] = {
224 0x0000, 0x0016, 0x02c0, 0x02d6, 0x5800, 0x5816, 0x5940, 0x5ad6,
225 0x294a, 0x295f, 0x2bea, 0x2bff, 0x7d4a, 0x7d5f, 0x7fea, 0x7fff
228 #ifndef SC_NO_CUTPASTE
229 static uint32_t mouse_buf32[256];
230 static uint16_t mouse_buf16[256];
231 static uint8_t mouse_buf8[256];
236 vga_nop(scr_stat *scp)
240 /* text mode renderer */
243 vga_txtclear(scr_stat *scp, int c, int attr)
245 sc_vtb_clear(&scp->scr, c, attr);
249 vga_txtborder(scr_stat *scp, int color)
251 vidd_set_border(scp->sc->adp, color);
255 vga_txtdraw(scr_stat *scp, int from, int count, int flip)
261 if (from + count > scp->xsize*scp->ysize)
262 count = scp->xsize*scp->ysize - from;
265 for (p = sc_vtb_pointer(&scp->scr, from); count-- > 0; ++from) {
266 c = sc_vtb_getc(&scp->vtb, from);
267 a = sc_vtb_geta(&scp->vtb, from);
268 a = (a & 0x8800) | ((a & 0x7000) >> 4)
269 | ((a & 0x0700) << 4);
270 p = sc_vtb_putchar(&scp->scr, p, c, a);
273 sc_vtb_copy(&scp->vtb, from, &scp->scr, from, count);
278 vga_txtcursor_shape(scr_stat *scp, int base, int height, int blink)
280 if (base < 0 || base >= scp->font_size)
282 /* the caller may set height <= 0 in order to disable the cursor */
284 scp->curs_attr.base = base;
285 scp->curs_attr.height = height;
287 vidd_set_hw_cursor_shape(scp->sc->adp, base, height,
288 scp->font_size, blink);
292 draw_txtcharcursor(scr_stat *scp, int at, u_short c, u_short a, int flip)
297 scp->cursor_saveunder_char = c;
298 scp->cursor_saveunder_attr = a;
300 #ifndef SC_NO_FONT_LOADING
301 if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
306 if (scp->font_size < 14) {
309 } else if (scp->font_size >= 16) {
316 if (scp->curs_attr.base >= h)
320 | ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
321 bcopy(font + c*h, font + sc->cursor_char*h, h);
322 font = font + sc->cursor_char*h;
323 for (i = imax(h - scp->curs_attr.base - scp->curs_attr.height, 0);
324 i < h - scp->curs_attr.base; ++i) {
328 vidd_load_font(sc->adp, 0, h, 8, font, sc->cursor_char, 1);
329 sc_vtb_putc(&scp->scr, at, sc->cursor_char, a);
331 #endif /* SC_NO_FONT_LOADING */
333 if ((a & 0x7000) == 0x7000) {
335 if ((a & 0x0700) == 0)
339 if ((a & 0x0700) == 0x0700)
344 | ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
345 sc_vtb_putc(&scp->scr, at, c, a);
350 vga_txtcursor(scr_stat *scp, int at, int blink, int on, int flip)
352 video_adapter_t *adp;
355 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
360 scp->status |= VR_CURSOR_BLINK;
362 scp->status |= VR_CURSOR_ON;
363 vidd_set_hw_cursor(adp, at%scp->xsize,
366 if (scp->status & VR_CURSOR_ON)
367 vidd_set_hw_cursor(adp, -1, -1);
368 scp->status &= ~VR_CURSOR_ON;
371 scp->status &= ~VR_CURSOR_BLINK;
373 scp->status |= VR_CURSOR_ON;
374 draw_txtcharcursor(scp, at,
375 sc_vtb_getc(&scp->scr, at),
376 sc_vtb_geta(&scp->scr, at),
379 cursor_attr = scp->cursor_saveunder_attr;
381 cursor_attr = (cursor_attr & 0x8800)
382 | ((cursor_attr & 0x7000) >> 4)
383 | ((cursor_attr & 0x0700) << 4);
384 if (scp->status & VR_CURSOR_ON)
385 sc_vtb_putc(&scp->scr, at,
386 scp->cursor_saveunder_char,
388 scp->status &= ~VR_CURSOR_ON;
394 vga_txtblink(scr_stat *scp, int at, int flip)
398 int sc_txtmouse_no_retrace_wait;
400 #ifndef SC_NO_CUTPASTE
403 draw_txtmouse(scr_stat *scp, int x, int y)
405 #ifndef SC_ALT_MOUSE_IMAGE
406 if (ISMOUSEAVAIL(scp->sc->adp->va_flags)) {
407 u_char font_buf[128];
411 int xoffset, yoffset;
415 /* prepare mousepointer char's bitmaps */
416 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
417 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos)*scp->font_size,
418 &font_buf[0], scp->font_size);
419 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos + 1)*scp->font_size,
420 &font_buf[32], scp->font_size);
422 + sc_vtb_getc(&scp->scr, pos + scp->xsize)*scp->font_size,
423 &font_buf[64], scp->font_size);
425 + sc_vtb_getc(&scp->scr, pos + scp->xsize + 1)*scp->font_size,
426 &font_buf[96], scp->font_size);
427 for (i = 0; i < scp->font_size; ++i) {
428 cursor[i] = font_buf[i]<<8 | font_buf[i+32];
429 cursor[i + scp->font_size] = font_buf[i+64]<<8 | font_buf[i+96];
432 /* now and-or in the mousepointer image */
434 yoffset = y%scp->font_size;
435 for (i = 0; i < 16; ++i) {
436 cursor[i + yoffset] =
437 (cursor[i + yoffset] & ~(mouse_and_mask[i] >> xoffset))
438 | (mouse_or_mask[i] >> xoffset);
440 for (i = 0; i < scp->font_size; ++i) {
441 font_buf[i] = (cursor[i] & 0xff00) >> 8;
442 font_buf[i + 32] = cursor[i] & 0xff;
443 font_buf[i + 64] = (cursor[i + scp->font_size] & 0xff00) >> 8;
444 font_buf[i + 96] = cursor[i + scp->font_size] & 0xff;
448 /* wait for vertical retrace to avoid jitter on some videocards */
449 crtc_addr = scp->sc->adp->va_crtc_addr;
450 while (!sc_txtmouse_no_retrace_wait &&
451 !(inb(crtc_addr + 6) & 0x08))
454 c = scp->sc->mouse_char;
455 vidd_load_font(scp->sc->adp, 0, 32, 8, font_buf, c, 4);
457 sc_vtb_putc(&scp->scr, pos, c, sc_vtb_geta(&scp->scr, pos));
458 /* FIXME: may be out of range! */
459 sc_vtb_putc(&scp->scr, pos + scp->xsize, c + 2,
460 sc_vtb_geta(&scp->scr, pos + scp->xsize));
461 if (x < (scp->xsize - 1)*8) {
462 sc_vtb_putc(&scp->scr, pos + 1, c + 1,
463 sc_vtb_geta(&scp->scr, pos + 1));
464 sc_vtb_putc(&scp->scr, pos + scp->xsize + 1, c + 3,
465 sc_vtb_geta(&scp->scr, pos + scp->xsize + 1));
468 #endif /* SC_ALT_MOUSE_IMAGE */
470 /* Red, magenta and brown are mapped to green to to keep it readable */
471 static const int col_conv[16] = {
472 6, 6, 6, 6, 2, 2, 2, 6, 14, 14, 14, 14, 10, 10, 10, 14
478 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
479 a = sc_vtb_geta(&scp->scr, pos);
480 if (scp->sc->adp->va_flags & V_ADP_COLOR)
481 color = (col_conv[(a & 0xf000) >> 12] << 12)
482 | ((a & 0x0f00) | 0x0800);
484 color = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
485 sc_vtb_putc(&scp->scr, pos, sc_vtb_getc(&scp->scr, pos), color);
490 remove_txtmouse(scr_stat *scp, int x, int y)
495 vga_txtmouse(scr_stat *scp, int x, int y, int on)
498 draw_txtmouse(scp, x, y);
500 remove_txtmouse(scp, x, y);
503 #endif /* SC_NO_CUTPASTE */
507 /* pixel (raster text) mode renderer */
510 vga_rndrinit(scr_stat *scp)
512 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PLANAR) {
513 scp->rndr->clear = vga_pxlclear_planar;
514 scp->rndr->draw_border = vga_pxlborder_planar;
515 scp->rndr->draw = vga_vgadraw_planar;
516 scp->rndr->draw_cursor = vga_pxlcursor_planar;
517 scp->rndr->blink_cursor = vga_pxlblink_planar;
518 scp->rndr->draw_mouse = vga_pxlmouse_planar;
520 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT ||
521 scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PACKED) {
522 scp->rndr->clear = vga_pxlclear_direct;
523 scp->rndr->draw_border = vga_pxlborder_direct;
524 scp->rndr->draw = vga_vgadraw_direct;
525 scp->rndr->draw_cursor = vga_pxlcursor_direct;
526 scp->rndr->blink_cursor = vga_pxlblink_direct;
527 scp->rndr->draw_mouse = vga_pxlmouse_direct;
532 vga_pxlclear_direct(scr_stat *scp, int c, int attr)
540 line_width = scp->sc->adp->va_line_width;
541 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
542 lines = scp->ysize * scp->font_size;
543 p = scp->sc->adp->va_window +
544 line_width * scp->yoff * scp->font_size +
545 scp->xoff * 8 * pixel_size;
547 for (i = 0; i < lines; ++i) {
548 bzero_io((void *)p, scp->xsize * 8 * pixel_size);
554 vga_pxlclear_planar(scr_stat *scp, int c, int attr)
561 /* XXX: we are just filling the screen with the background color... */
562 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
563 outw(GDCIDX, 0x0003); /* data rotate/function select */
564 outw(GDCIDX, 0x0f01); /* set/reset enable */
565 outw(GDCIDX, 0xff08); /* bit mask */
566 outw(GDCIDX, ((attr & 0xf000) >> 4) | 0x00); /* set/reset */
567 line_width = scp->sc->adp->va_line_width;
568 lines = scp->ysize*scp->font_size;
569 p = scp->sc->adp->va_window + line_width*scp->yoff*scp->font_size
571 for (i = 0; i < lines; ++i) {
572 bzero_io((void *)p, scp->xsize);
575 outw(GDCIDX, 0x0000); /* set/reset */
576 outw(GDCIDX, 0x0001); /* set/reset enable */
580 vga_pxlborder_direct(scr_stat *scp, int color)
591 line_width = scp->sc->adp->va_line_width;
592 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
595 s = scp->sc->adp->va_window;
596 e = s + line_width * scp->yoff * scp->font_size;
598 for (f = s; f < e; f += pixel_size)
599 DRAW_PIXEL(scp, f, color);
602 y = (scp->yoff + scp->ysize) * scp->font_size;
604 if (scp->ypixel > y) {
605 s = scp->sc->adp->va_window + line_width * y;
606 e = s + line_width * (scp->ypixel - y);
608 for (f = s; f < e; f += pixel_size)
609 DRAW_PIXEL(scp, f, color);
612 y = scp->yoff * scp->font_size;
613 x = scp->xpixel / 8 - scp->xoff - scp->xsize;
615 for (i = 0; i < scp->ysize * scp->font_size; ++i) {
617 s = scp->sc->adp->va_window + line_width * (y + i);
618 e = s + scp->xoff * 8 * pixel_size;
620 for (f = s; f < e; f += pixel_size)
621 DRAW_PIXEL(scp, f, color);
625 s = scp->sc->adp->va_window + line_width * (y + i) +
626 scp->xoff * 8 * pixel_size +
627 scp->xsize * 8 * pixel_size;
628 e = s + x * 8 * pixel_size;
630 for (f = s; f < e; f += pixel_size)
631 DRAW_PIXEL(scp, f, color);
637 vga_pxlborder_planar(scr_stat *scp, int color)
645 vidd_set_border(scp->sc->adp, color);
647 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
648 outw(GDCIDX, 0x0003); /* data rotate/function select */
649 outw(GDCIDX, 0x0f01); /* set/reset enable */
650 outw(GDCIDX, 0xff08); /* bit mask */
651 outw(GDCIDX, (color << 8) | 0x00); /* set/reset */
652 line_width = scp->sc->adp->va_line_width;
653 p = scp->sc->adp->va_window;
655 bzero_io((void *)p, line_width*scp->yoff*scp->font_size);
656 y = (scp->yoff + scp->ysize)*scp->font_size;
658 bzero_io((void *)(p + line_width*y), line_width*(scp->ypixel - y));
659 y = scp->yoff*scp->font_size;
660 x = scp->xpixel/8 - scp->xoff - scp->xsize;
661 for (i = 0; i < scp->ysize*scp->font_size; ++i) {
663 bzero_io((void *)(p + line_width*(y + i)), scp->xoff);
665 bzero_io((void *)(p + line_width*(y + i)
666 + scp->xoff + scp->xsize), x);
668 outw(GDCIDX, 0x0000); /* set/reset */
669 outw(GDCIDX, 0x0001); /* set/reset enable */
673 vga_egadraw(scr_stat *scp, int from, int count, int flip)
685 line_width = scp->sc->adp->va_line_width;
687 d = GET_PIXEL(scp, from, 1, line_width);
689 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
690 outw(GDCIDX, 0x0003); /* data rotate/function select */
691 outw(GDCIDX, 0x0f01); /* set/reset enable */
693 if (from + count > scp->xsize*scp->ysize)
694 count = scp->xsize*scp->ysize - from;
695 for (i = from; count-- > 0; ++i) {
696 a = sc_vtb_geta(&scp->vtb, i);
698 col1 = ((a & 0x7000) >> 4) | (a & 0x0800);
699 col2 = ((a & 0x8000) >> 4) | (a & 0x0700);
702 col2 = (a & 0xf000) >> 4;
704 /* set background color in EGA/VGA latch */
707 outw(GDCIDX, bg | 0x00); /* set/reset */
708 outw(GDCIDX, 0xff08); /* bit mask */
710 c = readb(d); /* set bg color in the latch */
712 /* foreground color */
713 outw(GDCIDX, col1 | 0x00); /* set/reset */
715 f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
716 for (j = 0; j < scp->font_size; ++j, ++f) {
717 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
722 if ((i % scp->xsize) == scp->xsize - 1)
723 d += scp->font_size * line_width - scp->xsize;
725 outw(GDCIDX, 0x0000); /* set/reset */
726 outw(GDCIDX, 0x0001); /* set/reset enable */
727 outw(GDCIDX, 0xff08); /* bit mask */
731 vga_vgadraw_direct(scr_stat *scp, int from, int count, int flip)
736 u_short col1, col2, color;
737 int line_width, pixel_size;
741 line_width = scp->sc->adp->va_line_width;
742 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
744 d = GET_PIXEL(scp, from, 8 * pixel_size, line_width);
746 if (from + count > scp->xsize * scp->ysize)
747 count = scp->xsize * scp->ysize - from;
749 for (i = from; count-- > 0; ++i) {
750 a = sc_vtb_geta(&scp->vtb, i);
753 col1 = (((a & 0x7000) >> 4) | (a & 0x0800)) >> 8;
754 col2 = (((a & 0x8000) >> 4) | (a & 0x0700)) >> 8;
756 col1 = (a & 0x0f00) >> 8;
757 col2 = (a & 0xf000) >> 12;
761 f = &(scp->font[sc_vtb_getc(&scp->vtb, i) * scp->font_size]);
763 for (j = 0; j < scp->font_size; ++j, ++f) {
764 for (k = 0; k < 8; ++k) {
765 color = *f & (1 << (7 - k)) ? col1 : col2;
766 DRAW_PIXEL(scp, e + pixel_size * k, color);
774 if ((i % scp->xsize) == scp->xsize - 1)
775 d += scp->font_size * line_width -
776 scp->xsize * 8 * pixel_size;
781 vga_vgadraw_planar(scr_stat *scp, int from, int count, int flip)
793 line_width = scp->sc->adp->va_line_width;
795 d = GET_PIXEL(scp, from, 1, line_width);
797 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
798 outw(GDCIDX, 0x0003); /* data rotate/function select */
799 outw(GDCIDX, 0x0f01); /* set/reset enable */
800 outw(GDCIDX, 0xff08); /* bit mask */
802 if (from + count > scp->xsize*scp->ysize)
803 count = scp->xsize*scp->ysize - from;
804 for (i = from; count-- > 0; ++i) {
805 a = sc_vtb_geta(&scp->vtb, i);
807 col1 = ((a & 0x7000) >> 4) | (a & 0x0800);
808 col2 = ((a & 0x8000) >> 4) | (a & 0x0700);
811 col2 = (a & 0xf000) >> 4;
813 /* set background color in EGA/VGA latch */
816 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
817 outw(GDCIDX, bg | 0x00); /* set/reset */
819 c = readb(d); /* set bg color in the latch */
820 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
822 /* foreground color */
823 outw(GDCIDX, col1 | 0x00); /* set/reset */
825 f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
826 for (j = 0; j < scp->font_size; ++j, ++f) {
831 if ((i % scp->xsize) == scp->xsize - 1)
832 d += scp->font_size * line_width - scp->xsize;
834 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
835 outw(GDCIDX, 0x0000); /* set/reset */
836 outw(GDCIDX, 0x0001); /* set/reset enable */
840 vga_pxlcursor_shape(scr_stat *scp, int base, int height, int blink)
842 if (base < 0 || base >= scp->font_size)
844 /* the caller may set height <= 0 in order to disable the cursor */
846 scp->curs_attr.base = base;
847 scp->curs_attr.height = height;
852 draw_pxlcursor_direct(scr_stat *scp, int at, int on, int flip)
856 int line_width, pixel_size;
858 int col1, col2, color;
862 line_width = scp->sc->adp->va_line_width;
863 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
865 d = GET_PIXEL(scp, at, 8 * pixel_size, line_width) +
866 (scp->font_size - scp->curs_attr.base - 1) * line_width;
868 a = sc_vtb_geta(&scp->vtb, at);
871 col1 = ((on) ? (a & 0x0f00) : ((a & 0xf000) >> 4)) >> 8;
872 col2 = ((on) ? ((a & 0xf000) >> 4) : (a & 0x0f00)) >> 8;
874 col1 = ((on) ? ((a & 0xf000) >> 4) : (a & 0x0f00)) >> 8;
875 col2 = ((on) ? (a & 0x0f00) : ((a & 0xf000) >> 4)) >> 8;
878 f = &(scp->font[sc_vtb_getc(&scp->vtb, at) * scp->font_size +
879 scp->font_size - scp->curs_attr.base - 1]);
881 height = imin(scp->curs_attr.height, scp->font_size);
883 for (i = 0; i < height; ++i, --f) {
884 for (j = 0; j < 8; ++j) {
885 color = *f & (1 << (7 - j)) ? col1 : col2;
886 DRAW_PIXEL(scp, d + pixel_size * j, color);
894 draw_pxlcursor_planar(scr_stat *scp, int at, int on, int flip)
905 line_width = scp->sc->adp->va_line_width;
907 d = GET_PIXEL(scp, at, 1, line_width) +
908 (scp->font_size - scp->curs_attr.base - 1) * line_width;
910 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
911 outw(GDCIDX, 0x0003); /* data rotate/function select */
912 outw(GDCIDX, 0x0f01); /* set/reset enable */
913 /* set background color in EGA/VGA latch */
914 a = sc_vtb_geta(&scp->vtb, at);
916 col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
918 col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
919 outw(GDCIDX, col | 0x00); /* set/reset */
920 outw(GDCIDX, 0xff08); /* bit mask */
922 c = readb(d); /* set bg color in the latch */
923 /* foreground color */
925 col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
927 col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
928 outw(GDCIDX, col | 0x00); /* set/reset */
929 f = &(scp->font[sc_vtb_getc(&scp->vtb, at)*scp->font_size
930 + scp->font_size - scp->curs_attr.base - 1]);
931 height = imin(scp->curs_attr.height, scp->font_size);
932 for (i = 0; i < height; ++i, --f) {
933 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
937 outw(GDCIDX, 0x0000); /* set/reset */
938 outw(GDCIDX, 0x0001); /* set/reset enable */
939 outw(GDCIDX, 0xff08); /* bit mask */
942 static int pxlblinkrate = 0;
945 vga_pxlcursor_direct(scr_stat *scp, int at, int blink, int on, int flip)
947 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
952 scp->status |= VR_CURSOR_ON;
953 draw_pxlcursor_direct(scp, at, on, flip);
954 } else if (++pxlblinkrate & 4) {
956 scp->status ^= VR_CURSOR_ON;
957 draw_pxlcursor_direct(scp, at,
958 scp->status & VR_CURSOR_ON,
962 if (scp->status & VR_CURSOR_ON)
963 draw_pxlcursor_direct(scp, at, on, flip);
964 scp->status &= ~VR_CURSOR_ON;
967 scp->status |= VR_CURSOR_BLINK;
969 scp->status &= ~VR_CURSOR_BLINK;
973 vga_pxlcursor_planar(scr_stat *scp, int at, int blink, int on, int flip)
975 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
980 scp->status |= VR_CURSOR_ON;
981 draw_pxlcursor_planar(scp, at, on, flip);
982 } else if (++pxlblinkrate & 4) {
984 scp->status ^= VR_CURSOR_ON;
985 draw_pxlcursor_planar(scp, at,
986 scp->status & VR_CURSOR_ON,
990 if (scp->status & VR_CURSOR_ON)
991 draw_pxlcursor_planar(scp, at, on, flip);
992 scp->status &= ~VR_CURSOR_ON;
995 scp->status |= VR_CURSOR_BLINK;
997 scp->status &= ~VR_CURSOR_BLINK;
1001 vga_pxlblink_direct(scr_stat *scp, int at, int flip)
1003 if (!(scp->status & VR_CURSOR_BLINK))
1005 if (!(++pxlblinkrate & 4))
1008 scp->status ^= VR_CURSOR_ON;
1009 draw_pxlcursor_direct(scp, at, scp->status & VR_CURSOR_ON, flip);
1013 vga_pxlblink_planar(scr_stat *scp, int at, int flip)
1015 if (!(scp->status & VR_CURSOR_BLINK))
1017 if (!(++pxlblinkrate & 4))
1020 scp->status ^= VR_CURSOR_ON;
1021 draw_pxlcursor_planar(scp, at, scp->status & VR_CURSOR_ON, flip);
1024 #ifndef SC_NO_CUTPASTE
1027 draw_pxlmouse_planar(scr_stat *scp, int x, int y)
1036 line_width = scp->sc->adp->va_line_width;
1037 xoff = (x - scp->xoff*8)%8;
1038 yoff = y - rounddown(y, line_width);
1039 ymax = imin(y + 16, scp->ypixel);
1041 outw(GDCIDX, 0x0805); /* read mode 1, write mode 0 */
1042 outw(GDCIDX, 0x0001); /* set/reset enable */
1043 outw(GDCIDX, 0x0002); /* color compare */
1044 outw(GDCIDX, 0x0007); /* color don't care */
1045 outw(GDCIDX, 0xff08); /* bit mask */
1046 outw(GDCIDX, 0x0803); /* data rotate/function select (and) */
1047 p = scp->sc->adp->va_window + line_width*y + x/8;
1048 if (x < scp->xpixel - 8) {
1049 for (i = y, j = 0; i < ymax; ++i, ++j) {
1050 m = ~(mouse_and_mask[j] >> xoff);
1051 #if defined(__i386__) || defined(__amd64__)
1052 *(u_char *)p &= m >> 8;
1053 *(u_char *)(p + 1) &= m;
1055 writeb(p, readb(p) & (m >> 8));
1056 writeb(p + 1, readb(p + 1) & (m >> 8));
1062 for (i = y, j = 0; i < ymax; ++i, ++j) {
1063 m = ~(mouse_and_mask[j] >> xoff);
1064 #if defined(__i386__) || defined(__amd64__)
1067 writeb(p, readb(p) & (m >> 8));
1072 outw(GDCIDX, 0x1003); /* data rotate/function select (or) */
1073 p = scp->sc->adp->va_window + line_width*y + x/8;
1074 if (x < scp->xpixel - 8) {
1075 for (i = y, j = 0; i < ymax; ++i, ++j) {
1076 m = mouse_or_mask[j] >> xoff;
1077 #if defined(__i386__) || defined(__amd64__)
1078 *(u_char *)p &= m >> 8;
1079 *(u_char *)(p + 1) &= m;
1081 writeb(p, readb(p) & (m >> 8));
1082 writeb(p + 1, readb(p + 1) & (m >> 8));
1087 for (i = y, j = 0; i < ymax; ++i, ++j) {
1088 m = mouse_or_mask[j] >> xoff;
1089 #if defined(__i386__) || defined(__amd64__)
1092 writeb(p, readb(p) & (m >> 8));
1097 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1098 outw(GDCIDX, 0x0003); /* data rotate/function select */
1102 remove_pxlmouse_planar(scr_stat *scp, int x, int y)
1111 /* erase the mouse cursor image */
1112 col = x/8 - scp->xoff;
1113 row = y/scp->font_size - scp->yoff;
1114 pos = row*scp->xsize + col;
1115 i = (col < scp->xsize - 1) ? 2 : 1;
1116 (*scp->rndr->draw)(scp, pos, i, FALSE);
1117 if (row < scp->ysize - 1)
1118 (*scp->rndr->draw)(scp, pos + scp->xsize, i, FALSE);
1120 /* paint border if necessary */
1121 line_width = scp->sc->adp->va_line_width;
1122 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1123 outw(GDCIDX, 0x0003); /* data rotate/function select */
1124 outw(GDCIDX, 0x0f01); /* set/reset enable */
1125 outw(GDCIDX, 0xff08); /* bit mask */
1126 outw(GDCIDX, (scp->border << 8) | 0x00); /* set/reset */
1127 if (row == scp->ysize - 1) {
1128 i = (scp->ysize + scp->yoff)*scp->font_size;
1129 ymax = imin(i + scp->font_size, scp->ypixel);
1130 p = scp->sc->adp->va_window + i*line_width + scp->xoff + col;
1131 if (col < scp->xsize - 1) {
1132 for (; i < ymax; ++i) {
1138 for (; i < ymax; ++i) {
1144 if ((col == scp->xsize - 1) && (scp->xoff > 0)) {
1145 i = (row + scp->yoff)*scp->font_size;
1146 ymax = imin(i + scp->font_size*2, scp->ypixel);
1147 p = scp->sc->adp->va_window + i*line_width
1148 + scp->xoff + scp->xsize;
1149 for (; i < ymax; ++i) {
1154 outw(GDCIDX, 0x0000); /* set/reset */
1155 outw(GDCIDX, 0x0001); /* set/reset enable */
1159 vga_pxlmouse_direct(scr_stat *scp, int x, int y, int on)
1162 int line_width, pixel_size;
1164 static int x_old = 0, xend_old = 0;
1165 static int y_old = 0, yend_old = 0;
1175 bpp = scp->sc->adp->va_info.vi_depth;
1177 if ((bpp == 16) && (scp->sc->adp->va_info.vi_pixel_fsizes[1] == 5))
1180 line_width = scp->sc->adp->va_line_width;
1181 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1183 xend = imin(x + 16, scp->xpixel);
1184 yend = imin(y + 16, scp->ypixel);
1186 p = scp->sc->adp->va_window + y_old * line_width + x_old * pixel_size;
1188 for (i = 0; i < (yend_old - y_old); i++) {
1189 for (j = (xend_old - x_old - 1); j >= 0; j--) {
1192 u32 = (uint32_t*)(p + j * pixel_size);
1193 writel(u32, mouse_buf32[i * 16 + j]);
1198 u16 = (uint16_t*)(p + j * pixel_size);
1199 writew(u16, mouse_buf16[i * 16 + j]);
1202 u8 = (uint8_t*)(p + j * pixel_size);
1203 writeb(u8, mouse_buf8[i * 16 + j]);
1211 p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1213 for (i = 0; i < (yend - y); i++) {
1214 for (j = (xend - x - 1); j >= 0; j--) {
1217 u32 = (uint32_t*)(p + j * pixel_size);
1218 mouse_buf32[i * 16 + j] = *u32;
1219 if (mouse_or_mask[i] & (1 << (15 - j)))
1220 writel(u32, vga_palette32[15]);
1221 else if (mouse_and_mask[i] & (1 << (15 - j)))
1225 u16 = (uint16_t*)(p + j * pixel_size);
1226 mouse_buf16[i * 16 + j] = *u16;
1227 if (mouse_or_mask[i] & (1 << (15 - j)))
1228 writew(u16, vga_palette16[15]);
1229 else if (mouse_and_mask[i] & (1 << (15 - j)))
1233 u16 = (uint16_t*)(p + j * pixel_size);
1234 mouse_buf16[i * 16 + j] = *u16;
1235 if (mouse_or_mask[i] & (1 << (15 - j)))
1236 writew(u16, vga_palette15[15]);
1237 else if (mouse_and_mask[i] & (1 << (15 - j)))
1241 u8 = (uint8_t*)(p + j * pixel_size);
1242 mouse_buf8[i * 16 + j] = *u8;
1243 if (mouse_or_mask[i] & (1 << (15 - j)))
1245 else if (mouse_and_mask[i] & (1 << (15 - j)))
1261 vga_pxlmouse_planar(scr_stat *scp, int x, int y, int on)
1264 draw_pxlmouse_planar(scp, x, y);
1266 remove_pxlmouse_planar(scp, x, y);
1269 #endif /* SC_NO_CUTPASTE */
1270 #endif /* SC_PIXEL_MODE */
1272 #ifndef SC_NO_MODE_CHANGE
1274 /* graphics mode renderer */
1277 vga_grborder(scr_stat *scp, int color)
1279 vidd_set_border(scp->sc->adp, color);