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) { \
184 writel(pos, vga_palette32[color]); \
187 if (((pos) & 1) == 0) { \
188 writew(pos, vga_palette32[color]); \
189 writeb(pos + 2, vga_palette32[color] >> 16);\
191 writeb(pos, vga_palette32[color]); \
192 writew(pos + 1, vga_palette32[color] >> 8);\
196 if (scp->sc->adp->va_info.vi_pixel_fsizes[1] == 5)\
197 writew(pos, vga_palette15[color]); \
199 writew(pos, vga_palette16[color]); \
202 writew(pos, vga_palette15[color]); \
205 writeb(pos, (uint8_t)color); \
208 static uint32_t vga_palette32[16] = {
209 0x000000, 0x0000ad, 0x00ad00, 0x00adad,
210 0xad0000, 0xad00ad, 0xad5200, 0xadadad,
211 0x525252, 0x5252ff, 0x52ff52, 0x52ffff,
212 0xff5252, 0xff52ff, 0xffff52, 0xffffff
215 static uint16_t vga_palette16[16] = {
216 0x0000, 0x0016, 0x0560, 0x0576, 0xb000, 0xb016, 0xb2a0, 0xb576,
217 0x52aa, 0x52bf, 0x57ea, 0x57ff, 0xfaaa, 0xfabf, 0xffea, 0xffff
220 static uint16_t vga_palette15[16] = {
221 0x0000, 0x0016, 0x02c0, 0x02d6, 0x5800, 0x5816, 0x5940, 0x5ad6,
222 0x294a, 0x295f, 0x2bea, 0x2bff, 0x7d4a, 0x7d5f, 0x7fea, 0x7fff
225 #ifndef SC_NO_CUTPASTE
226 static uint32_t mouse_buf32[256];
227 static uint16_t mouse_buf16[256];
228 static uint8_t mouse_buf8[256];
233 vga_nop(scr_stat *scp)
237 /* text mode renderer */
240 vga_txtclear(scr_stat *scp, int c, int attr)
242 sc_vtb_clear(&scp->scr, c, attr);
246 vga_txtborder(scr_stat *scp, int color)
248 vidd_set_border(scp->sc->adp, color);
252 vga_txtdraw(scr_stat *scp, int from, int count, int flip)
258 if (from + count > scp->xsize*scp->ysize)
259 count = scp->xsize*scp->ysize - from;
262 for (p = sc_vtb_pointer(&scp->scr, from); count-- > 0; ++from) {
263 c = sc_vtb_getc(&scp->vtb, from);
264 a = sc_vtb_geta(&scp->vtb, from);
265 a = (a & 0x8800) | ((a & 0x7000) >> 4)
266 | ((a & 0x0700) << 4);
267 p = sc_vtb_putchar(&scp->scr, p, c, a);
270 sc_vtb_copy(&scp->vtb, from, &scp->scr, from, count);
275 vga_txtcursor_shape(scr_stat *scp, int base, int height, int blink)
277 if (base < 0 || base >= scp->font_size)
279 /* the caller may set height <= 0 in order to disable the cursor */
281 scp->curs_attr.base = base;
282 scp->curs_attr.height = height;
284 vidd_set_hw_cursor_shape(scp->sc->adp, base, height,
285 scp->font_size, blink);
289 draw_txtcharcursor(scr_stat *scp, int at, u_short c, u_short a, int flip)
294 scp->cursor_saveunder_char = c;
295 scp->cursor_saveunder_attr = a;
297 #ifndef SC_NO_FONT_LOADING
298 if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
303 if (scp->font_size < 14) {
306 } else if (scp->font_size >= 16) {
313 if (scp->curs_attr.base >= h)
317 | ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
318 bcopy(font + c*h, font + sc->cursor_char*h, h);
319 font = font + sc->cursor_char*h;
320 for (i = imax(h - scp->curs_attr.base - scp->curs_attr.height, 0);
321 i < h - scp->curs_attr.base; ++i) {
325 vidd_load_font(sc->adp, 0, h, 8, font, sc->cursor_char, 1);
326 sc_vtb_putc(&scp->scr, at, sc->cursor_char, a);
328 #endif /* SC_NO_FONT_LOADING */
330 if ((a & 0x7000) == 0x7000) {
332 if ((a & 0x0700) == 0)
336 if ((a & 0x0700) == 0x0700)
341 | ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
342 sc_vtb_putc(&scp->scr, at, c, a);
347 vga_txtcursor(scr_stat *scp, int at, int blink, int on, int flip)
349 video_adapter_t *adp;
352 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
357 scp->status |= VR_CURSOR_BLINK;
359 scp->status |= VR_CURSOR_ON;
360 vidd_set_hw_cursor(adp, at%scp->xsize,
363 if (scp->status & VR_CURSOR_ON)
364 vidd_set_hw_cursor(adp, -1, -1);
365 scp->status &= ~VR_CURSOR_ON;
368 scp->status &= ~VR_CURSOR_BLINK;
370 scp->status |= VR_CURSOR_ON;
371 draw_txtcharcursor(scp, at,
372 sc_vtb_getc(&scp->scr, at),
373 sc_vtb_geta(&scp->scr, at),
376 cursor_attr = scp->cursor_saveunder_attr;
378 cursor_attr = (cursor_attr & 0x8800)
379 | ((cursor_attr & 0x7000) >> 4)
380 | ((cursor_attr & 0x0700) << 4);
381 if (scp->status & VR_CURSOR_ON)
382 sc_vtb_putc(&scp->scr, at,
383 scp->cursor_saveunder_char,
385 scp->status &= ~VR_CURSOR_ON;
391 vga_txtblink(scr_stat *scp, int at, int flip)
395 #ifndef SC_NO_CUTPASTE
398 draw_txtmouse(scr_stat *scp, int x, int y)
400 #ifndef SC_ALT_MOUSE_IMAGE
401 if (ISMOUSEAVAIL(scp->sc->adp->va_flags)) {
402 u_char font_buf[128];
406 int xoffset, yoffset;
410 /* prepare mousepointer char's bitmaps */
411 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
412 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos)*scp->font_size,
413 &font_buf[0], scp->font_size);
414 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos + 1)*scp->font_size,
415 &font_buf[32], scp->font_size);
417 + sc_vtb_getc(&scp->scr, pos + scp->xsize)*scp->font_size,
418 &font_buf[64], scp->font_size);
420 + sc_vtb_getc(&scp->scr, pos + scp->xsize + 1)*scp->font_size,
421 &font_buf[96], scp->font_size);
422 for (i = 0; i < scp->font_size; ++i) {
423 cursor[i] = font_buf[i]<<8 | font_buf[i+32];
424 cursor[i + scp->font_size] = font_buf[i+64]<<8 | font_buf[i+96];
427 /* now and-or in the mousepointer image */
429 yoffset = y%scp->font_size;
430 for (i = 0; i < 16; ++i) {
431 cursor[i + yoffset] =
432 (cursor[i + yoffset] & ~(mouse_and_mask[i] >> xoffset))
433 | (mouse_or_mask[i] >> xoffset);
435 for (i = 0; i < scp->font_size; ++i) {
436 font_buf[i] = (cursor[i] & 0xff00) >> 8;
437 font_buf[i + 32] = cursor[i] & 0xff;
438 font_buf[i + 64] = (cursor[i + scp->font_size] & 0xff00) >> 8;
439 font_buf[i + 96] = cursor[i + scp->font_size] & 0xff;
443 /* wait for vertical retrace to avoid jitter on some videocards */
444 crtc_addr = scp->sc->adp->va_crtc_addr;
445 while (!(inb(crtc_addr + 6) & 0x08)) /* idle */ ;
447 c = scp->sc->mouse_char;
448 vidd_load_font(scp->sc->adp, 0, 32, 8, font_buf, c, 4);
450 sc_vtb_putc(&scp->scr, pos, c, sc_vtb_geta(&scp->scr, pos));
451 /* FIXME: may be out of range! */
452 sc_vtb_putc(&scp->scr, pos + scp->xsize, c + 2,
453 sc_vtb_geta(&scp->scr, pos + scp->xsize));
454 if (x < (scp->xsize - 1)*8) {
455 sc_vtb_putc(&scp->scr, pos + 1, c + 1,
456 sc_vtb_geta(&scp->scr, pos + 1));
457 sc_vtb_putc(&scp->scr, pos + scp->xsize + 1, c + 3,
458 sc_vtb_geta(&scp->scr, pos + scp->xsize + 1));
461 #endif /* SC_ALT_MOUSE_IMAGE */
463 /* Red, magenta and brown are mapped to green to to keep it readable */
464 static const int col_conv[16] = {
465 6, 6, 6, 6, 2, 2, 2, 6, 14, 14, 14, 14, 10, 10, 10, 14
471 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
472 a = sc_vtb_geta(&scp->scr, pos);
473 if (scp->sc->adp->va_flags & V_ADP_COLOR)
474 color = (col_conv[(a & 0xf000) >> 12] << 12)
475 | ((a & 0x0f00) | 0x0800);
477 color = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
478 sc_vtb_putc(&scp->scr, pos, sc_vtb_getc(&scp->scr, pos), color);
483 remove_txtmouse(scr_stat *scp, int x, int y)
488 vga_txtmouse(scr_stat *scp, int x, int y, int on)
491 draw_txtmouse(scp, x, y);
493 remove_txtmouse(scp, x, y);
496 #endif /* SC_NO_CUTPASTE */
500 /* pixel (raster text) mode renderer */
503 vga_rndrinit(scr_stat *scp)
505 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PLANAR) {
506 scp->rndr->clear = vga_pxlclear_planar;
507 scp->rndr->draw_border = vga_pxlborder_planar;
508 scp->rndr->draw = vga_vgadraw_planar;
509 scp->rndr->draw_cursor = vga_pxlcursor_planar;
510 scp->rndr->blink_cursor = vga_pxlblink_planar;
511 scp->rndr->draw_mouse = vga_pxlmouse_planar;
513 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT ||
514 scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PACKED) {
515 scp->rndr->clear = vga_pxlclear_direct;
516 scp->rndr->draw_border = vga_pxlborder_direct;
517 scp->rndr->draw = vga_vgadraw_direct;
518 scp->rndr->draw_cursor = vga_pxlcursor_direct;
519 scp->rndr->blink_cursor = vga_pxlblink_direct;
520 scp->rndr->draw_mouse = vga_pxlmouse_direct;
525 vga_pxlclear_direct(scr_stat *scp, int c, int attr)
533 line_width = scp->sc->adp->va_line_width;
534 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
535 lines = scp->ysize * scp->font_size;
536 p = scp->sc->adp->va_window +
537 line_width * scp->yoff * scp->font_size +
538 scp->xoff * 8 * pixel_size;
540 for (i = 0; i < lines; ++i) {
541 bzero_io((void *)p, scp->xsize * 8 * pixel_size);
547 vga_pxlclear_planar(scr_stat *scp, int c, int attr)
554 /* XXX: we are just filling the screen with the background color... */
555 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
556 outw(GDCIDX, 0x0003); /* data rotate/function select */
557 outw(GDCIDX, 0x0f01); /* set/reset enable */
558 outw(GDCIDX, 0xff08); /* bit mask */
559 outw(GDCIDX, ((attr & 0xf000) >> 4) | 0x00); /* set/reset */
560 line_width = scp->sc->adp->va_line_width;
561 lines = scp->ysize*scp->font_size;
562 p = scp->sc->adp->va_window + line_width*scp->yoff*scp->font_size
564 for (i = 0; i < lines; ++i) {
565 bzero_io((void *)p, scp->xsize);
568 outw(GDCIDX, 0x0000); /* set/reset */
569 outw(GDCIDX, 0x0001); /* set/reset enable */
573 vga_pxlborder_direct(scr_stat *scp, int color)
584 line_width = scp->sc->adp->va_line_width;
585 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
588 s = scp->sc->adp->va_window;
589 e = s + line_width * scp->yoff * scp->font_size;
591 for (f = s; f < e; f += pixel_size)
592 vga_drawpxl(f, color);
595 y = (scp->yoff + scp->ysize) * scp->font_size;
597 if (scp->ypixel > y) {
598 s = scp->sc->adp->va_window + line_width * y;
599 e = s + line_width * (scp->ypixel - y);
601 for (f = s; f < e; f += pixel_size)
602 vga_drawpxl(f, color);
605 y = scp->yoff * scp->font_size;
606 x = scp->xpixel / 8 - scp->xoff - scp->xsize;
608 for (i = 0; i < scp->ysize * scp->font_size; ++i) {
610 s = scp->sc->adp->va_window + line_width * (y + i);
611 e = s + scp->xoff * 8 * pixel_size;
613 for (f = s; f < e; f += pixel_size)
614 vga_drawpxl(f, color);
618 s = scp->sc->adp->va_window + line_width * (y + i) +
619 scp->xoff * 8 * pixel_size +
620 scp->xsize * 8 * pixel_size;
621 e = s + x * 8 * pixel_size;
623 for (f = s; f < e; f += pixel_size)
624 vga_drawpxl(f, color);
630 vga_pxlborder_planar(scr_stat *scp, int color)
638 vidd_set_border(scp->sc->adp, color);
640 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
641 outw(GDCIDX, 0x0003); /* data rotate/function select */
642 outw(GDCIDX, 0x0f01); /* set/reset enable */
643 outw(GDCIDX, 0xff08); /* bit mask */
644 outw(GDCIDX, (color << 8) | 0x00); /* set/reset */
645 line_width = scp->sc->adp->va_line_width;
646 p = scp->sc->adp->va_window;
648 bzero_io((void *)p, line_width*scp->yoff*scp->font_size);
649 y = (scp->yoff + scp->ysize)*scp->font_size;
651 bzero_io((void *)(p + line_width*y), line_width*(scp->ypixel - y));
652 y = scp->yoff*scp->font_size;
653 x = scp->xpixel/8 - scp->xoff - scp->xsize;
654 for (i = 0; i < scp->ysize*scp->font_size; ++i) {
656 bzero_io((void *)(p + line_width*(y + i)), scp->xoff);
658 bzero_io((void *)(p + line_width*(y + i)
659 + scp->xoff + scp->xsize), x);
661 outw(GDCIDX, 0x0000); /* set/reset */
662 outw(GDCIDX, 0x0001); /* set/reset enable */
666 vga_egadraw(scr_stat *scp, int from, int count, int flip)
678 line_width = scp->sc->adp->va_line_width;
680 d = VIDEO_MEMORY_POS(scp, from, 1);
682 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
683 outw(GDCIDX, 0x0003); /* data rotate/function select */
684 outw(GDCIDX, 0x0f01); /* set/reset enable */
686 if (from + count > scp->xsize*scp->ysize)
687 count = scp->xsize*scp->ysize - from;
688 for (i = from; count-- > 0; ++i) {
689 a = sc_vtb_geta(&scp->vtb, i);
691 col1 = ((a & 0x7000) >> 4) | (a & 0x0800);
692 col2 = ((a & 0x8000) >> 4) | (a & 0x0700);
695 col2 = (a & 0xf000) >> 4;
697 /* set background color in EGA/VGA latch */
700 outw(GDCIDX, bg | 0x00); /* set/reset */
701 outw(GDCIDX, 0xff08); /* bit mask */
703 c = readb(d); /* set bg color in the latch */
705 /* foreground color */
706 outw(GDCIDX, col1 | 0x00); /* set/reset */
708 f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
709 for (j = 0; j < scp->font_size; ++j, ++f) {
710 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
715 if ((i % scp->xsize) == scp->xsize - 1)
717 + (scp->font_size - 1)*line_width;
719 outw(GDCIDX, 0x0000); /* set/reset */
720 outw(GDCIDX, 0x0001); /* set/reset enable */
721 outw(GDCIDX, 0xff08); /* bit mask */
725 vga_vgadraw_direct(scr_stat *scp, int from, int count, int flip)
730 u_short col1, col2, color;
731 int line_width, pixel_size;
735 line_width = scp->sc->adp->va_line_width;
736 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
738 d = VIDEO_MEMORY_POS(scp, from, 8 * pixel_size);
740 if (from + count > scp->xsize * scp->ysize)
741 count = scp->xsize * scp->ysize - from;
743 for (i = from; count-- > 0; ++i) {
744 a = sc_vtb_geta(&scp->vtb, i);
747 col1 = (((a & 0x7000) >> 4) | (a & 0x0800)) >> 8;
748 col2 = (((a & 0x8000) >> 4) | (a & 0x0700)) >> 8;
750 col1 = (a & 0x0f00) >> 8;
751 col2 = (a & 0xf000) >> 12;
755 f = &(scp->font[sc_vtb_getc(&scp->vtb, i) * scp->font_size]);
757 for (j = 0; j < scp->font_size; ++j, ++f) {
758 for (k = 0; k < 8; ++k) {
759 color = *f & (1 << (7 - k)) ? col1 : col2;
760 vga_drawpxl(e + pixel_size * k, color);
768 if ((i % scp->xsize) == scp->xsize - 1)
769 d += scp->xoff * 16 * pixel_size +
770 (scp->font_size - 1) * line_width;
775 vga_vgadraw_planar(scr_stat *scp, int from, int count, int flip)
787 d = VIDEO_MEMORY_POS(scp, from, 1);
789 line_width = scp->sc->adp->va_line_width;
791 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
792 outw(GDCIDX, 0x0003); /* data rotate/function select */
793 outw(GDCIDX, 0x0f01); /* set/reset enable */
794 outw(GDCIDX, 0xff08); /* bit mask */
796 if (from + count > scp->xsize*scp->ysize)
797 count = scp->xsize*scp->ysize - from;
798 for (i = from; count-- > 0; ++i) {
799 a = sc_vtb_geta(&scp->vtb, i);
801 col1 = ((a & 0x7000) >> 4) | (a & 0x0800);
802 col2 = ((a & 0x8000) >> 4) | (a & 0x0700);
805 col2 = (a & 0xf000) >> 4;
807 /* set background color in EGA/VGA latch */
810 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
811 outw(GDCIDX, bg | 0x00); /* set/reset */
813 c = readb(d); /* set bg color in the latch */
814 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
816 /* foreground color */
817 outw(GDCIDX, col1 | 0x00); /* set/reset */
819 f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
820 for (j = 0; j < scp->font_size; ++j, ++f) {
825 if ((i % scp->xsize) == scp->xsize - 1)
827 + (scp->font_size - 1)*line_width;
829 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
830 outw(GDCIDX, 0x0000); /* set/reset */
831 outw(GDCIDX, 0x0001); /* set/reset enable */
835 vga_pxlcursor_shape(scr_stat *scp, int base, int height, int blink)
837 if (base < 0 || base >= scp->font_size)
839 /* the caller may set height <= 0 in order to disable the cursor */
841 scp->curs_attr.base = base;
842 scp->curs_attr.height = height;
847 draw_pxlcursor_direct(scr_stat *scp, int at, int on, int flip)
851 int line_width, pixel_size;
853 int col1, col2, color;
857 line_width = scp->sc->adp->va_line_width;
858 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
860 d = VIDEO_MEMORY_POS(scp, at, 8 * pixel_size) +
861 (scp->font_size - scp->curs_attr.base - 1) * line_width;
863 a = sc_vtb_geta(&scp->vtb, at);
866 col1 = ((on) ? (a & 0x0f00) : ((a & 0xf000) >> 4)) >> 8;
867 col2 = ((on) ? ((a & 0xf000) >> 4) : (a & 0x0f00)) >> 8;
869 col1 = ((on) ? ((a & 0xf000) >> 4) : (a & 0x0f00)) >> 8;
870 col2 = ((on) ? (a & 0x0f00) : ((a & 0xf000) >> 4)) >> 8;
873 f = &(scp->font[sc_vtb_getc(&scp->vtb, at) * scp->font_size +
874 scp->font_size - scp->curs_attr.base - 1]);
876 height = imin(scp->curs_attr.height, scp->font_size);
878 for (i = 0; i < height; ++i, --f) {
879 for (j = 0; j < 8; ++j) {
880 color = *f & (1 << (7 - j)) ? col1 : col2;
881 vga_drawpxl(d + pixel_size * j, color);
889 draw_pxlcursor_planar(scr_stat *scp, int at, int on, int flip)
900 line_width = scp->sc->adp->va_line_width;
902 d = VIDEO_MEMORY_POS(scp, at, 1) +
903 (scp->font_size - scp->curs_attr.base - 1) * line_width;
905 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
906 outw(GDCIDX, 0x0003); /* data rotate/function select */
907 outw(GDCIDX, 0x0f01); /* set/reset enable */
908 /* set background color in EGA/VGA latch */
909 a = sc_vtb_geta(&scp->vtb, at);
911 col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
913 col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
914 outw(GDCIDX, col | 0x00); /* set/reset */
915 outw(GDCIDX, 0xff08); /* bit mask */
917 c = readb(d); /* set bg color in the latch */
918 /* foreground color */
920 col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
922 col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
923 outw(GDCIDX, col | 0x00); /* set/reset */
924 f = &(scp->font[sc_vtb_getc(&scp->vtb, at)*scp->font_size
925 + scp->font_size - scp->curs_attr.base - 1]);
926 height = imin(scp->curs_attr.height, scp->font_size);
927 for (i = 0; i < height; ++i, --f) {
928 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
932 outw(GDCIDX, 0x0000); /* set/reset */
933 outw(GDCIDX, 0x0001); /* set/reset enable */
934 outw(GDCIDX, 0xff08); /* bit mask */
937 static int pxlblinkrate = 0;
940 vga_pxlcursor_direct(scr_stat *scp, int at, int blink, int on, int flip)
942 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
947 scp->status |= VR_CURSOR_ON;
948 draw_pxlcursor_direct(scp, at, on, flip);
949 } else if (++pxlblinkrate & 4) {
951 scp->status ^= VR_CURSOR_ON;
952 draw_pxlcursor_direct(scp, at,
953 scp->status & VR_CURSOR_ON,
957 if (scp->status & VR_CURSOR_ON)
958 draw_pxlcursor_direct(scp, at, on, flip);
959 scp->status &= ~VR_CURSOR_ON;
962 scp->status |= VR_CURSOR_BLINK;
964 scp->status &= ~VR_CURSOR_BLINK;
968 vga_pxlcursor_planar(scr_stat *scp, int at, int blink, int on, int flip)
970 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
975 scp->status |= VR_CURSOR_ON;
976 draw_pxlcursor_planar(scp, at, on, flip);
977 } else if (++pxlblinkrate & 4) {
979 scp->status ^= VR_CURSOR_ON;
980 draw_pxlcursor_planar(scp, at,
981 scp->status & VR_CURSOR_ON,
985 if (scp->status & VR_CURSOR_ON)
986 draw_pxlcursor_planar(scp, at, on, flip);
987 scp->status &= ~VR_CURSOR_ON;
990 scp->status |= VR_CURSOR_BLINK;
992 scp->status &= ~VR_CURSOR_BLINK;
996 vga_pxlblink_direct(scr_stat *scp, int at, int flip)
998 if (!(scp->status & VR_CURSOR_BLINK))
1000 if (!(++pxlblinkrate & 4))
1003 scp->status ^= VR_CURSOR_ON;
1004 draw_pxlcursor_direct(scp, at, scp->status & VR_CURSOR_ON, flip);
1008 vga_pxlblink_planar(scr_stat *scp, int at, int flip)
1010 if (!(scp->status & VR_CURSOR_BLINK))
1012 if (!(++pxlblinkrate & 4))
1015 scp->status ^= VR_CURSOR_ON;
1016 draw_pxlcursor_planar(scp, at, scp->status & VR_CURSOR_ON, flip);
1019 #ifndef SC_NO_CUTPASTE
1022 draw_pxlmouse_planar(scr_stat *scp, int x, int y)
1031 line_width = scp->sc->adp->va_line_width;
1032 xoff = (x - scp->xoff*8)%8;
1033 yoff = y - (y/line_width)*line_width;
1034 ymax = imin(y + 16, scp->ypixel);
1036 outw(GDCIDX, 0x0805); /* read mode 1, write mode 0 */
1037 outw(GDCIDX, 0x0001); /* set/reset enable */
1038 outw(GDCIDX, 0x0002); /* color compare */
1039 outw(GDCIDX, 0x0007); /* color don't care */
1040 outw(GDCIDX, 0xff08); /* bit mask */
1041 outw(GDCIDX, 0x0803); /* data rotate/function select (and) */
1042 p = scp->sc->adp->va_window + line_width*y + x/8;
1043 if (x < scp->xpixel - 8) {
1044 for (i = y, j = 0; i < ymax; ++i, ++j) {
1045 m = ~(mouse_and_mask[j] >> xoff);
1046 #if defined(__i386__) || defined(__amd64__)
1047 *(u_char *)p &= m >> 8;
1048 *(u_char *)(p + 1) &= m;
1050 writeb(p, readb(p) & (m >> 8));
1051 writeb(p + 1, readb(p + 1) & (m >> 8));
1057 for (i = y, j = 0; i < ymax; ++i, ++j) {
1058 m = ~(mouse_and_mask[j] >> xoff);
1059 #if defined(__i386__) || defined(__amd64__)
1062 writeb(p, readb(p) & (m >> 8));
1067 outw(GDCIDX, 0x1003); /* data rotate/function select (or) */
1068 p = scp->sc->adp->va_window + line_width*y + x/8;
1069 if (x < scp->xpixel - 8) {
1070 for (i = y, j = 0; i < ymax; ++i, ++j) {
1071 m = mouse_or_mask[j] >> xoff;
1072 #if defined(__i386__) || defined(__amd64__)
1073 *(u_char *)p &= m >> 8;
1074 *(u_char *)(p + 1) &= m;
1076 writeb(p, readb(p) & (m >> 8));
1077 writeb(p + 1, readb(p + 1) & (m >> 8));
1082 for (i = y, j = 0; i < ymax; ++i, ++j) {
1083 m = mouse_or_mask[j] >> xoff;
1084 #if defined(__i386__) || defined(__amd64__)
1087 writeb(p, readb(p) & (m >> 8));
1092 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1093 outw(GDCIDX, 0x0003); /* data rotate/function select */
1097 remove_pxlmouse_planar(scr_stat *scp, int x, int y)
1106 /* erase the mouse cursor image */
1107 col = x/8 - scp->xoff;
1108 row = y/scp->font_size - scp->yoff;
1109 pos = row*scp->xsize + col;
1110 i = (col < scp->xsize - 1) ? 2 : 1;
1111 (*scp->rndr->draw)(scp, pos, i, FALSE);
1112 if (row < scp->ysize - 1)
1113 (*scp->rndr->draw)(scp, pos + scp->xsize, i, FALSE);
1115 /* paint border if necessary */
1116 line_width = scp->sc->adp->va_line_width;
1117 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1118 outw(GDCIDX, 0x0003); /* data rotate/function select */
1119 outw(GDCIDX, 0x0f01); /* set/reset enable */
1120 outw(GDCIDX, 0xff08); /* bit mask */
1121 outw(GDCIDX, (scp->border << 8) | 0x00); /* set/reset */
1122 if (row == scp->ysize - 1) {
1123 i = (scp->ysize + scp->yoff)*scp->font_size;
1124 ymax = imin(i + scp->font_size, scp->ypixel);
1125 p = scp->sc->adp->va_window + i*line_width + scp->xoff + col;
1126 if (col < scp->xsize - 1) {
1127 for (; i < ymax; ++i) {
1133 for (; i < ymax; ++i) {
1139 if ((col == scp->xsize - 1) && (scp->xoff > 0)) {
1140 i = (row + scp->yoff)*scp->font_size;
1141 ymax = imin(i + scp->font_size*2, scp->ypixel);
1142 p = scp->sc->adp->va_window + i*line_width
1143 + scp->xoff + scp->xsize;
1144 for (; i < ymax; ++i) {
1149 outw(GDCIDX, 0x0000); /* set/reset */
1150 outw(GDCIDX, 0x0001); /* set/reset enable */
1154 vga_pxlmouse_direct(scr_stat *scp, int x, int y, int on)
1157 int line_width, pixel_size;
1159 static int x_old = 0, xend_old = 0;
1160 static int y_old = 0, yend_old = 0;
1170 bpp = scp->sc->adp->va_info.vi_depth;
1172 if ((bpp == 16) && (scp->sc->adp->va_info.vi_pixel_fsizes[1] == 5))
1175 line_width = scp->sc->adp->va_line_width;
1176 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1178 xend = imin(x + 16, scp->xpixel);
1179 yend = imin(y + 16, scp->ypixel);
1181 p = scp->sc->adp->va_window + y_old * line_width + x_old * pixel_size;
1183 for (i = 0; i < (yend_old - y_old); i++) {
1184 for (j = (xend_old - x_old - 1); j >= 0; j--) {
1187 u32 = (uint32_t*)(p + j * pixel_size);
1188 writel(u32, mouse_buf32[i * 16 + j]);
1193 u16 = (uint16_t*)(p + j * pixel_size);
1194 writew(u16, mouse_buf16[i * 16 + j]);
1197 u8 = (uint8_t*)(p + j * pixel_size);
1198 writeb(u8, mouse_buf8[i * 16 + j]);
1206 p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1208 for (i = 0; i < (yend - y); i++) {
1209 for (j = (xend - x - 1); j >= 0; j--) {
1212 u32 = (uint32_t*)(p + j * pixel_size);
1213 mouse_buf32[i * 16 + j] = *u32;
1214 if (mouse_or_mask[i] & (1 << (15 - j)))
1215 writel(u32, vga_palette32[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_palette16[15]);
1224 else if (mouse_and_mask[i] & (1 << (15 - j)))
1228 u16 = (uint16_t*)(p + j * pixel_size);
1229 mouse_buf16[i * 16 + j] = *u16;
1230 if (mouse_or_mask[i] & (1 << (15 - j)))
1231 writew(u16, vga_palette15[15]);
1232 else if (mouse_and_mask[i] & (1 << (15 - j)))
1236 u8 = (uint8_t*)(p + j * pixel_size);
1237 mouse_buf8[i * 16 + j] = *u8;
1238 if (mouse_or_mask[i] & (1 << (15 - j)))
1240 else if (mouse_and_mask[i] & (1 << (15 - j)))
1256 vga_pxlmouse_planar(scr_stat *scp, int x, int y, int on)
1259 draw_pxlmouse_planar(scp, x, y);
1261 remove_pxlmouse_planar(scp, x, y);
1264 #endif /* SC_NO_CUTPASTE */
1265 #endif /* SC_PIXEL_MODE */
1267 #ifndef SC_NO_MODE_CHANGE
1269 /* graphics mode renderer */
1272 vga_grborder(scr_stat *scp, int color)
1274 vidd_set_border(scp->sc->adp, color);