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_setmdp(scr_stat *scp)
350 #if !defined(SC_NO_CUTPASTE) && \
351 (!defined(SC_ALT_MOUSE_IMAGE) || defined(SC_PIXEL_MODE))
352 const struct mousedata *mdp;
353 const struct mousedata * const *mdpp;
354 int aspect, best_i, best_v, i, n, v, wb, wi, xpixel, ypixel;
356 xpixel = scp->xpixel;
357 ypixel = scp->ypixel;
358 if (scp->sc->adp->va_flags & V_ADP_CWIDTH9)
359 xpixel = xpixel * 9 / 8;
361 /* If 16:9 +-1%, assume square pixels, else scale to 4:3 or full. */
362 aspect = xpixel * 900 / ypixel / 16;
363 if (aspect < 99 || aspect > 100)
364 aspect = xpixel * 300 / ypixel / 4 * vga_aspect_scale / 100;
367 * Use 10x16 cursors except even with 8x8 fonts except in ~200-
368 * line modes where pixels are very large and in text mode where
369 * even 13 pixels high is really 4 too many. Clipping a 16-high
370 * cursor at 9-high gives a variable tail which looks better than
371 * a smaller cursor with a constant tail.
373 * XXX: the IS*SC() macros don't work when this is called at the
374 * end of a mode switch since UNKNOWN_SC is still set.
376 if (scp->font_size <= 8 &&
377 (ypixel < 300 || !(scp->status & PIXEL_MODE))) {
378 mdpp = &mousesmall[0];
379 n = nitems(mousesmall);
381 mdpp = &mouselarge[0];
382 n = nitems(mouselarge);
384 if (scp->status & PIXEL_MODE) {
393 for (i = 0; i < n; i++) {
394 v = (wb * abs(mdpp[i]->md_baspect - aspect) +
395 wi * abs(mdpp[i]->md_iaspect - aspect)) / aspect;
402 scp->mouse_data = mdp;
403 #endif /* !SC_NO_CUTPASTE && (!SC_ALT_MOUSE_IMAGE || SC_PIXEL_MODE) */
407 vga_nop(scr_stat *scp)
411 /* text mode renderer */
414 vga_txtclear(scr_stat *scp, int c, int attr)
416 sc_vtb_clear(&scp->scr, c, attr);
420 vga_txtborder(scr_stat *scp, int color)
422 vidd_set_border(scp->sc->adp, color);
426 vga_txtdraw(scr_stat *scp, int from, int count, int flip)
432 if (from + count > scp->xsize*scp->ysize)
433 count = scp->xsize*scp->ysize - from;
436 for (p = sc_vtb_pointer(&scp->scr, from); count-- > 0; ++from) {
437 c = sc_vtb_getc(&scp->vtb, from);
438 a = sc_vtb_geta(&scp->vtb, from);
439 a = (a & 0x8800) | ((a & 0x7000) >> 4)
440 | ((a & 0x0700) << 4);
441 p = sc_vtb_putchar(&scp->scr, p, c, a);
444 sc_vtb_copy(&scp->vtb, from, &scp->scr, from, count);
449 vga_txtcursor_shape(scr_stat *scp, int base, int height, int blink)
452 if (base < 0 || base >= scp->font_size)
454 /* the caller may set height <= 0 in order to disable the cursor */
455 vidd_set_hw_cursor_shape(scp->sc->adp, base, height,
456 scp->font_size, blink);
460 draw_txtcharcursor(scr_stat *scp, int at, u_short c, u_short a, int flip)
466 #ifndef SC_NO_FONT_LOADING
467 if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
472 if (scp->font_size < 14) {
475 } else if (scp->font_size >= 16) {
482 if (scp->curs_attr.base >= h)
486 | ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
487 bcopy(font + c*h, font + sc->cursor_char*h, h);
488 font = font + sc->cursor_char*h;
489 for (i = imax(h - scp->curs_attr.base - scp->curs_attr.height, 0);
490 i < h - scp->curs_attr.base; ++i) {
494 vidd_load_font(sc->adp, 0, h, 8, font, sc->cursor_char, 1);
495 sc_vtb_putc(&scp->scr, at, sc->cursor_char, a);
497 #endif /* SC_NO_FONT_LOADING */
499 if ((a & 0x7000) == 0x7000) {
501 if ((a & 0x0700) == 0)
505 if ((a & 0x0700) == 0x0700)
510 | ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
511 sc_vtb_putc(&scp->scr, at, c, a);
516 vga_txtcursor(scr_stat *scp, int at, int blink, int on, int flip)
518 video_adapter_t *adp;
521 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
526 scp->status |= VR_CURSOR_BLINK;
528 scp->status |= VR_CURSOR_ON;
529 vidd_set_hw_cursor(adp, at%scp->xsize,
532 if (scp->status & VR_CURSOR_ON)
533 vidd_set_hw_cursor(adp, -1, -1);
534 scp->status &= ~VR_CURSOR_ON;
537 scp->status &= ~VR_CURSOR_BLINK;
539 scp->status |= VR_CURSOR_ON;
540 draw_txtcharcursor(scp, at,
541 sc_vtb_getc(&scp->vtb, at),
542 sc_vtb_geta(&scp->vtb, at),
545 cursor_attr = sc_vtb_geta(&scp->vtb, at);
547 cursor_attr = (cursor_attr & 0x8800)
548 | ((cursor_attr & 0x7000) >> 4)
549 | ((cursor_attr & 0x0700) << 4);
550 if (scp->status & VR_CURSOR_ON)
551 sc_vtb_putc(&scp->scr, at,
552 sc_vtb_getc(&scp->vtb, at),
554 scp->status &= ~VR_CURSOR_ON;
560 vga_txtblink(scr_stat *scp, int at, int flip)
564 int sc_txtmouse_no_retrace_wait;
566 #ifndef SC_NO_CUTPASTE
569 draw_txtmouse(scr_stat *scp, int x, int y)
571 #ifndef SC_ALT_MOUSE_IMAGE
572 if (ISMOUSEAVAIL(scp->sc->adp->va_flags)) {
573 const struct mousedata *mdp;
574 uint32_t border, interior;
575 u_char font_buf[128];
579 int xoffset, yoffset;
583 mdp = scp->mouse_data;
585 /* prepare mousepointer char's bitmaps */
586 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
587 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos)*scp->font_size,
588 &font_buf[0], scp->font_size);
589 bcopy(scp->font + sc_vtb_getc(&scp->scr, pos + 1)*scp->font_size,
590 &font_buf[32], scp->font_size);
592 + sc_vtb_getc(&scp->scr, pos + scp->xsize)*scp->font_size,
593 &font_buf[64], scp->font_size);
595 + sc_vtb_getc(&scp->scr, pos + scp->xsize + 1)*scp->font_size,
596 &font_buf[96], scp->font_size);
597 for (i = 0; i < scp->font_size; ++i) {
598 cursor[i] = font_buf[i]<<8 | font_buf[i+32];
599 cursor[i + scp->font_size] = font_buf[i+64]<<8 | font_buf[i+96];
602 /* now and-or in the mousepointer image */
604 yoffset = y%scp->font_size;
605 for (i = 0; i < 16; ++i) {
606 border = mdp->md_border[i] << 8; /* avoid right shifting out */
607 interior = mdp->md_interior[i] << 8;
608 border >>= xoffset; /* normalize */
609 interior >>= xoffset;
610 if (scp->sc->adp->va_flags & V_ADP_CWIDTH9) {
611 /* skip gaps between characters */
612 border = (border & 0xff0000) |
613 (border & 0x007f80) << 1 |
614 (border & 0x00003f) << 2;
615 interior = (interior & 0xff0000) |
616 (interior & 0x007f80) << 1 |
617 (interior & 0x00003f) << 2;
619 border >>= 8; /* back to normal position */
621 cursor[i + yoffset] = (cursor[i + yoffset] & ~border) |
624 for (i = 0; i < scp->font_size; ++i) {
625 font_buf[i] = (cursor[i] & 0xff00) >> 8;
626 font_buf[i + 32] = cursor[i] & 0xff;
627 font_buf[i + 64] = (cursor[i + scp->font_size] & 0xff00) >> 8;
628 font_buf[i + 96] = cursor[i + scp->font_size] & 0xff;
632 /* wait for vertical retrace to avoid jitter on some videocards */
633 crtc_addr = scp->sc->adp->va_crtc_addr;
634 while (!sc_txtmouse_no_retrace_wait &&
635 !(inb(crtc_addr + 6) & 0x08))
638 c = scp->sc->mouse_char;
639 vidd_load_font(scp->sc->adp, 0, 32, 8, font_buf, c, 4);
641 sc_vtb_putc(&scp->scr, pos, c, sc_vtb_geta(&scp->scr, pos));
642 /* FIXME: may be out of range! */
643 sc_vtb_putc(&scp->scr, pos + scp->xsize, c + 2,
644 sc_vtb_geta(&scp->scr, pos + scp->xsize));
645 if (x < (scp->xsize - 1)*8) {
646 sc_vtb_putc(&scp->scr, pos + 1, c + 1,
647 sc_vtb_geta(&scp->scr, pos + 1));
648 sc_vtb_putc(&scp->scr, pos + scp->xsize + 1, c + 3,
649 sc_vtb_geta(&scp->scr, pos + scp->xsize + 1));
652 #endif /* SC_ALT_MOUSE_IMAGE */
654 /* Red, magenta and brown are mapped to green to to keep it readable */
655 static const int col_conv[16] = {
656 6, 6, 6, 6, 2, 2, 2, 6, 14, 14, 14, 14, 10, 10, 10, 14
662 pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
663 a = sc_vtb_geta(&scp->scr, pos);
664 if (scp->sc->adp->va_flags & V_ADP_COLOR)
665 color = (col_conv[(a & 0xf000) >> 12] << 12)
666 | ((a & 0x0f00) | 0x0800);
668 color = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
669 sc_vtb_putc(&scp->scr, pos, sc_vtb_getc(&scp->scr, pos), color);
674 remove_txtmouse(scr_stat *scp, int x, int y)
679 vga_txtmouse(scr_stat *scp, int x, int y, int on)
682 draw_txtmouse(scp, x, y);
684 remove_txtmouse(scp, x, y);
687 #endif /* SC_NO_CUTPASTE */
691 /* pixel (raster text) mode renderer */
694 vga_rndrinit(scr_stat *scp)
696 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PLANAR) {
697 scp->rndr->clear = vga_pxlclear_planar;
698 scp->rndr->draw_border = vga_pxlborder_planar;
699 scp->rndr->draw = vga_vgadraw_planar;
700 scp->rndr->draw_cursor = vga_pxlcursor_planar;
701 scp->rndr->blink_cursor = vga_pxlblink_planar;
702 scp->rndr->draw_mouse = vga_pxlmouse_planar;
704 if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT ||
705 scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PACKED) {
706 scp->rndr->clear = vga_pxlclear_direct;
707 scp->rndr->draw_border = vga_pxlborder_direct;
708 scp->rndr->draw = vga_vgadraw_direct;
709 scp->rndr->draw_cursor = vga_pxlcursor_direct;
710 scp->rndr->blink_cursor = vga_pxlblink_direct;
711 scp->rndr->draw_mouse = vga_pxlmouse_direct;
716 vga_pxlclear_direct(scr_stat *scp, int c, int attr)
724 line_width = scp->sc->adp->va_line_width;
725 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
726 lines = scp->ysize * scp->font_size;
727 p = scp->sc->adp->va_window +
728 line_width * scp->yoff * scp->font_size +
729 scp->xoff * 8 * pixel_size;
731 for (i = 0; i < lines; ++i) {
732 bzero_io((void *)p, scp->xsize * 8 * pixel_size);
738 vga_pxlclear_planar(scr_stat *scp, int c, int attr)
745 /* XXX: we are just filling the screen with the background color... */
746 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
747 outw(GDCIDX, 0x0003); /* data rotate/function select */
748 outw(GDCIDX, 0x0f01); /* set/reset enable */
749 outw(GDCIDX, 0xff08); /* bit mask */
750 outw(GDCIDX, ((attr & 0xf000) >> 4) | 0x00); /* set/reset */
751 line_width = scp->sc->adp->va_line_width;
752 lines = scp->ysize*scp->font_size;
753 p = scp->sc->adp->va_window + line_width*scp->yoff*scp->font_size
755 for (i = 0; i < lines; ++i) {
756 bzero_io((void *)p, scp->xsize);
759 outw(GDCIDX, 0x0000); /* set/reset */
760 outw(GDCIDX, 0x0001); /* set/reset enable */
764 vga_pxlborder_direct(scr_stat *scp, int color)
775 line_width = scp->sc->adp->va_line_width;
776 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
779 s = scp->sc->adp->va_window;
780 e = s + line_width * scp->yoff * scp->font_size;
782 for (f = s; f < e; f += pixel_size)
783 DRAW_PIXEL(scp, f, color);
786 y = (scp->yoff + scp->ysize) * scp->font_size;
788 if (scp->ypixel > y) {
789 s = scp->sc->adp->va_window + line_width * y;
790 e = s + line_width * (scp->ypixel - y);
792 for (f = s; f < e; f += pixel_size)
793 DRAW_PIXEL(scp, f, color);
796 y = scp->yoff * scp->font_size;
797 x = scp->xpixel / 8 - scp->xoff - scp->xsize;
799 for (i = 0; i < scp->ysize * scp->font_size; ++i) {
801 s = scp->sc->adp->va_window + line_width * (y + i);
802 e = s + scp->xoff * 8 * pixel_size;
804 for (f = s; f < e; f += pixel_size)
805 DRAW_PIXEL(scp, f, color);
809 s = scp->sc->adp->va_window + line_width * (y + i) +
810 scp->xoff * 8 * pixel_size +
811 scp->xsize * 8 * pixel_size;
812 e = s + x * 8 * pixel_size;
814 for (f = s; f < e; f += pixel_size)
815 DRAW_PIXEL(scp, f, color);
821 vga_pxlborder_planar(scr_stat *scp, int color)
829 vidd_set_border(scp->sc->adp, color);
831 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
832 outw(GDCIDX, 0x0003); /* data rotate/function select */
833 outw(GDCIDX, 0x0f01); /* set/reset enable */
834 outw(GDCIDX, 0xff08); /* bit mask */
835 outw(GDCIDX, (color << 8) | 0x00); /* set/reset */
836 line_width = scp->sc->adp->va_line_width;
837 p = scp->sc->adp->va_window;
839 bzero_io((void *)p, line_width*scp->yoff*scp->font_size);
840 y = (scp->yoff + scp->ysize)*scp->font_size;
842 bzero_io((void *)(p + line_width*y), line_width*(scp->ypixel - y));
843 y = scp->yoff*scp->font_size;
844 x = scp->xpixel/8 - scp->xoff - scp->xsize;
845 for (i = 0; i < scp->ysize*scp->font_size; ++i) {
847 bzero_io((void *)(p + line_width*(y + i)), scp->xoff);
849 bzero_io((void *)(p + line_width*(y + i)
850 + scp->xoff + scp->xsize), x);
852 outw(GDCIDX, 0x0000); /* set/reset */
853 outw(GDCIDX, 0x0001); /* set/reset enable */
857 vga_vgadraw_direct(scr_stat *scp, int from, int count, int flip)
862 u_short col1, col2, color;
863 int line_width, pixel_size;
867 line_width = scp->sc->adp->va_line_width;
868 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
870 d = GET_PIXEL(scp, from, 8 * pixel_size, line_width);
872 if (from + count > scp->xsize * scp->ysize)
873 count = scp->xsize * scp->ysize - from;
875 for (i = from; count-- > 0; ++i) {
876 a = sc_vtb_geta(&scp->vtb, i);
879 col1 = (((a & 0x7000) >> 4) | (a & 0x0800)) >> 8;
880 col2 = (((a & 0x8000) >> 4) | (a & 0x0700)) >> 8;
882 col1 = (a & 0x0f00) >> 8;
883 col2 = (a & 0xf000) >> 12;
887 f = &(scp->font[sc_vtb_getc(&scp->vtb, i) * scp->font_size]);
889 for (j = 0; j < scp->font_size; ++j, ++f) {
890 for (k = 0; k < 8; ++k) {
891 color = *f & (1 << (7 - k)) ? col1 : col2;
892 DRAW_PIXEL(scp, e + pixel_size * k, color);
900 if ((i % scp->xsize) == scp->xsize - 1)
901 d += scp->font_size * line_width -
902 scp->xsize * 8 * pixel_size;
907 vga_vgadraw_planar(scr_stat *scp, int from, int count, int flip)
919 line_width = scp->sc->adp->va_line_width;
921 d = GET_PIXEL(scp, from, 1, line_width);
923 if (scp->sc->adp->va_type == KD_VGA) {
924 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
925 outw(GDCIDX, 0xff08); /* bit mask */
927 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
928 outw(GDCIDX, 0x0003); /* data rotate/function select */
929 outw(GDCIDX, 0x0f01); /* set/reset enable */
931 if (from + count > scp->xsize*scp->ysize)
932 count = scp->xsize*scp->ysize - from;
933 for (i = from; count-- > 0; ++i) {
934 a = sc_vtb_geta(&scp->vtb, i);
936 col1 = ((a & 0x7000) >> 4) | (a & 0x0800);
937 col2 = ((a & 0x8000) >> 4) | (a & 0x0700);
940 col2 = (a & 0xf000) >> 4;
942 /* set background color in EGA/VGA latch */
946 outw(GDCIDX, bg | 0x00); /* set/reset */
947 if (scp->sc->adp->va_type != KD_VGA)
948 outw(GDCIDX, 0xff08); /* bit mask */
950 c = readb(d); /* set bg color in the latch */
952 /* foreground color */
955 outw(GDCIDX, col1 | 0x00); /* set/reset */
958 f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
959 for (j = 0; j < scp->font_size; ++j, ++f) {
960 if (scp->sc->adp->va_type == KD_VGA)
963 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
969 if ((i % scp->xsize) == scp->xsize - 1)
970 d += scp->font_size * line_width - scp->xsize;
972 if (scp->sc->adp->va_type == KD_VGA)
973 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
975 outw(GDCIDX, 0xff08); /* bit mask */
976 outw(GDCIDX, 0x0000); /* set/reset */
977 outw(GDCIDX, 0x0001); /* set/reset enable */
981 vga_pxlcursor_shape(scr_stat *scp, int base, int height, int blink)
987 draw_pxlcursor_direct(scr_stat *scp, int at, int on, int flip)
991 int line_width, pixel_size;
993 int col1, col2, color;
997 line_width = scp->sc->adp->va_line_width;
998 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1000 d = GET_PIXEL(scp, at, 8 * pixel_size, line_width) +
1001 (scp->font_size - scp->curs_attr.base - 1) * line_width;
1003 a = sc_vtb_geta(&scp->vtb, at);
1006 col1 = ((on) ? (a & 0x0f00) : ((a & 0xf000) >> 4)) >> 8;
1007 col2 = ((on) ? ((a & 0xf000) >> 4) : (a & 0x0f00)) >> 8;
1009 col1 = ((on) ? ((a & 0xf000) >> 4) : (a & 0x0f00)) >> 8;
1010 col2 = ((on) ? (a & 0x0f00) : ((a & 0xf000) >> 4)) >> 8;
1013 f = &(scp->font[sc_vtb_getc(&scp->vtb, at) * scp->font_size +
1014 scp->font_size - scp->curs_attr.base - 1]);
1016 height = imin(scp->curs_attr.height, scp->font_size);
1018 for (i = 0; i < height; ++i, --f) {
1019 for (j = 0; j < 8; ++j) {
1020 color = *f & (1 << (7 - j)) ? col1 : col2;
1021 DRAW_PIXEL(scp, d + pixel_size * j, color);
1029 draw_pxlcursor_planar(scr_stat *scp, int at, int on, int flip)
1040 line_width = scp->sc->adp->va_line_width;
1042 d = GET_PIXEL(scp, at, 1, line_width) +
1043 (scp->font_size - scp->curs_attr.base - 1) * line_width;
1045 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1046 outw(GDCIDX, 0x0003); /* data rotate/function select */
1047 outw(GDCIDX, 0x0f01); /* set/reset enable */
1048 /* set background color in EGA/VGA latch */
1049 a = sc_vtb_geta(&scp->vtb, at);
1051 col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
1053 col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
1054 outw(GDCIDX, col | 0x00); /* set/reset */
1055 outw(GDCIDX, 0xff08); /* bit mask */
1057 c = readb(d); /* set bg color in the latch */
1058 /* foreground color */
1060 col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
1062 col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
1063 outw(GDCIDX, col | 0x00); /* set/reset */
1064 f = &(scp->font[sc_vtb_getc(&scp->vtb, at)*scp->font_size
1065 + scp->font_size - scp->curs_attr.base - 1]);
1066 height = imin(scp->curs_attr.height, scp->font_size);
1067 for (i = 0; i < height; ++i, --f) {
1068 outw(GDCIDX, (*f << 8) | 0x08); /* bit mask */
1072 outw(GDCIDX, 0x0000); /* set/reset */
1073 outw(GDCIDX, 0x0001); /* set/reset enable */
1074 outw(GDCIDX, 0xff08); /* bit mask */
1077 static int pxlblinkrate = 0;
1080 vga_pxlcursor_direct(scr_stat *scp, int at, int blink, int on, int flip)
1082 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
1087 scp->status |= VR_CURSOR_ON;
1088 draw_pxlcursor_direct(scp, at, on, flip);
1089 } else if (++pxlblinkrate & 4) {
1091 scp->status ^= VR_CURSOR_ON;
1092 draw_pxlcursor_direct(scp, at,
1093 scp->status & VR_CURSOR_ON,
1097 if (scp->status & VR_CURSOR_ON)
1098 draw_pxlcursor_direct(scp, at, on, flip);
1099 scp->status &= ~VR_CURSOR_ON;
1102 scp->status |= VR_CURSOR_BLINK;
1104 scp->status &= ~VR_CURSOR_BLINK;
1108 vga_pxlcursor_planar(scr_stat *scp, int at, int blink, int on, int flip)
1110 if (scp->curs_attr.height <= 0) /* the text cursor is disabled */
1115 scp->status |= VR_CURSOR_ON;
1116 draw_pxlcursor_planar(scp, at, on, flip);
1117 } else if (++pxlblinkrate & 4) {
1119 scp->status ^= VR_CURSOR_ON;
1120 draw_pxlcursor_planar(scp, at,
1121 scp->status & VR_CURSOR_ON,
1125 if (scp->status & VR_CURSOR_ON)
1126 draw_pxlcursor_planar(scp, at, on, flip);
1127 scp->status &= ~VR_CURSOR_ON;
1130 scp->status |= VR_CURSOR_BLINK;
1132 scp->status &= ~VR_CURSOR_BLINK;
1136 vga_pxlblink_direct(scr_stat *scp, int at, int flip)
1138 if (!(scp->status & VR_CURSOR_BLINK))
1140 if (!(++pxlblinkrate & 4))
1143 scp->status ^= VR_CURSOR_ON;
1144 draw_pxlcursor_direct(scp, at, scp->status & VR_CURSOR_ON, flip);
1148 vga_pxlblink_planar(scr_stat *scp, int at, int flip)
1150 if (!(scp->status & VR_CURSOR_BLINK))
1152 if (!(++pxlblinkrate & 4))
1155 scp->status ^= VR_CURSOR_ON;
1156 draw_pxlcursor_planar(scp, at, scp->status & VR_CURSOR_ON, flip);
1159 #ifndef SC_NO_CUTPASTE
1162 draw_pxlmouse_planar(scr_stat *scp, int x, int y)
1164 const struct mousedata *mdp;
1173 mdp = scp->mouse_data;
1174 line_width = scp->sc->adp->va_line_width;
1175 xoff = (x - scp->xoff*8)%8;
1176 yoff = y - rounddown(y, line_width);
1177 ymax = imin(y + mdp->md_height, scp->ypixel);
1179 if (scp->sc->adp->va_type == KD_VGA) {
1180 outw(GDCIDX, 0x0305); /* read mode 0, write mode 3 */
1181 outw(GDCIDX, 0xff08); /* bit mask */
1183 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1184 outw(GDCIDX, 0x0003); /* data rotate/function select */
1185 outw(GDCIDX, 0x0f01); /* set/reset enable */
1187 outw(GDCIDX, (0 << 8) | 0x00); /* set/reset */
1188 p = scp->sc->adp->va_window + line_width*y + x/8;
1189 for (i = y, j = 0; i < ymax; ++i, ++j) {
1190 m = mdp->md_border[j] << 8 >> xoff;
1191 for (k = 0; k < 3; ++k) {
1192 m1 = m >> (8 * (2 - k));
1193 if (m1 != 0 && x + 8 * k < scp->xpixel) {
1195 if (scp->sc->adp->va_type == KD_VGA)
1199 outw(GDCIDX, (m1 << 8) | 0x08);
1206 outw(GDCIDX, (15 << 8) | 0x00); /* set/reset */
1207 p = scp->sc->adp->va_window + line_width*y + x/8;
1208 for (i = y, j = 0; i < ymax; ++i, ++j) {
1209 m = mdp->md_interior[j] << 8 >> xoff;
1210 for (k = 0; k < 3; ++k) {
1211 m1 = m >> (8 * (2 - k));
1212 if (m1 != 0 && x + 8 * k < scp->xpixel) {
1214 if (scp->sc->adp->va_type == KD_VGA)
1218 outw(GDCIDX, (m1 << 8) | 0x08);
1225 if (scp->sc->adp->va_type == KD_VGA)
1226 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1228 outw(GDCIDX, 0xff08); /* bit mask */
1229 outw(GDCIDX, 0x0000); /* set/reset */
1230 outw(GDCIDX, 0x0001); /* set/reset enable */
1234 remove_pxlmouse_planar(scr_stat *scp, int x, int y)
1236 const struct mousedata *mdp;
1238 int bx, by, i, line_width, xend, xoff, yend, yoff;
1240 mdp = scp->mouse_data;
1243 * It is only necessary to remove the mouse image where it overlaps
1244 * the border. Determine the overlap, and do nothing if it is empty.
1246 bx = (scp->xoff + scp->xsize) * 8;
1247 by = (scp->yoff + scp->ysize) * scp->font_size;
1248 xend = imin(x + mdp->md_width, scp->xpixel);
1249 yend = imin(y + mdp->md_height, scp->ypixel);
1250 if (xend <= bx && yend <= by)
1253 /* Repaint the non-empty overlap. */
1254 line_width = scp->sc->adp->va_line_width;
1255 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
1256 outw(GDCIDX, 0x0003); /* data rotate/function select */
1257 outw(GDCIDX, 0x0f01); /* set/reset enable */
1258 outw(GDCIDX, 0xff08); /* bit mask */
1259 outw(GDCIDX, (scp->border << 8) | 0x00); /* set/reset */
1260 for (i = x / 8, xoff = i * 8; xoff < xend; ++i, xoff += 8) {
1261 yoff = (xoff >= bx) ? y : by;
1262 p = scp->sc->adp->va_window + yoff * line_width + i;
1263 for (; yoff < yend; ++yoff, p += line_width)
1266 outw(GDCIDX, 0x0000); /* set/reset */
1267 outw(GDCIDX, 0x0001); /* set/reset enable */
1271 vga_pxlmouse_direct(scr_stat *scp, int x, int y, int on)
1273 const struct mousedata *mdp;
1275 int line_width, pixel_size;
1279 mdp = scp->mouse_data;
1282 * Determine overlap with the border and then if removing, do nothing
1283 * if the overlap is empty.
1285 xend = imin(x + mdp->md_width, scp->xpixel);
1286 yend = imin(y + mdp->md_height, scp->ypixel);
1287 if (!on && xend <= (scp->xoff + scp->xsize) * 8 &&
1288 yend <= (scp->yoff + scp->ysize) * scp->font_size)
1291 line_width = scp->sc->adp->va_line_width;
1292 pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1297 /* Repaint overlap with the border (mess up the corner a little). */
1298 p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1299 for (i = 0; i < yend - y; i++, p += line_width)
1300 for (j = xend - x - 1; j >= 0; j--)
1301 DRAW_PIXEL(scp, p + j * pixel_size, scp->border);
1306 p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1307 for (i = 0; i < yend - y; i++, p += line_width)
1308 for (j = xend - x - 1; j >= 0; j--)
1309 if (mdp->md_interior[i] & (1 << (15 - j)))
1310 DRAW_PIXEL(scp, p + j * pixel_size, 15);
1311 else if (mdp->md_border[i] & (1 << (15 - j)))
1312 DRAW_PIXEL(scp, p + j * pixel_size, 0);
1316 vga_pxlmouse_planar(scr_stat *scp, int x, int y, int on)
1319 draw_pxlmouse_planar(scp, x, y);
1321 remove_pxlmouse_planar(scp, x, y);
1324 #endif /* SC_NO_CUTPASTE */
1325 #endif /* SC_PIXEL_MODE */
1327 #ifndef SC_NO_MODE_CHANGE
1329 /* graphics mode renderer */
1332 vga_grborder(scr_stat *scp, int color)
1334 vidd_set_border(scp->sc->adp, color);