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_vgadraw_direct;
75 static vr_draw_t vga_vgadraw_planar;
76 static vr_set_cursor_t vga_pxlcursor_shape;
77 static vr_draw_cursor_t vga_pxlcursor_direct;
78 static vr_draw_cursor_t vga_pxlcursor_planar;
79 static vr_blink_cursor_t vga_pxlblink_direct;
80 static vr_blink_cursor_t vga_pxlblink_planar;
81 #ifndef SC_NO_CUTPASTE
82 static vr_draw_mouse_t vga_pxlmouse_direct;
83 static vr_draw_mouse_t vga_pxlmouse_planar;
85 #define vga_pxlmouse_direct (vr_draw_mouse_t *)vga_nop
86 #define vga_pxlmouse_planar (vr_draw_mouse_t *)vga_nop
88 #endif /* SC_PIXEL_MODE */
90 #ifndef SC_NO_MODE_CHANGE
91 static vr_draw_border_t vga_grborder;
94 static void vga_nop(scr_stat *scp);
96 static sc_rndr_sw_t txtrndrsw = {
104 (vr_set_mouse_t *)vga_nop,
107 RENDERER(mda, 0, txtrndrsw, vga_set);
108 RENDERER(cga, 0, txtrndrsw, vga_set);
109 RENDERER(ega, 0, txtrndrsw, vga_set);
110 RENDERER(vga, 0, txtrndrsw, vga_set);
113 static sc_rndr_sw_t vgarndrsw = {
115 (vr_clear_t *)vga_nop,
116 (vr_draw_border_t *)vga_nop,
117 (vr_draw_t *)vga_nop,
119 (vr_draw_cursor_t *)vga_nop,
120 (vr_blink_cursor_t *)vga_nop,
121 (vr_set_mouse_t *)vga_nop,
122 (vr_draw_mouse_t *)vga_nop,
124 RENDERER(ega, PIXEL_MODE, vgarndrsw, vga_set);
125 RENDERER(vga, PIXEL_MODE, vgarndrsw, vga_set);
126 #endif /* SC_PIXEL_MODE */
128 #ifndef SC_NO_MODE_CHANGE
129 static sc_rndr_sw_t grrndrsw = {
130 (vr_init_t *)vga_nop,
131 (vr_clear_t *)vga_nop,
133 (vr_draw_t *)vga_nop,
134 (vr_set_cursor_t *)vga_nop,
135 (vr_draw_cursor_t *)vga_nop,
136 (vr_blink_cursor_t *)vga_nop,
137 (vr_set_mouse_t *)vga_nop,
138 (vr_draw_mouse_t *)vga_nop,
140 RENDERER(cga, GRAPHICS_MODE, grrndrsw, vga_set);
141 RENDERER(ega, GRAPHICS_MODE, grrndrsw, vga_set);
142 RENDERER(vga, GRAPHICS_MODE, grrndrsw, vga_set);
143 #endif /* SC_NO_MODE_CHANGE */
145 RENDERER_MODULE(vga, vga_set);
147 #ifndef SC_NO_CUTPASTE
148 #if !defined(SC_ALT_MOUSE_IMAGE) || defined(SC_PIXEL_MODE)
150 u_short md_border[16];
151 u_short md_interior[16];
156 static const struct mousedata mouse9x13 = { {
157 0xc000, 0xa000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x9780,
158 0xf200, 0x1200, 0x1900, 0x0900, 0x0f00, 0x0000, 0x0000, 0x0000, }, {
159 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7c00, 0x7e00, 0x6800,
160 0x0c00, 0x0c00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000, },
164 static const struct mousedata mouse10x16 = { {
165 0xc000, 0xa000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8080,
166 0x8040, 0x83c0, 0x9200, 0xa900, 0xc900, 0x0480, 0x0480, 0x0300, }, {
167 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7c00, 0x7e00, 0x7f00,
168 0x7f80, 0x7c00, 0x6c00, 0x4600, 0x0600, 0x0300, 0x0300, 0x0000, },
175 #define GET_PIXEL(scp, pos, x, w) \
177 (scp)->sc->adp->va_window + \
178 (x) * (scp)->xoff + \
179 (scp)->yoff * (scp)->font_size * (w) + \
180 (x) * ((pos) % (scp)->xsize) + \
181 (scp)->font_size * (w) * ((pos) / (scp)->xsize); \
184 #define DRAW_PIXEL(scp, pos, color) do { \
185 switch ((scp)->sc->adp->va_info.vi_depth) { \
187 writel((pos), vga_palette32[color]); \
190 if (((pos) & 1) == 0) { \
191 writew((pos), vga_palette32[color]); \
192 writeb((pos) + 2, vga_palette32[color] >> 16); \
194 writeb((pos), vga_palette32[color]); \
195 writew((pos) + 1, vga_palette32[color] >> 8); \
199 if ((scp)->sc->adp->va_info.vi_pixel_fsizes[1] == 5) \
200 writew((pos), vga_palette15[color]); \
202 writew((pos), vga_palette16[color]); \
205 writew((pos), vga_palette15[color]); \
208 writeb((pos), (uint8_t)(color)); \
212 static uint32_t vga_palette32[16] = {
213 0x000000, 0x0000ad, 0x00ad00, 0x00adad,
214 0xad0000, 0xad00ad, 0xad5200, 0xadadad,
215 0x525252, 0x5252ff, 0x52ff52, 0x52ffff,
216 0xff5252, 0xff52ff, 0xffff52, 0xffffff
219 static uint16_t vga_palette16[16] = {
220 0x0000, 0x0016, 0x0560, 0x0576, 0xb000, 0xb016, 0xb2a0, 0xb576,
221 0x52aa, 0x52bf, 0x57ea, 0x57ff, 0xfaaa, 0xfabf, 0xffea, 0xffff
224 static uint16_t vga_palette15[16] = {
225 0x0000, 0x0016, 0x02c0, 0x02d6, 0x5800, 0x5816, 0x5940, 0x5ad6,
226 0x294a, 0x295f, 0x2bea, 0x2bff, 0x7d4a, 0x7d5f, 0x7fea, 0x7fff
231 vga_nop(scr_stat *scp)
235 /* text mode renderer */
238 vga_txtclear(scr_stat *scp, int c, int attr)
240 sc_vtb_clear(&scp->scr, c, attr);
244 vga_txtborder(scr_stat *scp, int color)
246 vidd_set_border(scp->sc->adp, color);
250 vga_txtdraw(scr_stat *scp, int from, int count, int flip)
256 if (from + count > scp->xsize*scp->ysize)
257 count = scp->xsize*scp->ysize - from;
260 for (p = sc_vtb_pointer(&scp->scr, from); count-- > 0; ++from) {
261 c = sc_vtb_getc(&scp->vtb, from);
262 a = sc_vtb_geta(&scp->vtb, from);
263 a = (a & 0x8800) | ((a & 0x7000) >> 4)
264 | ((a & 0x0700) << 4);
265 p = sc_vtb_putchar(&scp->scr, p, c, a);
268 sc_vtb_copy(&scp->vtb, from, &scp->scr, from, count);
273 vga_txtcursor_shape(scr_stat *scp, int base, int height, int blink)
275 if (base < 0 || base >= scp->font_size)
277 /* the caller may set height <= 0 in order to disable the cursor */
279 scp->curs_attr.base = base;
280 scp->curs_attr.height = height;
282 vidd_set_hw_cursor_shape(scp->sc->adp, base, height,
283 scp->font_size, blink);
287 draw_txtcharcursor(scr_stat *scp, int at, u_short c, u_short a, int flip)
293 #ifndef SC_NO_FONT_LOADING
294 if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
299 if (scp->font_size < 14) {
302 } else if (scp->font_size >= 16) {
309 if (scp->curs_attr.base >= h)
313 | ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
314 bcopy(font + c*h, font + sc->cursor_char*h, h);
315 font = font + sc->cursor_char*h;
316 for (i = imax(h - scp->curs_attr.base - scp->curs_attr.height, 0);
317 i < h - scp->curs_attr.base; ++i) {
321 vidd_load_font(sc->adp, 0, h, 8, font, sc->cursor_char, 1);
322 sc_vtb_putc(&scp->scr, at, sc->cursor_char, a);
324 #endif /* SC_NO_FONT_LOADING */
326 if ((a & 0x7000) == 0x7000) {
328 if ((a & 0x0700) == 0)
332 if ((a & 0x0700) == 0x0700)
337 | ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
338 sc_vtb_putc(&scp->scr, at, c, a);
343 vga_txtcursor(scr_stat *scp, int at, int blink, int on, int flip)
345 video_adapter_t *adp;
348 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
353 scp->status |= VR_CURSOR_BLINK;
355 scp->status |= VR_CURSOR_ON;
356 vidd_set_hw_cursor(adp, at%scp->xsize,
359 if (scp->status & VR_CURSOR_ON)
360 vidd_set_hw_cursor(adp, -1, -1);
361 scp->status &= ~VR_CURSOR_ON;
364 scp->status &= ~VR_CURSOR_BLINK;
366 scp->status |= VR_CURSOR_ON;
367 draw_txtcharcursor(scp, at,
368 sc_vtb_getc(&scp->vtb, at),
369 sc_vtb_geta(&scp->vtb, at),
372 cursor_attr = sc_vtb_geta(&scp->vtb, at);
374 cursor_attr = (cursor_attr & 0x8800)
375 | ((cursor_attr & 0x7000) >> 4)
376 | ((cursor_attr & 0x0700) << 4);
377 if (scp->status & VR_CURSOR_ON)
378 sc_vtb_putc(&scp->scr, at,
379 sc_vtb_getc(&scp->vtb, at),
381 scp->status &= ~VR_CURSOR_ON;
387 vga_txtblink(scr_stat *scp, int at, int flip)
391 int sc_txtmouse_no_retrace_wait;
393 #ifndef SC_NO_CUTPASTE
396 draw_txtmouse(scr_stat *scp, int x, int y)
398 #ifndef SC_ALT_MOUSE_IMAGE
399 if (ISMOUSEAVAIL(scp->sc->adp->va_flags)) {
400 const struct mousedata *mdp;
401 uint32_t border, interior;
402 u_char font_buf[128];
406 int xoffset, yoffset;
410 mdp = (scp->font_size < 14) ? &mouse9x13 : &mouse10x16;
412 /* prepare mousepointer char's bitmaps */
413 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
414 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos)*scp->font_size,
415 &font_buf[0], scp->font_size);
416 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos + 1)*scp->font_size,
417 &font_buf[32], scp->font_size);
419 + sc_vtb_getc(&scp->scr, pos + scp->xsize)*scp->font_size,
420 &font_buf[64], scp->font_size);
422 + sc_vtb_getc(&scp->scr, pos + scp->xsize + 1)*scp->font_size,
423 &font_buf[96], scp->font_size);
424 for (i = 0; i < scp->font_size; ++i) {
425 cursor[i] = font_buf[i]<<8 | font_buf[i+32];
426 cursor[i + scp->font_size] = font_buf[i+64]<<8 | font_buf[i+96];
429 /* now and-or in the mousepointer image */
431 yoffset = y%scp->font_size;
432 for (i = 0; i < 16; ++i) {
433 border = mdp->md_border[i] << 8; /* avoid right shifting out */
434 interior = mdp->md_interior[i] << 8;
435 border >>= xoffset; /* normalize */
436 interior >>= xoffset;
437 if (scp->sc->adp->va_flags & V_ADP_CWIDTH9) {
438 /* skip gaps between characters */
439 border = (border & 0xff0000) |
440 (border & 0x007f80) << 1 |
441 (border & 0x00003f) << 2;
442 interior = (interior & 0xff0000) |
443 (interior & 0x007f80) << 1 |
444 (interior & 0x00003f) << 2;
446 border >>= 8; /* back to normal position */
448 cursor[i + yoffset] = (cursor[i + yoffset] & ~border) |
451 for (i = 0; i < scp->font_size; ++i) {
452 font_buf[i] = (cursor[i] & 0xff00) >> 8;
453 font_buf[i + 32] = cursor[i] & 0xff;
454 font_buf[i + 64] = (cursor[i + scp->font_size] & 0xff00) >> 8;
455 font_buf[i + 96] = cursor[i + scp->font_size] & 0xff;
459 /* wait for vertical retrace to avoid jitter on some videocards */
460 crtc_addr = scp->sc->adp->va_crtc_addr;
461 while (!sc_txtmouse_no_retrace_wait &&
462 !(inb(crtc_addr + 6) & 0x08))
465 c = scp->sc->mouse_char;
466 vidd_load_font(scp->sc->adp, 0, 32, 8, font_buf, c, 4);
468 sc_vtb_putc(&scp->scr, pos, c, sc_vtb_geta(&scp->scr, pos));
469 /* FIXME: may be out of range! */
470 sc_vtb_putc(&scp->scr, pos + scp->xsize, c + 2,
471 sc_vtb_geta(&scp->scr, pos + scp->xsize));
472 if (x < (scp->xsize - 1)*8) {
473 sc_vtb_putc(&scp->scr, pos + 1, c + 1,
474 sc_vtb_geta(&scp->scr, pos + 1));
475 sc_vtb_putc(&scp->scr, pos + scp->xsize + 1, c + 3,
476 sc_vtb_geta(&scp->scr, pos + scp->xsize + 1));
479 #endif /* SC_ALT_MOUSE_IMAGE */
481 /* Red, magenta and brown are mapped to green to to keep it readable */
482 static const int col_conv[16] = {
483 6, 6, 6, 6, 2, 2, 2, 6, 14, 14, 14, 14, 10, 10, 10, 14
489 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
490 a = sc_vtb_geta(&scp->scr, pos);
491 if (scp->sc->adp->va_flags & V_ADP_COLOR)
492 color = (col_conv[(a & 0xf000) >> 12] << 12)
493 | ((a & 0x0f00) | 0x0800);
495 color = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
496 sc_vtb_putc(&scp->scr, pos, sc_vtb_getc(&scp->scr, pos), color);
501 remove_txtmouse(scr_stat *scp, int x, int y)
506 vga_txtmouse(scr_stat *scp, int x, int y, int on)
509 draw_txtmouse(scp, x, y);
511 remove_txtmouse(scp, x, y);
514 #endif /* SC_NO_CUTPASTE */
518 /* pixel (raster text) mode renderer */
521 vga_rndrinit(scr_stat *scp)
523 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PLANAR) {
524 scp->rndr->clear = vga_pxlclear_planar;
525 scp->rndr->draw_border = vga_pxlborder_planar;
526 scp->rndr->draw = vga_vgadraw_planar;
527 scp->rndr->draw_cursor = vga_pxlcursor_planar;
528 scp->rndr->blink_cursor = vga_pxlblink_planar;
529 scp->rndr->draw_mouse = vga_pxlmouse_planar;
531 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT ||
532 scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PACKED) {
533 scp->rndr->clear = vga_pxlclear_direct;
534 scp->rndr->draw_border = vga_pxlborder_direct;
535 scp->rndr->draw = vga_vgadraw_direct;
536 scp->rndr->draw_cursor = vga_pxlcursor_direct;
537 scp->rndr->blink_cursor = vga_pxlblink_direct;
538 scp->rndr->draw_mouse = vga_pxlmouse_direct;
543 vga_pxlclear_direct(scr_stat *scp, int c, int attr)
551 line_width = scp->sc->adp->va_line_width;
552 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
553 lines = scp->ysize * scp->font_size;
554 p = scp->sc->adp->va_window +
555 line_width * scp->yoff * scp->font_size +
556 scp->xoff * 8 * pixel_size;
558 for (i = 0; i < lines; ++i) {
559 bzero_io((void *)p, scp->xsize * 8 * pixel_size);
565 vga_pxlclear_planar(scr_stat *scp, int c, int attr)
572 /* XXX: we are just filling the screen with the background color... */
573 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
574 outw(GDCIDX, 0x0003); /* data rotate/function select */
575 outw(GDCIDX, 0x0f01); /* set/reset enable */
576 outw(GDCIDX, 0xff08); /* bit mask */
577 outw(GDCIDX, ((attr & 0xf000) >> 4) | 0x00); /* set/reset */
578 line_width = scp->sc->adp->va_line_width;
579 lines = scp->ysize*scp->font_size;
580 p = scp->sc->adp->va_window + line_width*scp->yoff*scp->font_size
582 for (i = 0; i < lines; ++i) {
583 bzero_io((void *)p, scp->xsize);
586 outw(GDCIDX, 0x0000); /* set/reset */
587 outw(GDCIDX, 0x0001); /* set/reset enable */
591 vga_pxlborder_direct(scr_stat *scp, int color)
602 line_width = scp->sc->adp->va_line_width;
603 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
606 s = scp->sc->adp->va_window;
607 e = s + line_width * scp->yoff * scp->font_size;
609 for (f = s; f < e; f += pixel_size)
610 DRAW_PIXEL(scp, f, color);
613 y = (scp->yoff + scp->ysize) * scp->font_size;
615 if (scp->ypixel > y) {
616 s = scp->sc->adp->va_window + line_width * y;
617 e = s + line_width * (scp->ypixel - y);
619 for (f = s; f < e; f += pixel_size)
620 DRAW_PIXEL(scp, f, color);
623 y = scp->yoff * scp->font_size;
624 x = scp->xpixel / 8 - scp->xoff - scp->xsize;
626 for (i = 0; i < scp->ysize * scp->font_size; ++i) {
628 s = scp->sc->adp->va_window + line_width * (y + i);
629 e = s + scp->xoff * 8 * pixel_size;
631 for (f = s; f < e; f += pixel_size)
632 DRAW_PIXEL(scp, f, color);
636 s = scp->sc->adp->va_window + line_width * (y + i) +
637 scp->xoff * 8 * pixel_size +
638 scp->xsize * 8 * pixel_size;
639 e = s + x * 8 * pixel_size;
641 for (f = s; f < e; f += pixel_size)
642 DRAW_PIXEL(scp, f, color);
648 vga_pxlborder_planar(scr_stat *scp, int color)
656 vidd_set_border(scp->sc->adp, color);
658 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
659 outw(GDCIDX, 0x0003); /* data rotate/function select */
660 outw(GDCIDX, 0x0f01); /* set/reset enable */
661 outw(GDCIDX, 0xff08); /* bit mask */
662 outw(GDCIDX, (color << 8) | 0x00); /* set/reset */
663 line_width = scp->sc->adp->va_line_width;
664 p = scp->sc->adp->va_window;
666 bzero_io((void *)p, line_width*scp->yoff*scp->font_size);
667 y = (scp->yoff + scp->ysize)*scp->font_size;
669 bzero_io((void *)(p + line_width*y), line_width*(scp->ypixel - y));
670 y = scp->yoff*scp->font_size;
671 x = scp->xpixel/8 - scp->xoff - scp->xsize;
672 for (i = 0; i < scp->ysize*scp->font_size; ++i) {
674 bzero_io((void *)(p + line_width*(y + i)), scp->xoff);
676 bzero_io((void *)(p + line_width*(y + i)
677 + scp->xoff + scp->xsize), x);
679 outw(GDCIDX, 0x0000); /* set/reset */
680 outw(GDCIDX, 0x0001); /* set/reset enable */
684 vga_vgadraw_direct(scr_stat *scp, int from, int count, int flip)
689 u_short col1, col2, color;
690 int line_width, pixel_size;
694 line_width = scp->sc->adp->va_line_width;
695 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
697 d = GET_PIXEL(scp, from, 8 * pixel_size, line_width);
699 if (from + count > scp->xsize * scp->ysize)
700 count = scp->xsize * scp->ysize - from;
702 for (i = from; count-- > 0; ++i) {
703 a = sc_vtb_geta(&scp->vtb, i);
706 col1 = (((a & 0x7000) >> 4) | (a & 0x0800)) >> 8;
707 col2 = (((a & 0x8000) >> 4) | (a & 0x0700)) >> 8;
709 col1 = (a & 0x0f00) >> 8;
710 col2 = (a & 0xf000) >> 12;
714 f = &(scp->font[sc_vtb_getc(&scp->vtb, i) * scp->font_size]);
716 for (j = 0; j < scp->font_size; ++j, ++f) {
717 for (k = 0; k < 8; ++k) {
718 color = *f & (1 << (7 - k)) ? col1 : col2;
719 DRAW_PIXEL(scp, e + pixel_size * k, color);
727 if ((i % scp->xsize) == scp->xsize - 1)
728 d += scp->font_size * line_width -
729 scp->xsize * 8 * pixel_size;
734 vga_vgadraw_planar(scr_stat *scp, int from, int count, int flip)
746 line_width = scp->sc->adp->va_line_width;
748 d = GET_PIXEL(scp, from, 1, line_width);
750 if (scp->sc->adp->va_type == KD_VGA) {
751 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
752 outw(GDCIDX, 0xff08); /* bit mask */
754 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
755 outw(GDCIDX, 0x0003); /* data rotate/function select */
756 outw(GDCIDX, 0x0f01); /* set/reset enable */
758 if (from + count > scp->xsize*scp->ysize)
759 count = scp->xsize*scp->ysize - from;
760 for (i = from; count-- > 0; ++i) {
761 a = sc_vtb_geta(&scp->vtb, i);
763 col1 = ((a & 0x7000) >> 4) | (a & 0x0800);
764 col2 = ((a & 0x8000) >> 4) | (a & 0x0700);
767 col2 = (a & 0xf000) >> 4;
769 /* set background color in EGA/VGA latch */
773 outw(GDCIDX, bg | 0x00); /* set/reset */
774 if (scp->sc->adp->va_type != KD_VGA)
775 outw(GDCIDX, 0xff08); /* bit mask */
777 c = readb(d); /* set bg color in the latch */
779 /* foreground color */
782 outw(GDCIDX, col1 | 0x00); /* set/reset */
785 f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
786 for (j = 0; j < scp->font_size; ++j, ++f) {
787 if (scp->sc->adp->va_type == KD_VGA)
790 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
796 if ((i % scp->xsize) == scp->xsize - 1)
797 d += scp->font_size * line_width - scp->xsize;
799 if (scp->sc->adp->va_type == KD_VGA)
800 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
802 outw(GDCIDX, 0xff08); /* bit mask */
803 outw(GDCIDX, 0x0000); /* set/reset */
804 outw(GDCIDX, 0x0001); /* set/reset enable */
808 vga_pxlcursor_shape(scr_stat *scp, int base, int height, int blink)
810 if (base < 0 || base >= scp->font_size)
812 /* the caller may set height <= 0 in order to disable the cursor */
814 scp->curs_attr.base = base;
815 scp->curs_attr.height = height;
820 draw_pxlcursor_direct(scr_stat *scp, int at, int on, int flip)
824 int line_width, pixel_size;
826 int col1, col2, color;
830 line_width = scp->sc->adp->va_line_width;
831 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
833 d = GET_PIXEL(scp, at, 8 * pixel_size, line_width) +
834 (scp->font_size - scp->curs_attr.base - 1) * line_width;
836 a = sc_vtb_geta(&scp->vtb, at);
839 col1 = ((on) ? (a & 0x0f00) : ((a & 0xf000) >> 4)) >> 8;
840 col2 = ((on) ? ((a & 0xf000) >> 4) : (a & 0x0f00)) >> 8;
842 col1 = ((on) ? ((a & 0xf000) >> 4) : (a & 0x0f00)) >> 8;
843 col2 = ((on) ? (a & 0x0f00) : ((a & 0xf000) >> 4)) >> 8;
846 f = &(scp->font[sc_vtb_getc(&scp->vtb, at) * scp->font_size +
847 scp->font_size - scp->curs_attr.base - 1]);
849 height = imin(scp->curs_attr.height, scp->font_size);
851 for (i = 0; i < height; ++i, --f) {
852 for (j = 0; j < 8; ++j) {
853 color = *f & (1 << (7 - j)) ? col1 : col2;
854 DRAW_PIXEL(scp, d + pixel_size * j, color);
862 draw_pxlcursor_planar(scr_stat *scp, int at, int on, int flip)
873 line_width = scp->sc->adp->va_line_width;
875 d = GET_PIXEL(scp, at, 1, line_width) +
876 (scp->font_size - scp->curs_attr.base - 1) * line_width;
878 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
879 outw(GDCIDX, 0x0003); /* data rotate/function select */
880 outw(GDCIDX, 0x0f01); /* set/reset enable */
881 /* set background color in EGA/VGA latch */
882 a = sc_vtb_geta(&scp->vtb, at);
884 col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
886 col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
887 outw(GDCIDX, col | 0x00); /* set/reset */
888 outw(GDCIDX, 0xff08); /* bit mask */
890 c = readb(d); /* set bg color in the latch */
891 /* foreground color */
893 col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
895 col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
896 outw(GDCIDX, col | 0x00); /* set/reset */
897 f = &(scp->font[sc_vtb_getc(&scp->vtb, at)*scp->font_size
898 + scp->font_size - scp->curs_attr.base - 1]);
899 height = imin(scp->curs_attr.height, scp->font_size);
900 for (i = 0; i < height; ++i, --f) {
901 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
905 outw(GDCIDX, 0x0000); /* set/reset */
906 outw(GDCIDX, 0x0001); /* set/reset enable */
907 outw(GDCIDX, 0xff08); /* bit mask */
910 static int pxlblinkrate = 0;
913 vga_pxlcursor_direct(scr_stat *scp, int at, int blink, int on, int flip)
915 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
920 scp->status |= VR_CURSOR_ON;
921 draw_pxlcursor_direct(scp, at, on, flip);
922 } else if (++pxlblinkrate & 4) {
924 scp->status ^= VR_CURSOR_ON;
925 draw_pxlcursor_direct(scp, at,
926 scp->status & VR_CURSOR_ON,
930 if (scp->status & VR_CURSOR_ON)
931 draw_pxlcursor_direct(scp, at, on, flip);
932 scp->status &= ~VR_CURSOR_ON;
935 scp->status |= VR_CURSOR_BLINK;
937 scp->status &= ~VR_CURSOR_BLINK;
941 vga_pxlcursor_planar(scr_stat *scp, int at, int blink, int on, int flip)
943 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
948 scp->status |= VR_CURSOR_ON;
949 draw_pxlcursor_planar(scp, at, on, flip);
950 } else if (++pxlblinkrate & 4) {
952 scp->status ^= VR_CURSOR_ON;
953 draw_pxlcursor_planar(scp, at,
954 scp->status & VR_CURSOR_ON,
958 if (scp->status & VR_CURSOR_ON)
959 draw_pxlcursor_planar(scp, at, on, flip);
960 scp->status &= ~VR_CURSOR_ON;
963 scp->status |= VR_CURSOR_BLINK;
965 scp->status &= ~VR_CURSOR_BLINK;
969 vga_pxlblink_direct(scr_stat *scp, int at, int flip)
971 if (!(scp->status & VR_CURSOR_BLINK))
973 if (!(++pxlblinkrate & 4))
976 scp->status ^= VR_CURSOR_ON;
977 draw_pxlcursor_direct(scp, at, scp->status & VR_CURSOR_ON, flip);
981 vga_pxlblink_planar(scr_stat *scp, int at, int flip)
983 if (!(scp->status & VR_CURSOR_BLINK))
985 if (!(++pxlblinkrate & 4))
988 scp->status ^= VR_CURSOR_ON;
989 draw_pxlcursor_planar(scp, at, scp->status & VR_CURSOR_ON, flip);
992 #ifndef SC_NO_CUTPASTE
995 draw_pxlmouse_planar(scr_stat *scp, int x, int y)
997 const struct mousedata *mdp;
1006 mdp = (scp->font_size < 14) ? &mouse9x13 : &mouse10x16;
1007 line_width = scp->sc->adp->va_line_width;
1008 xoff = (x - scp->xoff*8)%8;
1009 yoff = y - rounddown(y, line_width);
1010 ymax = imin(y + mdp->md_height, scp->ypixel);
1012 if (scp->sc->adp->va_type == KD_VGA) {
1013 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
1014 outw(GDCIDX, 0xff08); /* bit mask */
1016 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1017 outw(GDCIDX, 0x0003); /* data rotate/function select */
1018 outw(GDCIDX, 0x0f01); /* set/reset enable */
1020 outw(GDCIDX, (0 << 8) | 0x00); /* set/reset */
1021 p = scp->sc->adp->va_window + line_width*y + x/8;
1022 for (i = y, j = 0; i < ymax; ++i, ++j) {
1023 m = mdp->md_border[j] << 8 >> xoff;
1024 for (k = 0; k < 3; ++k) {
1025 m1 = m >> (8 * (2 - k));
1026 if (m1 != 0 && x + 8 * k < scp->xpixel) {
1028 if (scp->sc->adp->va_type == KD_VGA)
1032 outw(GDCIDX, (m1 << 8) | 0x08);
1039 outw(GDCIDX, (15 << 8) | 0x00); /* set/reset */
1040 p = scp->sc->adp->va_window + line_width*y + x/8;
1041 for (i = y, j = 0; i < ymax; ++i, ++j) {
1042 m = mdp->md_interior[j] << 8 >> xoff;
1043 for (k = 0; k < 3; ++k) {
1044 m1 = m >> (8 * (2 - k));
1045 if (m1 != 0 && x + 8 * k < scp->xpixel) {
1047 if (scp->sc->adp->va_type == KD_VGA)
1051 outw(GDCIDX, (m1 << 8) | 0x08);
1058 if (scp->sc->adp->va_type == KD_VGA)
1059 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1061 outw(GDCIDX, 0xff08); /* bit mask */
1062 outw(GDCIDX, 0x0000); /* set/reset */
1063 outw(GDCIDX, 0x0001); /* set/reset enable */
1067 remove_pxlmouse_planar(scr_stat *scp, int x, int y)
1069 const struct mousedata *mdp;
1071 int bx, by, i, line_width, xend, xoff, yend, yoff;
1073 mdp = (scp->font_size < 14) ? &mouse9x13 : &mouse10x16;
1076 * It is only necessary to remove the mouse image where it overlaps
1077 * the border. Determine the overlap, and do nothing if it is empty.
1079 bx = (scp->xoff + scp->xsize) * 8;
1080 by = (scp->yoff + scp->ysize) * scp->font_size;
1081 xend = imin(x + mdp->md_width, scp->xpixel);
1082 yend = imin(y + mdp->md_height, scp->ypixel);
1083 if (xend <= bx && yend <= by)
1086 /* Repaint the non-empty overlap. */
1087 line_width = scp->sc->adp->va_line_width;
1088 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1089 outw(GDCIDX, 0x0003); /* data rotate/function select */
1090 outw(GDCIDX, 0x0f01); /* set/reset enable */
1091 outw(GDCIDX, 0xff08); /* bit mask */
1092 outw(GDCIDX, (scp->border << 8) | 0x00); /* set/reset */
1093 for (i = x / 8, xoff = i * 8; xoff < xend; ++i, xoff += 8) {
1094 yoff = (xoff >= bx) ? y : by;
1095 p = scp->sc->adp->va_window + yoff * line_width + i;
1096 for (; yoff < yend; ++yoff, p += line_width)
1099 outw(GDCIDX, 0x0000); /* set/reset */
1100 outw(GDCIDX, 0x0001); /* set/reset enable */
1104 vga_pxlmouse_direct(scr_stat *scp, int x, int y, int on)
1106 const struct mousedata *mdp;
1108 int line_width, pixel_size;
1112 mdp = (scp->font_size < 14) ? &mouse9x13 : &mouse10x16;
1115 * Determine overlap with the border and then if removing, do nothing
1116 * if the overlap is empty.
1118 xend = imin(x + mdp->md_width, scp->xpixel);
1119 yend = imin(y + mdp->md_height, scp->ypixel);
1120 if (!on && xend <= (scp->xoff + scp->xsize) * 8 &&
1121 yend <= (scp->yoff + scp->ysize) * scp->font_size)
1124 line_width = scp->sc->adp->va_line_width;
1125 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1130 /* Repaint overlap with the border (mess up the corner a little). */
1131 p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1132 for (i = 0; i < yend - y; i++, p += line_width)
1133 for (j = xend - x - 1; j >= 0; j--)
1134 DRAW_PIXEL(scp, p + j * pixel_size, scp->border);
1139 p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1140 for (i = 0; i < yend - y; i++, p += line_width)
1141 for (j = xend - x - 1; j >= 0; j--)
1142 if (mdp->md_interior[i] & (1 << (15 - j)))
1143 DRAW_PIXEL(scp, p + j * pixel_size, 15);
1144 else if (mdp->md_border[i] & (1 << (15 - j)))
1145 DRAW_PIXEL(scp, p + j * pixel_size, 0);
1149 vga_pxlmouse_planar(scr_stat *scp, int x, int y, int on)
1152 draw_pxlmouse_planar(scp, x, y);
1154 remove_pxlmouse_planar(scp, x, y);
1157 #endif /* SC_NO_CUTPASTE */
1158 #endif /* SC_PIXEL_MODE */
1160 #ifndef SC_NO_MODE_CHANGE
1162 /* graphics mode renderer */
1165 vga_grborder(scr_stat *scp, int color)
1167 vidd_set_border(scp->sc->adp, color);