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 #ifndef SC_NO_CUTPASTE
401 draw_txtmouse(scr_stat *scp, int x, int y)
403 #ifndef SC_ALT_MOUSE_IMAGE
404 if (ISMOUSEAVAIL(scp->sc->adp->va_flags)) {
405 u_char font_buf[128];
409 int xoffset, yoffset;
413 /* prepare mousepointer char's bitmaps */
414 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
415 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos)*scp->font_size,
416 &font_buf[0], scp->font_size);
417 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos + 1)*scp->font_size,
418 &font_buf[32], scp->font_size);
420 + sc_vtb_getc(&scp->scr, pos + scp->xsize)*scp->font_size,
421 &font_buf[64], scp->font_size);
423 + sc_vtb_getc(&scp->scr, pos + scp->xsize + 1)*scp->font_size,
424 &font_buf[96], scp->font_size);
425 for (i = 0; i < scp->font_size; ++i) {
426 cursor[i] = font_buf[i]<<8 | font_buf[i+32];
427 cursor[i + scp->font_size] = font_buf[i+64]<<8 | font_buf[i+96];
430 /* now and-or in the mousepointer image */
432 yoffset = y%scp->font_size;
433 for (i = 0; i < 16; ++i) {
434 cursor[i + yoffset] =
435 (cursor[i + yoffset] & ~(mouse_and_mask[i] >> xoffset))
436 | (mouse_or_mask[i] >> xoffset);
438 for (i = 0; i < scp->font_size; ++i) {
439 font_buf[i] = (cursor[i] & 0xff00) >> 8;
440 font_buf[i + 32] = cursor[i] & 0xff;
441 font_buf[i + 64] = (cursor[i + scp->font_size] & 0xff00) >> 8;
442 font_buf[i + 96] = cursor[i + scp->font_size] & 0xff;
446 /* wait for vertical retrace to avoid jitter on some videocards */
447 crtc_addr = scp->sc->adp->va_crtc_addr;
448 while (!(inb(crtc_addr + 6) & 0x08)) /* idle */ ;
450 c = scp->sc->mouse_char;
451 vidd_load_font(scp->sc->adp, 0, 32, 8, font_buf, c, 4);
453 sc_vtb_putc(&scp->scr, pos, c, sc_vtb_geta(&scp->scr, pos));
454 /* FIXME: may be out of range! */
455 sc_vtb_putc(&scp->scr, pos + scp->xsize, c + 2,
456 sc_vtb_geta(&scp->scr, pos + scp->xsize));
457 if (x < (scp->xsize - 1)*8) {
458 sc_vtb_putc(&scp->scr, pos + 1, c + 1,
459 sc_vtb_geta(&scp->scr, pos + 1));
460 sc_vtb_putc(&scp->scr, pos + scp->xsize + 1, c + 3,
461 sc_vtb_geta(&scp->scr, pos + scp->xsize + 1));
464 #endif /* SC_ALT_MOUSE_IMAGE */
466 /* Red, magenta and brown are mapped to green to to keep it readable */
467 static const int col_conv[16] = {
468 6, 6, 6, 6, 2, 2, 2, 6, 14, 14, 14, 14, 10, 10, 10, 14
474 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
475 a = sc_vtb_geta(&scp->scr, pos);
476 if (scp->sc->adp->va_flags & V_ADP_COLOR)
477 color = (col_conv[(a & 0xf000) >> 12] << 12)
478 | ((a & 0x0f00) | 0x0800);
480 color = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
481 sc_vtb_putc(&scp->scr, pos, sc_vtb_getc(&scp->scr, pos), color);
486 remove_txtmouse(scr_stat *scp, int x, int y)
491 vga_txtmouse(scr_stat *scp, int x, int y, int on)
494 draw_txtmouse(scp, x, y);
496 remove_txtmouse(scp, x, y);
499 #endif /* SC_NO_CUTPASTE */
503 /* pixel (raster text) mode renderer */
506 vga_rndrinit(scr_stat *scp)
508 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PLANAR) {
509 scp->rndr->clear = vga_pxlclear_planar;
510 scp->rndr->draw_border = vga_pxlborder_planar;
511 scp->rndr->draw = vga_vgadraw_planar;
512 scp->rndr->draw_cursor = vga_pxlcursor_planar;
513 scp->rndr->blink_cursor = vga_pxlblink_planar;
514 scp->rndr->draw_mouse = vga_pxlmouse_planar;
516 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT ||
517 scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PACKED) {
518 scp->rndr->clear = vga_pxlclear_direct;
519 scp->rndr->draw_border = vga_pxlborder_direct;
520 scp->rndr->draw = vga_vgadraw_direct;
521 scp->rndr->draw_cursor = vga_pxlcursor_direct;
522 scp->rndr->blink_cursor = vga_pxlblink_direct;
523 scp->rndr->draw_mouse = vga_pxlmouse_direct;
528 vga_pxlclear_direct(scr_stat *scp, int c, int attr)
536 line_width = scp->sc->adp->va_line_width;
537 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
538 lines = scp->ysize * scp->font_size;
539 p = scp->sc->adp->va_window +
540 line_width * scp->yoff * scp->font_size +
541 scp->xoff * 8 * pixel_size;
543 for (i = 0; i < lines; ++i) {
544 bzero_io((void *)p, scp->xsize * 8 * pixel_size);
550 vga_pxlclear_planar(scr_stat *scp, int c, int attr)
557 /* XXX: we are just filling the screen with the background color... */
558 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
559 outw(GDCIDX, 0x0003); /* data rotate/function select */
560 outw(GDCIDX, 0x0f01); /* set/reset enable */
561 outw(GDCIDX, 0xff08); /* bit mask */
562 outw(GDCIDX, ((attr & 0xf000) >> 4) | 0x00); /* set/reset */
563 line_width = scp->sc->adp->va_line_width;
564 lines = scp->ysize*scp->font_size;
565 p = scp->sc->adp->va_window + line_width*scp->yoff*scp->font_size
567 for (i = 0; i < lines; ++i) {
568 bzero_io((void *)p, scp->xsize);
571 outw(GDCIDX, 0x0000); /* set/reset */
572 outw(GDCIDX, 0x0001); /* set/reset enable */
576 vga_pxlborder_direct(scr_stat *scp, int color)
587 line_width = scp->sc->adp->va_line_width;
588 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
591 s = scp->sc->adp->va_window;
592 e = s + line_width * scp->yoff * scp->font_size;
594 for (f = s; f < e; f += pixel_size)
595 DRAW_PIXEL(scp, f, color);
598 y = (scp->yoff + scp->ysize) * scp->font_size;
600 if (scp->ypixel > y) {
601 s = scp->sc->adp->va_window + line_width * y;
602 e = s + line_width * (scp->ypixel - y);
604 for (f = s; f < e; f += pixel_size)
605 DRAW_PIXEL(scp, f, color);
608 y = scp->yoff * scp->font_size;
609 x = scp->xpixel / 8 - scp->xoff - scp->xsize;
611 for (i = 0; i < scp->ysize * scp->font_size; ++i) {
613 s = scp->sc->adp->va_window + line_width * (y + i);
614 e = s + scp->xoff * 8 * pixel_size;
616 for (f = s; f < e; f += pixel_size)
617 DRAW_PIXEL(scp, f, color);
621 s = scp->sc->adp->va_window + line_width * (y + i) +
622 scp->xoff * 8 * pixel_size +
623 scp->xsize * 8 * pixel_size;
624 e = s + x * 8 * pixel_size;
626 for (f = s; f < e; f += pixel_size)
627 DRAW_PIXEL(scp, f, color);
633 vga_pxlborder_planar(scr_stat *scp, int color)
641 vidd_set_border(scp->sc->adp, color);
643 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
644 outw(GDCIDX, 0x0003); /* data rotate/function select */
645 outw(GDCIDX, 0x0f01); /* set/reset enable */
646 outw(GDCIDX, 0xff08); /* bit mask */
647 outw(GDCIDX, (color << 8) | 0x00); /* set/reset */
648 line_width = scp->sc->adp->va_line_width;
649 p = scp->sc->adp->va_window;
651 bzero_io((void *)p, line_width*scp->yoff*scp->font_size);
652 y = (scp->yoff + scp->ysize)*scp->font_size;
654 bzero_io((void *)(p + line_width*y), line_width*(scp->ypixel - y));
655 y = scp->yoff*scp->font_size;
656 x = scp->xpixel/8 - scp->xoff - scp->xsize;
657 for (i = 0; i < scp->ysize*scp->font_size; ++i) {
659 bzero_io((void *)(p + line_width*(y + i)), scp->xoff);
661 bzero_io((void *)(p + line_width*(y + i)
662 + scp->xoff + scp->xsize), x);
664 outw(GDCIDX, 0x0000); /* set/reset */
665 outw(GDCIDX, 0x0001); /* set/reset enable */
669 vga_egadraw(scr_stat *scp, int from, int count, int flip)
681 line_width = scp->sc->adp->va_line_width;
683 d = GET_PIXEL(scp, from, 1, line_width);
685 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
686 outw(GDCIDX, 0x0003); /* data rotate/function select */
687 outw(GDCIDX, 0x0f01); /* set/reset enable */
689 if (from + count > scp->xsize*scp->ysize)
690 count = scp->xsize*scp->ysize - from;
691 for (i = from; count-- > 0; ++i) {
692 a = sc_vtb_geta(&scp->vtb, i);
694 col1 = ((a & 0x7000) >> 4) | (a & 0x0800);
695 col2 = ((a & 0x8000) >> 4) | (a & 0x0700);
698 col2 = (a & 0xf000) >> 4;
700 /* set background color in EGA/VGA latch */
703 outw(GDCIDX, bg | 0x00); /* set/reset */
704 outw(GDCIDX, 0xff08); /* bit mask */
706 c = readb(d); /* set bg color in the latch */
708 /* foreground color */
709 outw(GDCIDX, col1 | 0x00); /* set/reset */
711 f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
712 for (j = 0; j < scp->font_size; ++j, ++f) {
713 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
718 if ((i % scp->xsize) == scp->xsize - 1)
719 d += scp->font_size * line_width - scp->xsize;
721 outw(GDCIDX, 0x0000); /* set/reset */
722 outw(GDCIDX, 0x0001); /* set/reset enable */
723 outw(GDCIDX, 0xff08); /* bit mask */
727 vga_vgadraw_direct(scr_stat *scp, int from, int count, int flip)
732 u_short col1, col2, color;
733 int line_width, pixel_size;
737 line_width = scp->sc->adp->va_line_width;
738 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
740 d = GET_PIXEL(scp, from, 8 * pixel_size, line_width);
742 if (from + count > scp->xsize * scp->ysize)
743 count = scp->xsize * scp->ysize - from;
745 for (i = from; count-- > 0; ++i) {
746 a = sc_vtb_geta(&scp->vtb, i);
749 col1 = (((a & 0x7000) >> 4) | (a & 0x0800)) >> 8;
750 col2 = (((a & 0x8000) >> 4) | (a & 0x0700)) >> 8;
752 col1 = (a & 0x0f00) >> 8;
753 col2 = (a & 0xf000) >> 12;
757 f = &(scp->font[sc_vtb_getc(&scp->vtb, i) * scp->font_size]);
759 for (j = 0; j < scp->font_size; ++j, ++f) {
760 for (k = 0; k < 8; ++k) {
761 color = *f & (1 << (7 - k)) ? col1 : col2;
762 DRAW_PIXEL(scp, e + pixel_size * k, color);
770 if ((i % scp->xsize) == scp->xsize - 1)
771 d += scp->font_size * line_width -
772 scp->xsize * 8 * pixel_size;
777 vga_vgadraw_planar(scr_stat *scp, int from, int count, int flip)
789 line_width = scp->sc->adp->va_line_width;
791 d = GET_PIXEL(scp, from, 1, line_width);
793 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
794 outw(GDCIDX, 0x0003); /* data rotate/function select */
795 outw(GDCIDX, 0x0f01); /* set/reset enable */
796 outw(GDCIDX, 0xff08); /* bit mask */
798 if (from + count > scp->xsize*scp->ysize)
799 count = scp->xsize*scp->ysize - from;
800 for (i = from; count-- > 0; ++i) {
801 a = sc_vtb_geta(&scp->vtb, i);
803 col1 = ((a & 0x7000) >> 4) | (a & 0x0800);
804 col2 = ((a & 0x8000) >> 4) | (a & 0x0700);
807 col2 = (a & 0xf000) >> 4;
809 /* set background color in EGA/VGA latch */
812 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
813 outw(GDCIDX, bg | 0x00); /* set/reset */
815 c = readb(d); /* set bg color in the latch */
816 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
818 /* foreground color */
819 outw(GDCIDX, col1 | 0x00); /* set/reset */
821 f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
822 for (j = 0; j < scp->font_size; ++j, ++f) {
827 if ((i % scp->xsize) == scp->xsize - 1)
828 d += scp->font_size * line_width - scp->xsize;
830 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
831 outw(GDCIDX, 0x0000); /* set/reset */
832 outw(GDCIDX, 0x0001); /* set/reset enable */
836 vga_pxlcursor_shape(scr_stat *scp, int base, int height, int blink)
838 if (base < 0 || base >= scp->font_size)
840 /* the caller may set height <= 0 in order to disable the cursor */
842 scp->curs_attr.base = base;
843 scp->curs_attr.height = height;
848 draw_pxlcursor_direct(scr_stat *scp, int at, int on, int flip)
852 int line_width, pixel_size;
854 int col1, col2, color;
858 line_width = scp->sc->adp->va_line_width;
859 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
861 d = GET_PIXEL(scp, at, 8 * pixel_size, line_width) +
862 (scp->font_size - scp->curs_attr.base - 1) * line_width;
864 a = sc_vtb_geta(&scp->vtb, at);
867 col1 = ((on) ? (a & 0x0f00) : ((a & 0xf000) >> 4)) >> 8;
868 col2 = ((on) ? ((a & 0xf000) >> 4) : (a & 0x0f00)) >> 8;
870 col1 = ((on) ? ((a & 0xf000) >> 4) : (a & 0x0f00)) >> 8;
871 col2 = ((on) ? (a & 0x0f00) : ((a & 0xf000) >> 4)) >> 8;
874 f = &(scp->font[sc_vtb_getc(&scp->vtb, at) * scp->font_size +
875 scp->font_size - scp->curs_attr.base - 1]);
877 height = imin(scp->curs_attr.height, scp->font_size);
879 for (i = 0; i < height; ++i, --f) {
880 for (j = 0; j < 8; ++j) {
881 color = *f & (1 << (7 - j)) ? col1 : col2;
882 DRAW_PIXEL(scp, d + pixel_size * j, color);
890 draw_pxlcursor_planar(scr_stat *scp, int at, int on, int flip)
901 line_width = scp->sc->adp->va_line_width;
903 d = GET_PIXEL(scp, at, 1, line_width) +
904 (scp->font_size - scp->curs_attr.base - 1) * line_width;
906 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
907 outw(GDCIDX, 0x0003); /* data rotate/function select */
908 outw(GDCIDX, 0x0f01); /* set/reset enable */
909 /* set background color in EGA/VGA latch */
910 a = sc_vtb_geta(&scp->vtb, at);
912 col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
914 col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
915 outw(GDCIDX, col | 0x00); /* set/reset */
916 outw(GDCIDX, 0xff08); /* bit mask */
918 c = readb(d); /* set bg color in the latch */
919 /* foreground color */
921 col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
923 col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
924 outw(GDCIDX, col | 0x00); /* set/reset */
925 f = &(scp->font[sc_vtb_getc(&scp->vtb, at)*scp->font_size
926 + scp->font_size - scp->curs_attr.base - 1]);
927 height = imin(scp->curs_attr.height, scp->font_size);
928 for (i = 0; i < height; ++i, --f) {
929 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
933 outw(GDCIDX, 0x0000); /* set/reset */
934 outw(GDCIDX, 0x0001); /* set/reset enable */
935 outw(GDCIDX, 0xff08); /* bit mask */
938 static int pxlblinkrate = 0;
941 vga_pxlcursor_direct(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_direct(scp, at, on, flip);
950 } else if (++pxlblinkrate & 4) {
952 scp->status ^= VR_CURSOR_ON;
953 draw_pxlcursor_direct(scp, at,
954 scp->status & VR_CURSOR_ON,
958 if (scp->status & VR_CURSOR_ON)
959 draw_pxlcursor_direct(scp, at, on, flip);
960 scp->status &= ~VR_CURSOR_ON;
963 scp->status |= VR_CURSOR_BLINK;
965 scp->status &= ~VR_CURSOR_BLINK;
969 vga_pxlcursor_planar(scr_stat *scp, int at, int blink, int on, int flip)
971 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
976 scp->status |= VR_CURSOR_ON;
977 draw_pxlcursor_planar(scp, at, on, flip);
978 } else if (++pxlblinkrate & 4) {
980 scp->status ^= VR_CURSOR_ON;
981 draw_pxlcursor_planar(scp, at,
982 scp->status & VR_CURSOR_ON,
986 if (scp->status & VR_CURSOR_ON)
987 draw_pxlcursor_planar(scp, at, on, flip);
988 scp->status &= ~VR_CURSOR_ON;
991 scp->status |= VR_CURSOR_BLINK;
993 scp->status &= ~VR_CURSOR_BLINK;
997 vga_pxlblink_direct(scr_stat *scp, int at, int flip)
999 if (!(scp->status & VR_CURSOR_BLINK))
1001 if (!(++pxlblinkrate & 4))
1004 scp->status ^= VR_CURSOR_ON;
1005 draw_pxlcursor_direct(scp, at, scp->status & VR_CURSOR_ON, flip);
1009 vga_pxlblink_planar(scr_stat *scp, int at, int flip)
1011 if (!(scp->status & VR_CURSOR_BLINK))
1013 if (!(++pxlblinkrate & 4))
1016 scp->status ^= VR_CURSOR_ON;
1017 draw_pxlcursor_planar(scp, at, scp->status & VR_CURSOR_ON, flip);
1020 #ifndef SC_NO_CUTPASTE
1023 draw_pxlmouse_planar(scr_stat *scp, int x, int y)
1032 line_width = scp->sc->adp->va_line_width;
1033 xoff = (x - scp->xoff*8)%8;
1034 yoff = y - (y/line_width)*line_width;
1035 ymax = imin(y + 16, scp->ypixel);
1037 outw(GDCIDX, 0x0805); /* read mode 1, write mode 0 */
1038 outw(GDCIDX, 0x0001); /* set/reset enable */
1039 outw(GDCIDX, 0x0002); /* color compare */
1040 outw(GDCIDX, 0x0007); /* color don't care */
1041 outw(GDCIDX, 0xff08); /* bit mask */
1042 outw(GDCIDX, 0x0803); /* data rotate/function select (and) */
1043 p = scp->sc->adp->va_window + line_width*y + x/8;
1044 if (x < scp->xpixel - 8) {
1045 for (i = y, j = 0; i < ymax; ++i, ++j) {
1046 m = ~(mouse_and_mask[j] >> xoff);
1047 #if defined(__i386__) || defined(__amd64__)
1048 *(u_char *)p &= m >> 8;
1049 *(u_char *)(p + 1) &= m;
1051 writeb(p, readb(p) & (m >> 8));
1052 writeb(p + 1, readb(p + 1) & (m >> 8));
1058 for (i = y, j = 0; i < ymax; ++i, ++j) {
1059 m = ~(mouse_and_mask[j] >> xoff);
1060 #if defined(__i386__) || defined(__amd64__)
1063 writeb(p, readb(p) & (m >> 8));
1068 outw(GDCIDX, 0x1003); /* data rotate/function select (or) */
1069 p = scp->sc->adp->va_window + line_width*y + x/8;
1070 if (x < scp->xpixel - 8) {
1071 for (i = y, j = 0; i < ymax; ++i, ++j) {
1072 m = mouse_or_mask[j] >> xoff;
1073 #if defined(__i386__) || defined(__amd64__)
1074 *(u_char *)p &= m >> 8;
1075 *(u_char *)(p + 1) &= m;
1077 writeb(p, readb(p) & (m >> 8));
1078 writeb(p + 1, readb(p + 1) & (m >> 8));
1083 for (i = y, j = 0; i < ymax; ++i, ++j) {
1084 m = mouse_or_mask[j] >> xoff;
1085 #if defined(__i386__) || defined(__amd64__)
1088 writeb(p, readb(p) & (m >> 8));
1093 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1094 outw(GDCIDX, 0x0003); /* data rotate/function select */
1098 remove_pxlmouse_planar(scr_stat *scp, int x, int y)
1107 /* erase the mouse cursor image */
1108 col = x/8 - scp->xoff;
1109 row = y/scp->font_size - scp->yoff;
1110 pos = row*scp->xsize + col;
1111 i = (col < scp->xsize - 1) ? 2 : 1;
1112 (*scp->rndr->draw)(scp, pos, i, FALSE);
1113 if (row < scp->ysize - 1)
1114 (*scp->rndr->draw)(scp, pos + scp->xsize, i, FALSE);
1116 /* paint border if necessary */
1117 line_width = scp->sc->adp->va_line_width;
1118 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1119 outw(GDCIDX, 0x0003); /* data rotate/function select */
1120 outw(GDCIDX, 0x0f01); /* set/reset enable */
1121 outw(GDCIDX, 0xff08); /* bit mask */
1122 outw(GDCIDX, (scp->border << 8) | 0x00); /* set/reset */
1123 if (row == scp->ysize - 1) {
1124 i = (scp->ysize + scp->yoff)*scp->font_size;
1125 ymax = imin(i + scp->font_size, scp->ypixel);
1126 p = scp->sc->adp->va_window + i*line_width + scp->xoff + col;
1127 if (col < scp->xsize - 1) {
1128 for (; i < ymax; ++i) {
1134 for (; i < ymax; ++i) {
1140 if ((col == scp->xsize - 1) && (scp->xoff > 0)) {
1141 i = (row + scp->yoff)*scp->font_size;
1142 ymax = imin(i + scp->font_size*2, scp->ypixel);
1143 p = scp->sc->adp->va_window + i*line_width
1144 + scp->xoff + scp->xsize;
1145 for (; i < ymax; ++i) {
1150 outw(GDCIDX, 0x0000); /* set/reset */
1151 outw(GDCIDX, 0x0001); /* set/reset enable */
1155 vga_pxlmouse_direct(scr_stat *scp, int x, int y, int on)
1158 int line_width, pixel_size;
1160 static int x_old = 0, xend_old = 0;
1161 static int y_old = 0, yend_old = 0;
1171 bpp = scp->sc->adp->va_info.vi_depth;
1173 if ((bpp == 16) && (scp->sc->adp->va_info.vi_pixel_fsizes[1] == 5))
1176 line_width = scp->sc->adp->va_line_width;
1177 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1179 xend = imin(x + 16, scp->xpixel);
1180 yend = imin(y + 16, scp->ypixel);
1182 p = scp->sc->adp->va_window + y_old * line_width + x_old * pixel_size;
1184 for (i = 0; i < (yend_old - y_old); i++) {
1185 for (j = (xend_old - x_old - 1); j >= 0; j--) {
1188 u32 = (uint32_t*)(p + j * pixel_size);
1189 writel(u32, mouse_buf32[i * 16 + j]);
1194 u16 = (uint16_t*)(p + j * pixel_size);
1195 writew(u16, mouse_buf16[i * 16 + j]);
1198 u8 = (uint8_t*)(p + j * pixel_size);
1199 writeb(u8, mouse_buf8[i * 16 + j]);
1207 p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1209 for (i = 0; i < (yend - y); i++) {
1210 for (j = (xend - x - 1); j >= 0; j--) {
1213 u32 = (uint32_t*)(p + j * pixel_size);
1214 mouse_buf32[i * 16 + j] = *u32;
1215 if (mouse_or_mask[i] & (1 << (15 - j)))
1216 writel(u32, vga_palette32[15]);
1217 else if (mouse_and_mask[i] & (1 << (15 - j)))
1221 u16 = (uint16_t*)(p + j * pixel_size);
1222 mouse_buf16[i * 16 + j] = *u16;
1223 if (mouse_or_mask[i] & (1 << (15 - j)))
1224 writew(u16, vga_palette16[15]);
1225 else if (mouse_and_mask[i] & (1 << (15 - j)))
1229 u16 = (uint16_t*)(p + j * pixel_size);
1230 mouse_buf16[i * 16 + j] = *u16;
1231 if (mouse_or_mask[i] & (1 << (15 - j)))
1232 writew(u16, vga_palette15[15]);
1233 else if (mouse_and_mask[i] & (1 << (15 - j)))
1237 u8 = (uint8_t*)(p + j * pixel_size);
1238 mouse_buf8[i * 16 + j] = *u8;
1239 if (mouse_or_mask[i] & (1 << (15 - j)))
1241 else if (mouse_and_mask[i] & (1 << (15 - j)))
1257 vga_pxlmouse_planar(scr_stat *scp, int x, int y, int on)
1260 draw_pxlmouse_planar(scp, x, y);
1262 remove_pxlmouse_planar(scp, x, y);
1265 #endif /* SC_NO_CUTPASTE */
1266 #endif /* SC_PIXEL_MODE */
1268 #ifndef SC_NO_MODE_CHANGE
1270 /* graphics mode renderer */
1273 vga_grborder(scr_stat *scp, int color)
1275 vidd_set_border(scp->sc->adp, color);