xref: /freebsd/sys/dev/syscons/scvgarndr.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The DragonFly Project
8  * by Sascha Wildner <saw@online.de>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer as
15  *    the first lines of this file unmodified.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #include <sys/cdefs.h>
34 #include "opt_syscons.h"
35 #include "opt_vga.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/fbio.h>
42 #include <sys/consio.h>
43 #include <sys/sysctl.h>
44 
45 #include <machine/bus.h>
46 
47 #include <dev/fb/fbreg.h>
48 #include <dev/fb/vgareg.h>
49 #include <dev/syscons/syscons.h>
50 
51 #include <isa/isareg.h>
52 
53 #ifndef SC_RENDER_DEBUG
54 #define SC_RENDER_DEBUG		0
55 #endif
56 
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;
65 #else
66 #define vga_txtmouse		(vr_draw_mouse_t *)vga_nop
67 #endif
68 
69 #ifdef SC_PIXEL_MODE
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;
85 #else
86 #define vga_pxlmouse_direct	(vr_draw_mouse_t *)vga_nop
87 #define vga_pxlmouse_planar	(vr_draw_mouse_t *)vga_nop
88 #endif
89 #endif /* SC_PIXEL_MODE */
90 
91 #ifndef SC_NO_MODE_CHANGE
92 static vr_draw_border_t		vga_grborder;
93 #endif
94 
95 static void			vga_nop(scr_stat *scp);
96 
97 static sc_rndr_sw_t txtrndrsw = {
98 	(vr_init_t *)vga_nop,
99 	vga_txtclear,
100 	vga_txtborder,
101 	vga_txtdraw,
102 	vga_txtcursor_shape,
103 	vga_txtcursor,
104 	vga_txtblink,
105 	(vr_set_mouse_t *)vga_nop,
106 	vga_txtmouse,
107 };
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);
112 
113 #ifdef SC_PIXEL_MODE
114 static sc_rndr_sw_t vgarndrsw = {
115 	vga_rndrinit,
116 	(vr_clear_t *)vga_nop,
117 	(vr_draw_border_t *)vga_nop,
118 	(vr_draw_t *)vga_nop,
119 	vga_pxlcursor_shape,
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,
124 };
125 RENDERER(ega, PIXEL_MODE, vgarndrsw, vga_set);
126 RENDERER(vga, PIXEL_MODE, vgarndrsw, vga_set);
127 #endif /* SC_PIXEL_MODE */
128 
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,
133 	vga_grborder,
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,
140 };
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 */
145 
146 RENDERER_MODULE(vga, vga_set);
147 
148 #ifndef SC_NO_CUTPASTE
149 #if !defined(SC_ALT_MOUSE_IMAGE) || defined(SC_PIXEL_MODE)
150 struct mousedata {
151 	u_short	md_border[16];
152 	u_short	md_interior[16];
153 	u_char	md_width;
154 	u_char	md_height;
155 	u_char	md_baspect;
156 	u_char	md_iaspect;
157 	const char *md_name;
158 };
159 
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",
166 };
167 
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",
174 };
175 
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",
182 };
183 
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",
190 };
191 
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",
198 };
199 
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",
206 };
207 
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",
214 };
215 
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",
222 };
223 
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",
230 };
231 
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",
238 };
239 
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",
246 };
247 
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",
254 };
255 
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",
262 };
263 
264 static const struct mousedata * const mouselarge[] = {
265 	&mouse10x16_50,
266 	&mouse8x14_67,
267 	&mouse10x16_75,
268 	&mouse10x16_90,
269 	&mouse10x16_100,
270 	&mouse10x16_120,
271 	&mouse10x16_133,
272 	&mouse14x10_240,
273 };
274 
275 static const struct mousedata * const mousesmall[] = {
276 	&mouse8x14_67,
277 	&mouse8x13_75,
278 	&mouse9x13_90,
279 	&mouse9x13_100,
280 	&mouse10x14_120,
281 	&mouse9x13_133,
282 	&mouse14x10_240,
283 };
284 #endif
285 #endif
286 
287 #ifdef SC_PIXEL_MODE
288 #define	GET_PIXEL(scp, pos, x, w)					\
289 ({									\
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);		\
295 })
296 
297 #define	DRAW_PIXEL(scp, pos, color) do {				\
298 	switch ((scp)->sc->adp->va_info.vi_depth) {			\
299 	case 32:							\
300 		writel((pos), vga_palette32[color]);			\
301 		break;							\
302 	case 24:							\
303 		if (((pos) & 1) == 0) {					\
304 			writew((pos), vga_palette32[color]);		\
305 			writeb((pos) + 2, vga_palette32[color] >> 16);	\
306 		} else {						\
307 			writeb((pos), vga_palette32[color]);		\
308 			writew((pos) + 1, vga_palette32[color] >> 8);	\
309 		}							\
310 		break;							\
311 	case 16:							\
312 		if ((scp)->sc->adp->va_info.vi_pixel_fsizes[1] == 5)	\
313 			writew((pos), vga_palette15[color]);		\
314 		else							\
315 			writew((pos), vga_palette16[color]);		\
316 		break;							\
317 	case 15:							\
318 		writew((pos), vga_palette15[color]);			\
319 		break;							\
320 	case 8:								\
321 		writeb((pos), (uint8_t)(color));			\
322 	}								\
323 } while (0)
324 
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
330 };
331 
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
335 };
336 
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
340 };
341 #endif
342 
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");
346 
347 static u_short
348 vga_flipattr(u_short a, int blink)
349 {
350 	if (blink)
351 		a = (a & 0x8800) | ((a & 0x7000) >> 4) |
352 		    ((a & 0x0700) << 4);
353 	else
354 		a = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
355 	return (a);
356 }
357 
358 static u_short
359 vga_cursorattr_adj(scr_stat *scp, u_short a, int blink)
360 {
361 	int i;
362 	u_short bg, bgmask, fg, newbg;
363 
364 	/*
365 	 * The cursor attribute is usually that of the underlying char
366 	 * with only the bg changed, to the first preferred color that
367 	 * differs from both the fg and bg.  If there is no such color,
368 	 * use reverse video.
369 	 */
370 	bgmask = blink ? 0x7000 : 0xf000;
371 	bg = a & bgmask;
372 	fg = a & 0x0f00;
373 	for (i = 0; i < nitems(scp->curs_attr.bg); i++) {
374 		newbg = (scp->curs_attr.bg[i] << 12) & bgmask;
375 		if (newbg != bg && newbg != (fg << 4))
376 			break;
377 	}
378 	if (i == nitems(scp->curs_attr.bg))
379 		return (vga_flipattr(a, blink));
380 	return (fg | newbg | (blink ? a & 0x8000 : 0));
381 }
382 
383 static void
384 vga_setmdp(scr_stat *scp)
385 {
386 #if !defined(SC_NO_CUTPASTE) && \
387    (!defined(SC_ALT_MOUSE_IMAGE) || defined(SC_PIXEL_MODE))
388 	const struct mousedata *mdp;
389 	const struct mousedata * const *mdpp;
390 	int aspect, best_i, best_v, i, n, v, wb, wi, xpixel, ypixel;
391 
392 	xpixel = scp->xpixel;
393 	ypixel = scp->ypixel;
394 	if (scp->sc->adp->va_flags & V_ADP_CWIDTH9)
395 		xpixel = xpixel * 9 / 8;
396 
397 	/* If 16:9 +-1%, assume square pixels, else scale to 4:3 or full. */
398 	aspect = xpixel * 900 / ypixel / 16;
399 	if (aspect < 99 || aspect > 100)
400 		aspect = xpixel * 300 / ypixel / 4 * vga_aspect_scale / 100;
401 
402 	/*
403 	 * Use 10x16 cursors except even with 8x8 fonts except in ~200-
404 	 * line modes where pixels are very large and in text mode where
405 	 * even 13 pixels high is really 4 too many.  Clipping a 16-high
406 	 * cursor at 9-high gives a variable tail which looks better than
407 	 * a smaller cursor with a constant tail.
408 	 *
409 	 * XXX: the IS*SC() macros don't work when this is called at the
410 	 * end of a mode switch since UNKNOWN_SC is still set.
411 	 */
412 	if (scp->font_size <= 8 &&
413 	    (ypixel < 300 || !(scp->status & PIXEL_MODE))) {
414 		mdpp = &mousesmall[0];
415 		n = nitems(mousesmall);
416 	} else {
417 		mdpp = &mouselarge[0];
418 		n = nitems(mouselarge);
419 	}
420 	if (scp->status & PIXEL_MODE) {
421 		wb = 1024;
422 		wi = 256;
423 	} else {
424 		wb = 256;
425 		wi = 1024;
426 	}
427 	best_i = 0;
428 	best_v = 0x7fffffff;
429 	for (i = 0; i < n; i++) {
430 		v = (wb * abs(mdpp[i]->md_baspect - aspect) +
431 		     wi * abs(mdpp[i]->md_iaspect - aspect)) / aspect;
432 		if (best_v > v) {
433 			best_v = v;
434 			best_i = i;
435 		}
436 	}
437 	mdp = mdpp[best_i];
438 	scp->mouse_data = mdp;
439 #endif /* !SC_NO_CUTPASTE && (!SC_ALT_MOUSE_IMAGE || SC_PIXEL_MODE) */
440 }
441 
442 static void
443 vga_nop(scr_stat *scp)
444 {
445 }
446 
447 /* text mode renderer */
448 
449 static void
450 vga_txtclear(scr_stat *scp, int c, int attr)
451 {
452 	sc_vtb_clear(&scp->scr, c, attr);
453 }
454 
455 static void
456 vga_txtborder(scr_stat *scp, int color)
457 {
458 	vidd_set_border(scp->sc->adp, color);
459 }
460 
461 static void
462 vga_txtdraw(scr_stat *scp, int from, int count, int flip)
463 {
464 	vm_offset_t p;
465 	int c;
466 	int a;
467 
468 	if (from + count > scp->xsize*scp->ysize)
469 		count = scp->xsize*scp->ysize - from;
470 
471 	if (flip) {
472 		for (p = sc_vtb_pointer(&scp->scr, from); count-- > 0; ++from) {
473 			c = sc_vtb_getc(&scp->vtb, from);
474 			a = sc_vtb_geta(&scp->vtb, from);
475 			a = vga_flipattr(a, TRUE);
476 			p = sc_vtb_putchar(&scp->scr, p, c, a);
477 		}
478 	} else {
479 		sc_vtb_copy(&scp->vtb, from, &scp->scr, from, count);
480 	}
481 }
482 
483 static void
484 vga_txtcursor_shape(scr_stat *scp, int base, int height, int blink)
485 {
486 	vga_setmdp(scp);
487 	if (base < 0 || base >= scp->font_size)
488 		return;
489 	/* the caller may set height <= 0 in order to disable the cursor */
490 	vidd_set_hw_cursor_shape(scp->sc->adp, base, height,
491 	    scp->font_size, blink);
492 }
493 
494 static void
495 draw_txtcharcursor(scr_stat *scp, int at, u_short c, u_short a, int flip)
496 {
497 #ifndef SC_NO_FONT_LOADING
498 	sc_softc_t *sc;
499 
500 	sc = scp->sc;
501 
502 	if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
503 		unsigned char *font;
504 		int h;
505 		int i;
506 
507 		if (scp->font_size < 14) {
508 			font = sc->font_8;
509 			h = 8;
510 		} else if (scp->font_size >= 16) {
511 			font = sc->font_16;
512 			h = 16;
513 		} else {
514 			font = sc->font_14;
515 			h = 14;
516 		}
517 		if (scp->curs_attr.base >= h)
518 			return;
519 		if (flip)
520 			a = vga_flipattr(a, TRUE);
521 		/*
522 		 * This clause handles partial-block cursors in text mode.
523 		 * We want to change the attribute only under the partial
524 		 * block, but in text mode we can only change full blocks.
525 		 * Use reverse video instead.
526 		 */
527 		bcopy(font + c*h, font + sc->cursor_char*h, h);
528 		font = font + sc->cursor_char*h;
529 		for (i = imax(h - scp->curs_attr.base - scp->curs_attr.height, 0);
530 			i < h - scp->curs_attr.base; ++i) {
531 			font[i] ^= 0xff;
532 		}
533 		/* XXX */
534 		vidd_load_font(sc->adp, 0, h, 8, font, sc->cursor_char, 1);
535 		sc_vtb_putc(&scp->scr, at, sc->cursor_char, a);
536 	} else
537 #endif /* SC_NO_FONT_LOADING */
538 	{
539 		if (flip)
540 			a = vga_flipattr(a, TRUE);
541 		a = vga_cursorattr_adj(scp, a, TRUE);
542 		sc_vtb_putc(&scp->scr, at, c, a);
543 	}
544 }
545 
546 static void
547 vga_txtcursor(scr_stat *scp, int at, int blink, int on, int flip)
548 {
549 	video_adapter_t *adp;
550 	int cursor_attr;
551 
552 	if (scp->curs_attr.height <= 0)	/* the text cursor is disabled */
553 		return;
554 
555 	adp = scp->sc->adp;
556 	if (blink) {
557 		scp->status |= VR_CURSOR_BLINK;
558 		if (on) {
559 			scp->status |= VR_CURSOR_ON;
560 			vidd_set_hw_cursor(adp, at%scp->xsize,
561 			    at/scp->xsize);
562 		} else {
563 			if (scp->status & VR_CURSOR_ON)
564 				vidd_set_hw_cursor(adp, -1, -1);
565 			scp->status &= ~VR_CURSOR_ON;
566 		}
567 	} else {
568 		scp->status &= ~VR_CURSOR_BLINK;
569 		if (on) {
570 			scp->status |= VR_CURSOR_ON;
571 			draw_txtcharcursor(scp, at,
572 					   sc_vtb_getc(&scp->vtb, at),
573 					   sc_vtb_geta(&scp->vtb, at),
574 					   flip);
575 		} else {
576 			cursor_attr = sc_vtb_geta(&scp->vtb, at);
577 			if (flip)
578 				cursor_attr = vga_flipattr(cursor_attr, TRUE);
579 			if (scp->status & VR_CURSOR_ON)
580 				sc_vtb_putc(&scp->scr, at,
581 					    sc_vtb_getc(&scp->vtb, at),
582 					    cursor_attr);
583 			scp->status &= ~VR_CURSOR_ON;
584 		}
585 	}
586 }
587 
588 static void
589 vga_txtblink(scr_stat *scp, int at, int flip)
590 {
591 }
592 
593 int sc_txtmouse_no_retrace_wait;
594 
595 #ifndef SC_NO_CUTPASTE
596 
597 static void
598 draw_txtmouse(scr_stat *scp, int x, int y)
599 {
600 #ifndef SC_ALT_MOUSE_IMAGE
601     if (ISMOUSEAVAIL(scp->sc->adp->va_flags)) {
602 	const struct mousedata *mdp;
603 	uint32_t border, interior;
604 	u_char font_buf[128];
605 	u_short cursor[32];
606 	u_char c;
607 	int pos;
608 	int xoffset, yoffset;
609 	int crtc_addr;
610 	int i;
611 
612 	mdp = scp->mouse_data;
613 
614 	/* prepare mousepointer char's bitmaps */
615 	pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
616 	bcopy(scp->font + sc_vtb_getc(&scp->scr, pos)*scp->font_size,
617 	      &font_buf[0], scp->font_size);
618 	bcopy(scp->font + sc_vtb_getc(&scp->scr, pos + 1)*scp->font_size,
619 	      &font_buf[32], scp->font_size);
620 	bcopy(scp->font
621 		 + sc_vtb_getc(&scp->scr, pos + scp->xsize)*scp->font_size,
622 	      &font_buf[64], scp->font_size);
623 	bcopy(scp->font
624 		 + sc_vtb_getc(&scp->scr, pos + scp->xsize + 1)*scp->font_size,
625 	      &font_buf[96], scp->font_size);
626 	for (i = 0; i < scp->font_size; ++i) {
627 		cursor[i] = font_buf[i]<<8 | font_buf[i+32];
628 		cursor[i + scp->font_size] = font_buf[i+64]<<8 | font_buf[i+96];
629 	}
630 
631 	/* now and-or in the mousepointer image */
632 	xoffset = x%8;
633 	yoffset = y%scp->font_size;
634 	for (i = 0; i < 16; ++i) {
635 		border = mdp->md_border[i] << 8; /* avoid right shifting out */
636 		interior = mdp->md_interior[i] << 8;
637 		border >>= xoffset;		/* normalize */
638 		interior >>= xoffset;
639 		if (scp->sc->adp->va_flags & V_ADP_CWIDTH9) {
640 			/* skip gaps between characters */
641 			border = (border & 0xff0000) |
642 				 (border & 0x007f80) << 1 |
643 				 (border & 0x00003f) << 2;
644 			interior = (interior & 0xff0000) |
645 				   (interior & 0x007f80) << 1 |
646 				   (interior & 0x00003f) << 2;
647 		}
648 		border >>= 8;			/* back to normal position */
649 		interior >>= 8;
650 		cursor[i + yoffset] = (cursor[i + yoffset]  & ~border) |
651 				      interior;
652 	}
653 	for (i = 0; i < scp->font_size; ++i) {
654 		font_buf[i] = (cursor[i] & 0xff00) >> 8;
655 		font_buf[i + 32] = cursor[i] & 0xff;
656 		font_buf[i + 64] = (cursor[i + scp->font_size] & 0xff00) >> 8;
657 		font_buf[i + 96] = cursor[i + scp->font_size] & 0xff;
658 	}
659 
660 #if 1
661 	/* wait for vertical retrace to avoid jitter on some videocards */
662 	crtc_addr = scp->sc->adp->va_crtc_addr;
663 	while (!sc_txtmouse_no_retrace_wait &&
664 	    !(inb(crtc_addr + 6) & 0x08))
665 		/* idle */ ;
666 #endif
667 	c = scp->sc->mouse_char;
668 	vidd_load_font(scp->sc->adp, 0, 32, 8, font_buf, c, 4);
669 
670 	sc_vtb_putc(&scp->scr, pos, c, sc_vtb_geta(&scp->scr, pos));
671 	/* FIXME: may be out of range! */
672 	sc_vtb_putc(&scp->scr, pos + scp->xsize, c + 2,
673 		    sc_vtb_geta(&scp->scr, pos + scp->xsize));
674 	if (x < (scp->xsize - 1)*8) {
675 		sc_vtb_putc(&scp->scr, pos + 1, c + 1,
676 			    sc_vtb_geta(&scp->scr, pos + 1));
677 		sc_vtb_putc(&scp->scr, pos + scp->xsize + 1, c + 3,
678 			    sc_vtb_geta(&scp->scr, pos + scp->xsize + 1));
679 	}
680     } else
681 #endif /* SC_ALT_MOUSE_IMAGE */
682     {
683 	/* Red, magenta and brown are mapped to green to keep it readable */
684 	static const int col_conv[16] = {
685 		6, 6, 6, 6, 2, 2, 2, 6, 14, 14, 14, 14, 10, 10, 10, 14
686 	};
687 	int pos;
688 	int color;
689 	int a;
690 
691 	pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
692 	a = sc_vtb_geta(&scp->scr, pos);
693 	if (scp->sc->adp->va_flags & V_ADP_COLOR)
694 		color = (col_conv[(a & 0xf000) >> 12] << 12)
695 			| ((a & 0x0f00) | 0x0800);
696 	else
697 		color = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
698 	sc_vtb_putc(&scp->scr, pos, sc_vtb_getc(&scp->scr, pos), color);
699     }
700 }
701 
702 static void
703 remove_txtmouse(scr_stat *scp, int x, int y)
704 {
705 }
706 
707 static void
708 vga_txtmouse(scr_stat *scp, int x, int y, int on)
709 {
710 	if (on)
711 		draw_txtmouse(scp, x, y);
712 	else
713 		remove_txtmouse(scp, x, y);
714 }
715 
716 #endif /* SC_NO_CUTPASTE */
717 
718 #ifdef SC_PIXEL_MODE
719 
720 /* pixel (raster text) mode renderer */
721 
722 static void
723 vga_rndrinit(scr_stat *scp)
724 {
725 	if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PLANAR) {
726 		scp->rndr->clear = vga_pxlclear_planar;
727 		scp->rndr->draw_border = vga_pxlborder_planar;
728 		scp->rndr->draw = vga_vgadraw_planar;
729 		scp->rndr->draw_cursor = vga_pxlcursor_planar;
730 		scp->rndr->blink_cursor = vga_pxlblink_planar;
731 		scp->rndr->draw_mouse = vga_pxlmouse_planar;
732 	} else
733 	if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT ||
734 	    scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PACKED) {
735 		scp->rndr->clear = vga_pxlclear_direct;
736 		scp->rndr->draw_border = vga_pxlborder_direct;
737 		scp->rndr->draw = vga_vgadraw_direct;
738 		scp->rndr->draw_cursor = vga_pxlcursor_direct;
739 		scp->rndr->blink_cursor = vga_pxlblink_direct;
740 		scp->rndr->draw_mouse = vga_pxlmouse_direct;
741 	}
742 }
743 
744 static void
745 vga_pxlclear_direct(scr_stat *scp, int c, int attr)
746 {
747 	vm_offset_t p;
748 	int line_width;
749 	int pixel_size;
750 	int lines;
751 	int i;
752 
753 	line_width = scp->sc->adp->va_line_width;
754 	pixel_size = scp->sc->adp->va_info.vi_pixel_size;
755 	lines = scp->ysize * scp->font_size;
756 	p = scp->sc->adp->va_window +
757 	    line_width * scp->yoff * scp->font_size +
758 	    scp->xoff * 8 * pixel_size;
759 
760 	for (i = 0; i < lines; ++i) {
761 		bzero_io((void *)p, scp->xsize * 8 * pixel_size);
762 		p += line_width;
763 	}
764 }
765 
766 static void
767 vga_pxlclear_planar(scr_stat *scp, int c, int attr)
768 {
769 	vm_offset_t p;
770 	int line_width;
771 	int lines;
772 	int i;
773 
774 	/* XXX: we are just filling the screen with the background color... */
775 	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
776 	outw(GDCIDX, 0x0003);		/* data rotate/function select */
777 	outw(GDCIDX, 0x0f01);		/* set/reset enable */
778 	outw(GDCIDX, 0xff08);		/* bit mask */
779 	outw(GDCIDX, ((attr & 0xf000) >> 4) | 0x00); /* set/reset */
780 	line_width = scp->sc->adp->va_line_width;
781 	lines = scp->ysize*scp->font_size;
782 	p = scp->sc->adp->va_window + line_width*scp->yoff*scp->font_size
783 		+ scp->xoff;
784 	for (i = 0; i < lines; ++i) {
785 		bzero_io((void *)p, scp->xsize);
786 		p += line_width;
787 	}
788 	outw(GDCIDX, 0x0000);		/* set/reset */
789 	outw(GDCIDX, 0x0001);		/* set/reset enable */
790 }
791 
792 static void
793 vga_pxlborder_direct(scr_stat *scp, int color)
794 {
795 	vm_offset_t s;
796 	vm_offset_t e;
797 	vm_offset_t f;
798 	int line_width;
799 	int pixel_size;
800 	int x;
801 	int y;
802 	int i;
803 
804 	line_width = scp->sc->adp->va_line_width;
805 	pixel_size = scp->sc->adp->va_info.vi_pixel_size;
806 
807 	if (scp->yoff > 0) {
808 		s = scp->sc->adp->va_window;
809 		e = s + line_width * scp->yoff * scp->font_size;
810 
811 		for (f = s; f < e; f += pixel_size)
812 			DRAW_PIXEL(scp, f, color);
813 	}
814 
815 	y = (scp->yoff + scp->ysize) * scp->font_size;
816 
817 	if (scp->ypixel > y) {
818 		s = scp->sc->adp->va_window + line_width * y;
819 		e = s + line_width * (scp->ypixel - y);
820 
821 		for (f = s; f < e; f += pixel_size)
822 			DRAW_PIXEL(scp, f, color);
823 	}
824 
825 	y = scp->yoff * scp->font_size;
826 	x = scp->xpixel / 8 - scp->xoff - scp->xsize;
827 
828 	for (i = 0; i < scp->ysize * scp->font_size; ++i) {
829 		if (scp->xoff > 0) {
830 			s = scp->sc->adp->va_window + line_width * (y + i);
831 			e = s + scp->xoff * 8 * pixel_size;
832 
833 			for (f = s; f < e; f += pixel_size)
834 				DRAW_PIXEL(scp, f, color);
835 		}
836 
837 		if (x > 0) {
838 			s = scp->sc->adp->va_window + line_width * (y + i) +
839 			    scp->xoff * 8 * pixel_size +
840 			    scp->xsize * 8 * pixel_size;
841 			e = s + x * 8 * pixel_size;
842 
843 			for (f = s; f < e; f += pixel_size)
844 				DRAW_PIXEL(scp, f, color);
845 		}
846 	}
847 }
848 
849 static void
850 vga_pxlborder_planar(scr_stat *scp, int color)
851 {
852 	vm_offset_t p;
853 	int line_width;
854 	int x;
855 	int y;
856 	int i;
857 
858 	vidd_set_border(scp->sc->adp, color);
859 
860 	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
861 	outw(GDCIDX, 0x0003);		/* data rotate/function select */
862 	outw(GDCIDX, 0x0f01);		/* set/reset enable */
863 	outw(GDCIDX, 0xff08);		/* bit mask */
864 	outw(GDCIDX, (color << 8) | 0x00);	/* set/reset */
865 	line_width = scp->sc->adp->va_line_width;
866 	p = scp->sc->adp->va_window;
867 	if (scp->yoff > 0)
868 		bzero_io((void *)p, line_width*scp->yoff*scp->font_size);
869 	y = (scp->yoff + scp->ysize)*scp->font_size;
870 	if (scp->ypixel > y)
871 		bzero_io((void *)(p + line_width*y), line_width*(scp->ypixel - y));
872 	y = scp->yoff*scp->font_size;
873 	x = scp->xpixel/8 - scp->xoff - scp->xsize;
874 	for (i = 0; i < scp->ysize*scp->font_size; ++i) {
875 		if (scp->xoff > 0)
876 			bzero_io((void *)(p + line_width*(y + i)), scp->xoff);
877 		if (x > 0)
878 			bzero_io((void *)(p + line_width*(y + i)
879 				     + scp->xoff + scp->xsize), x);
880 	}
881 	outw(GDCIDX, 0x0000);		/* set/reset */
882 	outw(GDCIDX, 0x0001);		/* set/reset enable */
883 }
884 
885 static void
886 vga_vgadraw_direct(scr_stat *scp, int from, int count, int flip)
887 {
888 	vm_offset_t d;
889 	vm_offset_t e;
890 	u_char *f;
891 	u_short col1, col2, color;
892 	int line_width, pixel_size;
893 	int i, j, k;
894 	int a;
895 
896 	line_width = scp->sc->adp->va_line_width;
897 	pixel_size = scp->sc->adp->va_info.vi_pixel_size;
898 
899 	d = GET_PIXEL(scp, from, 8 * pixel_size, line_width);
900 
901 	if (from + count > scp->xsize * scp->ysize)
902 		count = scp->xsize * scp->ysize - from;
903 
904 	for (i = from; count-- > 0; ++i) {
905 		a = sc_vtb_geta(&scp->vtb, i);
906 
907 		if (flip)
908 			a = vga_flipattr(a, FALSE);
909 		col1 = (a & 0x0f00) >> 8;
910 		col2 = (a & 0xf000) >> 12;
911 
912 		e = d;
913 		f = &(scp->font[sc_vtb_getc(&scp->vtb, i) * scp->font_size]);
914 
915 		for (j = 0; j < scp->font_size; ++j, ++f) {
916 			for (k = 0; k < 8; ++k) {
917 				color = *f & (1 << (7 - k)) ? col1 : col2;
918 				DRAW_PIXEL(scp, e + pixel_size * k, color);
919 			}
920 
921 			e += line_width;
922 		}
923 
924 		d += 8 * pixel_size;
925 
926 		if ((i % scp->xsize) == scp->xsize - 1)
927 			d += scp->font_size * line_width -
928 			    scp->xsize * 8 * pixel_size;
929 	}
930 }
931 
932 static void
933 vga_vgadraw_planar(scr_stat *scp, int from, int count, int flip)
934 {
935 	vm_offset_t d;
936 	vm_offset_t e;
937 	u_char *f;
938 	u_short bg, fg;
939 	u_short col1, col2;
940 	int line_width;
941 	int i, j;
942 	int a;
943 
944 	line_width = scp->sc->adp->va_line_width;
945 
946 	d = GET_PIXEL(scp, from, 1, line_width);
947 
948 	if (scp->sc->adp->va_type == KD_VGA) {
949 		outw(GDCIDX, 0x0305);	/* read mode 0, write mode 3 */
950 		outw(GDCIDX, 0xff08);	/* bit mask */
951 	} else
952 		outw(GDCIDX, 0x0005);	/* read mode 0, write mode 0 */
953 	outw(GDCIDX, 0x0003);		/* data rotate/function select */
954 	outw(GDCIDX, 0x0f01);		/* set/reset enable */
955 	fg = bg = -1;
956 	if (from + count > scp->xsize*scp->ysize)
957 		count = scp->xsize*scp->ysize - from;
958 	for (i = from; count-- > 0; ++i) {
959 		a = sc_vtb_geta(&scp->vtb, i);
960 		if (flip)
961 			a = vga_flipattr(a, FALSE);
962 		col1 = a & 0x0f00;
963 		col2 = (a & 0xf000) >> 4;
964 		/* set background color in EGA/VGA latch */
965 		if (bg != col2) {
966 			bg = col2;
967 			fg = -1;
968 			outw(GDCIDX, bg | 0x00); /* set/reset */
969 			if (scp->sc->adp->va_type != KD_VGA)
970 				outw(GDCIDX, 0xff08); /* bit mask */
971 			writeb(d, 0xff);
972 			(void)readb(d);		/* set bg color in the latch */
973 		}
974 		/* foreground color */
975 		if (fg != col1) {
976 			fg = col1;
977 			outw(GDCIDX, col1 | 0x00); /* set/reset */
978 		}
979 		e = d;
980 		f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
981 		for (j = 0; j < scp->font_size; ++j, ++f) {
982 			if (scp->sc->adp->va_type == KD_VGA)
983 				writeb(e, *f);
984 			else {
985 				outw(GDCIDX, (*f << 8) | 0x08);	/* bit mask */
986 				writeb(e, 0);
987 			}
988 			e += line_width;
989 		}
990 		++d;
991 		if ((i % scp->xsize) == scp->xsize - 1)
992 			d += scp->font_size * line_width - scp->xsize;
993 	}
994 	if (scp->sc->adp->va_type == KD_VGA)
995 		outw(GDCIDX, 0x0005);	/* read mode 0, write mode 0 */
996 	else
997 		outw(GDCIDX, 0xff08);	/* bit mask */
998 	outw(GDCIDX, 0x0000);		/* set/reset */
999 	outw(GDCIDX, 0x0001);		/* set/reset enable */
1000 }
1001 
1002 static void
1003 vga_pxlcursor_shape(scr_stat *scp, int base, int height, int blink)
1004 {
1005 	vga_setmdp(scp);
1006 }
1007 
1008 static void
1009 draw_pxlcursor_direct(scr_stat *scp, int at, int on, int flip)
1010 {
1011 	vm_offset_t d;
1012 	u_char *f;
1013 	int line_width, pixel_size;
1014 	int height;
1015 	int col1, col2, color;
1016 	int a;
1017 	int i, j;
1018 
1019 	line_width = scp->sc->adp->va_line_width;
1020 	pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1021 
1022 	d = GET_PIXEL(scp, at, 8 * pixel_size, line_width) +
1023 	    (scp->font_size - scp->curs_attr.base - 1) * line_width;
1024 
1025 	a = sc_vtb_geta(&scp->vtb, at);
1026 
1027 	if (flip)
1028 		a = vga_flipattr(a, FALSE);
1029 	if (on)
1030 		a = vga_cursorattr_adj(scp, a, FALSE);
1031 	col1 = (a & 0x0f00) >> 8;
1032 	col2 = a >> 12;
1033 
1034 	f = &(scp->font[sc_vtb_getc(&scp->vtb, at) * scp->font_size +
1035 	      scp->font_size - scp->curs_attr.base - 1]);
1036 
1037 	height = imin(scp->curs_attr.height, scp->font_size);
1038 
1039 	for (i = 0; i < height; ++i, --f) {
1040 		for (j = 0; j < 8; ++j) {
1041 			color = *f & (1 << (7 - j)) ? col1 : col2;
1042 			DRAW_PIXEL(scp, d + pixel_size * j, color);
1043 		}
1044 
1045 		d -= line_width;
1046 	}
1047 }
1048 
1049 static void
1050 draw_pxlcursor_planar(scr_stat *scp, int at, int on, int flip)
1051 {
1052 	vm_offset_t d;
1053 	u_char *f;
1054 	int line_width;
1055 	int height;
1056 	int col;
1057 	int a;
1058 	int i;
1059 
1060 	line_width = scp->sc->adp->va_line_width;
1061 
1062 	d = GET_PIXEL(scp, at, 1, line_width) +
1063 	    (scp->font_size - scp->curs_attr.base - 1) * line_width;
1064 
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);
1070 	if (flip)
1071 		a = vga_flipattr(a, FALSE);
1072 	if (on)
1073 		a = vga_cursorattr_adj(scp, a, FALSE);
1074 	col = (a & 0xf000) >> 4;
1075 	outw(GDCIDX, col | 0x00);	/* set/reset */
1076 	outw(GDCIDX, 0xff08);		/* bit mask */
1077 	writeb(d, 0);
1078 	(void)readb(d);			/* set bg color in the latch */
1079 	/* foreground color */
1080 	col = a & 0x0f00;
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 */
1087 	       	writeb(d, 0);
1088 		d -= line_width;
1089 	}
1090 	outw(GDCIDX, 0x0000);		/* set/reset */
1091 	outw(GDCIDX, 0x0001);		/* set/reset enable */
1092 	outw(GDCIDX, 0xff08);		/* bit mask */
1093 }
1094 
1095 static int pxlblinkrate = 0;
1096 
1097 static void
1098 vga_pxlcursor_direct(scr_stat *scp, int at, int blink, int on, int flip)
1099 {
1100 	if (scp->curs_attr.height <= 0)	/* the text cursor is disabled */
1101 		return;
1102 
1103 	if (on) {
1104 		if (!blink) {
1105 			scp->status |= VR_CURSOR_ON;
1106 			draw_pxlcursor_direct(scp, at, on, flip);
1107 		} else if (++pxlblinkrate & 4) {
1108 			pxlblinkrate = 0;
1109 			scp->status ^= VR_CURSOR_ON;
1110 			draw_pxlcursor_direct(scp, at,
1111 					      scp->status & VR_CURSOR_ON,
1112 					      flip);
1113 		}
1114 	} else {
1115 		if (scp->status & VR_CURSOR_ON)
1116 			draw_pxlcursor_direct(scp, at, on, flip);
1117 		scp->status &= ~VR_CURSOR_ON;
1118 	}
1119 	if (blink)
1120 		scp->status |= VR_CURSOR_BLINK;
1121 	else
1122 		scp->status &= ~VR_CURSOR_BLINK;
1123 }
1124 
1125 static void
1126 vga_pxlcursor_planar(scr_stat *scp, int at, int blink, int on, int flip)
1127 {
1128 	if (scp->curs_attr.height <= 0)	/* the text cursor is disabled */
1129 		return;
1130 
1131 	if (on) {
1132 		if (!blink) {
1133 			scp->status |= VR_CURSOR_ON;
1134 			draw_pxlcursor_planar(scp, at, on, flip);
1135 		} else if (++pxlblinkrate & 4) {
1136 			pxlblinkrate = 0;
1137 			scp->status ^= VR_CURSOR_ON;
1138 			draw_pxlcursor_planar(scp, at,
1139 					      scp->status & VR_CURSOR_ON,
1140 					      flip);
1141 		}
1142 	} else {
1143 		if (scp->status & VR_CURSOR_ON)
1144 			draw_pxlcursor_planar(scp, at, on, flip);
1145 		scp->status &= ~VR_CURSOR_ON;
1146 	}
1147 	if (blink)
1148 		scp->status |= VR_CURSOR_BLINK;
1149 	else
1150 		scp->status &= ~VR_CURSOR_BLINK;
1151 }
1152 
1153 static void
1154 vga_pxlblink_direct(scr_stat *scp, int at, int flip)
1155 {
1156 	if (!(scp->status & VR_CURSOR_BLINK))
1157 		return;
1158 	if (!(++pxlblinkrate & 4))
1159 		return;
1160 	pxlblinkrate = 0;
1161 	scp->status ^= VR_CURSOR_ON;
1162 	draw_pxlcursor_direct(scp, at, scp->status & VR_CURSOR_ON, flip);
1163 }
1164 
1165 static void
1166 vga_pxlblink_planar(scr_stat *scp, int at, int flip)
1167 {
1168 	if (!(scp->status & VR_CURSOR_BLINK))
1169 		return;
1170 	if (!(++pxlblinkrate & 4))
1171 		return;
1172 	pxlblinkrate = 0;
1173 	scp->status ^= VR_CURSOR_ON;
1174 	draw_pxlcursor_planar(scp, at, scp->status & VR_CURSOR_ON, flip);
1175 }
1176 
1177 #ifndef SC_NO_CUTPASTE
1178 
1179 static void
1180 draw_pxlmouse_planar(scr_stat *scp, int x, int y)
1181 {
1182 	const struct mousedata *mdp;
1183 	vm_offset_t p;
1184 	int line_width;
1185 	int xoff;
1186 	int ymax;
1187 	uint32_t m;
1188 	int i, j, k;
1189 	uint8_t m1;
1190 
1191 	mdp = scp->mouse_data;
1192 	line_width = scp->sc->adp->va_line_width;
1193 	xoff = (x - scp->xoff*8)%8;
1194 	ymax = imin(y + mdp->md_height, scp->ypixel);
1195 
1196 	if (scp->sc->adp->va_type == KD_VGA) {
1197 		outw(GDCIDX, 0x0305);	/* read mode 0, write mode 3 */
1198 		outw(GDCIDX, 0xff08);	/* bit mask */
1199 	} else
1200 		outw(GDCIDX, 0x0005);	/* read mode 0, write mode 0 */
1201 	outw(GDCIDX, 0x0003);		/* data rotate/function select */
1202 	outw(GDCIDX, 0x0f01);		/* set/reset enable */
1203 
1204 	outw(GDCIDX, (scp->curs_attr.mouse_ba << 8) | 0x00); /* set/reset */
1205 	p = scp->sc->adp->va_window + line_width*y + x/8;
1206 	for (i = y, j = 0; i < ymax; ++i, ++j) {
1207 		m = mdp->md_border[j] << 8 >> xoff;
1208 		for (k = 0; k < 3; ++k) {
1209 			m1 = m >> (8 * (2 - k));
1210 			if (m1 != 0 && x + 8 * k < scp->xpixel) {
1211 				readb(p + k);
1212 				if (scp->sc->adp->va_type == KD_VGA)
1213 					writeb(p + k, m1);
1214 				else {
1215 					/* bit mask: */
1216 					outw(GDCIDX, (m1 << 8) | 0x08);
1217 					writeb(p + k, 0);
1218 				}
1219 			}
1220 		}
1221 		p += line_width;
1222 	}
1223 	outw(GDCIDX, (scp->curs_attr.mouse_ia << 8) | 0x00); /* set/reset */
1224 	p = scp->sc->adp->va_window + line_width*y + x/8;
1225 	for (i = y, j = 0; i < ymax; ++i, ++j) {
1226 		m = mdp->md_interior[j] << 8 >> xoff;
1227 		for (k = 0; k < 3; ++k) {
1228 			m1 = m >> (8 * (2 - k));
1229 			if (m1 != 0 && x + 8 * k < scp->xpixel) {
1230 				readb(p + k);
1231 				if (scp->sc->adp->va_type == KD_VGA)
1232 					writeb(p + k, m1);
1233 				else {
1234 					/* bit mask: */
1235 					outw(GDCIDX, (m1 << 8) | 0x08);
1236 					writeb(p + k, 0);
1237 				}
1238 			}
1239 		}
1240 		p += line_width;
1241 	}
1242 	if (scp->sc->adp->va_type == KD_VGA)
1243 		outw(GDCIDX, 0x0005);	/* read mode 0, write mode 0 */
1244 	else
1245 		outw(GDCIDX, 0xff08);	/* bit mask */
1246 	outw(GDCIDX, 0x0000);		/* set/reset */
1247 	outw(GDCIDX, 0x0001);		/* set/reset enable */
1248 }
1249 
1250 static void
1251 remove_pxlmouse_planar(scr_stat *scp, int x, int y)
1252 {
1253 	const struct mousedata *mdp;
1254 	vm_offset_t p;
1255 	int bx, by, i, line_width, xend, xoff, yend, yoff;
1256 
1257 	mdp = scp->mouse_data;
1258 
1259 	/*
1260 	 * It is only necessary to remove the mouse image where it overlaps
1261 	 * the border.  Determine the overlap, and do nothing if it is empty.
1262 	 */
1263 	bx = (scp->xoff + scp->xsize) * 8;
1264 	by = (scp->yoff + scp->ysize) * scp->font_size;
1265 	xend = imin(x + mdp->md_width, scp->xpixel);
1266 	yend = imin(y + mdp->md_height, scp->ypixel);
1267 	if (xend <= bx && yend <= by)
1268 		return;
1269 
1270 	/* Repaint the non-empty overlap. */
1271 	line_width = scp->sc->adp->va_line_width;
1272 	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
1273 	outw(GDCIDX, 0x0003);		/* data rotate/function select */
1274 	outw(GDCIDX, 0x0f01);		/* set/reset enable */
1275 	outw(GDCIDX, 0xff08);		/* bit mask */
1276 	outw(GDCIDX, (scp->border << 8) | 0x00);	/* set/reset */
1277 	for (i = x / 8, xoff = i * 8; xoff < xend; ++i, xoff += 8) {
1278 		yoff = (xoff >= bx) ? y : by;
1279 		p = scp->sc->adp->va_window + yoff * line_width + i;
1280 		for (; yoff < yend; ++yoff, p += line_width)
1281 			writeb(p, 0);
1282 	}
1283 	outw(GDCIDX, 0x0000);		/* set/reset */
1284 	outw(GDCIDX, 0x0001);		/* set/reset enable */
1285 }
1286 
1287 static void
1288 vga_pxlmouse_direct(scr_stat *scp, int x, int y, int on)
1289 {
1290 	const struct mousedata *mdp;
1291 	vm_offset_t p;
1292 	int line_width, pixel_size;
1293 	int xend, yend;
1294 	int i, j;
1295 
1296 	mdp = scp->mouse_data;
1297 
1298 	/*
1299 	 * Determine overlap with the border and then if removing, do nothing
1300 	 * if the overlap is empty.
1301 	 */
1302 	xend = imin(x + mdp->md_width, scp->xpixel);
1303 	yend = imin(y + mdp->md_height, scp->ypixel);
1304 	if (!on && xend <= (scp->xoff + scp->xsize) * 8 &&
1305 	    yend <= (scp->yoff + scp->ysize) * scp->font_size)
1306 		return;
1307 
1308 	line_width = scp->sc->adp->va_line_width;
1309 	pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1310 
1311 	if (on)
1312 		goto do_on;
1313 
1314 	/* Repaint overlap with the border (mess up the corner a little). */
1315 	p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1316 	for (i = 0; i < yend - y; i++, p += line_width)
1317 		for (j = xend - x - 1; j >= 0; j--)
1318 			DRAW_PIXEL(scp, p + j * pixel_size, scp->border);
1319 
1320 	return;
1321 
1322 do_on:
1323 	p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1324 	for (i = 0; i < yend - y; i++, p += line_width)
1325 		for (j = xend - x - 1; j >= 0; j--)
1326 			if (mdp->md_interior[i] & (1 << (15 - j)))
1327 				DRAW_PIXEL(scp, p + j * pixel_size,
1328 				    scp->curs_attr.mouse_ia);
1329 			else if (mdp->md_border[i] & (1 << (15 - j)))
1330 				DRAW_PIXEL(scp, p + j * pixel_size,
1331 				    scp->curs_attr.mouse_ba);
1332 }
1333 
1334 static void
1335 vga_pxlmouse_planar(scr_stat *scp, int x, int y, int on)
1336 {
1337 	if (on)
1338 		draw_pxlmouse_planar(scp, x, y);
1339 	else
1340 		remove_pxlmouse_planar(scp, x, y);
1341 }
1342 
1343 #endif /* SC_NO_CUTPASTE */
1344 #endif /* SC_PIXEL_MODE */
1345 
1346 #ifndef SC_NO_MODE_CHANGE
1347 
1348 /* graphics mode renderer */
1349 
1350 static void
1351 vga_grborder(scr_stat *scp, int color)
1352 {
1353 	vidd_set_border(scp->sc->adp, color);
1354 }
1355 
1356 #endif
1357