xref: /freebsd/sys/dev/syscons/scvgarndr.c (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
35 
36 #include "opt_syscons.h"
37 #include "opt_vga.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/fbio.h>
44 #include <sys/consio.h>
45 #include <sys/sysctl.h>
46 
47 #include <machine/bus.h>
48 
49 #include <dev/fb/fbreg.h>
50 #include <dev/fb/vgareg.h>
51 #include <dev/syscons/syscons.h>
52 
53 #include <isa/isareg.h>
54 
55 #ifndef SC_RENDER_DEBUG
56 #define SC_RENDER_DEBUG		0
57 #endif
58 
59 static vr_clear_t		vga_txtclear;
60 static vr_draw_border_t		vga_txtborder;
61 static vr_draw_t		vga_txtdraw;
62 static vr_set_cursor_t		vga_txtcursor_shape;
63 static vr_draw_cursor_t		vga_txtcursor;
64 static vr_blink_cursor_t	vga_txtblink;
65 #ifndef SC_NO_CUTPASTE
66 static vr_draw_mouse_t		vga_txtmouse;
67 #else
68 #define vga_txtmouse		(vr_draw_mouse_t *)vga_nop
69 #endif
70 
71 #ifdef SC_PIXEL_MODE
72 static vr_init_t		vga_rndrinit;
73 static vr_clear_t		vga_pxlclear_direct;
74 static vr_clear_t		vga_pxlclear_planar;
75 static vr_draw_border_t		vga_pxlborder_direct;
76 static vr_draw_border_t		vga_pxlborder_planar;
77 static vr_draw_t		vga_vgadraw_direct;
78 static vr_draw_t		vga_vgadraw_planar;
79 static vr_set_cursor_t		vga_pxlcursor_shape;
80 static vr_draw_cursor_t		vga_pxlcursor_direct;
81 static vr_draw_cursor_t		vga_pxlcursor_planar;
82 static vr_blink_cursor_t	vga_pxlblink_direct;
83 static vr_blink_cursor_t	vga_pxlblink_planar;
84 #ifndef SC_NO_CUTPASTE
85 static vr_draw_mouse_t		vga_pxlmouse_direct;
86 static vr_draw_mouse_t		vga_pxlmouse_planar;
87 #else
88 #define vga_pxlmouse_direct	(vr_draw_mouse_t *)vga_nop
89 #define vga_pxlmouse_planar	(vr_draw_mouse_t *)vga_nop
90 #endif
91 #endif /* SC_PIXEL_MODE */
92 
93 #ifndef SC_NO_MODE_CHANGE
94 static vr_draw_border_t		vga_grborder;
95 #endif
96 
97 static void			vga_nop(scr_stat *scp);
98 
99 static sc_rndr_sw_t txtrndrsw = {
100 	(vr_init_t *)vga_nop,
101 	vga_txtclear,
102 	vga_txtborder,
103 	vga_txtdraw,
104 	vga_txtcursor_shape,
105 	vga_txtcursor,
106 	vga_txtblink,
107 	(vr_set_mouse_t *)vga_nop,
108 	vga_txtmouse,
109 };
110 RENDERER(mda, 0, txtrndrsw, vga_set);
111 RENDERER(cga, 0, txtrndrsw, vga_set);
112 RENDERER(ega, 0, txtrndrsw, vga_set);
113 RENDERER(vga, 0, txtrndrsw, vga_set);
114 
115 #ifdef SC_PIXEL_MODE
116 static sc_rndr_sw_t vgarndrsw = {
117 	vga_rndrinit,
118 	(vr_clear_t *)vga_nop,
119 	(vr_draw_border_t *)vga_nop,
120 	(vr_draw_t *)vga_nop,
121 	vga_pxlcursor_shape,
122 	(vr_draw_cursor_t *)vga_nop,
123 	(vr_blink_cursor_t *)vga_nop,
124 	(vr_set_mouse_t *)vga_nop,
125 	(vr_draw_mouse_t *)vga_nop,
126 };
127 RENDERER(ega, PIXEL_MODE, vgarndrsw, vga_set);
128 RENDERER(vga, PIXEL_MODE, vgarndrsw, vga_set);
129 #endif /* SC_PIXEL_MODE */
130 
131 #ifndef SC_NO_MODE_CHANGE
132 static sc_rndr_sw_t grrndrsw = {
133 	(vr_init_t *)vga_nop,
134 	(vr_clear_t *)vga_nop,
135 	vga_grborder,
136 	(vr_draw_t *)vga_nop,
137 	(vr_set_cursor_t *)vga_nop,
138 	(vr_draw_cursor_t *)vga_nop,
139 	(vr_blink_cursor_t *)vga_nop,
140 	(vr_set_mouse_t *)vga_nop,
141 	(vr_draw_mouse_t *)vga_nop,
142 };
143 RENDERER(cga, GRAPHICS_MODE, grrndrsw, vga_set);
144 RENDERER(ega, GRAPHICS_MODE, grrndrsw, vga_set);
145 RENDERER(vga, GRAPHICS_MODE, grrndrsw, vga_set);
146 #endif /* SC_NO_MODE_CHANGE */
147 
148 RENDERER_MODULE(vga, vga_set);
149 
150 #ifndef SC_NO_CUTPASTE
151 #if !defined(SC_ALT_MOUSE_IMAGE) || defined(SC_PIXEL_MODE)
152 struct mousedata {
153 	u_short	md_border[16];
154 	u_short	md_interior[16];
155 	u_char	md_width;
156 	u_char	md_height;
157 	u_char	md_baspect;
158 	u_char	md_iaspect;
159 	const char *md_name;
160 };
161 
162 static const struct mousedata mouse10x16_50 = { {
163 	0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8200,
164 	0x8400, 0x8400, 0x8400, 0x9200, 0xB200, 0xA900, 0xC900, 0x8600, }, {
165 	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7C00,
166 	0x7800, 0x7800, 0x7800, 0x6C00, 0x4C00, 0x4600, 0x0600, 0x0000, },
167 	10, 16, 49, 52, "mouse10x16_50",
168 };
169 
170 static const struct mousedata mouse8x14_67 = { {
171 	0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8700,
172 	0x8400, 0x9200, 0xB200, 0xA900, 0xC900, 0x0600, 0x0000, 0x0000, }, {
173 	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7800,
174 	0x7800, 0x6C00, 0x4C00, 0x4600, 0x0600, 0x0000, 0x0000, 0x0000, },
175 	8, 14, 64, 65, "mouse8x14_67",
176 };
177 
178 static const struct mousedata mouse8x13_75 = { {
179 	0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8600, 0x8400,
180 	0xB200, 0xD200, 0x0900, 0x0900, 0x0600, 0x0000, 0x0000, 0x0000, }, {
181 	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7800, 0x7800,
182 	0x4C00, 0x0C00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000, },
183 	8, 13, 75, 80, "mouse8x13_75",
184 };
185 
186 static const struct mousedata mouse10x16_75 = { {
187 	0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8700,
188 	0x8400, 0x9200, 0xB200, 0xC900, 0x0900, 0x0480, 0x0480, 0x0300, }, {
189 	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7800,
190 	0x7800, 0x6C00, 0x4C00, 0x0600, 0x0600, 0x0300, 0x0300, 0x0000, },
191 	10, 16, 72, 75, "mouse10x16_75",
192 };
193 
194 static const struct mousedata mouse9x13_90 = { {
195 	0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8780,
196 	0x9200, 0xB200, 0xD900, 0x8900, 0x0600, 0x0000, 0x0000, 0x0000, }, {
197 	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7800,
198 	0x6C00, 0x4C00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000, },
199 	9, 13, 89, 89, "mouse9x13_90",
200 };
201 
202 static const struct mousedata mouse10x16_90 = { {
203 	0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8080,
204 	0x8040, 0x83E0, 0x8200, 0x9900, 0xA900, 0xC480, 0x8480, 0x0300, }, {
205 	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7F00,
206 	0x7F80, 0x7C00, 0x7C00, 0x6600, 0x4600, 0x0300, 0x0300, 0x0000, },
207 	10, 16, 89, 89, "mouse10x16_90",
208 };
209 
210 static const struct mousedata mouse9x13_100 = { {
211 	0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8780,
212 	0xB200, 0xD200, 0x8900, 0x0900, 0x0600, 0x0000, 0x0000, 0x0000, }, {
213 	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7800,
214 	0x4C00, 0x0C00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000, },
215 	9, 13, 106, 113, "mouse9x13_100",
216 };
217 
218 static const struct mousedata mouse10x16_100 = { {
219 	0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8080,
220 	0x8040, 0x83C0, 0x9200, 0xA900, 0xC900, 0x0480, 0x0480, 0x0300, }, {
221 	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7F00,
222 	0x7F80, 0x7C00, 0x6C00, 0x4600, 0x0600, 0x0300, 0x0300, 0x0000, },
223 	10, 16, 96, 106, "mouse10x16_100",
224 };
225 
226 static const struct mousedata mouse10x14_120 = { {
227 	0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8080,
228 	0x97C0, 0xB200, 0xF200, 0xC900, 0x8900, 0x0600, 0x0000, 0x0000, }, {
229 	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7F00,
230 	0x6800, 0x4C00, 0x0C00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, },
231 	10, 14, 120, 124, "mouse10x14_120",
232 };
233 
234 static const struct mousedata mouse10x16_120 = { {
235 	0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8080,
236 	0x97C0, 0xB200, 0xF200, 0xC900, 0x8900, 0x0480, 0x0480, 0x0300, }, {
237 	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7F00,
238 	0x6800, 0x4C00, 0x0C00, 0x0600, 0x0600, 0x0300, 0x0300, 0x0000, },
239 	10, 16, 120, 124, "mouse10x16_120",
240 };
241 
242 static const struct mousedata mouse9x13_133 = { {
243 	0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8080,
244 	0x9780, 0xB200, 0xC900, 0x0900, 0x0600, 0x0000, 0x0000, 0x0000, }, {
245 	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7F00,
246 	0x6800, 0x4C00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000, },
247 	9, 13, 142, 124, "mouse9x13_133",
248 };
249 
250 static const struct mousedata mouse10x16_133 = { {
251 	0xC000, 0xA000, 0x9000, 0x8800, 0x8400, 0x8200, 0x8100, 0x8080,
252 	0x8040, 0x93E0, 0xB200, 0xC900, 0x8900, 0x0480, 0x0480, 0x0300, }, {
253 	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7F00,
254 	0x7F80, 0x6C00, 0x4C00, 0x0600, 0x0600, 0x0300, 0x0300, 0x0000, },
255 	10, 16, 120, 133, "mouse10x16_133",
256 };
257 
258 static const struct mousedata mouse14x10_240 = { {
259 	0xF800, 0xCE00, 0xC380, 0xC0E0, 0xC038, 0xC1FC, 0xDCC0, 0xF660,
260 	0xC330, 0x01E0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, }, {
261 	0x0000, 0x3000, 0x3C00, 0x3F00, 0x3FC0, 0x3E00, 0x2300, 0x0180,
262 	0x00C0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, },
263 	14, 10, 189, 189, "mouse14x10_240",
264 };
265 
266 static const struct mousedata * const mouselarge[] = {
267 	&mouse10x16_50,
268 	&mouse8x14_67,
269 	&mouse10x16_75,
270 	&mouse10x16_90,
271 	&mouse10x16_100,
272 	&mouse10x16_120,
273 	&mouse10x16_133,
274 	&mouse14x10_240,
275 };
276 
277 static const struct mousedata * const mousesmall[] = {
278 	&mouse8x14_67,
279 	&mouse8x13_75,
280 	&mouse9x13_90,
281 	&mouse9x13_100,
282 	&mouse10x14_120,
283 	&mouse9x13_133,
284 	&mouse14x10_240,
285 };
286 #endif
287 #endif
288 
289 #ifdef SC_PIXEL_MODE
290 #define	GET_PIXEL(scp, pos, x, w)					\
291 ({									\
292 	(scp)->sc->adp->va_window +					\
293 	    (x) * (scp)->xoff +						\
294 	    (scp)->yoff * (scp)->font_size * (w) +			\
295 	    (x) * ((pos) % (scp)->xsize) +				\
296 	    (scp)->font_size * (w) * ((pos) / (scp)->xsize);		\
297 })
298 
299 #define	DRAW_PIXEL(scp, pos, color) do {				\
300 	switch ((scp)->sc->adp->va_info.vi_depth) {			\
301 	case 32:							\
302 		writel((pos), vga_palette32[color]);			\
303 		break;							\
304 	case 24:							\
305 		if (((pos) & 1) == 0) {					\
306 			writew((pos), vga_palette32[color]);		\
307 			writeb((pos) + 2, vga_palette32[color] >> 16);	\
308 		} else {						\
309 			writeb((pos), vga_palette32[color]);		\
310 			writew((pos) + 1, vga_palette32[color] >> 8);	\
311 		}							\
312 		break;							\
313 	case 16:							\
314 		if ((scp)->sc->adp->va_info.vi_pixel_fsizes[1] == 5)	\
315 			writew((pos), vga_palette15[color]);		\
316 		else							\
317 			writew((pos), vga_palette16[color]);		\
318 		break;							\
319 	case 15:							\
320 		writew((pos), vga_palette15[color]);			\
321 		break;							\
322 	case 8:								\
323 		writeb((pos), (uint8_t)(color));			\
324 	}								\
325 } while (0)
326 
327 static uint32_t vga_palette32[16] = {
328 	0x000000, 0x0000ad, 0x00ad00, 0x00adad,
329 	0xad0000, 0xad00ad, 0xad5200, 0xadadad,
330 	0x525252, 0x5252ff, 0x52ff52, 0x52ffff,
331 	0xff5252, 0xff52ff, 0xffff52, 0xffffff
332 };
333 
334 static uint16_t vga_palette16[16] = {
335 	0x0000, 0x0016, 0x0560, 0x0576, 0xb000, 0xb016, 0xb2a0, 0xb576,
336 	0x52aa, 0x52bf, 0x57ea, 0x57ff, 0xfaaa, 0xfabf, 0xffea, 0xffff
337 };
338 
339 static uint16_t vga_palette15[16] = {
340 	0x0000, 0x0016, 0x02c0, 0x02d6, 0x5800, 0x5816, 0x5940, 0x5ad6,
341 	0x294a, 0x295f, 0x2bea, 0x2bff, 0x7d4a, 0x7d5f, 0x7fea, 0x7fff
342 };
343 #endif
344 
345 static int vga_aspect_scale= 100;
346 SYSCTL_INT(_machdep, OID_AUTO, vga_aspect_scale, CTLFLAG_RW,
347     &vga_aspect_scale, 0, "Aspect scale ratio (3:4):actual times 100");
348 
349 static u_short
350 vga_flipattr(u_short a, int blink)
351 {
352 	if (blink)
353 		a = (a & 0x8800) | ((a & 0x7000) >> 4) |
354 		    ((a & 0x0700) << 4);
355 	else
356 		a = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
357 	return (a);
358 }
359 
360 static u_short
361 vga_cursorattr_adj(scr_stat *scp, u_short a, int blink)
362 {
363 	int i;
364 	u_short bg, bgmask, fg, newbg;
365 
366 	/*
367 	 * The cursor attribute is usually that of the underlying char
368 	 * with only the bg changed, to the first preferred color that
369 	 * differs from both the fg and bg.  If there is no such color,
370 	 * use reverse video.
371 	 */
372 	bgmask = blink ? 0x7000 : 0xf000;
373 	bg = a & bgmask;
374 	fg = a & 0x0f00;
375 	for (i = 0; i < nitems(scp->curs_attr.bg); i++) {
376 		newbg = (scp->curs_attr.bg[i] << 12) & bgmask;
377 		if (newbg != bg && newbg != (fg << 4))
378 			break;
379 	}
380 	if (i == nitems(scp->curs_attr.bg))
381 		return (vga_flipattr(a, blink));
382 	return (fg | newbg | (blink ? a & 0x8000 : 0));
383 }
384 
385 static void
386 vga_setmdp(scr_stat *scp)
387 {
388 #if !defined(SC_NO_CUTPASTE) && \
389    (!defined(SC_ALT_MOUSE_IMAGE) || defined(SC_PIXEL_MODE))
390 	const struct mousedata *mdp;
391 	const struct mousedata * const *mdpp;
392 	int aspect, best_i, best_v, i, n, v, wb, wi, xpixel, ypixel;
393 
394 	xpixel = scp->xpixel;
395 	ypixel = scp->ypixel;
396 	if (scp->sc->adp->va_flags & V_ADP_CWIDTH9)
397 		xpixel = xpixel * 9 / 8;
398 
399 	/* If 16:9 +-1%, assume square pixels, else scale to 4:3 or full. */
400 	aspect = xpixel * 900 / ypixel / 16;
401 	if (aspect < 99 || aspect > 100)
402 		aspect = xpixel * 300 / ypixel / 4 * vga_aspect_scale / 100;
403 
404 	/*
405 	 * Use 10x16 cursors except even with 8x8 fonts except in ~200-
406 	 * line modes where pixels are very large and in text mode where
407 	 * even 13 pixels high is really 4 too many.  Clipping a 16-high
408 	 * cursor at 9-high gives a variable tail which looks better than
409 	 * a smaller cursor with a constant tail.
410 	 *
411 	 * XXX: the IS*SC() macros don't work when this is called at the
412 	 * end of a mode switch since UNKNOWN_SC is still set.
413 	 */
414 	if (scp->font_size <= 8 &&
415 	    (ypixel < 300 || !(scp->status & PIXEL_MODE))) {
416 		mdpp = &mousesmall[0];
417 		n = nitems(mousesmall);
418 	} else {
419 		mdpp = &mouselarge[0];
420 		n = nitems(mouselarge);
421 	}
422 	if (scp->status & PIXEL_MODE) {
423 		wb = 1024;
424 		wi = 256;
425 	} else {
426 		wb = 256;
427 		wi = 1024;
428 	}
429 	best_i = 0;
430 	best_v = 0x7fffffff;
431 	for (i = 0; i < n; i++) {
432 		v = (wb * abs(mdpp[i]->md_baspect - aspect) +
433 		     wi * abs(mdpp[i]->md_iaspect - aspect)) / aspect;
434 		if (best_v > v) {
435 			best_v = v;
436 			best_i = i;
437 		}
438 	}
439 	mdp = mdpp[best_i];
440 	scp->mouse_data = mdp;
441 #endif /* !SC_NO_CUTPASTE && (!SC_ALT_MOUSE_IMAGE || SC_PIXEL_MODE) */
442 }
443 
444 static void
445 vga_nop(scr_stat *scp)
446 {
447 }
448 
449 /* text mode renderer */
450 
451 static void
452 vga_txtclear(scr_stat *scp, int c, int attr)
453 {
454 	sc_vtb_clear(&scp->scr, c, attr);
455 }
456 
457 static void
458 vga_txtborder(scr_stat *scp, int color)
459 {
460 	vidd_set_border(scp->sc->adp, color);
461 }
462 
463 static void
464 vga_txtdraw(scr_stat *scp, int from, int count, int flip)
465 {
466 	vm_offset_t p;
467 	int c;
468 	int a;
469 
470 	if (from + count > scp->xsize*scp->ysize)
471 		count = scp->xsize*scp->ysize - from;
472 
473 	if (flip) {
474 		for (p = sc_vtb_pointer(&scp->scr, from); count-- > 0; ++from) {
475 			c = sc_vtb_getc(&scp->vtb, from);
476 			a = sc_vtb_geta(&scp->vtb, from);
477 			a = vga_flipattr(a, TRUE);
478 			p = sc_vtb_putchar(&scp->scr, p, c, a);
479 		}
480 	} else {
481 		sc_vtb_copy(&scp->vtb, from, &scp->scr, from, count);
482 	}
483 }
484 
485 static void
486 vga_txtcursor_shape(scr_stat *scp, int base, int height, int blink)
487 {
488 	vga_setmdp(scp);
489 	if (base < 0 || base >= scp->font_size)
490 		return;
491 	/* the caller may set height <= 0 in order to disable the cursor */
492 	vidd_set_hw_cursor_shape(scp->sc->adp, base, height,
493 	    scp->font_size, blink);
494 }
495 
496 static void
497 draw_txtcharcursor(scr_stat *scp, int at, u_short c, u_short a, int flip)
498 {
499 	sc_softc_t *sc;
500 
501 	sc = scp->sc;
502 
503 #ifndef SC_NO_FONT_LOADING
504 	if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
505 		unsigned char *font;
506 		int h;
507 		int i;
508 
509 		if (scp->font_size < 14) {
510 			font = sc->font_8;
511 			h = 8;
512 		} else if (scp->font_size >= 16) {
513 			font = sc->font_16;
514 			h = 16;
515 		} else {
516 			font = sc->font_14;
517 			h = 14;
518 		}
519 		if (scp->curs_attr.base >= h)
520 			return;
521 		if (flip)
522 			a = vga_flipattr(a, TRUE);
523 		/*
524 		 * This clause handles partial-block cursors in text mode.
525 		 * We want to change the attribute only under the partial
526 		 * block, but in text mode we can only change full blocks.
527 		 * Use reverse video instead.
528 		 */
529 		bcopy(font + c*h, font + sc->cursor_char*h, h);
530 		font = font + sc->cursor_char*h;
531 		for (i = imax(h - scp->curs_attr.base - scp->curs_attr.height, 0);
532 			i < h - scp->curs_attr.base; ++i) {
533 			font[i] ^= 0xff;
534 		}
535 		/* XXX */
536 		vidd_load_font(sc->adp, 0, h, 8, font, sc->cursor_char, 1);
537 		sc_vtb_putc(&scp->scr, at, sc->cursor_char, a);
538 	} else
539 #endif /* SC_NO_FONT_LOADING */
540 	{
541 		if (flip)
542 			a = vga_flipattr(a, TRUE);
543 		a = vga_cursorattr_adj(scp, a, TRUE);
544 		sc_vtb_putc(&scp->scr, at, c, a);
545 	}
546 }
547 
548 static void
549 vga_txtcursor(scr_stat *scp, int at, int blink, int on, int flip)
550 {
551 	video_adapter_t *adp;
552 	int cursor_attr;
553 
554 	if (scp->curs_attr.height <= 0)	/* the text cursor is disabled */
555 		return;
556 
557 	adp = scp->sc->adp;
558 	if (blink) {
559 		scp->status |= VR_CURSOR_BLINK;
560 		if (on) {
561 			scp->status |= VR_CURSOR_ON;
562 			vidd_set_hw_cursor(adp, at%scp->xsize,
563 			    at/scp->xsize);
564 		} else {
565 			if (scp->status & VR_CURSOR_ON)
566 				vidd_set_hw_cursor(adp, -1, -1);
567 			scp->status &= ~VR_CURSOR_ON;
568 		}
569 	} else {
570 		scp->status &= ~VR_CURSOR_BLINK;
571 		if (on) {
572 			scp->status |= VR_CURSOR_ON;
573 			draw_txtcharcursor(scp, at,
574 					   sc_vtb_getc(&scp->vtb, at),
575 					   sc_vtb_geta(&scp->vtb, at),
576 					   flip);
577 		} else {
578 			cursor_attr = sc_vtb_geta(&scp->vtb, at);
579 			if (flip)
580 				cursor_attr = vga_flipattr(cursor_attr, TRUE);
581 			if (scp->status & VR_CURSOR_ON)
582 				sc_vtb_putc(&scp->scr, at,
583 					    sc_vtb_getc(&scp->vtb, at),
584 					    cursor_attr);
585 			scp->status &= ~VR_CURSOR_ON;
586 		}
587 	}
588 }
589 
590 static void
591 vga_txtblink(scr_stat *scp, int at, int flip)
592 {
593 }
594 
595 int sc_txtmouse_no_retrace_wait;
596 
597 #ifndef SC_NO_CUTPASTE
598 
599 static void
600 draw_txtmouse(scr_stat *scp, int x, int y)
601 {
602 #ifndef SC_ALT_MOUSE_IMAGE
603     if (ISMOUSEAVAIL(scp->sc->adp->va_flags)) {
604 	const struct mousedata *mdp;
605 	uint32_t border, interior;
606 	u_char font_buf[128];
607 	u_short cursor[32];
608 	u_char c;
609 	int pos;
610 	int xoffset, yoffset;
611 	int crtc_addr;
612 	int i;
613 
614 	mdp = scp->mouse_data;
615 
616 	/* prepare mousepointer char's bitmaps */
617 	pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
618 	bcopy(scp->font + sc_vtb_getc(&scp->scr, pos)*scp->font_size,
619 	      &font_buf[0], scp->font_size);
620 	bcopy(scp->font + sc_vtb_getc(&scp->scr, pos + 1)*scp->font_size,
621 	      &font_buf[32], scp->font_size);
622 	bcopy(scp->font
623 		 + sc_vtb_getc(&scp->scr, pos + scp->xsize)*scp->font_size,
624 	      &font_buf[64], scp->font_size);
625 	bcopy(scp->font
626 		 + sc_vtb_getc(&scp->scr, pos + scp->xsize + 1)*scp->font_size,
627 	      &font_buf[96], scp->font_size);
628 	for (i = 0; i < scp->font_size; ++i) {
629 		cursor[i] = font_buf[i]<<8 | font_buf[i+32];
630 		cursor[i + scp->font_size] = font_buf[i+64]<<8 | font_buf[i+96];
631 	}
632 
633 	/* now and-or in the mousepointer image */
634 	xoffset = x%8;
635 	yoffset = y%scp->font_size;
636 	for (i = 0; i < 16; ++i) {
637 		border = mdp->md_border[i] << 8; /* avoid right shifting out */
638 		interior = mdp->md_interior[i] << 8;
639 		border >>= xoffset;		/* normalize */
640 		interior >>= xoffset;
641 		if (scp->sc->adp->va_flags & V_ADP_CWIDTH9) {
642 			/* skip gaps between characters */
643 			border = (border & 0xff0000) |
644 				 (border & 0x007f80) << 1 |
645 				 (border & 0x00003f) << 2;
646 			interior = (interior & 0xff0000) |
647 				   (interior & 0x007f80) << 1 |
648 				   (interior & 0x00003f) << 2;
649 		}
650 		border >>= 8;			/* back to normal position */
651 		interior >>= 8;
652 		cursor[i + yoffset] = (cursor[i + yoffset]  & ~border) |
653 				      interior;
654 	}
655 	for (i = 0; i < scp->font_size; ++i) {
656 		font_buf[i] = (cursor[i] & 0xff00) >> 8;
657 		font_buf[i + 32] = cursor[i] & 0xff;
658 		font_buf[i + 64] = (cursor[i + scp->font_size] & 0xff00) >> 8;
659 		font_buf[i + 96] = cursor[i + scp->font_size] & 0xff;
660 	}
661 
662 #if 1
663 	/* wait for vertical retrace to avoid jitter on some videocards */
664 	crtc_addr = scp->sc->adp->va_crtc_addr;
665 	while (!sc_txtmouse_no_retrace_wait &&
666 	    !(inb(crtc_addr + 6) & 0x08))
667 		/* idle */ ;
668 #endif
669 	c = scp->sc->mouse_char;
670 	vidd_load_font(scp->sc->adp, 0, 32, 8, font_buf, c, 4);
671 
672 	sc_vtb_putc(&scp->scr, pos, c, sc_vtb_geta(&scp->scr, pos));
673 	/* FIXME: may be out of range! */
674 	sc_vtb_putc(&scp->scr, pos + scp->xsize, c + 2,
675 		    sc_vtb_geta(&scp->scr, pos + scp->xsize));
676 	if (x < (scp->xsize - 1)*8) {
677 		sc_vtb_putc(&scp->scr, pos + 1, c + 1,
678 			    sc_vtb_geta(&scp->scr, pos + 1));
679 		sc_vtb_putc(&scp->scr, pos + scp->xsize + 1, c + 3,
680 			    sc_vtb_geta(&scp->scr, pos + scp->xsize + 1));
681 	}
682     } else
683 #endif /* SC_ALT_MOUSE_IMAGE */
684     {
685 	/* Red, magenta and brown are mapped to green to keep it readable */
686 	static const int col_conv[16] = {
687 		6, 6, 6, 6, 2, 2, 2, 6, 14, 14, 14, 14, 10, 10, 10, 14
688 	};
689 	int pos;
690 	int color;
691 	int a;
692 
693 	pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
694 	a = sc_vtb_geta(&scp->scr, pos);
695 	if (scp->sc->adp->va_flags & V_ADP_COLOR)
696 		color = (col_conv[(a & 0xf000) >> 12] << 12)
697 			| ((a & 0x0f00) | 0x0800);
698 	else
699 		color = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
700 	sc_vtb_putc(&scp->scr, pos, sc_vtb_getc(&scp->scr, pos), color);
701     }
702 }
703 
704 static void
705 remove_txtmouse(scr_stat *scp, int x, int y)
706 {
707 }
708 
709 static void
710 vga_txtmouse(scr_stat *scp, int x, int y, int on)
711 {
712 	if (on)
713 		draw_txtmouse(scp, x, y);
714 	else
715 		remove_txtmouse(scp, x, y);
716 }
717 
718 #endif /* SC_NO_CUTPASTE */
719 
720 #ifdef SC_PIXEL_MODE
721 
722 /* pixel (raster text) mode renderer */
723 
724 static void
725 vga_rndrinit(scr_stat *scp)
726 {
727 	if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PLANAR) {
728 		scp->rndr->clear = vga_pxlclear_planar;
729 		scp->rndr->draw_border = vga_pxlborder_planar;
730 		scp->rndr->draw = vga_vgadraw_planar;
731 		scp->rndr->draw_cursor = vga_pxlcursor_planar;
732 		scp->rndr->blink_cursor = vga_pxlblink_planar;
733 		scp->rndr->draw_mouse = vga_pxlmouse_planar;
734 	} else
735 	if (scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_DIRECT ||
736 	    scp->sc->adp->va_info.vi_mem_model == V_INFO_MM_PACKED) {
737 		scp->rndr->clear = vga_pxlclear_direct;
738 		scp->rndr->draw_border = vga_pxlborder_direct;
739 		scp->rndr->draw = vga_vgadraw_direct;
740 		scp->rndr->draw_cursor = vga_pxlcursor_direct;
741 		scp->rndr->blink_cursor = vga_pxlblink_direct;
742 		scp->rndr->draw_mouse = vga_pxlmouse_direct;
743 	}
744 }
745 
746 static void
747 vga_pxlclear_direct(scr_stat *scp, int c, int attr)
748 {
749 	vm_offset_t p;
750 	int line_width;
751 	int pixel_size;
752 	int lines;
753 	int i;
754 
755 	line_width = scp->sc->adp->va_line_width;
756 	pixel_size = scp->sc->adp->va_info.vi_pixel_size;
757 	lines = scp->ysize * scp->font_size;
758 	p = scp->sc->adp->va_window +
759 	    line_width * scp->yoff * scp->font_size +
760 	    scp->xoff * 8 * pixel_size;
761 
762 	for (i = 0; i < lines; ++i) {
763 		bzero_io((void *)p, scp->xsize * 8 * pixel_size);
764 		p += line_width;
765 	}
766 }
767 
768 static void
769 vga_pxlclear_planar(scr_stat *scp, int c, int attr)
770 {
771 	vm_offset_t p;
772 	int line_width;
773 	int lines;
774 	int i;
775 
776 	/* XXX: we are just filling the screen with the background color... */
777 	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
778 	outw(GDCIDX, 0x0003);		/* data rotate/function select */
779 	outw(GDCIDX, 0x0f01);		/* set/reset enable */
780 	outw(GDCIDX, 0xff08);		/* bit mask */
781 	outw(GDCIDX, ((attr & 0xf000) >> 4) | 0x00); /* set/reset */
782 	line_width = scp->sc->adp->va_line_width;
783 	lines = scp->ysize*scp->font_size;
784 	p = scp->sc->adp->va_window + line_width*scp->yoff*scp->font_size
785 		+ scp->xoff;
786 	for (i = 0; i < lines; ++i) {
787 		bzero_io((void *)p, scp->xsize);
788 		p += line_width;
789 	}
790 	outw(GDCIDX, 0x0000);		/* set/reset */
791 	outw(GDCIDX, 0x0001);		/* set/reset enable */
792 }
793 
794 static void
795 vga_pxlborder_direct(scr_stat *scp, int color)
796 {
797 	vm_offset_t s;
798 	vm_offset_t e;
799 	vm_offset_t f;
800 	int line_width;
801 	int pixel_size;
802 	int x;
803 	int y;
804 	int i;
805 
806 	line_width = scp->sc->adp->va_line_width;
807 	pixel_size = scp->sc->adp->va_info.vi_pixel_size;
808 
809 	if (scp->yoff > 0) {
810 		s = scp->sc->adp->va_window;
811 		e = s + line_width * scp->yoff * scp->font_size;
812 
813 		for (f = s; f < e; f += pixel_size)
814 			DRAW_PIXEL(scp, f, color);
815 	}
816 
817 	y = (scp->yoff + scp->ysize) * scp->font_size;
818 
819 	if (scp->ypixel > y) {
820 		s = scp->sc->adp->va_window + line_width * y;
821 		e = s + line_width * (scp->ypixel - y);
822 
823 		for (f = s; f < e; f += pixel_size)
824 			DRAW_PIXEL(scp, f, color);
825 	}
826 
827 	y = scp->yoff * scp->font_size;
828 	x = scp->xpixel / 8 - scp->xoff - scp->xsize;
829 
830 	for (i = 0; i < scp->ysize * scp->font_size; ++i) {
831 		if (scp->xoff > 0) {
832 			s = scp->sc->adp->va_window + line_width * (y + i);
833 			e = s + scp->xoff * 8 * pixel_size;
834 
835 			for (f = s; f < e; f += pixel_size)
836 				DRAW_PIXEL(scp, f, color);
837 		}
838 
839 		if (x > 0) {
840 			s = scp->sc->adp->va_window + line_width * (y + i) +
841 			    scp->xoff * 8 * pixel_size +
842 			    scp->xsize * 8 * pixel_size;
843 			e = s + x * 8 * pixel_size;
844 
845 			for (f = s; f < e; f += pixel_size)
846 				DRAW_PIXEL(scp, f, color);
847 		}
848 	}
849 }
850 
851 static void
852 vga_pxlborder_planar(scr_stat *scp, int color)
853 {
854 	vm_offset_t p;
855 	int line_width;
856 	int x;
857 	int y;
858 	int i;
859 
860 	vidd_set_border(scp->sc->adp, color);
861 
862 	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
863 	outw(GDCIDX, 0x0003);		/* data rotate/function select */
864 	outw(GDCIDX, 0x0f01);		/* set/reset enable */
865 	outw(GDCIDX, 0xff08);		/* bit mask */
866 	outw(GDCIDX, (color << 8) | 0x00);	/* set/reset */
867 	line_width = scp->sc->adp->va_line_width;
868 	p = scp->sc->adp->va_window;
869 	if (scp->yoff > 0)
870 		bzero_io((void *)p, line_width*scp->yoff*scp->font_size);
871 	y = (scp->yoff + scp->ysize)*scp->font_size;
872 	if (scp->ypixel > y)
873 		bzero_io((void *)(p + line_width*y), line_width*(scp->ypixel - y));
874 	y = scp->yoff*scp->font_size;
875 	x = scp->xpixel/8 - scp->xoff - scp->xsize;
876 	for (i = 0; i < scp->ysize*scp->font_size; ++i) {
877 		if (scp->xoff > 0)
878 			bzero_io((void *)(p + line_width*(y + i)), scp->xoff);
879 		if (x > 0)
880 			bzero_io((void *)(p + line_width*(y + i)
881 				     + scp->xoff + scp->xsize), x);
882 	}
883 	outw(GDCIDX, 0x0000);		/* set/reset */
884 	outw(GDCIDX, 0x0001);		/* set/reset enable */
885 }
886 
887 static void
888 vga_vgadraw_direct(scr_stat *scp, int from, int count, int flip)
889 {
890 	vm_offset_t d;
891 	vm_offset_t e;
892 	u_char *f;
893 	u_short col1, col2, color;
894 	int line_width, pixel_size;
895 	int i, j, k;
896 	int a;
897 
898 	line_width = scp->sc->adp->va_line_width;
899 	pixel_size = scp->sc->adp->va_info.vi_pixel_size;
900 
901 	d = GET_PIXEL(scp, from, 8 * pixel_size, line_width);
902 
903 	if (from + count > scp->xsize * scp->ysize)
904 		count = scp->xsize * scp->ysize - from;
905 
906 	for (i = from; count-- > 0; ++i) {
907 		a = sc_vtb_geta(&scp->vtb, i);
908 
909 		if (flip)
910 			a = vga_flipattr(a, FALSE);
911 		col1 = (a & 0x0f00) >> 8;
912 		col2 = (a & 0xf000) >> 12;
913 
914 		e = d;
915 		f = &(scp->font[sc_vtb_getc(&scp->vtb, i) * scp->font_size]);
916 
917 		for (j = 0; j < scp->font_size; ++j, ++f) {
918 			for (k = 0; k < 8; ++k) {
919 				color = *f & (1 << (7 - k)) ? col1 : col2;
920 				DRAW_PIXEL(scp, e + pixel_size * k, color);
921 			}
922 
923 			e += line_width;
924 		}
925 
926 		d += 8 * pixel_size;
927 
928 		if ((i % scp->xsize) == scp->xsize - 1)
929 			d += scp->font_size * line_width -
930 			    scp->xsize * 8 * pixel_size;
931 	}
932 }
933 
934 static void
935 vga_vgadraw_planar(scr_stat *scp, int from, int count, int flip)
936 {
937 	vm_offset_t d;
938 	vm_offset_t e;
939 	u_char *f;
940 	u_short bg, fg;
941 	u_short col1, col2;
942 	int line_width;
943 	int i, j;
944 	int a;
945 	u_char c;
946 
947 	line_width = scp->sc->adp->va_line_width;
948 
949 	d = GET_PIXEL(scp, from, 1, line_width);
950 
951 	if (scp->sc->adp->va_type == KD_VGA) {
952 		outw(GDCIDX, 0x0305);	/* read mode 0, write mode 3 */
953 		outw(GDCIDX, 0xff08);	/* bit mask */
954 	} else
955 		outw(GDCIDX, 0x0005);	/* read mode 0, write mode 0 */
956 	outw(GDCIDX, 0x0003);		/* data rotate/function select */
957 	outw(GDCIDX, 0x0f01);		/* set/reset enable */
958 	fg = bg = -1;
959 	if (from + count > scp->xsize*scp->ysize)
960 		count = scp->xsize*scp->ysize - from;
961 	for (i = from; count-- > 0; ++i) {
962 		a = sc_vtb_geta(&scp->vtb, i);
963 		if (flip)
964 			a = vga_flipattr(a, FALSE);
965 		col1 = a & 0x0f00;
966 		col2 = (a & 0xf000) >> 4;
967 		/* set background color in EGA/VGA latch */
968 		if (bg != col2) {
969 			bg = col2;
970 			fg = -1;
971 			outw(GDCIDX, bg | 0x00); /* set/reset */
972 			if (scp->sc->adp->va_type != KD_VGA)
973 				outw(GDCIDX, 0xff08); /* bit mask */
974 			writeb(d, 0xff);
975 			c = readb(d);		/* set bg color in the latch */
976 		}
977 		/* foreground color */
978 		if (fg != col1) {
979 			fg = col1;
980 			outw(GDCIDX, col1 | 0x00); /* set/reset */
981 		}
982 		e = d;
983 		f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
984 		for (j = 0; j < scp->font_size; ++j, ++f) {
985 			if (scp->sc->adp->va_type == KD_VGA)
986 				writeb(e, *f);
987 			else {
988 				outw(GDCIDX, (*f << 8) | 0x08);	/* bit mask */
989 				writeb(e, 0);
990 			}
991 			e += line_width;
992 		}
993 		++d;
994 		if ((i % scp->xsize) == scp->xsize - 1)
995 			d += scp->font_size * line_width - scp->xsize;
996 	}
997 	if (scp->sc->adp->va_type == KD_VGA)
998 		outw(GDCIDX, 0x0005);	/* read mode 0, write mode 0 */
999 	else
1000 		outw(GDCIDX, 0xff08);	/* bit mask */
1001 	outw(GDCIDX, 0x0000);		/* set/reset */
1002 	outw(GDCIDX, 0x0001);		/* set/reset enable */
1003 }
1004 
1005 static void
1006 vga_pxlcursor_shape(scr_stat *scp, int base, int height, int blink)
1007 {
1008 	vga_setmdp(scp);
1009 }
1010 
1011 static void
1012 draw_pxlcursor_direct(scr_stat *scp, int at, int on, int flip)
1013 {
1014 	vm_offset_t d;
1015 	u_char *f;
1016 	int line_width, pixel_size;
1017 	int height;
1018 	int col1, col2, color;
1019 	int a;
1020 	int i, j;
1021 
1022 	line_width = scp->sc->adp->va_line_width;
1023 	pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1024 
1025 	d = GET_PIXEL(scp, at, 8 * pixel_size, line_width) +
1026 	    (scp->font_size - scp->curs_attr.base - 1) * line_width;
1027 
1028 	a = sc_vtb_geta(&scp->vtb, at);
1029 
1030 	if (flip)
1031 		a = vga_flipattr(a, FALSE);
1032 	if (on)
1033 		a = vga_cursorattr_adj(scp, a, FALSE);
1034 	col1 = (a & 0x0f00) >> 8;
1035 	col2 = a >> 12;
1036 
1037 	f = &(scp->font[sc_vtb_getc(&scp->vtb, at) * scp->font_size +
1038 	      scp->font_size - scp->curs_attr.base - 1]);
1039 
1040 	height = imin(scp->curs_attr.height, scp->font_size);
1041 
1042 	for (i = 0; i < height; ++i, --f) {
1043 		for (j = 0; j < 8; ++j) {
1044 			color = *f & (1 << (7 - j)) ? col1 : col2;
1045 			DRAW_PIXEL(scp, d + pixel_size * j, color);
1046 		}
1047 
1048 		d -= line_width;
1049 	}
1050 }
1051 
1052 static void
1053 draw_pxlcursor_planar(scr_stat *scp, int at, int on, int flip)
1054 {
1055 	vm_offset_t d;
1056 	u_char *f;
1057 	int line_width;
1058 	int height;
1059 	int col;
1060 	int a;
1061 	int i;
1062 	u_char c;
1063 
1064 	line_width = scp->sc->adp->va_line_width;
1065 
1066 	d = GET_PIXEL(scp, at, 1, line_width) +
1067 	    (scp->font_size - scp->curs_attr.base - 1) * line_width;
1068 
1069 	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
1070 	outw(GDCIDX, 0x0003);		/* data rotate/function select */
1071 	outw(GDCIDX, 0x0f01);		/* set/reset enable */
1072 	/* set background color in EGA/VGA latch */
1073 	a = sc_vtb_geta(&scp->vtb, at);
1074 	if (flip)
1075 		a = vga_flipattr(a, FALSE);
1076 	if (on)
1077 		a = vga_cursorattr_adj(scp, a, FALSE);
1078 	col = (a & 0xf000) >> 4;
1079 	outw(GDCIDX, col | 0x00);	/* set/reset */
1080 	outw(GDCIDX, 0xff08);		/* bit mask */
1081 	writeb(d, 0);
1082 	c = readb(d);			/* set bg color in the latch */
1083 	/* foreground color */
1084 	col = a & 0x0f00;
1085 	outw(GDCIDX, col | 0x00);	/* set/reset */
1086 	f = &(scp->font[sc_vtb_getc(&scp->vtb, at)*scp->font_size
1087 		+ scp->font_size - scp->curs_attr.base - 1]);
1088 	height = imin(scp->curs_attr.height, scp->font_size);
1089 	for (i = 0; i < height; ++i, --f) {
1090 		outw(GDCIDX, (*f << 8) | 0x08);	/* bit mask */
1091 	       	writeb(d, 0);
1092 		d -= line_width;
1093 	}
1094 	outw(GDCIDX, 0x0000);		/* set/reset */
1095 	outw(GDCIDX, 0x0001);		/* set/reset enable */
1096 	outw(GDCIDX, 0xff08);		/* bit mask */
1097 }
1098 
1099 static int pxlblinkrate = 0;
1100 
1101 static void
1102 vga_pxlcursor_direct(scr_stat *scp, int at, int blink, int on, int flip)
1103 {
1104 	if (scp->curs_attr.height <= 0)	/* the text cursor is disabled */
1105 		return;
1106 
1107 	if (on) {
1108 		if (!blink) {
1109 			scp->status |= VR_CURSOR_ON;
1110 			draw_pxlcursor_direct(scp, at, on, flip);
1111 		} else if (++pxlblinkrate & 4) {
1112 			pxlblinkrate = 0;
1113 			scp->status ^= VR_CURSOR_ON;
1114 			draw_pxlcursor_direct(scp, at,
1115 					      scp->status & VR_CURSOR_ON,
1116 					      flip);
1117 		}
1118 	} else {
1119 		if (scp->status & VR_CURSOR_ON)
1120 			draw_pxlcursor_direct(scp, at, on, flip);
1121 		scp->status &= ~VR_CURSOR_ON;
1122 	}
1123 	if (blink)
1124 		scp->status |= VR_CURSOR_BLINK;
1125 	else
1126 		scp->status &= ~VR_CURSOR_BLINK;
1127 }
1128 
1129 static void
1130 vga_pxlcursor_planar(scr_stat *scp, int at, int blink, int on, int flip)
1131 {
1132 	if (scp->curs_attr.height <= 0)	/* the text cursor is disabled */
1133 		return;
1134 
1135 	if (on) {
1136 		if (!blink) {
1137 			scp->status |= VR_CURSOR_ON;
1138 			draw_pxlcursor_planar(scp, at, on, flip);
1139 		} else if (++pxlblinkrate & 4) {
1140 			pxlblinkrate = 0;
1141 			scp->status ^= VR_CURSOR_ON;
1142 			draw_pxlcursor_planar(scp, at,
1143 					      scp->status & VR_CURSOR_ON,
1144 					      flip);
1145 		}
1146 	} else {
1147 		if (scp->status & VR_CURSOR_ON)
1148 			draw_pxlcursor_planar(scp, at, on, flip);
1149 		scp->status &= ~VR_CURSOR_ON;
1150 	}
1151 	if (blink)
1152 		scp->status |= VR_CURSOR_BLINK;
1153 	else
1154 		scp->status &= ~VR_CURSOR_BLINK;
1155 }
1156 
1157 static void
1158 vga_pxlblink_direct(scr_stat *scp, int at, int flip)
1159 {
1160 	if (!(scp->status & VR_CURSOR_BLINK))
1161 		return;
1162 	if (!(++pxlblinkrate & 4))
1163 		return;
1164 	pxlblinkrate = 0;
1165 	scp->status ^= VR_CURSOR_ON;
1166 	draw_pxlcursor_direct(scp, at, scp->status & VR_CURSOR_ON, flip);
1167 }
1168 
1169 static void
1170 vga_pxlblink_planar(scr_stat *scp, int at, int flip)
1171 {
1172 	if (!(scp->status & VR_CURSOR_BLINK))
1173 		return;
1174 	if (!(++pxlblinkrate & 4))
1175 		return;
1176 	pxlblinkrate = 0;
1177 	scp->status ^= VR_CURSOR_ON;
1178 	draw_pxlcursor_planar(scp, at, scp->status & VR_CURSOR_ON, flip);
1179 }
1180 
1181 #ifndef SC_NO_CUTPASTE
1182 
1183 static void
1184 draw_pxlmouse_planar(scr_stat *scp, int x, int y)
1185 {
1186 	const struct mousedata *mdp;
1187 	vm_offset_t p;
1188 	int line_width;
1189 	int xoff, yoff;
1190 	int ymax;
1191 	uint32_t m;
1192 	int i, j, k;
1193 	uint8_t m1;
1194 
1195 	mdp = scp->mouse_data;
1196 	line_width = scp->sc->adp->va_line_width;
1197 	xoff = (x - scp->xoff*8)%8;
1198 	yoff = y - rounddown(y, line_width);
1199 	ymax = imin(y + mdp->md_height, scp->ypixel);
1200 
1201 	if (scp->sc->adp->va_type == KD_VGA) {
1202 		outw(GDCIDX, 0x0305);	/* read mode 0, write mode 3 */
1203 		outw(GDCIDX, 0xff08);	/* bit mask */
1204 	} else
1205 		outw(GDCIDX, 0x0005);	/* read mode 0, write mode 0 */
1206 	outw(GDCIDX, 0x0003);		/* data rotate/function select */
1207 	outw(GDCIDX, 0x0f01);		/* set/reset enable */
1208 
1209 	outw(GDCIDX, (scp->curs_attr.mouse_ba << 8) | 0x00); /* set/reset */
1210 	p = scp->sc->adp->va_window + line_width*y + x/8;
1211 	for (i = y, j = 0; i < ymax; ++i, ++j) {
1212 		m = mdp->md_border[j] << 8 >> xoff;
1213 		for (k = 0; k < 3; ++k) {
1214 			m1 = m >> (8 * (2 - k));
1215 			if (m1 != 0 && x + 8 * k < scp->xpixel) {
1216 				readb(p + k);
1217 				if (scp->sc->adp->va_type == KD_VGA)
1218 					writeb(p + k, m1);
1219 				else {
1220 					/* bit mask: */
1221 					outw(GDCIDX, (m1 << 8) | 0x08);
1222 					writeb(p + k, 0);
1223 				}
1224 			}
1225 		}
1226 		p += line_width;
1227 	}
1228 	outw(GDCIDX, (scp->curs_attr.mouse_ia << 8) | 0x00); /* set/reset */
1229 	p = scp->sc->adp->va_window + line_width*y + x/8;
1230 	for (i = y, j = 0; i < ymax; ++i, ++j) {
1231 		m = mdp->md_interior[j] << 8 >> xoff;
1232 		for (k = 0; k < 3; ++k) {
1233 			m1 = m >> (8 * (2 - k));
1234 			if (m1 != 0 && x + 8 * k < scp->xpixel) {
1235 				readb(p + k);
1236 				if (scp->sc->adp->va_type == KD_VGA)
1237 					writeb(p + k, m1);
1238 				else {
1239 					/* bit mask: */
1240 					outw(GDCIDX, (m1 << 8) | 0x08);
1241 					writeb(p + k, 0);
1242 				}
1243 			}
1244 		}
1245 		p += line_width;
1246 	}
1247 	if (scp->sc->adp->va_type == KD_VGA)
1248 		outw(GDCIDX, 0x0005);	/* read mode 0, write mode 0 */
1249 	else
1250 		outw(GDCIDX, 0xff08);	/* bit mask */
1251 	outw(GDCIDX, 0x0000);		/* set/reset */
1252 	outw(GDCIDX, 0x0001);		/* set/reset enable */
1253 }
1254 
1255 static void
1256 remove_pxlmouse_planar(scr_stat *scp, int x, int y)
1257 {
1258 	const struct mousedata *mdp;
1259 	vm_offset_t p;
1260 	int bx, by, i, line_width, xend, xoff, yend, yoff;
1261 
1262 	mdp = scp->mouse_data;
1263 
1264 	/*
1265 	 * It is only necessary to remove the mouse image where it overlaps
1266 	 * the border.  Determine the overlap, and do nothing if it is empty.
1267 	 */
1268 	bx = (scp->xoff + scp->xsize) * 8;
1269 	by = (scp->yoff + scp->ysize) * scp->font_size;
1270 	xend = imin(x + mdp->md_width, scp->xpixel);
1271 	yend = imin(y + mdp->md_height, scp->ypixel);
1272 	if (xend <= bx && yend <= by)
1273 		return;
1274 
1275 	/* Repaint the non-empty overlap. */
1276 	line_width = scp->sc->adp->va_line_width;
1277 	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
1278 	outw(GDCIDX, 0x0003);		/* data rotate/function select */
1279 	outw(GDCIDX, 0x0f01);		/* set/reset enable */
1280 	outw(GDCIDX, 0xff08);		/* bit mask */
1281 	outw(GDCIDX, (scp->border << 8) | 0x00);	/* set/reset */
1282 	for (i = x / 8, xoff = i * 8; xoff < xend; ++i, xoff += 8) {
1283 		yoff = (xoff >= bx) ? y : by;
1284 		p = scp->sc->adp->va_window + yoff * line_width + i;
1285 		for (; yoff < yend; ++yoff, p += line_width)
1286 			writeb(p, 0);
1287 	}
1288 	outw(GDCIDX, 0x0000);		/* set/reset */
1289 	outw(GDCIDX, 0x0001);		/* set/reset enable */
1290 }
1291 
1292 static void
1293 vga_pxlmouse_direct(scr_stat *scp, int x, int y, int on)
1294 {
1295 	const struct mousedata *mdp;
1296 	vm_offset_t p;
1297 	int line_width, pixel_size;
1298 	int xend, yend;
1299 	int i, j;
1300 
1301 	mdp = scp->mouse_data;
1302 
1303 	/*
1304 	 * Determine overlap with the border and then if removing, do nothing
1305 	 * if the overlap is empty.
1306 	 */
1307 	xend = imin(x + mdp->md_width, scp->xpixel);
1308 	yend = imin(y + mdp->md_height, scp->ypixel);
1309 	if (!on && xend <= (scp->xoff + scp->xsize) * 8 &&
1310 	    yend <= (scp->yoff + scp->ysize) * scp->font_size)
1311 		return;
1312 
1313 	line_width = scp->sc->adp->va_line_width;
1314 	pixel_size = scp->sc->adp->va_info.vi_pixel_size;
1315 
1316 	if (on)
1317 		goto do_on;
1318 
1319 	/* Repaint overlap with the border (mess up the corner a little). */
1320 	p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1321 	for (i = 0; i < yend - y; i++, p += line_width)
1322 		for (j = xend - x - 1; j >= 0; j--)
1323 			DRAW_PIXEL(scp, p + j * pixel_size, scp->border);
1324 
1325 	return;
1326 
1327 do_on:
1328 	p = scp->sc->adp->va_window + y * line_width + x * pixel_size;
1329 	for (i = 0; i < yend - y; i++, p += line_width)
1330 		for (j = xend - x - 1; j >= 0; j--)
1331 			if (mdp->md_interior[i] & (1 << (15 - j)))
1332 				DRAW_PIXEL(scp, p + j * pixel_size,
1333 				    scp->curs_attr.mouse_ia);
1334 			else if (mdp->md_border[i] & (1 << (15 - j)))
1335 				DRAW_PIXEL(scp, p + j * pixel_size,
1336 				    scp->curs_attr.mouse_ba);
1337 }
1338 
1339 static void
1340 vga_pxlmouse_planar(scr_stat *scp, int x, int y, int on)
1341 {
1342 	if (on)
1343 		draw_pxlmouse_planar(scp, x, y);
1344 	else
1345 		remove_pxlmouse_planar(scp, x, y);
1346 }
1347 
1348 #endif /* SC_NO_CUTPASTE */
1349 #endif /* SC_PIXEL_MODE */
1350 
1351 #ifndef SC_NO_MODE_CHANGE
1352 
1353 /* graphics mode renderer */
1354 
1355 static void
1356 vga_grborder(scr_stat *scp, int color)
1357 {
1358 	vidd_set_border(scp->sc->adp, color);
1359 }
1360 
1361 #endif
1362