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 VIDEO_MEMORY_POS(scp, pos, x) \
175 scp->sc->adp->va_window + \
177 scp->yoff * scp->font_size * scp->sc->adp->va_line_width + \
178 x * (pos % scp->xsize) + \
179 scp->font_size * scp->sc->adp->va_line_width * (pos / scp->xsize)
181 #define vga_drawpxl(pos, color) \
182 switch (scp->sc->adp->va_info.vi_depth) { \
185 writel(pos, vga_palette32[color]); \
188 if (scp->sc->adp->va_info.vi_pixel_fsizes[1] == 5)\
189 writew(pos, vga_palette15[color]); \
191 writew(pos, vga_palette16[color]); \
194 writew(pos, vga_palette15[color]); \
197 writeb(pos, (uint8_t)color); \
200 static uint32_t vga_palette32[16] = {
201 0x000000, 0x0000ad, 0x00ad00, 0x00adad,
202 0xad0000, 0xad00ad, 0xad5200, 0xadadad,
203 0x525252, 0x5252ff, 0x52ff52, 0x52ffff,
204 0xff5252, 0xff52ff, 0xffff52, 0xffffff
207 static uint16_t vga_palette16[16] = {
208 0x0000, 0x0016, 0x0560, 0x0576, 0xb000, 0xb016, 0xb2a0, 0xb576,
209 0x52aa, 0x52bf, 0x57ea, 0x57ff, 0xfaaa, 0xfabf, 0xffea, 0xffff
212 static uint16_t vga_palette15[16] = {
213 0x0000, 0x0016, 0x02c0, 0x02d6, 0x5800, 0x5816, 0x5940, 0x5ad6,
214 0x294a, 0x295f, 0x2bea, 0x2bff, 0x7d4a, 0x7d5f, 0x7fea, 0x7fff
217 #ifndef SC_NO_CUTPASTE
218 static uint32_t mouse_buf32[256];
219 static uint16_t mouse_buf16[256];
220 static uint8_t mouse_buf8[256];
225 vga_nop(scr_stat *scp)
229 /* text mode renderer */
232 vga_txtclear(scr_stat *scp, int c, int attr)
234 sc_vtb_clear(&scp->scr, c, attr);
238 vga_txtborder(scr_stat *scp, int color)
240 vidd_set_border(scp->sc->adp, color);
244 vga_txtdraw(scr_stat *scp, int from, int count, int flip)
250 if (from + count > scp->xsize*scp->ysize)
251 count = scp->xsize*scp->ysize - from;
254 for (p = sc_vtb_pointer(&scp->scr, from); count-- > 0; ++from) {
255 c = sc_vtb_getc(&scp->vtb, from);
256 a = sc_vtb_geta(&scp->vtb, from);
257 a = (a & 0x8800) | ((a & 0x7000) >> 4)
258 | ((a & 0x0700) << 4);
259 p = sc_vtb_putchar(&scp->scr, p, c, a);
262 sc_vtb_copy(&scp->vtb, from, &scp->scr, from, count);
267 vga_txtcursor_shape(scr_stat *scp, int base, int height, int blink)
269 if (base < 0 || base >= scp->font_size)
271 /* the caller may set height <= 0 in order to disable the cursor */
273 scp->curs_attr.base = base;
274 scp->curs_attr.height = height;
276 vidd_set_hw_cursor_shape(scp->sc->adp, base, height,
277 scp->font_size, blink);
281 draw_txtcharcursor(scr_stat *scp, int at, u_short c, u_short a, int flip)
286 scp->cursor_saveunder_char = c;
287 scp->cursor_saveunder_attr = a;
289 #ifndef SC_NO_FONT_LOADING
290 if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
295 if (scp->font_size < 14) {
298 } else if (scp->font_size >= 16) {
305 if (scp->curs_attr.base >= h)
309 | ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
310 bcopy(font + c*h, font + sc->cursor_char*h, h);
311 font = font + sc->cursor_char*h;
312 for (i = imax(h - scp->curs_attr.base - scp->curs_attr.height, 0);
313 i < h - scp->curs_attr.base; ++i) {
317 vidd_load_font(sc->adp, 0, h, 8, font, sc->cursor_char, 1);
318 sc_vtb_putc(&scp->scr, at, sc->cursor_char, a);
320 #endif /* SC_NO_FONT_LOADING */
322 if ((a & 0x7000) == 0x7000) {
324 if ((a & 0x0700) == 0)
328 if ((a & 0x0700) == 0x0700)
333 | ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
334 sc_vtb_putc(&scp->scr, at, c, a);
339 vga_txtcursor(scr_stat *scp, int at, int blink, int on, int flip)
341 video_adapter_t *adp;
344 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
349 scp->status |= VR_CURSOR_BLINK;
351 scp->status |= VR_CURSOR_ON;
352 vidd_set_hw_cursor(adp, at%scp->xsize,
355 if (scp->status & VR_CURSOR_ON)
356 vidd_set_hw_cursor(adp, -1, -1);
357 scp->status &= ~VR_CURSOR_ON;
360 scp->status &= ~VR_CURSOR_BLINK;
362 scp->status |= VR_CURSOR_ON;
363 draw_txtcharcursor(scp, at,
364 sc_vtb_getc(&scp->scr, at),
365 sc_vtb_geta(&scp->scr, at),
368 cursor_attr = scp->cursor_saveunder_attr;
370 cursor_attr = (cursor_attr & 0x8800)
371 | ((cursor_attr & 0x7000) >> 4)
372 | ((cursor_attr & 0x0700) << 4);
373 if (scp->status & VR_CURSOR_ON)
374 sc_vtb_putc(&scp->scr, at,
375 scp->cursor_saveunder_char,
377 scp->status &= ~VR_CURSOR_ON;
383 vga_txtblink(scr_stat *scp, int at, int flip)
387 #ifndef SC_NO_CUTPASTE
390 draw_txtmouse(scr_stat *scp, int x, int y)
392 #ifndef SC_ALT_MOUSE_IMAGE
393 if (ISMOUSEAVAIL(scp->sc->adp->va_flags)) {
394 u_char font_buf[128];
398 int xoffset, yoffset;
402 /* prepare mousepointer char's bitmaps */
403 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
404 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos)*scp->font_size,
405 &font_buf[0], scp->font_size);
406 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos + 1)*scp->font_size,
407 &font_buf[32], scp->font_size);
409 + sc_vtb_getc(&scp->scr, pos + scp->xsize)*scp->font_size,
410 &font_buf[64], scp->font_size);
412 + sc_vtb_getc(&scp->scr, pos + scp->xsize + 1)*scp->font_size,
413 &font_buf[96], scp->font_size);
414 for (i = 0; i < scp->font_size; ++i) {
415 cursor[i] = font_buf[i]<<8 | font_buf[i+32];
416 cursor[i + scp->font_size] = font_buf[i+64]<<8 | font_buf[i+96];
419 /* now and-or in the mousepointer image */
421 yoffset = y%scp->font_size;
422 for (i = 0; i < 16; ++i) {
423 cursor[i + yoffset] =
424 (cursor[i + yoffset] & ~(mouse_and_mask[i] >> xoffset))
425 | (mouse_or_mask[i] >> xoffset);
427 for (i = 0; i < scp->font_size; ++i) {
428 font_buf[i] = (cursor[i] & 0xff00) >> 8;
429 font_buf[i + 32] = cursor[i] & 0xff;
430 font_buf[i + 64] = (cursor[i + scp->font_size] & 0xff00) >> 8;
431 font_buf[i + 96] = cursor[i + scp->font_size] & 0xff;
435 /* wait for vertical retrace to avoid jitter on some videocards */
436 crtc_addr = scp->sc->adp->va_crtc_addr;
437 while (!(inb(crtc_addr + 6) & 0x08)) /* idle */ ;
439 c = scp->sc->mouse_char;
440 vidd_load_font(scp->sc->adp, 0, 32, 8, font_buf, c, 4);
442 sc_vtb_putc(&scp->scr, pos, c, sc_vtb_geta(&scp->scr, pos));
443 /* FIXME: may be out of range! */
444 sc_vtb_putc(&scp->scr, pos + scp->xsize, c + 2,
445 sc_vtb_geta(&scp->scr, pos + scp->xsize));
446 if (x < (scp->xsize - 1)*8) {
447 sc_vtb_putc(&scp->scr, pos + 1, c + 1,
448 sc_vtb_geta(&scp->scr, pos + 1));
449 sc_vtb_putc(&scp->scr, pos + scp->xsize + 1, c + 3,
450 sc_vtb_geta(&scp->scr, pos + scp->xsize + 1));
453 #endif /* SC_ALT_MOUSE_IMAGE */
455 /* Red, magenta and brown are mapped to green to to keep it readable */
456 static const int col_conv[16] = {
457 6, 6, 6, 6, 2, 2, 2, 6, 14, 14, 14, 14, 10, 10, 10, 14
463 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
464 a = sc_vtb_geta(&scp->scr, pos);
465 if (scp->sc->adp->va_flags & V_ADP_COLOR)
466 color = (col_conv[(a & 0xf000) >> 12] << 12)
467 | ((a & 0x0f00) | 0x0800);
469 color = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
470 sc_vtb_putc(&scp->scr, pos, sc_vtb_getc(&scp->scr, pos), color);
475 remove_txtmouse(scr_stat *scp, int x, int y)
480 vga_txtmouse(scr_stat *scp, int x, int y, int on)
483 draw_txtmouse(scp, x, y);
485 remove_txtmouse(scp, x, y);
488 #endif /* SC_NO_CUTPASTE */
492 /* pixel (raster text) mode renderer */
495 vga_rndrinit(scr_stat *scp)
497 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PLANAR) {
498 scp->rndr->clear = vga_pxlclear_planar;
499 scp->rndr->draw_border = vga_pxlborder_planar;
500 scp->rndr->draw = vga_vgadraw_planar;
501 scp->rndr->draw_cursor = vga_pxlcursor_planar;
502 scp->rndr->blink_cursor = vga_pxlblink_planar;
503 scp->rndr->draw_mouse = vga_pxlmouse_planar;
505 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT ||
506 scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PACKED) {
507 scp->rndr->clear = vga_pxlclear_direct;
508 scp->rndr->draw_border = vga_pxlborder_direct;
509 scp->rndr->draw = vga_vgadraw_direct;
510 scp->rndr->draw_cursor = vga_pxlcursor_direct;
511 scp->rndr->blink_cursor = vga_pxlblink_direct;
512 scp->rndr->draw_mouse = vga_pxlmouse_direct;
517 vga_pxlclear_direct(scr_stat *scp, int c, int attr)
525 line_width = scp->sc->adp->va_line_width;
526 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
527 lines = scp->ysize * scp->font_size;
528 p = scp->sc->adp->va_window +
529 line_width * scp->yoff * scp->font_size +
530 scp->xoff * 8 * pixel_size;
532 for (i = 0; i < lines; ++i) {
533 bzero_io((void *)p, scp->xsize * 8 * pixel_size);
539 vga_pxlclear_planar(scr_stat *scp, int c, int attr)
546 /* XXX: we are just filling the screen with the background color... */
547 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
548 outw(GDCIDX, 0x0003); /* data rotate/function select */
549 outw(GDCIDX, 0x0f01); /* set/reset enable */
550 outw(GDCIDX, 0xff08); /* bit mask */
551 outw(GDCIDX, ((attr & 0xf000) >> 4) | 0x00); /* set/reset */
552 line_width = scp->sc->adp->va_line_width;
553 lines = scp->ysize*scp->font_size;
554 p = scp->sc->adp->va_window + line_width*scp->yoff*scp->font_size
556 for (i = 0; i < lines; ++i) {
557 bzero_io((void *)p, scp->xsize);
560 outw(GDCIDX, 0x0000); /* set/reset */
561 outw(GDCIDX, 0x0001); /* set/reset enable */
565 vga_pxlborder_direct(scr_stat *scp, int color)
576 line_width = scp->sc->adp->va_line_width;
577 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
580 s = scp->sc->adp->va_window;
581 e = s + line_width * scp->yoff * scp->font_size;
583 for (f = s; f < e; f += pixel_size)
584 vga_drawpxl(f, color);
587 y = (scp->yoff + scp->ysize) * scp->font_size;
589 if (scp->ypixel > y) {
590 s = scp->sc->adp->va_window + line_width * y;
591 e = s + line_width * (scp->ypixel - y);
593 for (f = s; f < e; f += pixel_size)
594 vga_drawpxl(f, color);
597 y = scp->yoff * scp->font_size;
598 x = scp->xpixel / 8 - scp->xoff - scp->xsize;
600 for (i = 0; i < scp->ysize * scp->font_size; ++i) {
602 s = scp->sc->adp->va_window + line_width * (y + i);
603 e = s + scp->xoff * 8 * pixel_size;
605 for (f = s; f < e; f += pixel_size)
606 vga_drawpxl(f, color);
610 s = scp->sc->adp->va_window + line_width * (y + i) +
611 scp->xoff * 8 * pixel_size +
612 scp->xsize * 8 * pixel_size;
613 e = s + x * 8 * pixel_size;
615 for (f = s; f < e; f += pixel_size)
616 vga_drawpxl(f, color);
622 vga_pxlborder_planar(scr_stat *scp, int color)
630 vidd_set_border(scp->sc->adp, color);
632 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
633 outw(GDCIDX, 0x0003); /* data rotate/function select */
634 outw(GDCIDX, 0x0f01); /* set/reset enable */
635 outw(GDCIDX, 0xff08); /* bit mask */
636 outw(GDCIDX, (color << 8) | 0x00); /* set/reset */
637 line_width = scp->sc->adp->va_line_width;
638 p = scp->sc->adp->va_window;
640 bzero_io((void *)p, line_width*scp->yoff*scp->font_size);
641 y = (scp->yoff + scp->ysize)*scp->font_size;
643 bzero_io((void *)(p + line_width*y), line_width*(scp->ypixel - y));
644 y = scp->yoff*scp->font_size;
645 x = scp->xpixel/8 - scp->xoff - scp->xsize;
646 for (i = 0; i < scp->ysize*scp->font_size; ++i) {
648 bzero_io((void *)(p + line_width*(y + i)), scp->xoff);
650 bzero_io((void *)(p + line_width*(y + i)
651 + scp->xoff + scp->xsize), x);
653 outw(GDCIDX, 0x0000); /* set/reset */
654 outw(GDCIDX, 0x0001); /* set/reset enable */
658 vga_egadraw(scr_stat *scp, int from, int count, int flip)
670 line_width = scp->sc->adp->va_line_width;
672 d = VIDEO_MEMORY_POS(scp, from, 1);
674 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
675 outw(GDCIDX, 0x0003); /* data rotate/function select */
676 outw(GDCIDX, 0x0f01); /* set/reset enable */
678 if (from + count > scp->xsize*scp->ysize)
679 count = scp->xsize*scp->ysize - from;
680 for (i = from; count-- > 0; ++i) {
681 a = sc_vtb_geta(&scp->vtb, i);
683 col1 = ((a & 0x7000) >> 4) | (a & 0x0800);
684 col2 = ((a & 0x8000) >> 4) | (a & 0x0700);
687 col2 = (a & 0xf000) >> 4;
689 /* set background color in EGA/VGA latch */
692 outw(GDCIDX, bg | 0x00); /* set/reset */
693 outw(GDCIDX, 0xff08); /* bit mask */
695 c = readb(d); /* set bg color in the latch */
697 /* foreground color */
698 outw(GDCIDX, col1 | 0x00); /* set/reset */
700 f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
701 for (j = 0; j < scp->font_size; ++j, ++f) {
702 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
707 if ((i % scp->xsize) == scp->xsize - 1)
709 + (scp->font_size - 1)*line_width;
711 outw(GDCIDX, 0x0000); /* set/reset */
712 outw(GDCIDX, 0x0001); /* set/reset enable */
713 outw(GDCIDX, 0xff08); /* bit mask */
717 vga_vgadraw_direct(scr_stat *scp, int from, int count, int flip)
722 u_short col1, col2, color;
723 int line_width, pixel_size;
727 line_width = scp->sc->adp->va_line_width;
728 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
730 d = VIDEO_MEMORY_POS(scp, from, 8 * pixel_size);
732 if (from + count > scp->xsize * scp->ysize)
733 count = scp->xsize * scp->ysize - from;
735 for (i = from; count-- > 0; ++i) {
736 a = sc_vtb_geta(&scp->vtb, i);
739 col1 = (((a & 0x7000) >> 4) | (a & 0x0800)) >> 8;
740 col2 = (((a & 0x8000) >> 4) | (a & 0x0700)) >> 8;
742 col1 = (a & 0x0f00) >> 8;
743 col2 = (a & 0xf000) >> 12;
747 f = &(scp->font[sc_vtb_getc(&scp->vtb, i) * scp->font_size]);
749 for (j = 0; j < scp->font_size; ++j, ++f) {
750 for (k = 0; k < 8; ++k) {
751 color = *f & (1 << (7 - k)) ? col1 : col2;
752 vga_drawpxl(e + pixel_size * k, color);
760 if ((i % scp->xsize) == scp->xsize - 1)
761 d += scp->xoff * 16 * pixel_size +
762 (scp->font_size - 1) * line_width;
767 vga_vgadraw_planar(scr_stat *scp, int from, int count, int flip)
779 d = VIDEO_MEMORY_POS(scp, from, 1);
781 line_width = scp->sc->adp->va_line_width;
783 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
784 outw(GDCIDX, 0x0003); /* data rotate/function select */
785 outw(GDCIDX, 0x0f01); /* set/reset enable */
786 outw(GDCIDX, 0xff08); /* bit mask */
788 if (from + count > scp->xsize*scp->ysize)
789 count = scp->xsize*scp->ysize - from;
790 for (i = from; count-- > 0; ++i) {
791 a = sc_vtb_geta(&scp->vtb, i);
793 col1 = ((a & 0x7000) >> 4) | (a & 0x0800);
794 col2 = ((a & 0x8000) >> 4) | (a & 0x0700);
797 col2 = (a & 0xf000) >> 4;
799 /* set background color in EGA/VGA latch */
802 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
803 outw(GDCIDX, bg | 0x00); /* set/reset */
805 c = readb(d); /* set bg color in the latch */
806 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
808 /* foreground color */
809 outw(GDCIDX, col1 | 0x00); /* set/reset */
811 f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
812 for (j = 0; j < scp->font_size; ++j, ++f) {
817 if ((i % scp->xsize) == scp->xsize - 1)
819 + (scp->font_size - 1)*line_width;
821 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
822 outw(GDCIDX, 0x0000); /* set/reset */
823 outw(GDCIDX, 0x0001); /* set/reset enable */
827 vga_pxlcursor_shape(scr_stat *scp, int base, int height, int blink)
829 if (base < 0 || base >= scp->font_size)
831 /* the caller may set height <= 0 in order to disable the cursor */
833 scp->curs_attr.base = base;
834 scp->curs_attr.height = height;
839 draw_pxlcursor_direct(scr_stat *scp, int at, int on, int flip)
843 int line_width, pixel_size;
845 int col1, col2, color;
849 line_width = scp->sc->adp->va_line_width;
850 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
852 d = VIDEO_MEMORY_POS(scp, at, 8 * pixel_size) +
853 (scp->font_size - scp->curs_attr.base - 1) * line_width;
855 a = sc_vtb_geta(&scp->vtb, at);
858 col1 = ((on) ? (a & 0x0f00) : ((a & 0xf000) >> 4)) >> 8;
859 col2 = ((on) ? ((a & 0xf000) >> 4) : (a & 0x0f00)) >> 8;
861 col1 = ((on) ? ((a & 0xf000) >> 4) : (a & 0x0f00)) >> 8;
862 col2 = ((on) ? (a & 0x0f00) : ((a & 0xf000) >> 4)) >> 8;
865 f = &(scp->font[sc_vtb_getc(&scp->vtb, at) * scp->font_size +
866 scp->font_size - scp->curs_attr.base - 1]);
868 height = imin(scp->curs_attr.height, scp->font_size);
870 for (i = 0; i < height; ++i, --f) {
871 for (j = 0; j < 8; ++j) {
872 color = *f & (1 << (7 - j)) ? col1 : col2;
873 vga_drawpxl(d + pixel_size * j, color);
881 draw_pxlcursor_planar(scr_stat *scp, int at, int on, int flip)
892 line_width = scp->sc->adp->va_line_width;
894 d = VIDEO_MEMORY_POS(scp, at, 1) +
895 (scp->font_size - scp->curs_attr.base - 1) * line_width;
897 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
898 outw(GDCIDX, 0x0003); /* data rotate/function select */
899 outw(GDCIDX, 0x0f01); /* set/reset enable */
900 /* set background color in EGA/VGA latch */
901 a = sc_vtb_geta(&scp->vtb, at);
903 col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
905 col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
906 outw(GDCIDX, col | 0x00); /* set/reset */
907 outw(GDCIDX, 0xff08); /* bit mask */
909 c = readb(d); /* set bg color in the latch */
910 /* foreground color */
912 col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
914 col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
915 outw(GDCIDX, col | 0x00); /* set/reset */
916 f = &(scp->font[sc_vtb_getc(&scp->vtb, at)*scp->font_size
917 + scp->font_size - scp->curs_attr.base - 1]);
918 height = imin(scp->curs_attr.height, scp->font_size);
919 for (i = 0; i < height; ++i, --f) {
920 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
924 outw(GDCIDX, 0x0000); /* set/reset */
925 outw(GDCIDX, 0x0001); /* set/reset enable */
926 outw(GDCIDX, 0xff08); /* bit mask */
929 static int pxlblinkrate = 0;
932 vga_pxlcursor_direct(scr_stat *scp, int at, int blink, int on, int flip)
934 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
939 scp->status |= VR_CURSOR_ON;
940 draw_pxlcursor_direct(scp, at, on, flip);
941 } else if (++pxlblinkrate & 4) {
943 scp->status ^= VR_CURSOR_ON;
944 draw_pxlcursor_direct(scp, at,
945 scp->status & VR_CURSOR_ON,
949 if (scp->status & VR_CURSOR_ON)
950 draw_pxlcursor_direct(scp, at, on, flip);
951 scp->status &= ~VR_CURSOR_ON;
954 scp->status |= VR_CURSOR_BLINK;
956 scp->status &= ~VR_CURSOR_BLINK;
960 vga_pxlcursor_planar(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_planar(scp, at, on, flip);
969 } else if (++pxlblinkrate & 4) {
971 scp->status ^= VR_CURSOR_ON;
972 draw_pxlcursor_planar(scp, at,
973 scp->status & VR_CURSOR_ON,
977 if (scp->status & VR_CURSOR_ON)
978 draw_pxlcursor_planar(scp, at, on, flip);
979 scp->status &= ~VR_CURSOR_ON;
982 scp->status |= VR_CURSOR_BLINK;
984 scp->status &= ~VR_CURSOR_BLINK;
988 vga_pxlblink_direct(scr_stat *scp, int at, int flip)
990 if (!(scp->status & VR_CURSOR_BLINK))
992 if (!(++pxlblinkrate & 4))
995 scp->status ^= VR_CURSOR_ON;
996 draw_pxlcursor_direct(scp, at, scp->status & VR_CURSOR_ON, flip);
1000 vga_pxlblink_planar(scr_stat *scp, int at, int flip)
1002 if (!(scp->status & VR_CURSOR_BLINK))
1004 if (!(++pxlblinkrate & 4))
1007 scp->status ^= VR_CURSOR_ON;
1008 draw_pxlcursor_planar(scp, at, scp->status & VR_CURSOR_ON, flip);
1011 #ifndef SC_NO_CUTPASTE
1014 draw_pxlmouse_planar(scr_stat *scp, int x, int y)
1023 line_width = scp->sc->adp->va_line_width;
1024 xoff = (x - scp->xoff*8)%8;
1025 yoff = y - (y/line_width)*line_width;
1026 ymax = imin(y + 16, scp->ypixel);
1028 outw(GDCIDX, 0x0805); /* read mode 1, write mode 0 */
1029 outw(GDCIDX, 0x0001); /* set/reset enable */
1030 outw(GDCIDX, 0x0002); /* color compare */
1031 outw(GDCIDX, 0x0007); /* color don't care */
1032 outw(GDCIDX, 0xff08); /* bit mask */
1033 outw(GDCIDX, 0x0803); /* data rotate/function select (and) */
1034 p = scp->sc->adp->va_window + line_width*y + x/8;
1035 if (x < scp->xpixel - 8) {
1036 for (i = y, j = 0; i < ymax; ++i, ++j) {
1037 m = ~(mouse_and_mask[j] >> xoff);
1038 #if defined(__i386__) || defined(__amd64__)
1039 *(u_char *)p &= m >> 8;
1040 *(u_char *)(p + 1) &= m;
1042 writeb(p, readb(p) & (m >> 8));
1043 writeb(p + 1, readb(p + 1) & (m >> 8));
1049 for (i = y, j = 0; i < ymax; ++i, ++j) {
1050 m = ~(mouse_and_mask[j] >> xoff);
1051 #if defined(__i386__) || defined(__amd64__)
1054 writeb(p, readb(p) & (m >> 8));
1059 outw(GDCIDX, 0x1003); /* data rotate/function select (or) */
1060 p = scp->sc->adp->va_window + line_width*y + x/8;
1061 if (x < scp->xpixel - 8) {
1062 for (i = y, j = 0; i < ymax; ++i, ++j) {
1063 m = mouse_or_mask[j] >> xoff;
1064 #if defined(__i386__) || defined(__amd64__)
1065 *(u_char *)p &= m >> 8;
1066 *(u_char *)(p + 1) &= m;
1068 writeb(p, readb(p) & (m >> 8));
1069 writeb(p + 1, readb(p + 1) & (m >> 8));
1074 for (i = y, j = 0; i < ymax; ++i, ++j) {
1075 m = mouse_or_mask[j] >> xoff;
1076 #if defined(__i386__) || defined(__amd64__)
1079 writeb(p, readb(p) & (m >> 8));
1084 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1085 outw(GDCIDX, 0x0003); /* data rotate/function select */
1089 remove_pxlmouse_planar(scr_stat *scp, int x, int y)
1098 /* erase the mouse cursor image */
1099 col = x/8 - scp->xoff;
1100 row = y/scp->font_size - scp->yoff;
1101 pos = row*scp->xsize + col;
1102 i = (col < scp->xsize - 1) ? 2 : 1;
1103 (*scp->rndr->draw)(scp, pos, i, FALSE);
1104 if (row < scp->ysize - 1)
1105 (*scp->rndr->draw)(scp, pos + scp->xsize, i, FALSE);
1107 /* paint border if necessary */
1108 line_width = scp->sc->adp->va_line_width;
1109 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1110 outw(GDCIDX, 0x0003); /* data rotate/function select */
1111 outw(GDCIDX, 0x0f01); /* set/reset enable */
1112 outw(GDCIDX, 0xff08); /* bit mask */
1113 outw(GDCIDX, (scp->border << 8) | 0x00); /* set/reset */
1114 if (row == scp->ysize - 1) {
1115 i = (scp->ysize + scp->yoff)*scp->font_size;
1116 ymax = imin(i + scp->font_size, scp->ypixel);
1117 p = scp->sc->adp->va_window + i*line_width + scp->xoff + col;
1118 if (col < scp->xsize - 1) {
1119 for (; i < ymax; ++i) {
1125 for (; i < ymax; ++i) {
1131 if ((col == scp->xsize - 1) && (scp->xoff > 0)) {
1132 i = (row + scp->yoff)*scp->font_size;
1133 ymax = imin(i + scp->font_size*2, scp->ypixel);
1134 p = scp->sc->adp->va_window + i*line_width
1135 + scp->xoff + scp->xsize;
1136 for (; i < ymax; ++i) {
1141 outw(GDCIDX, 0x0000); /* set/reset */
1142 outw(GDCIDX, 0x0001); /* set/reset enable */
1146 vga_pxlmouse_direct(scr_stat *scp, int x, int y, int on)
1149 int line_width, pixel_size;
1151 static int x_old = 0, xend_old = 0;
1152 static int y_old = 0, yend_old = 0;
1162 bpp = scp->sc->adp->va_info.vi_depth;
1164 if ((bpp == 16) && (scp->sc->adp->va_info.vi_pixel_fsizes[1] == 5))
1167 line_width = scp->sc->adp->va_line_width;
1168 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1170 xend = imin(x + 16, scp->xpixel);
1171 yend = imin(y + 16, scp->ypixel);
1173 p = scp->sc->adp->va_window + y_old * line_width + x_old * pixel_size;
1175 for (i = 0; i < (yend_old - y_old); i++) {
1176 for (j = (xend_old - x_old - 1); j >= 0; j--) {
1179 u32 = (uint32_t*)(p + j * pixel_size);
1180 writel(u32, mouse_buf32[i * 16 + j]);
1185 u16 = (uint16_t*)(p + j * pixel_size);
1186 writew(u16, mouse_buf16[i * 16 + j]);
1189 u8 = (uint8_t*)(p + j * pixel_size);
1190 writeb(u8, mouse_buf8[i * 16 + j]);
1198 p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1200 for (i = 0; i < (yend - y); i++) {
1201 for (j = (xend - x - 1); j >= 0; j--) {
1204 u32 = (uint32_t*)(p + j * pixel_size);
1205 mouse_buf32[i * 16 + j] = *u32;
1206 if (mouse_or_mask[i] & (1 << (15 - j)))
1207 writel(u32, vga_palette32[15]);
1208 else if (mouse_and_mask[i] & (1 << (15 - j)))
1212 u16 = (uint16_t*)(p + j * pixel_size);
1213 mouse_buf16[i * 16 + j] = *u16;
1214 if (mouse_or_mask[i] & (1 << (15 - j)))
1215 writew(u16, vga_palette16[15]);
1216 else if (mouse_and_mask[i] & (1 << (15 - j)))
1220 u16 = (uint16_t*)(p + j * pixel_size);
1221 mouse_buf16[i * 16 + j] = *u16;
1222 if (mouse_or_mask[i] & (1 << (15 - j)))
1223 writew(u16, vga_palette15[15]);
1224 else if (mouse_and_mask[i] & (1 << (15 - j)))
1228 u8 = (uint8_t*)(p + j * pixel_size);
1229 mouse_buf8[i * 16 + j] = *u8;
1230 if (mouse_or_mask[i] & (1 << (15 - j)))
1232 else if (mouse_and_mask[i] & (1 << (15 - j)))
1248 vga_pxlmouse_planar(scr_stat *scp, int x, int y, int on)
1251 draw_pxlmouse_planar(scp, x, y);
1253 remove_pxlmouse_planar(scp, x, y);
1256 #endif /* SC_NO_CUTPASTE */
1257 #endif /* SC_PIXEL_MODE */
1259 #ifndef SC_NO_MODE_CHANGE
1261 /* graphics mode renderer */
1264 vga_grborder(scr_stat *scp, int color)
1266 vidd_set_border(scp->sc->adp, color);