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>
43 #include <sys/sysctl.h>
45 #include <machine/bus.h>
47 #include <dev/fb/fbreg.h>
48 #include <dev/fb/vgareg.h>
49 #include <dev/syscons/syscons.h>
51 #include <isa/isareg.h>
53 #ifndef SC_RENDER_DEBUG
54 #define SC_RENDER_DEBUG 0
57 static vr_clear_t vga_txtclear;
58 static vr_draw_border_t vga_txtborder;
59 static vr_draw_t vga_txtdraw;
60 static vr_set_cursor_t vga_txtcursor_shape;
61 static vr_draw_cursor_t vga_txtcursor;
62 static vr_blink_cursor_t vga_txtblink;
63 #ifndef SC_NO_CUTPASTE
64 static vr_draw_mouse_t vga_txtmouse;
66 #define vga_txtmouse (vr_draw_mouse_t *)vga_nop
70 static vr_init_t vga_rndrinit;
71 static vr_clear_t vga_pxlclear_direct;
72 static vr_clear_t vga_pxlclear_planar;
73 static vr_draw_border_t vga_pxlborder_direct;
74 static vr_draw_border_t vga_pxlborder_planar;
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 vgarndrsw = {
116 (vr_clear_t *)vga_nop,
117 (vr_draw_border_t *)vga_nop,
118 (vr_draw_t *)vga_nop,
120 (vr_draw_cursor_t *)vga_nop,
121 (vr_blink_cursor_t *)vga_nop,
122 (vr_set_mouse_t *)vga_nop,
123 (vr_draw_mouse_t *)vga_nop,
125 RENDERER(ega, PIXEL_MODE, vgarndrsw, vga_set);
126 RENDERER(vga, PIXEL_MODE, vgarndrsw, vga_set);
127 #endif /* SC_PIXEL_MODE */
129 #ifndef SC_NO_MODE_CHANGE
130 static sc_rndr_sw_t grrndrsw = {
131 (vr_init_t *)vga_nop,
132 (vr_clear_t *)vga_nop,
134 (vr_draw_t *)vga_nop,
135 (vr_set_cursor_t *)vga_nop,
136 (vr_draw_cursor_t *)vga_nop,
137 (vr_blink_cursor_t *)vga_nop,
138 (vr_set_mouse_t *)vga_nop,
139 (vr_draw_mouse_t *)vga_nop,
141 RENDERER(cga, GRAPHICS_MODE, grrndrsw, vga_set);
142 RENDERER(ega, GRAPHICS_MODE, grrndrsw, vga_set);
143 RENDERER(vga, GRAPHICS_MODE, grrndrsw, vga_set);
144 #endif /* SC_NO_MODE_CHANGE */
146 RENDERER_MODULE(vga, vga_set);
148 #ifndef SC_NO_CUTPASTE
149 #if !defined(SC_ALT_MOUSE_IMAGE) || defined(SC_PIXEL_MODE)
151 u_short md_border[16];
152 u_short md_interior[16];
160 static const struct mousedata mouse10x16_50 = { {
161 0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8200,
162 0x8400, 0x8400, 0x8400, 0x9200, 0xB200, 0xA900, 0xC900, 0x8600, }, {
163 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7C00,
164 0x7800, 0x7800, 0x7800, 0x6C00, 0x4C00, 0x4600, 0x0600, 0x0000, },
165 10, 16, 49, 52, "mouse10x16_50",
168 static const struct mousedata mouse8x14_67 = { {
169 0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8700,
170 0x8400, 0x9200, 0xB200, 0xA900, 0xC900, 0x0600, 0x0000, 0x0000, }, {
171 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7800,
172 0x7800, 0x6C00, 0x4C00, 0x4600, 0x0600, 0x0000, 0x0000, 0x0000, },
173 8, 14, 64, 65, "mouse8x14_67",
176 static const struct mousedata mouse8x13_75 = { {
177 0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8600, 0x8400,
178 0xB200, 0xD200, 0x0900, 0x0900, 0x0600, 0x0000, 0x0000, 0x0000, }, {
179 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7800, 0x7800,
180 0x4C00, 0x0C00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000, },
181 8, 13, 75, 80, "mouse8x13_75",
184 static const struct mousedata mouse10x16_75 = { {
185 0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8700,
186 0x8400, 0x9200, 0xB200, 0xC900, 0x0900, 0x0480, 0x0480, 0x0300, }, {
187 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7800,
188 0x7800, 0x6C00, 0x4C00, 0x0600, 0x0600, 0x0300, 0x0300, 0x0000, },
189 10, 16, 72, 75, "mouse10x16_75",
192 static const struct mousedata mouse9x13_90 = { {
193 0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8780,
194 0x9200, 0xB200, 0xD900, 0x8900, 0x0600, 0x0000, 0x0000, 0x0000, }, {
195 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7800,
196 0x6C00, 0x4C00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000, },
197 9, 13, 89, 89, "mouse9x13_90",
200 static const struct mousedata mouse10x16_90 = { {
201 0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8080,
202 0x8040, 0x83E0, 0x8200, 0x9900, 0xA900, 0xC480, 0x8480, 0x0300, }, {
203 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7F00,
204 0x7F80, 0x7C00, 0x7C00, 0x6600, 0x4600, 0x0300, 0x0300, 0x0000, },
205 10, 16, 89, 89, "mouse10x16_90",
208 static const struct mousedata mouse9x13_100 = { {
209 0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8780,
210 0xB200, 0xD200, 0x8900, 0x0900, 0x0600, 0x0000, 0x0000, 0x0000, }, {
211 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7800,
212 0x4C00, 0x0C00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000, },
213 9, 13, 106, 113, "mouse9x13_100",
216 static const struct mousedata mouse10x16_100 = { {
217 0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8080,
218 0x8040, 0x83C0, 0x9200, 0xA900, 0xC900, 0x0480, 0x0480, 0x0300, }, {
219 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7F00,
220 0x7F80, 0x7C00, 0x6C00, 0x4600, 0x0600, 0x0300, 0x0300, 0x0000, },
221 10, 16, 96, 106, "mouse10x16_100",
224 static const struct mousedata mouse10x14_120 = { {
225 0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8080,
226 0x97C0, 0xB200, 0xF200, 0xC900, 0x8900, 0x0600, 0x0000, 0x0000, }, {
227 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7F00,
228 0x6800, 0x4C00, 0x0C00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, },
229 10, 14, 120, 124, "mouse10x14_120",
232 static const struct mousedata mouse10x16_120 = { {
233 0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8080,
234 0x97C0, 0xB200, 0xF200, 0xC900, 0x8900, 0x0480, 0x0480, 0x0300, }, {
235 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7F00,
236 0x6800, 0x4C00, 0x0C00, 0x0600, 0x0600, 0x0300, 0x0300, 0x0000, },
237 10, 16, 120, 124, "mouse10x16_120",
240 static const struct mousedata mouse9x13_133 = { {
241 0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8080,
242 0x9780, 0xB200, 0xC900, 0x0900, 0x0600, 0x0000, 0x0000, 0x0000, }, {
243 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7F00,
244 0x6800, 0x4C00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000, },
245 9, 13, 142, 124, "mouse9x13_133",
248 static const struct mousedata mouse10x16_133 = { {
249 0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8080,
250 0x8040, 0x93E0, 0xB200, 0xC900, 0x8900, 0x0480, 0x0480, 0x0300, }, {
251 0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7F00,
252 0x7F80, 0x6C00, 0x4C00, 0x0600, 0x0600, 0x0300, 0x0300, 0x0000, },
253 10, 16, 120, 133, "mouse10x16_133",
256 static const struct mousedata mouse14x10_240 = { {
257 0xF800, 0xCE00, 0xC380, 0xC0E0, 0xC038, 0xC1FC, 0xDCC0, 0xF660,
258 0xC330, 0x01E0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, }, {
259 0x0000, 0x3000, 0x3C00, 0x3F00, 0x3FC0, 0x3E00, 0x2300, 0x0180,
260 0x00C0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
261 14, 10, 189, 189, "mouse14x10_240",
264 static const struct mousedata * const mouselarge[] = {
275 static const struct mousedata * const mousesmall[] = {
288 #define GET_PIXEL(scp, pos, x, w) \
290 (scp)->sc->adp->va_window + \
291 (x) * (scp)->xoff + \
292 (scp)->yoff * (scp)->font_size * (w) + \
293 (x) * ((pos) % (scp)->xsize) + \
294 (scp)->font_size * (w) * ((pos) / (scp)->xsize); \
297 #define DRAW_PIXEL(scp, pos, color) do { \
298 switch ((scp)->sc->adp->va_info.vi_depth) { \
300 writel((pos), vga_palette32[color]); \
303 if (((pos) & 1) == 0) { \
304 writew((pos), vga_palette32[color]); \
305 writeb((pos) + 2, vga_palette32[color] >> 16); \
307 writeb((pos), vga_palette32[color]); \
308 writew((pos) + 1, vga_palette32[color] >> 8); \
312 if ((scp)->sc->adp->va_info.vi_pixel_fsizes[1] == 5) \
313 writew((pos), vga_palette15[color]); \
315 writew((pos), vga_palette16[color]); \
318 writew((pos), vga_palette15[color]); \
321 writeb((pos), (uint8_t)(color)); \
325 static uint32_t vga_palette32[16] = {
326 0x000000, 0x0000ad, 0x00ad00, 0x00adad,
327 0xad0000, 0xad00ad, 0xad5200, 0xadadad,
328 0x525252, 0x5252ff, 0x52ff52, 0x52ffff,
329 0xff5252, 0xff52ff, 0xffff52, 0xffffff
332 static uint16_t vga_palette16[16] = {
333 0x0000, 0x0016, 0x0560, 0x0576, 0xb000, 0xb016, 0xb2a0, 0xb576,
334 0x52aa, 0x52bf, 0x57ea, 0x57ff, 0xfaaa, 0xfabf, 0xffea, 0xffff
337 static uint16_t vga_palette15[16] = {
338 0x0000, 0x0016, 0x02c0, 0x02d6, 0x5800, 0x5816, 0x5940, 0x5ad6,
339 0x294a, 0x295f, 0x2bea, 0x2bff, 0x7d4a, 0x7d5f, 0x7fea, 0x7fff
343 static int vga_aspect_scale= 100;
344 SYSCTL_INT(_machdep, OID_AUTO, vga_aspect_scale, CTLFLAG_RW,
345 &vga_aspect_scale, 0, "Aspect scale ratio (3:4):actual times 100");
348 vga_flipattr(u_short a, int blink)
351 a = (a & 0x8800) | ((a & 0x7000) >> 4) |
354 a = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
359 vga_cursorattr_adj(u_short a, int blink)
362 * !blink means pixel mode, and the cursor attribute in that case
363 * is simplistic reverse video.
366 return (vga_flipattr(a, blink));
369 * The cursor attribute is usually that of the underlying char
370 * with the bg changed to white. If the bg is already white,
371 * then the bg is changed to black. The fg is usually not
372 * changed, but if it is the same as the new bg then it is
373 * changed to the inverse of the new bg.
375 if ((a & 0x7000) == 0x7000) {
377 if ((a & 0x0700) == 0)
381 if ((a & 0x0700) == 0x0700)
388 vga_setmdp(scr_stat *scp)
390 #if !defined(SC_NO_CUTPASTE) && \
391 (!defined(SC_ALT_MOUSE_IMAGE) || defined(SC_PIXEL_MODE))
392 const struct mousedata *mdp;
393 const struct mousedata * const *mdpp;
394 int aspect, best_i, best_v, i, n, v, wb, wi, xpixel, ypixel;
396 xpixel = scp->xpixel;
397 ypixel = scp->ypixel;
398 if (scp->sc->adp->va_flags & V_ADP_CWIDTH9)
399 xpixel = xpixel * 9 / 8;
401 /* If 16:9 +-1%, assume square pixels, else scale to 4:3 or full. */
402 aspect = xpixel * 900 / ypixel / 16;
403 if (aspect < 99 || aspect > 100)
404 aspect = xpixel * 300 / ypixel / 4 * vga_aspect_scale / 100;
407 * Use 10x16 cursors except even with 8x8 fonts except in ~200-
408 * line modes where pixels are very large and in text mode where
409 * even 13 pixels high is really 4 too many. Clipping a 16-high
410 * cursor at 9-high gives a variable tail which looks better than
411 * a smaller cursor with a constant tail.
413 * XXX: the IS*SC() macros don't work when this is called at the
414 * end of a mode switch since UNKNOWN_SC is still set.
416 if (scp->font_size <= 8 &&
417 (ypixel < 300 || !(scp->status & PIXEL_MODE))) {
418 mdpp = &mousesmall[0];
419 n = nitems(mousesmall);
421 mdpp = &mouselarge[0];
422 n = nitems(mouselarge);
424 if (scp->status & PIXEL_MODE) {
433 for (i = 0; i < n; i++) {
434 v = (wb * abs(mdpp[i]->md_baspect - aspect) +
435 wi * abs(mdpp[i]->md_iaspect - aspect)) / aspect;
442 scp->mouse_data = mdp;
443 #endif /* !SC_NO_CUTPASTE && (!SC_ALT_MOUSE_IMAGE || SC_PIXEL_MODE) */
447 vga_nop(scr_stat *scp)
451 /* text mode renderer */
454 vga_txtclear(scr_stat *scp, int c, int attr)
456 sc_vtb_clear(&scp->scr, c, attr);
460 vga_txtborder(scr_stat *scp, int color)
462 vidd_set_border(scp->sc->adp, color);
466 vga_txtdraw(scr_stat *scp, int from, int count, int flip)
472 if (from + count > scp->xsize*scp->ysize)
473 count = scp->xsize*scp->ysize - from;
476 for (p = sc_vtb_pointer(&scp->scr, from); count-- > 0; ++from) {
477 c = sc_vtb_getc(&scp->vtb, from);
478 a = sc_vtb_geta(&scp->vtb, from);
479 a = vga_flipattr(a, TRUE);
480 p = sc_vtb_putchar(&scp->scr, p, c, a);
483 sc_vtb_copy(&scp->vtb, from, &scp->scr, from, count);
488 vga_txtcursor_shape(scr_stat *scp, int base, int height, int blink)
491 if (base < 0 || base >= scp->font_size)
493 /* the caller may set height <= 0 in order to disable the cursor */
494 vidd_set_hw_cursor_shape(scp->sc->adp, base, height,
495 scp->font_size, blink);
499 draw_txtcharcursor(scr_stat *scp, int at, u_short c, u_short a, int flip)
505 #ifndef SC_NO_FONT_LOADING
506 if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
511 if (scp->font_size < 14) {
514 } else if (scp->font_size >= 16) {
521 if (scp->curs_attr.base >= h)
524 a = vga_flipattr(a, TRUE);
525 bcopy(font + c*h, font + sc->cursor_char*h, h);
526 font = font + sc->cursor_char*h;
527 for (i = imax(h - scp->curs_attr.base - scp->curs_attr.height, 0);
528 i < h - scp->curs_attr.base; ++i) {
532 vidd_load_font(sc->adp, 0, h, 8, font, sc->cursor_char, 1);
533 sc_vtb_putc(&scp->scr, at, sc->cursor_char, a);
535 #endif /* SC_NO_FONT_LOADING */
538 a = vga_flipattr(a, TRUE);
539 a = vga_cursorattr_adj(a, TRUE);
540 sc_vtb_putc(&scp->scr, at, c, a);
545 vga_txtcursor(scr_stat *scp, int at, int blink, int on, int flip)
547 video_adapter_t *adp;
550 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
555 scp->status |= VR_CURSOR_BLINK;
557 scp->status |= VR_CURSOR_ON;
558 vidd_set_hw_cursor(adp, at%scp->xsize,
561 if (scp->status & VR_CURSOR_ON)
562 vidd_set_hw_cursor(adp, -1, -1);
563 scp->status &= ~VR_CURSOR_ON;
566 scp->status &= ~VR_CURSOR_BLINK;
568 scp->status |= VR_CURSOR_ON;
569 draw_txtcharcursor(scp, at,
570 sc_vtb_getc(&scp->vtb, at),
571 sc_vtb_geta(&scp->vtb, at),
574 cursor_attr = sc_vtb_geta(&scp->vtb, at);
576 cursor_attr = vga_flipattr(cursor_attr, TRUE);
577 if (scp->status & VR_CURSOR_ON)
578 sc_vtb_putc(&scp->scr, at,
579 sc_vtb_getc(&scp->vtb, at),
581 scp->status &= ~VR_CURSOR_ON;
587 vga_txtblink(scr_stat *scp, int at, int flip)
591 int sc_txtmouse_no_retrace_wait;
593 #ifndef SC_NO_CUTPASTE
596 draw_txtmouse(scr_stat *scp, int x, int y)
598 #ifndef SC_ALT_MOUSE_IMAGE
599 if (ISMOUSEAVAIL(scp->sc->adp->va_flags)) {
600 const struct mousedata *mdp;
601 uint32_t border, interior;
602 u_char font_buf[128];
606 int xoffset, yoffset;
610 mdp = scp->mouse_data;
612 /* prepare mousepointer char's bitmaps */
613 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
614 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos)*scp->font_size,
615 &font_buf[0], scp->font_size);
616 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos + 1)*scp->font_size,
617 &font_buf[32], scp->font_size);
619 + sc_vtb_getc(&scp->scr, pos + scp->xsize)*scp->font_size,
620 &font_buf[64], scp->font_size);
622 + sc_vtb_getc(&scp->scr, pos + scp->xsize + 1)*scp->font_size,
623 &font_buf[96], scp->font_size);
624 for (i = 0; i < scp->font_size; ++i) {
625 cursor[i] = font_buf[i]<<8 | font_buf[i+32];
626 cursor[i + scp->font_size] = font_buf[i+64]<<8 | font_buf[i+96];
629 /* now and-or in the mousepointer image */
631 yoffset = y%scp->font_size;
632 for (i = 0; i < 16; ++i) {
633 border = mdp->md_border[i] << 8; /* avoid right shifting out */
634 interior = mdp->md_interior[i] << 8;
635 border >>= xoffset; /* normalize */
636 interior >>= xoffset;
637 if (scp->sc->adp->va_flags & V_ADP_CWIDTH9) {
638 /* skip gaps between characters */
639 border = (border & 0xff0000) |
640 (border & 0x007f80) << 1 |
641 (border & 0x00003f) << 2;
642 interior = (interior & 0xff0000) |
643 (interior & 0x007f80) << 1 |
644 (interior & 0x00003f) << 2;
646 border >>= 8; /* back to normal position */
648 cursor[i + yoffset] = (cursor[i + yoffset] & ~border) |
651 for (i = 0; i < scp->font_size; ++i) {
652 font_buf[i] = (cursor[i] & 0xff00) >> 8;
653 font_buf[i + 32] = cursor[i] & 0xff;
654 font_buf[i + 64] = (cursor[i + scp->font_size] & 0xff00) >> 8;
655 font_buf[i + 96] = cursor[i + scp->font_size] & 0xff;
659 /* wait for vertical retrace to avoid jitter on some videocards */
660 crtc_addr = scp->sc->adp->va_crtc_addr;
661 while (!sc_txtmouse_no_retrace_wait &&
662 !(inb(crtc_addr + 6) & 0x08))
665 c = scp->sc->mouse_char;
666 vidd_load_font(scp->sc->adp, 0, 32, 8, font_buf, c, 4);
668 sc_vtb_putc(&scp->scr, pos, c, sc_vtb_geta(&scp->scr, pos));
669 /* FIXME: may be out of range! */
670 sc_vtb_putc(&scp->scr, pos + scp->xsize, c + 2,
671 sc_vtb_geta(&scp->scr, pos + scp->xsize));
672 if (x < (scp->xsize - 1)*8) {
673 sc_vtb_putc(&scp->scr, pos + 1, c + 1,
674 sc_vtb_geta(&scp->scr, pos + 1));
675 sc_vtb_putc(&scp->scr, pos + scp->xsize + 1, c + 3,
676 sc_vtb_geta(&scp->scr, pos + scp->xsize + 1));
679 #endif /* SC_ALT_MOUSE_IMAGE */
681 /* Red, magenta and brown are mapped to green to to keep it readable */
682 static const int col_conv[16] = {
683 6, 6, 6, 6, 2, 2, 2, 6, 14, 14, 14, 14, 10, 10, 10, 14
689 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
690 a = sc_vtb_geta(&scp->scr, pos);
691 if (scp->sc->adp->va_flags & V_ADP_COLOR)
692 color = (col_conv[(a & 0xf000) >> 12] << 12)
693 | ((a & 0x0f00) | 0x0800);
695 color = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
696 sc_vtb_putc(&scp->scr, pos, sc_vtb_getc(&scp->scr, pos), color);
701 remove_txtmouse(scr_stat *scp, int x, int y)
706 vga_txtmouse(scr_stat *scp, int x, int y, int on)
709 draw_txtmouse(scp, x, y);
711 remove_txtmouse(scp, x, y);
714 #endif /* SC_NO_CUTPASTE */
718 /* pixel (raster text) mode renderer */
721 vga_rndrinit(scr_stat *scp)
723 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PLANAR) {
724 scp->rndr->clear = vga_pxlclear_planar;
725 scp->rndr->draw_border = vga_pxlborder_planar;
726 scp->rndr->draw = vga_vgadraw_planar;
727 scp->rndr->draw_cursor = vga_pxlcursor_planar;
728 scp->rndr->blink_cursor = vga_pxlblink_planar;
729 scp->rndr->draw_mouse = vga_pxlmouse_planar;
731 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT ||
732 scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PACKED) {
733 scp->rndr->clear = vga_pxlclear_direct;
734 scp->rndr->draw_border = vga_pxlborder_direct;
735 scp->rndr->draw = vga_vgadraw_direct;
736 scp->rndr->draw_cursor = vga_pxlcursor_direct;
737 scp->rndr->blink_cursor = vga_pxlblink_direct;
738 scp->rndr->draw_mouse = vga_pxlmouse_direct;
743 vga_pxlclear_direct(scr_stat *scp, int c, int attr)
751 line_width = scp->sc->adp->va_line_width;
752 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
753 lines = scp->ysize * scp->font_size;
754 p = scp->sc->adp->va_window +
755 line_width * scp->yoff * scp->font_size +
756 scp->xoff * 8 * pixel_size;
758 for (i = 0; i < lines; ++i) {
759 bzero_io((void *)p, scp->xsize * 8 * pixel_size);
765 vga_pxlclear_planar(scr_stat *scp, int c, int attr)
772 /* XXX: we are just filling the screen with the background color... */
773 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
774 outw(GDCIDX, 0x0003); /* data rotate/function select */
775 outw(GDCIDX, 0x0f01); /* set/reset enable */
776 outw(GDCIDX, 0xff08); /* bit mask */
777 outw(GDCIDX, ((attr & 0xf000) >> 4) | 0x00); /* set/reset */
778 line_width = scp->sc->adp->va_line_width;
779 lines = scp->ysize*scp->font_size;
780 p = scp->sc->adp->va_window + line_width*scp->yoff*scp->font_size
782 for (i = 0; i < lines; ++i) {
783 bzero_io((void *)p, scp->xsize);
786 outw(GDCIDX, 0x0000); /* set/reset */
787 outw(GDCIDX, 0x0001); /* set/reset enable */
791 vga_pxlborder_direct(scr_stat *scp, int color)
802 line_width = scp->sc->adp->va_line_width;
803 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
806 s = scp->sc->adp->va_window;
807 e = s + line_width * scp->yoff * scp->font_size;
809 for (f = s; f < e; f += pixel_size)
810 DRAW_PIXEL(scp, f, color);
813 y = (scp->yoff + scp->ysize) * scp->font_size;
815 if (scp->ypixel > y) {
816 s = scp->sc->adp->va_window + line_width * y;
817 e = s + line_width * (scp->ypixel - y);
819 for (f = s; f < e; f += pixel_size)
820 DRAW_PIXEL(scp, f, color);
823 y = scp->yoff * scp->font_size;
824 x = scp->xpixel / 8 - scp->xoff - scp->xsize;
826 for (i = 0; i < scp->ysize * scp->font_size; ++i) {
828 s = scp->sc->adp->va_window + line_width * (y + i);
829 e = s + scp->xoff * 8 * pixel_size;
831 for (f = s; f < e; f += pixel_size)
832 DRAW_PIXEL(scp, f, color);
836 s = scp->sc->adp->va_window + line_width * (y + i) +
837 scp->xoff * 8 * pixel_size +
838 scp->xsize * 8 * pixel_size;
839 e = s + x * 8 * pixel_size;
841 for (f = s; f < e; f += pixel_size)
842 DRAW_PIXEL(scp, f, color);
848 vga_pxlborder_planar(scr_stat *scp, int color)
856 vidd_set_border(scp->sc->adp, color);
858 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
859 outw(GDCIDX, 0x0003); /* data rotate/function select */
860 outw(GDCIDX, 0x0f01); /* set/reset enable */
861 outw(GDCIDX, 0xff08); /* bit mask */
862 outw(GDCIDX, (color << 8) | 0x00); /* set/reset */
863 line_width = scp->sc->adp->va_line_width;
864 p = scp->sc->adp->va_window;
866 bzero_io((void *)p, line_width*scp->yoff*scp->font_size);
867 y = (scp->yoff + scp->ysize)*scp->font_size;
869 bzero_io((void *)(p + line_width*y), line_width*(scp->ypixel - y));
870 y = scp->yoff*scp->font_size;
871 x = scp->xpixel/8 - scp->xoff - scp->xsize;
872 for (i = 0; i < scp->ysize*scp->font_size; ++i) {
874 bzero_io((void *)(p + line_width*(y + i)), scp->xoff);
876 bzero_io((void *)(p + line_width*(y + i)
877 + scp->xoff + scp->xsize), x);
879 outw(GDCIDX, 0x0000); /* set/reset */
880 outw(GDCIDX, 0x0001); /* set/reset enable */
884 vga_vgadraw_direct(scr_stat *scp, int from, int count, int flip)
889 u_short col1, col2, color;
890 int line_width, pixel_size;
894 line_width = scp->sc->adp->va_line_width;
895 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
897 d = GET_PIXEL(scp, from, 8 * pixel_size, line_width);
899 if (from + count > scp->xsize * scp->ysize)
900 count = scp->xsize * scp->ysize - from;
902 for (i = from; count-- > 0; ++i) {
903 a = sc_vtb_geta(&scp->vtb, i);
906 a = vga_flipattr(a, FALSE);
907 col1 = (a & 0x0f00) >> 8;
908 col2 = (a & 0xf000) >> 12;
911 f = &(scp->font[sc_vtb_getc(&scp->vtb, i) * scp->font_size]);
913 for (j = 0; j < scp->font_size; ++j, ++f) {
914 for (k = 0; k < 8; ++k) {
915 color = *f & (1 << (7 - k)) ? col1 : col2;
916 DRAW_PIXEL(scp, e + pixel_size * k, color);
924 if ((i % scp->xsize) == scp->xsize - 1)
925 d += scp->font_size * line_width -
926 scp->xsize * 8 * pixel_size;
931 vga_vgadraw_planar(scr_stat *scp, int from, int count, int flip)
943 line_width = scp->sc->adp->va_line_width;
945 d = GET_PIXEL(scp, from, 1, line_width);
947 if (scp->sc->adp->va_type == KD_VGA) {
948 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
949 outw(GDCIDX, 0xff08); /* bit mask */
951 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
952 outw(GDCIDX, 0x0003); /* data rotate/function select */
953 outw(GDCIDX, 0x0f01); /* set/reset enable */
955 if (from + count > scp->xsize*scp->ysize)
956 count = scp->xsize*scp->ysize - from;
957 for (i = from; count-- > 0; ++i) {
958 a = sc_vtb_geta(&scp->vtb, i);
960 a = vga_flipattr(a, FALSE);
962 col2 = (a & 0xf000) >> 4;
963 /* set background color in EGA/VGA latch */
967 outw(GDCIDX, bg | 0x00); /* set/reset */
968 if (scp->sc->adp->va_type != KD_VGA)
969 outw(GDCIDX, 0xff08); /* bit mask */
971 c = readb(d); /* set bg color in the latch */
973 /* foreground color */
976 outw(GDCIDX, col1 | 0x00); /* set/reset */
979 f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
980 for (j = 0; j < scp->font_size; ++j, ++f) {
981 if (scp->sc->adp->va_type == KD_VGA)
984 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
990 if ((i % scp->xsize) == scp->xsize - 1)
991 d += scp->font_size * line_width - scp->xsize;
993 if (scp->sc->adp->va_type == KD_VGA)
994 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
996 outw(GDCIDX, 0xff08); /* bit mask */
997 outw(GDCIDX, 0x0000); /* set/reset */
998 outw(GDCIDX, 0x0001); /* set/reset enable */
1002 vga_pxlcursor_shape(scr_stat *scp, int base, int height, int blink)
1008 draw_pxlcursor_direct(scr_stat *scp, int at, int on, int flip)
1012 int line_width, pixel_size;
1014 int col1, col2, color;
1018 line_width = scp->sc->adp->va_line_width;
1019 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1021 d = GET_PIXEL(scp, at, 8 * pixel_size, line_width) +
1022 (scp->font_size - scp->curs_attr.base - 1) * line_width;
1024 a = sc_vtb_geta(&scp->vtb, at);
1027 a = vga_flipattr(a, FALSE);
1029 a = vga_cursorattr_adj(a, FALSE);
1030 col1 = (a & 0x0f00) >> 8;
1033 f = &(scp->font[sc_vtb_getc(&scp->vtb, at) * scp->font_size +
1034 scp->font_size - scp->curs_attr.base - 1]);
1036 height = imin(scp->curs_attr.height, scp->font_size);
1038 for (i = 0; i < height; ++i, --f) {
1039 for (j = 0; j < 8; ++j) {
1040 color = *f & (1 << (7 - j)) ? col1 : col2;
1041 DRAW_PIXEL(scp, d + pixel_size * j, color);
1049 draw_pxlcursor_planar(scr_stat *scp, int at, int on, int flip)
1060 line_width = scp->sc->adp->va_line_width;
1062 d = GET_PIXEL(scp, at, 1, line_width) +
1063 (scp->font_size - scp->curs_attr.base - 1) * line_width;
1065 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1066 outw(GDCIDX, 0x0003); /* data rotate/function select */
1067 outw(GDCIDX, 0x0f01); /* set/reset enable */
1068 /* set background color in EGA/VGA latch */
1069 a = sc_vtb_geta(&scp->vtb, at);
1071 a = vga_flipattr(a, FALSE);
1073 a = vga_cursorattr_adj(a, FALSE);
1074 col = (a & 0xf000) >> 4;
1075 outw(GDCIDX, col | 0x00); /* set/reset */
1076 outw(GDCIDX, 0xff08); /* bit mask */
1078 c = readb(d); /* set bg color in the latch */
1079 /* foreground color */
1081 outw(GDCIDX, col | 0x00); /* set/reset */
1082 f = &(scp->font[sc_vtb_getc(&scp->vtb, at)*scp->font_size
1083 + scp->font_size - scp->curs_attr.base - 1]);
1084 height = imin(scp->curs_attr.height, scp->font_size);
1085 for (i = 0; i < height; ++i, --f) {
1086 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
1090 outw(GDCIDX, 0x0000); /* set/reset */
1091 outw(GDCIDX, 0x0001); /* set/reset enable */
1092 outw(GDCIDX, 0xff08); /* bit mask */
1095 static int pxlblinkrate = 0;
1098 vga_pxlcursor_direct(scr_stat *scp, int at, int blink, int on, int flip)
1100 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
1105 scp->status |= VR_CURSOR_ON;
1106 draw_pxlcursor_direct(scp, at, on, flip);
1107 } else if (++pxlblinkrate & 4) {
1109 scp->status ^= VR_CURSOR_ON;
1110 draw_pxlcursor_direct(scp, at,
1111 scp->status & VR_CURSOR_ON,
1115 if (scp->status & VR_CURSOR_ON)
1116 draw_pxlcursor_direct(scp, at, on, flip);
1117 scp->status &= ~VR_CURSOR_ON;
1120 scp->status |= VR_CURSOR_BLINK;
1122 scp->status &= ~VR_CURSOR_BLINK;
1126 vga_pxlcursor_planar(scr_stat *scp, int at, int blink, int on, int flip)
1128 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
1133 scp->status |= VR_CURSOR_ON;
1134 draw_pxlcursor_planar(scp, at, on, flip);
1135 } else if (++pxlblinkrate & 4) {
1137 scp->status ^= VR_CURSOR_ON;
1138 draw_pxlcursor_planar(scp, at,
1139 scp->status & VR_CURSOR_ON,
1143 if (scp->status & VR_CURSOR_ON)
1144 draw_pxlcursor_planar(scp, at, on, flip);
1145 scp->status &= ~VR_CURSOR_ON;
1148 scp->status |= VR_CURSOR_BLINK;
1150 scp->status &= ~VR_CURSOR_BLINK;
1154 vga_pxlblink_direct(scr_stat *scp, int at, int flip)
1156 if (!(scp->status & VR_CURSOR_BLINK))
1158 if (!(++pxlblinkrate & 4))
1161 scp->status ^= VR_CURSOR_ON;
1162 draw_pxlcursor_direct(scp, at, scp->status & VR_CURSOR_ON, flip);
1166 vga_pxlblink_planar(scr_stat *scp, int at, int flip)
1168 if (!(scp->status & VR_CURSOR_BLINK))
1170 if (!(++pxlblinkrate & 4))
1173 scp->status ^= VR_CURSOR_ON;
1174 draw_pxlcursor_planar(scp, at, scp->status & VR_CURSOR_ON, flip);
1177 #ifndef SC_NO_CUTPASTE
1180 draw_pxlmouse_planar(scr_stat *scp, int x, int y)
1182 const struct mousedata *mdp;
1191 mdp = scp->mouse_data;
1192 line_width = scp->sc->adp->va_line_width;
1193 xoff = (x - scp->xoff*8)%8;
1194 yoff = y - rounddown(y, line_width);
1195 ymax = imin(y + mdp->md_height, scp->ypixel);
1197 if (scp->sc->adp->va_type == KD_VGA) {
1198 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
1199 outw(GDCIDX, 0xff08); /* bit mask */
1201 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1202 outw(GDCIDX, 0x0003); /* data rotate/function select */
1203 outw(GDCIDX, 0x0f01); /* set/reset enable */
1205 outw(GDCIDX, (0 << 8) | 0x00); /* set/reset */
1206 p = scp->sc->adp->va_window + line_width*y + x/8;
1207 for (i = y, j = 0; i < ymax; ++i, ++j) {
1208 m = mdp->md_border[j] << 8 >> xoff;
1209 for (k = 0; k < 3; ++k) {
1210 m1 = m >> (8 * (2 - k));
1211 if (m1 != 0 && x + 8 * k < scp->xpixel) {
1213 if (scp->sc->adp->va_type == KD_VGA)
1217 outw(GDCIDX, (m1 << 8) | 0x08);
1224 outw(GDCIDX, (15 << 8) | 0x00); /* set/reset */
1225 p = scp->sc->adp->va_window + line_width*y + x/8;
1226 for (i = y, j = 0; i < ymax; ++i, ++j) {
1227 m = mdp->md_interior[j] << 8 >> xoff;
1228 for (k = 0; k < 3; ++k) {
1229 m1 = m >> (8 * (2 - k));
1230 if (m1 != 0 && x + 8 * k < scp->xpixel) {
1232 if (scp->sc->adp->va_type == KD_VGA)
1236 outw(GDCIDX, (m1 << 8) | 0x08);
1243 if (scp->sc->adp->va_type == KD_VGA)
1244 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1246 outw(GDCIDX, 0xff08); /* bit mask */
1247 outw(GDCIDX, 0x0000); /* set/reset */
1248 outw(GDCIDX, 0x0001); /* set/reset enable */
1252 remove_pxlmouse_planar(scr_stat *scp, int x, int y)
1254 const struct mousedata *mdp;
1256 int bx, by, i, line_width, xend, xoff, yend, yoff;
1258 mdp = scp->mouse_data;
1261 * It is only necessary to remove the mouse image where it overlaps
1262 * the border. Determine the overlap, and do nothing if it is empty.
1264 bx = (scp->xoff + scp->xsize) * 8;
1265 by = (scp->yoff + scp->ysize) * scp->font_size;
1266 xend = imin(x + mdp->md_width, scp->xpixel);
1267 yend = imin(y + mdp->md_height, scp->ypixel);
1268 if (xend <= bx && yend <= by)
1271 /* Repaint the non-empty overlap. */
1272 line_width = scp->sc->adp->va_line_width;
1273 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1274 outw(GDCIDX, 0x0003); /* data rotate/function select */
1275 outw(GDCIDX, 0x0f01); /* set/reset enable */
1276 outw(GDCIDX, 0xff08); /* bit mask */
1277 outw(GDCIDX, (scp->border << 8) | 0x00); /* set/reset */
1278 for (i = x / 8, xoff = i * 8; xoff < xend; ++i, xoff += 8) {
1279 yoff = (xoff >= bx) ? y : by;
1280 p = scp->sc->adp->va_window + yoff * line_width + i;
1281 for (; yoff < yend; ++yoff, p += line_width)
1284 outw(GDCIDX, 0x0000); /* set/reset */
1285 outw(GDCIDX, 0x0001); /* set/reset enable */
1289 vga_pxlmouse_direct(scr_stat *scp, int x, int y, int on)
1291 const struct mousedata *mdp;
1293 int line_width, pixel_size;
1297 mdp = scp->mouse_data;
1300 * Determine overlap with the border and then if removing, do nothing
1301 * if the overlap is empty.
1303 xend = imin(x + mdp->md_width, scp->xpixel);
1304 yend = imin(y + mdp->md_height, scp->ypixel);
1305 if (!on && xend <= (scp->xoff + scp->xsize) * 8 &&
1306 yend <= (scp->yoff + scp->ysize) * scp->font_size)
1309 line_width = scp->sc->adp->va_line_width;
1310 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1315 /* Repaint overlap with the border (mess up the corner a little). */
1316 p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1317 for (i = 0; i < yend - y; i++, p += line_width)
1318 for (j = xend - x - 1; j >= 0; j--)
1319 DRAW_PIXEL(scp, p + j * pixel_size, scp->border);
1324 p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1325 for (i = 0; i < yend - y; i++, p += line_width)
1326 for (j = xend - x - 1; j >= 0; j--)
1327 if (mdp->md_interior[i] & (1 << (15 - j)))
1328 DRAW_PIXEL(scp, p + j * pixel_size, 15);
1329 else if (mdp->md_border[i] & (1 << (15 - j)))
1330 DRAW_PIXEL(scp, p + j * pixel_size, 0);
1334 vga_pxlmouse_planar(scr_stat *scp, int x, int y, int on)
1337 draw_pxlmouse_planar(scp, x, y);
1339 remove_pxlmouse_planar(scp, x, y);
1342 #endif /* SC_NO_CUTPASTE */
1343 #endif /* SC_PIXEL_MODE */
1345 #ifndef SC_NO_MODE_CHANGE
1347 /* graphics mode renderer */
1350 vga_grborder(scr_stat *scp, int color)
1352 vidd_set_border(scp->sc->adp, color);