xref: /freebsd/sys/dev/syscons/scvgarndr.c (revision 683b025adebfc2bb0d488c1d8d89e14379045ccc)
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Sascha Wildner <saw@online.de>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
17  *
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.
28  *
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
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(u_short a, int blink)
360 {
361 	/*
362 	 * !blink means pixel mode, and the cursor attribute in that case
363 	 * is simplistic reverse video.
364 	 */
365 	if (!blink)
366 		return (vga_flipattr(a, blink));
367 
368 	/*
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.
374 	 */
375 	if ((a & 0x7000) == 0x7000) {
376 		a &= 0x8f00;
377 		if ((a & 0x0700) == 0)
378 			a |= 0x0700;
379 	} else {
380 		a |= 0x7000;
381 		if ((a & 0x0700) == 0x0700)
382 			a &= 0xf000;
383 	}
384 	return (a);
385 }
386 
387 static void
388 vga_setmdp(scr_stat *scp)
389 {
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;
395 
396 	xpixel = scp->xpixel;
397 	ypixel = scp->ypixel;
398 	if (scp->sc->adp->va_flags & V_ADP_CWIDTH9)
399 		xpixel = xpixel * 9 / 8;
400 
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;
405 
406 	/*
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.
412 	 *
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.
415 	 */
416 	if (scp->font_size <= 8 &&
417 	    (ypixel < 300 || !(scp->status & PIXEL_MODE))) {
418 		mdpp = &mousesmall[0];
419 		n = nitems(mousesmall);
420 	} else {
421 		mdpp = &mouselarge[0];
422 		n = nitems(mouselarge);
423 	}
424 	if (scp->status & PIXEL_MODE) {
425 		wb = 1024;
426 		wi = 256;
427 	} else {
428 		wb = 256;
429 		wi = 1024;
430 	}
431 	best_i = 0;
432 	best_v = 0x7fffffff;
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;
436 		if (best_v > v) {
437 			best_v = v;
438 			best_i = i;
439 		}
440 	}
441 	mdp = mdpp[best_i];
442 	scp->mouse_data = mdp;
443 #endif /* !SC_NO_CUTPASTE && (!SC_ALT_MOUSE_IMAGE || SC_PIXEL_MODE) */
444 }
445 
446 static void
447 vga_nop(scr_stat *scp)
448 {
449 }
450 
451 /* text mode renderer */
452 
453 static void
454 vga_txtclear(scr_stat *scp, int c, int attr)
455 {
456 	sc_vtb_clear(&scp->scr, c, attr);
457 }
458 
459 static void
460 vga_txtborder(scr_stat *scp, int color)
461 {
462 	vidd_set_border(scp->sc->adp, color);
463 }
464 
465 static void
466 vga_txtdraw(scr_stat *scp, int from, int count, int flip)
467 {
468 	vm_offset_t p;
469 	int c;
470 	int a;
471 
472 	if (from + count > scp->xsize*scp->ysize)
473 		count = scp->xsize*scp->ysize - from;
474 
475 	if (flip) {
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);
481 		}
482 	} else {
483 		sc_vtb_copy(&scp->vtb, from, &scp->scr, from, count);
484 	}
485 }
486 
487 static void
488 vga_txtcursor_shape(scr_stat *scp, int base, int height, int blink)
489 {
490 	vga_setmdp(scp);
491 	if (base < 0 || base >= scp->font_size)
492 		return;
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);
496 }
497 
498 static void
499 draw_txtcharcursor(scr_stat *scp, int at, u_short c, u_short a, int flip)
500 {
501 	sc_softc_t *sc;
502 
503 	sc = scp->sc;
504 
505 #ifndef SC_NO_FONT_LOADING
506 	if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
507 		unsigned char *font;
508 		int h;
509 		int i;
510 
511 		if (scp->font_size < 14) {
512 			font = sc->font_8;
513 			h = 8;
514 		} else if (scp->font_size >= 16) {
515 			font = sc->font_16;
516 			h = 16;
517 		} else {
518 			font = sc->font_14;
519 			h = 14;
520 		}
521 		if (scp->curs_attr.base >= h)
522 			return;
523 		if (flip)
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) {
529 			font[i] ^= 0xff;
530 		}
531 		/* XXX */
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);
534 	} else
535 #endif /* SC_NO_FONT_LOADING */
536 	{
537 		if (flip)
538 			a = vga_flipattr(a, TRUE);
539 		a = vga_cursorattr_adj(a, TRUE);
540 		sc_vtb_putc(&scp->scr, at, c, a);
541 	}
542 }
543 
544 static void
545 vga_txtcursor(scr_stat *scp, int at, int blink, int on, int flip)
546 {
547 	video_adapter_t *adp;
548 	int cursor_attr;
549 
550 	if (scp->curs_attr.height <= 0)	/* the text cursor is disabled */
551 		return;
552 
553 	adp = scp->sc->adp;
554 	if (blink) {
555 		scp->status |= VR_CURSOR_BLINK;
556 		if (on) {
557 			scp->status |= VR_CURSOR_ON;
558 			vidd_set_hw_cursor(adp, at%scp->xsize,
559 			    at/scp->xsize);
560 		} else {
561 			if (scp->status & VR_CURSOR_ON)
562 				vidd_set_hw_cursor(adp, -1, -1);
563 			scp->status &= ~VR_CURSOR_ON;
564 		}
565 	} else {
566 		scp->status &= ~VR_CURSOR_BLINK;
567 		if (on) {
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),
572 					   flip);
573 		} else {
574 			cursor_attr = sc_vtb_geta(&scp->vtb, at);
575 			if (flip)
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),
580 					    cursor_attr);
581 			scp->status &= ~VR_CURSOR_ON;
582 		}
583 	}
584 }
585 
586 static void
587 vga_txtblink(scr_stat *scp, int at, int flip)
588 {
589 }
590 
591 int sc_txtmouse_no_retrace_wait;
592 
593 #ifndef SC_NO_CUTPASTE
594 
595 static void
596 draw_txtmouse(scr_stat *scp, int x, int y)
597 {
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];
603 	u_short cursor[32];
604 	u_char c;
605 	int pos;
606 	int xoffset, yoffset;
607 	int crtc_addr;
608 	int i;
609 
610 	mdp = scp->mouse_data;
611 
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);
618 	bcopy(scp->font
619 		 + sc_vtb_getc(&scp->scr, pos + scp->xsize)*scp->font_size,
620 	      &font_buf[64], scp->font_size);
621 	bcopy(scp->font
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];
627 	}
628 
629 	/* now and-or in the mousepointer image */
630 	xoffset = x%8;
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;
645 		}
646 		border >>= 8;			/* back to normal position */
647 		interior >>= 8;
648 		cursor[i + yoffset] = (cursor[i + yoffset]  & ~border) |
649 				      interior;
650 	}
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;
656 	}
657 
658 #if 1
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))
663 		/* idle */ ;
664 #endif
665 	c = scp->sc->mouse_char;
666 	vidd_load_font(scp->sc->adp, 0, 32, 8, font_buf, c, 4);
667 
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));
677 	}
678     } else
679 #endif /* SC_ALT_MOUSE_IMAGE */
680     {
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
684 	};
685 	int pos;
686 	int color;
687 	int a;
688 
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);
694 	else
695 		color = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
696 	sc_vtb_putc(&scp->scr, pos, sc_vtb_getc(&scp->scr, pos), color);
697     }
698 }
699 
700 static void
701 remove_txtmouse(scr_stat *scp, int x, int y)
702 {
703 }
704 
705 static void
706 vga_txtmouse(scr_stat *scp, int x, int y, int on)
707 {
708 	if (on)
709 		draw_txtmouse(scp, x, y);
710 	else
711 		remove_txtmouse(scp, x, y);
712 }
713 
714 #endif /* SC_NO_CUTPASTE */
715 
716 #ifdef SC_PIXEL_MODE
717 
718 /* pixel (raster text) mode renderer */
719 
720 static void
721 vga_rndrinit(scr_stat *scp)
722 {
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;
730 	} else
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;
739 	}
740 }
741 
742 static void
743 vga_pxlclear_direct(scr_stat *scp, int c, int attr)
744 {
745 	vm_offset_t p;
746 	int line_width;
747 	int pixel_size;
748 	int lines;
749 	int i;
750 
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;
757 
758 	for (i = 0; i < lines; ++i) {
759 		bzero_io((void *)p, scp->xsize * 8 * pixel_size);
760 		p += line_width;
761 	}
762 }
763 
764 static void
765 vga_pxlclear_planar(scr_stat *scp, int c, int attr)
766 {
767 	vm_offset_t p;
768 	int line_width;
769 	int lines;
770 	int i;
771 
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
781 		+ scp->xoff;
782 	for (i = 0; i < lines; ++i) {
783 		bzero_io((void *)p, scp->xsize);
784 		p += line_width;
785 	}
786 	outw(GDCIDX, 0x0000);		/* set/reset */
787 	outw(GDCIDX, 0x0001);		/* set/reset enable */
788 }
789 
790 static void
791 vga_pxlborder_direct(scr_stat *scp, int color)
792 {
793 	vm_offset_t s;
794 	vm_offset_t e;
795 	vm_offset_t f;
796 	int line_width;
797 	int pixel_size;
798 	int x;
799 	int y;
800 	int i;
801 
802 	line_width = scp->sc->adp->va_line_width;
803 	pixel_size = scp->sc->adp->va_info.vi_pixel_size;
804 
805 	if (scp->yoff > 0) {
806 		s = scp->sc->adp->va_window;
807 		e = s + line_width * scp->yoff * scp->font_size;
808 
809 		for (f = s; f < e; f += pixel_size)
810 			DRAW_PIXEL(scp, f, color);
811 	}
812 
813 	y = (scp->yoff + scp->ysize) * scp->font_size;
814 
815 	if (scp->ypixel > y) {
816 		s = scp->sc->adp->va_window + line_width * y;
817 		e = s + line_width * (scp->ypixel - y);
818 
819 		for (f = s; f < e; f += pixel_size)
820 			DRAW_PIXEL(scp, f, color);
821 	}
822 
823 	y = scp->yoff * scp->font_size;
824 	x = scp->xpixel / 8 - scp->xoff - scp->xsize;
825 
826 	for (i = 0; i < scp->ysize * scp->font_size; ++i) {
827 		if (scp->xoff > 0) {
828 			s = scp->sc->adp->va_window + line_width * (y + i);
829 			e = s + scp->xoff * 8 * pixel_size;
830 
831 			for (f = s; f < e; f += pixel_size)
832 				DRAW_PIXEL(scp, f, color);
833 		}
834 
835 		if (x > 0) {
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;
840 
841 			for (f = s; f < e; f += pixel_size)
842 				DRAW_PIXEL(scp, f, color);
843 		}
844 	}
845 }
846 
847 static void
848 vga_pxlborder_planar(scr_stat *scp, int color)
849 {
850 	vm_offset_t p;
851 	int line_width;
852 	int x;
853 	int y;
854 	int i;
855 
856 	vidd_set_border(scp->sc->adp, color);
857 
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;
865 	if (scp->yoff > 0)
866 		bzero_io((void *)p, line_width*scp->yoff*scp->font_size);
867 	y = (scp->yoff + scp->ysize)*scp->font_size;
868 	if (scp->ypixel > y)
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) {
873 		if (scp->xoff > 0)
874 			bzero_io((void *)(p + line_width*(y + i)), scp->xoff);
875 		if (x > 0)
876 			bzero_io((void *)(p + line_width*(y + i)
877 				     + scp->xoff + scp->xsize), x);
878 	}
879 	outw(GDCIDX, 0x0000);		/* set/reset */
880 	outw(GDCIDX, 0x0001);		/* set/reset enable */
881 }
882 
883 static void
884 vga_vgadraw_direct(scr_stat *scp, int from, int count, int flip)
885 {
886 	vm_offset_t d;
887 	vm_offset_t e;
888 	u_char *f;
889 	u_short col1, col2, color;
890 	int line_width, pixel_size;
891 	int i, j, k;
892 	int a;
893 
894 	line_width = scp->sc->adp->va_line_width;
895 	pixel_size = scp->sc->adp->va_info.vi_pixel_size;
896 
897 	d = GET_PIXEL(scp, from, 8 * pixel_size, line_width);
898 
899 	if (from + count > scp->xsize * scp->ysize)
900 		count = scp->xsize * scp->ysize - from;
901 
902 	for (i = from; count-- > 0; ++i) {
903 		a = sc_vtb_geta(&scp->vtb, i);
904 
905 		if (flip)
906 			a = vga_flipattr(a, FALSE);
907 		col1 = (a & 0x0f00) >> 8;
908 		col2 = (a & 0xf000) >> 12;
909 
910 		e = d;
911 		f = &(scp->font[sc_vtb_getc(&scp->vtb, i) * scp->font_size]);
912 
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);
917 			}
918 
919 			e += line_width;
920 		}
921 
922 		d += 8 * pixel_size;
923 
924 		if ((i % scp->xsize) == scp->xsize - 1)
925 			d += scp->font_size * line_width -
926 			    scp->xsize * 8 * pixel_size;
927 	}
928 }
929 
930 static void
931 vga_vgadraw_planar(scr_stat *scp, int from, int count, int flip)
932 {
933 	vm_offset_t d;
934 	vm_offset_t e;
935 	u_char *f;
936 	u_short bg, fg;
937 	u_short col1, col2;
938 	int line_width;
939 	int i, j;
940 	int a;
941 	u_char c;
942 
943 	line_width = scp->sc->adp->va_line_width;
944 
945 	d = GET_PIXEL(scp, from, 1, line_width);
946 
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 */
950 	} else
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 */
954 	fg = bg = -1;
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);
959 		if (flip)
960 			a = vga_flipattr(a, FALSE);
961 		col1 = a & 0x0f00;
962 		col2 = (a & 0xf000) >> 4;
963 		/* set background color in EGA/VGA latch */
964 		if (bg != col2) {
965 			bg = col2;
966 			fg = -1;
967 			outw(GDCIDX, bg | 0x00); /* set/reset */
968 			if (scp->sc->adp->va_type != KD_VGA)
969 				outw(GDCIDX, 0xff08); /* bit mask */
970 			writeb(d, 0xff);
971 			c = readb(d);		/* set bg color in the latch */
972 		}
973 		/* foreground color */
974 		if (fg != col1) {
975 			fg = col1;
976 			outw(GDCIDX, col1 | 0x00); /* set/reset */
977 		}
978 		e = d;
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)
982 				writeb(e, *f);
983 			else {
984 				outw(GDCIDX, (*f << 8) | 0x08);	/* bit mask */
985 				writeb(e, 0);
986 			}
987 			e += line_width;
988 		}
989 		++d;
990 		if ((i % scp->xsize) == scp->xsize - 1)
991 			d += scp->font_size * line_width - scp->xsize;
992 	}
993 	if (scp->sc->adp->va_type == KD_VGA)
994 		outw(GDCIDX, 0x0005);	/* read mode 0, write mode 0 */
995 	else
996 		outw(GDCIDX, 0xff08);	/* bit mask */
997 	outw(GDCIDX, 0x0000);		/* set/reset */
998 	outw(GDCIDX, 0x0001);		/* set/reset enable */
999 }
1000 
1001 static void
1002 vga_pxlcursor_shape(scr_stat *scp, int base, int height, int blink)
1003 {
1004 	vga_setmdp(scp);
1005 }
1006 
1007 static void
1008 draw_pxlcursor_direct(scr_stat *scp, int at, int on, int flip)
1009 {
1010 	vm_offset_t d;
1011 	u_char *f;
1012 	int line_width, pixel_size;
1013 	int height;
1014 	int col1, col2, color;
1015 	int a;
1016 	int i, j;
1017 
1018 	line_width = scp->sc->adp->va_line_width;
1019 	pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1020 
1021 	d = GET_PIXEL(scp, at, 8 * pixel_size, line_width) +
1022 	    (scp->font_size - scp->curs_attr.base - 1) * line_width;
1023 
1024 	a = sc_vtb_geta(&scp->vtb, at);
1025 
1026 	if (flip)
1027 		a = vga_flipattr(a, FALSE);
1028 	if (on)
1029 		a = vga_cursorattr_adj(a, FALSE);
1030 	col1 = (a & 0x0f00) >> 8;
1031 	col2 = a >> 12;
1032 
1033 	f = &(scp->font[sc_vtb_getc(&scp->vtb, at) * scp->font_size +
1034 	      scp->font_size - scp->curs_attr.base - 1]);
1035 
1036 	height = imin(scp->curs_attr.height, scp->font_size);
1037 
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);
1042 		}
1043 
1044 		d -= line_width;
1045 	}
1046 }
1047 
1048 static void
1049 draw_pxlcursor_planar(scr_stat *scp, int at, int on, int flip)
1050 {
1051 	vm_offset_t d;
1052 	u_char *f;
1053 	int line_width;
1054 	int height;
1055 	int col;
1056 	int a;
1057 	int i;
1058 	u_char c;
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(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 	c = 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, yoff;
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 	yoff = y - rounddown(y, line_width);
1195 	ymax = imin(y + mdp->md_height, scp->ypixel);
1196 
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 */
1200 	} else
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 */
1204 
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) {
1212 				readb(p + k);
1213 				if (scp->sc->adp->va_type == KD_VGA)
1214 					writeb(p + k, m1);
1215 				else {
1216 					/* bit mask: */
1217 					outw(GDCIDX, (m1 << 8) | 0x08);
1218 					writeb(p + k, 0);
1219 				}
1220 			}
1221 		}
1222 		p += line_width;
1223 	}
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) {
1231 				readb(p + k);
1232 				if (scp->sc->adp->va_type == KD_VGA)
1233 					writeb(p + k, m1);
1234 				else {
1235 					/* bit mask: */
1236 					outw(GDCIDX, (m1 << 8) | 0x08);
1237 					writeb(p + k, 0);
1238 				}
1239 			}
1240 		}
1241 		p += line_width;
1242 	}
1243 	if (scp->sc->adp->va_type == KD_VGA)
1244 		outw(GDCIDX, 0x0005);	/* read mode 0, write mode 0 */
1245 	else
1246 		outw(GDCIDX, 0xff08);	/* bit mask */
1247 	outw(GDCIDX, 0x0000);		/* set/reset */
1248 	outw(GDCIDX, 0x0001);		/* set/reset enable */
1249 }
1250 
1251 static void
1252 remove_pxlmouse_planar(scr_stat *scp, int x, int y)
1253 {
1254 	const struct mousedata *mdp;
1255 	vm_offset_t p;
1256 	int bx, by, i, line_width, xend, xoff, yend, yoff;
1257 
1258 	mdp = scp->mouse_data;
1259 
1260 	/*
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.
1263 	 */
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)
1269 		return;
1270 
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)
1282 			writeb(p, 0);
1283 	}
1284 	outw(GDCIDX, 0x0000);		/* set/reset */
1285 	outw(GDCIDX, 0x0001);		/* set/reset enable */
1286 }
1287 
1288 static void
1289 vga_pxlmouse_direct(scr_stat *scp, int x, int y, int on)
1290 {
1291 	const struct mousedata *mdp;
1292 	vm_offset_t p;
1293 	int line_width, pixel_size;
1294 	int xend, yend;
1295 	int i, j;
1296 
1297 	mdp = scp->mouse_data;
1298 
1299 	/*
1300 	 * Determine overlap with the border and then if removing, do nothing
1301 	 * if the overlap is empty.
1302 	 */
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)
1307 		return;
1308 
1309 	line_width = scp->sc->adp->va_line_width;
1310 	pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1311 
1312 	if (on)
1313 		goto do_on;
1314 
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);
1320 
1321 	return;
1322 
1323 do_on:
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);
1331 }
1332 
1333 static void
1334 vga_pxlmouse_planar(scr_stat *scp, int x, int y, int on)
1335 {
1336 	if (on)
1337 		draw_pxlmouse_planar(scp, x, y);
1338 	else
1339 		remove_pxlmouse_planar(scp, x, y);
1340 }
1341 
1342 #endif /* SC_NO_CUTPASTE */
1343 #endif /* SC_PIXEL_MODE */
1344 
1345 #ifndef SC_NO_MODE_CHANGE
1346 
1347 /* graphics mode renderer */
1348 
1349 static void
1350 vga_grborder(scr_stat *scp, int color)
1351 {
1352 	vidd_set_border(scp->sc->adp, color);
1353 }
1354 
1355 #endif
1356