scgfbrndr.c revision 65176
1/*-
2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer as
10 *    the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/dev/syscons/scgfbrndr.c 65176 2000-08-28 21:48:13Z dfr $
27 */
28
29#include "opt_syscons.h"
30#include "opt_vga.h"
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/kernel.h>
35
36#include <machine/console.h>
37#include <machine/bus.h>
38
39#include <dev/fb/fbreg.h>
40#include <dev/fb/vgareg.h>
41#include <dev/syscons/syscons.h>
42
43#include <isa/isareg.h>
44
45#ifndef SC_RENDER_DEBUG
46#define SC_RENDER_DEBUG		0
47#endif
48
49static vr_clear_t		vga_txtclear;
50static vr_draw_border_t		vga_txtborder;
51static vr_draw_t		vga_txtdraw;
52static vr_set_cursor_t		vga_txtcursor_shape;
53static vr_draw_cursor_t		vga_txtcursor;
54static vr_blink_cursor_t	vga_txtblink;
55#ifndef SC_NO_CUTPASTE
56static vr_draw_mouse_t		vga_txtmouse;
57#else
58#define vga_txtmouse		(vr_draw_mouse_t *)vga_nop
59#endif
60
61#ifdef SC_PIXEL_MODE
62static vr_clear_t		vga_pxlclear;
63static vr_draw_border_t		vga_pxlborder;
64static vr_draw_t		vga_egadraw;
65static vr_draw_t		vga_vgadraw;
66static vr_set_cursor_t		vga_pxlcursor_shape;
67static vr_draw_cursor_t		vga_pxlcursor;
68static vr_blink_cursor_t	vga_pxlblink;
69#ifndef SC_NO_CUTPASTE
70static vr_draw_mouse_t		vga_pxlmouse;
71#else
72#define vga_pxlmouse		(vr_draw_mouse_t *)vga_nop
73#endif
74#endif /* SC_PIXEL_MODE */
75
76#ifndef SC_NO_MODE_CHANGE
77static vr_draw_border_t		vga_grborder;
78#endif
79
80static void			vga_nop(scr_stat *scp, ...);
81
82static struct linker_set	vga_set;
83
84static sc_rndr_sw_t txtrndrsw = {
85	vga_txtclear,
86	vga_txtborder,
87	vga_txtdraw,
88	vga_txtcursor_shape,
89	vga_txtcursor,
90	vga_txtblink,
91	(vr_set_mouse_t *)vga_nop,
92	vga_txtmouse,
93};
94RENDERER(mda, 0, txtrndrsw, vga_set);
95RENDERER(cga, 0, txtrndrsw, vga_set);
96RENDERER(ega, 0, txtrndrsw, vga_set);
97RENDERER(vga, 0, txtrndrsw, vga_set);
98
99#ifdef SC_PIXEL_MODE
100static sc_rndr_sw_t egarndrsw = {
101	vga_pxlclear,
102	vga_pxlborder,
103	vga_egadraw,
104	vga_pxlcursor_shape,
105	vga_pxlcursor,
106	vga_pxlblink,
107	(vr_set_mouse_t *)vga_nop,
108	vga_pxlmouse,
109};
110RENDERER(ega, PIXEL_MODE, egarndrsw, vga_set);
111
112static sc_rndr_sw_t vgarndrsw = {
113	vga_pxlclear,
114	vga_pxlborder,
115	vga_vgadraw,
116	vga_pxlcursor_shape,
117	vga_pxlcursor,
118	vga_pxlblink,
119	(vr_set_mouse_t *)vga_nop,
120	vga_pxlmouse,
121};
122RENDERER(vga, PIXEL_MODE, vgarndrsw, vga_set);
123#endif /* SC_PIXEL_MODE */
124
125#ifndef SC_NO_MODE_CHANGE
126static sc_rndr_sw_t grrndrsw = {
127	(vr_clear_t *)vga_nop,
128	vga_grborder,
129	(vr_draw_t *)vga_nop,
130	(vr_set_cursor_t *)vga_nop,
131	(vr_draw_cursor_t *)vga_nop,
132	(vr_blink_cursor_t *)vga_nop,
133	(vr_set_mouse_t *)vga_nop,
134	(vr_draw_mouse_t *)vga_nop,
135};
136RENDERER(cga, GRAPHICS_MODE, grrndrsw, vga_set);
137RENDERER(ega, GRAPHICS_MODE, grrndrsw, vga_set);
138RENDERER(vga, GRAPHICS_MODE, grrndrsw, vga_set);
139#endif /* SC_NO_MODE_CHANGE */
140
141RENDERER_MODULE(vga, vga_set);
142
143#ifndef SC_NO_CUTPASTE
144static u_short mouse_and_mask[16] = {
145	0xc000, 0xe000, 0xf000, 0xf800, 0xfc00, 0xfe00, 0xff00, 0xff80,
146	0xfe00, 0x1e00, 0x1f00, 0x0f00, 0x0f00, 0x0000, 0x0000, 0x0000
147};
148static u_short mouse_or_mask[16] = {
149	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7c00, 0x7e00, 0x6800,
150	0x0c00, 0x0c00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000
151};
152#endif
153
154static void
155vga_nop(scr_stat *scp, ...)
156{
157}
158
159/* text mode renderer */
160
161static void
162vga_txtclear(scr_stat *scp, int c, int attr)
163{
164	sc_vtb_clear(&scp->scr, c, attr);
165}
166
167static void
168vga_txtborder(scr_stat *scp, int color)
169{
170	(*vidsw[scp->sc->adapter]->set_border)(scp->sc->adp, color);
171}
172
173static void
174vga_txtdraw(scr_stat *scp, int from, int count, int flip)
175{
176	vm_offset_t p;
177	int c;
178	int a;
179
180	if (from + count > scp->xsize*scp->ysize)
181		count = scp->xsize*scp->ysize - from;
182
183	if (flip) {
184		for (p = sc_vtb_pointer(&scp->scr, from); count-- > 0; ++from) {
185			c = sc_vtb_getc(&scp->vtb, from);
186			a = sc_vtb_geta(&scp->vtb, from);
187			a = (a & 0x8800) | ((a & 0x7000) >> 4)
188				| ((a & 0x0700) << 4);
189			p = sc_vtb_putchar(&scp->scr, p, c, a);
190		}
191	} else {
192		sc_vtb_copy(&scp->vtb, from, &scp->scr, from, count);
193	}
194}
195
196static void
197vga_txtcursor_shape(scr_stat *scp, int base, int height, int blink)
198{
199	if (base < 0 || base >= scp->font_size)
200		return;
201	/* the caller may set height <= 0 in order to disable the cursor */
202#if 0
203	scp->cursor_base = base;
204	scp->cursor_height = height;
205#endif
206	(*vidsw[scp->sc->adapter]->set_hw_cursor_shape)(scp->sc->adp,
207							base, height,
208							scp->font_size, blink);
209}
210
211static void
212draw_txtcharcursor(scr_stat *scp, int at, u_short c, u_short a, int flip)
213{
214	sc_softc_t *sc;
215
216	sc = scp->sc;
217	scp->cursor_saveunder_char = c;
218	scp->cursor_saveunder_attr = a;
219
220#ifndef SC_NO_FONT_LOADING
221	if (sc->flags & SC_CHAR_CURSOR) {
222		unsigned char *font;
223		int h;
224		int i;
225
226		if (scp->font_size < 14) {
227			font = sc->font_8;
228			h = 8;
229		} else if (scp->font_size >= 16) {
230			font = sc->font_16;
231			h = 16;
232		} else {
233			font = sc->font_14;
234			h = 14;
235		}
236		if (scp->cursor_base >= h)
237			return;
238		if (flip)
239			a = (a & 0x8800)
240				| ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
241		bcopy(font + c*h, font + sc->cursor_char*h, h);
242		font = font + sc->cursor_char*h;
243		for (i = imax(h - scp->cursor_base - scp->cursor_height, 0);
244			i < h - scp->cursor_base; ++i) {
245			font[i] ^= 0xff;
246		}
247		sc->font_loading_in_progress = TRUE;
248		/* XXX */
249		(*vidsw[sc->adapter]->load_font)(sc->adp, 0, h, font,
250						 sc->cursor_char, 1);
251		sc->font_loading_in_progress = FALSE;
252		sc_vtb_putc(&scp->scr, at, sc->cursor_char, a);
253	} else
254#endif /* SC_NO_FONT_LOADING */
255	{
256		if ((a & 0x7000) == 0x7000) {
257			a &= 0x8f00;
258			if ((a & 0x0700) == 0)
259				a |= 0x0700;
260		} else {
261			a |= 0x7000;
262			if ((a & 0x0700) == 0x0700)
263				a &= 0xf000;
264		}
265		if (flip)
266			a = (a & 0x8800)
267				| ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
268		sc_vtb_putc(&scp->scr, at, c, a);
269	}
270}
271
272static void
273vga_txtcursor(scr_stat *scp, int at, int blink, int on, int flip)
274{
275	video_adapter_t *adp;
276	int cursor_attr;
277
278	if (scp->cursor_height <= 0)	/* the text cursor is disabled */
279		return;
280
281	adp = scp->sc->adp;
282	if (blink) {
283		scp->status |= VR_CURSOR_BLINK;
284		if (on) {
285			scp->status |= VR_CURSOR_ON;
286			(*vidsw[adp->va_index]->set_hw_cursor)(adp,
287							       at%scp->xsize,
288							       at/scp->xsize);
289		} else {
290			if (scp->status & VR_CURSOR_ON)
291				(*vidsw[adp->va_index]->set_hw_cursor)(adp,
292								       -1, -1);
293			scp->status &= ~VR_CURSOR_ON;
294		}
295	} else {
296		scp->status &= ~VR_CURSOR_BLINK;
297		if (on) {
298			scp->status |= VR_CURSOR_ON;
299			draw_txtcharcursor(scp, at,
300					   sc_vtb_getc(&scp->scr, at),
301					   sc_vtb_geta(&scp->scr, at),
302					   flip);
303		} else {
304			cursor_attr = scp->cursor_saveunder_attr;
305			if (flip)
306				cursor_attr = (cursor_attr & 0x8800)
307					| ((cursor_attr & 0x7000) >> 4)
308					| ((cursor_attr & 0x0700) << 4);
309			if (scp->status & VR_CURSOR_ON)
310				sc_vtb_putc(&scp->scr, at,
311					    scp->cursor_saveunder_char,
312					    cursor_attr);
313			scp->status &= ~VR_CURSOR_ON;
314		}
315	}
316}
317
318static void
319vga_txtblink(scr_stat *scp, int at, int flip)
320{
321}
322
323#ifndef SC_NO_CUTPASTE
324
325static void
326draw_txtmouse(scr_stat *scp, int x, int y)
327{
328#ifndef SC_ALT_MOUSE_IMAGE
329	u_char font_buf[128];
330	u_short cursor[32];
331	u_char c;
332	int pos;
333	int xoffset, yoffset;
334	int crtc_addr;
335	int i;
336
337	/* prepare mousepointer char's bitmaps */
338	pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
339	bcopy(scp->font + sc_vtb_getc(&scp->scr, pos)*scp->font_size,
340	      &font_buf[0], scp->font_size);
341	bcopy(scp->font + sc_vtb_getc(&scp->scr, pos + 1)*scp->font_size,
342	      &font_buf[32], scp->font_size);
343	bcopy(scp->font
344		 + sc_vtb_getc(&scp->scr, pos + scp->xsize)*scp->font_size,
345	      &font_buf[64], scp->font_size);
346	bcopy(scp->font
347		 + sc_vtb_getc(&scp->scr, pos + scp->xsize + 1)*scp->font_size,
348	      &font_buf[96], scp->font_size);
349	for (i = 0; i < scp->font_size; ++i) {
350		cursor[i] = font_buf[i]<<8 | font_buf[i+32];
351		cursor[i + scp->font_size] = font_buf[i+64]<<8 | font_buf[i+96];
352	}
353
354	/* now and-or in the mousepointer image */
355	xoffset = x%8;
356	yoffset = y%scp->font_size;
357	for (i = 0; i < 16; ++i) {
358		cursor[i + yoffset] =
359	    		(cursor[i + yoffset] & ~(mouse_and_mask[i] >> xoffset))
360	    		| (mouse_or_mask[i] >> xoffset);
361	}
362	for (i = 0; i < scp->font_size; ++i) {
363		font_buf[i] = (cursor[i] & 0xff00) >> 8;
364		font_buf[i + 32] = cursor[i] & 0xff;
365		font_buf[i + 64] = (cursor[i + scp->font_size] & 0xff00) >> 8;
366		font_buf[i + 96] = cursor[i + scp->font_size] & 0xff;
367	}
368
369#if 1
370	/* wait for vertical retrace to avoid jitter on some videocards */
371	crtc_addr = scp->sc->adp->va_crtc_addr;
372	while (!(inb(crtc_addr + 6) & 0x08)) /* idle */ ;
373#endif
374	c = scp->sc->mouse_char;
375	(*vidsw[scp->sc->adapter]->load_font)(scp->sc->adp, 0, 32, font_buf,
376					      c, 4);
377
378	sc_vtb_putc(&scp->scr, pos, c, sc_vtb_geta(&scp->scr, pos));
379	/* FIXME: may be out of range! */
380	sc_vtb_putc(&scp->scr, pos + scp->xsize, c + 2,
381		    sc_vtb_geta(&scp->scr, pos + scp->xsize));
382	if (x < (scp->xsize - 1)*8) {
383		sc_vtb_putc(&scp->scr, pos + 1, c + 1,
384			    sc_vtb_geta(&scp->scr, pos + 1));
385		sc_vtb_putc(&scp->scr, pos + scp->xsize + 1, c + 3,
386			    sc_vtb_geta(&scp->scr, pos + scp->xsize + 1));
387	}
388#else /* SC_ALT_MOUSE_IMAGE */
389	/* Red, magenta and brown are mapped to green to to keep it readable */
390	static const int col_conv[16] = {
391		6, 6, 6, 6, 2, 2, 2, 6, 14, 14, 14, 14, 10, 10, 10, 14
392	};
393	int pos;
394	int color;
395	int a;
396
397	pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
398	a = sc_vtb_geta(&scp->scr, pos);
399	if (scp->sc->adp->va_flags & V_ADP_COLOR)
400		color = (col_conv[(a & 0xf000) >> 12] << 12)
401			| ((a & 0x0f00) | 0x0800);
402	else
403		color = ((a & 0xf000) >> 4) | ((a & 0x0f00) << 4);
404	sc_vtb_putc(&scp->scr, pos, sc_vtb_getc(&scp->scr, pos), color);
405#endif /* SC_ALT_MOUSE_IMAGE */
406}
407
408static void
409remove_txtmouse(scr_stat *scp, int x, int y)
410{
411}
412
413static void
414vga_txtmouse(scr_stat *scp, int x, int y, int on)
415{
416	if (on)
417		draw_txtmouse(scp, x, y);
418	else
419		remove_txtmouse(scp, x, y);
420}
421
422#endif /* SC_NO_CUTPASTE */
423
424#ifdef SC_PIXEL_MODE
425
426/* pixel (raster text) mode renderer */
427
428static void
429vga_pxlclear(scr_stat *scp, int c, int attr)
430{
431	vm_offset_t p;
432	int line_width;
433	int lines;
434	int i;
435
436	/* XXX: we are just filling the screen with the background color... */
437	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
438	outw(GDCIDX, 0x0003);		/* data rotate/function select */
439	outw(GDCIDX, 0x0f01);		/* set/reset enable */
440	outw(GDCIDX, 0xff08);		/* bit mask */
441	outw(GDCIDX, ((attr & 0xf000) >> 4) | 0x00); /* set/reset */
442	line_width = scp->sc->adp->va_line_width;
443	lines = scp->ysize*scp->font_size;
444	p = scp->sc->adp->va_window + line_width*scp->yoff*scp->font_size
445		+ scp->xoff;
446	for (i = 0; i < lines; ++i) {
447		bzero_io((void *)p, scp->xsize);
448		p += line_width;
449	}
450	outw(GDCIDX, 0x0000);		/* set/reset */
451	outw(GDCIDX, 0x0001);		/* set/reset enable */
452}
453
454static void
455vga_pxlborder(scr_stat *scp, int color)
456{
457	vm_offset_t p;
458	int line_width;
459	int x;
460	int y;
461	int i;
462
463	(*vidsw[scp->sc->adapter]->set_border)(scp->sc->adp, color);
464
465	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
466	outw(GDCIDX, 0x0003);		/* data rotate/function select */
467	outw(GDCIDX, 0x0f01);		/* set/reset enable */
468	outw(GDCIDX, 0xff08);		/* bit mask */
469	outw(GDCIDX, (color << 8) | 0x00);	/* set/reset */
470	line_width = scp->sc->adp->va_line_width;
471	p = scp->sc->adp->va_window;
472	if (scp->yoff > 0)
473		bzero_io((void *)p, line_width*scp->yoff*scp->font_size);
474	y = (scp->yoff + scp->ysize)*scp->font_size;
475	if (scp->ypixel > y)
476		bzero_io((void *)(p + line_width*y), line_width*(scp->ypixel - y));
477	y = scp->yoff*scp->font_size;
478	x = scp->xpixel/8 - scp->xoff - scp->xsize;
479	for (i = 0; i < scp->ysize*scp->font_size; ++i) {
480		if (scp->xoff > 0)
481			bzero_io((void *)(p + line_width*(y + i)), scp->xoff);
482		if (x > 0)
483			bzero_io((void *)(p + line_width*(y + i)
484				     + scp->xoff + scp->xsize), x);
485	}
486	outw(GDCIDX, 0x0000);		/* set/reset */
487	outw(GDCIDX, 0x0001);		/* set/reset enable */
488}
489
490static void
491vga_egadraw(scr_stat *scp, int from, int count, int flip)
492{
493	vm_offset_t d;
494	vm_offset_t e;
495	u_char *f;
496	u_short bg;
497	u_short col1, col2;
498	int line_width;
499	int i, j;
500	int a;
501	u_char c;
502
503	line_width = scp->sc->adp->va_line_width;
504	d = scp->sc->adp->va_window
505		+ scp->xoff
506		+ scp->yoff*scp->font_size*line_width
507		+ (from%scp->xsize)
508		+ scp->font_size*line_width*(from/scp->xsize);
509
510	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
511	outw(GDCIDX, 0x0003);		/* data rotate/function select */
512	outw(GDCIDX, 0x0f01);		/* set/reset enable */
513	bg = -1;
514	if (from + count > scp->xsize*scp->ysize)
515		count = scp->xsize*scp->ysize - from;
516	for (i = from; count-- > 0; ++i) {
517		a = sc_vtb_geta(&scp->vtb, i);
518		if (flip) {
519			col1 = ((a & 0x7000) >> 4) | (a & 0x0800);
520			col2 = ((a & 0x8000) >> 4) | (a & 0x0700);
521		} else {
522			col1 = (a & 0x0f00);
523			col2 = (a & 0xf000) >> 4;
524		}
525		/* set background color in EGA/VGA latch */
526		if (bg != col2) {
527			bg = col2;
528			outw(GDCIDX, bg | 0x00);	/* set/reset */
529			outw(GDCIDX, 0xff08);		/* bit mask */
530			writeb(d, 0);
531			c = readb(d);	/* set bg color in the latch */
532		}
533		/* foreground color */
534		outw(GDCIDX, col1 | 0x00);		/* set/reset */
535		e = d;
536		f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
537		for (j = 0; j < scp->font_size; ++j, ++f) {
538			outw(GDCIDX, (*f << 8) | 0x08);	/* bit mask */
539	        	writeb(e, 0);
540			e += line_width;
541		}
542		++d;
543		if ((i % scp->xsize) == scp->xsize - 1)
544			d += scp->xoff*2
545				 + (scp->font_size - 1)*line_width;
546	}
547	outw(GDCIDX, 0x0000);		/* set/reset */
548	outw(GDCIDX, 0x0001);		/* set/reset enable */
549	outw(GDCIDX, 0xff08);		/* bit mask */
550}
551
552static void
553vga_vgadraw(scr_stat *scp, int from, int count, int flip)
554{
555	vm_offset_t d;
556	vm_offset_t e;
557	u_char *f;
558	u_short bg;
559	u_short col1, col2;
560	int line_width;
561	int i, j;
562	int a;
563	u_char c;
564
565	line_width = scp->sc->adp->va_line_width;
566	d = scp->sc->adp->va_window
567		+ scp->xoff
568		+ scp->yoff*scp->font_size*line_width
569		+ (from%scp->xsize)
570		+ scp->font_size*line_width*(from/scp->xsize);
571
572	outw(GDCIDX, 0x0305);		/* read mode 0, write mode 3 */
573	outw(GDCIDX, 0x0003);		/* data rotate/function select */
574	outw(GDCIDX, 0x0f01);		/* set/reset enable */
575	outw(GDCIDX, 0xff08);		/* bit mask */
576	bg = -1;
577	if (from + count > scp->xsize*scp->ysize)
578		count = scp->xsize*scp->ysize - from;
579	for (i = from; count-- > 0; ++i) {
580		a = sc_vtb_geta(&scp->vtb, i);
581		if (flip) {
582			col1 = ((a & 0x7000) >> 4) | (a & 0x0800);
583			col2 = ((a & 0x8000) >> 4) | (a & 0x0700);
584		} else {
585			col1 = (a & 0x0f00);
586			col2 = (a & 0xf000) >> 4;
587		}
588		/* set background color in EGA/VGA latch */
589		if (bg != col2) {
590			bg = col2;
591			outw(GDCIDX, 0x0005);	/* read mode 0, write mode 0 */
592			outw(GDCIDX, bg | 0x00); /* set/reset */
593			writeb(d, 0);
594			c = readb(d);		/* set bg color in the latch */
595			outw(GDCIDX, 0x0305);	/* read mode 0, write mode 3 */
596		}
597		/* foreground color */
598		outw(GDCIDX, col1 | 0x00);	/* set/reset */
599		e = d;
600		f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_size]);
601		for (j = 0; j < scp->font_size; ++j, ++f) {
602	        	writeb(e, *f);
603			e += line_width;
604		}
605		++d;
606		if ((i % scp->xsize) == scp->xsize - 1)
607			d += scp->xoff*2
608				 + (scp->font_size - 1)*line_width;
609	}
610	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
611	outw(GDCIDX, 0x0000);		/* set/reset */
612	outw(GDCIDX, 0x0001);		/* set/reset enable */
613}
614
615static void
616vga_pxlcursor_shape(scr_stat *scp, int base, int height, int blink)
617{
618	if (base < 0 || base >= scp->font_size)
619		return;
620	/* the caller may set height <= 0 in order to disable the cursor */
621#if 0
622	scp->cursor_base = base;
623	scp->cursor_height = height;
624#endif
625}
626
627static void
628draw_pxlcursor(scr_stat *scp, int at, int on, int flip)
629{
630	vm_offset_t d;
631	u_char *f;
632	int line_width;
633	int height;
634	int col;
635	int a;
636	int i;
637	u_char c;
638
639	line_width = scp->sc->adp->va_line_width;
640	d = scp->sc->adp->va_window
641		+ scp->xoff
642		+ scp->yoff*scp->font_size*line_width
643		+ (at%scp->xsize)
644		+ scp->font_size*line_width*(at/scp->xsize)
645		+ (scp->font_size - scp->cursor_base - 1)*line_width;
646
647	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
648	outw(GDCIDX, 0x0003);		/* data rotate/function select */
649	outw(GDCIDX, 0x0f01);		/* set/reset enable */
650	/* set background color in EGA/VGA latch */
651	a = sc_vtb_geta(&scp->vtb, at);
652	if (flip)
653		col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
654	else
655		col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
656	outw(GDCIDX, col | 0x00);	/* set/reset */
657	outw(GDCIDX, 0xff08);		/* bit mask */
658	writeb(d, 0);
659	c = readb(d);			/* set bg color in the latch */
660	/* foreground color */
661	if (flip)
662		col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
663	else
664		col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
665	outw(GDCIDX, col | 0x00);	/* set/reset */
666	f = &(scp->font[sc_vtb_getc(&scp->vtb, at)*scp->font_size
667		+ scp->font_size - scp->cursor_base - 1]);
668	height = imin(scp->cursor_height, scp->font_size);
669	for (i = 0; i < height; ++i, --f) {
670		outw(GDCIDX, (*f << 8) | 0x08);	/* bit mask */
671	       	writeb(d, 0);
672		d -= line_width;
673	}
674	outw(GDCIDX, 0x0000);		/* set/reset */
675	outw(GDCIDX, 0x0001);		/* set/reset enable */
676	outw(GDCIDX, 0xff08);		/* bit mask */
677}
678
679static int pxlblinkrate = 0;
680
681static void
682vga_pxlcursor(scr_stat *scp, int at, int blink, int on, int flip)
683{
684	if (scp->cursor_height <= 0)	/* the text cursor is disabled */
685		return;
686
687	if (on) {
688		if (!blink) {
689			scp->status |= VR_CURSOR_ON;
690			draw_pxlcursor(scp, at, on, flip);
691		} else if (++pxlblinkrate & 4) {
692			pxlblinkrate = 0;
693			scp->status ^= VR_CURSOR_ON;
694			draw_pxlcursor(scp, at,
695				       scp->status & VR_CURSOR_ON,
696				       flip);
697		}
698	} else {
699		if (scp->status & VR_CURSOR_ON)
700			draw_pxlcursor(scp, at, on, flip);
701		scp->status &= ~VR_CURSOR_ON;
702	}
703	if (blink)
704		scp->status |= VR_CURSOR_BLINK;
705	else
706		scp->status &= ~VR_CURSOR_BLINK;
707}
708
709static void
710vga_pxlblink(scr_stat *scp, int at, int flip)
711{
712	if (!(scp->status & VR_CURSOR_BLINK))
713		return;
714	if (!(++pxlblinkrate & 4))
715		return;
716	pxlblinkrate = 0;
717	scp->status ^= VR_CURSOR_ON;
718	draw_pxlcursor(scp, at, scp->status & VR_CURSOR_ON, flip);
719}
720
721#ifndef SC_NO_CUTPASTE
722
723static void
724draw_pxlmouse(scr_stat *scp, int x, int y)
725{
726	vm_offset_t p;
727	int line_width;
728	int xoff, yoff;
729	int ymax;
730	u_short m;
731	int i, j;
732
733	line_width = scp->sc->adp->va_line_width;
734	xoff = (x - scp->xoff*8)%8;
735	yoff = y - (y/line_width)*line_width;
736	ymax = imin(y + 16, scp->ypixel);
737
738	outw(GDCIDX, 0x0805);		/* read mode 1, write mode 0 */
739	outw(GDCIDX, 0x0001);		/* set/reset enable */
740	outw(GDCIDX, 0x0002);		/* color compare */
741	outw(GDCIDX, 0x0007);		/* color don't care */
742	outw(GDCIDX, 0xff08);		/* bit mask */
743	outw(GDCIDX, 0x0803);		/* data rotate/function select (and) */
744	p = scp->sc->adp->va_window + line_width*y + x/8;
745	if (x < scp->xpixel - 8) {
746		for (i = y, j = 0; i < ymax; ++i, ++j) {
747			m = ~(mouse_and_mask[j] >> xoff);
748#ifdef __i386__
749			*(u_char *)p &= m >> 8;
750			*(u_char *)(p + 1) &= m;
751#elif defined(__alpha__)
752			writeb(p, readb(p) & (m >> 8));
753			writeb(p + 1, readb(p + 1) & (m >> 8));
754#endif
755			p += line_width;
756		}
757	} else {
758		xoff += 8;
759		for (i = y, j = 0; i < ymax; ++i, ++j) {
760			m = ~(mouse_and_mask[j] >> xoff);
761#ifdef __i386__
762			*(u_char *)p &= m;
763#elif defined(__alpha__)
764			writeb(p, readb(p) & (m >> 8));
765#endif
766			p += line_width;
767		}
768	}
769	outw(GDCIDX, 0x1003);		/* data rotate/function select (or) */
770	p = scp->sc->adp->va_window + line_width*y + x/8;
771	if (x < scp->xpixel - 8) {
772		for (i = y, j = 0; i < ymax; ++i, ++j) {
773			m = mouse_or_mask[j] >> xoff;
774#ifdef __i386__
775			*(u_char *)p &= m >> 8;
776			*(u_char *)(p + 1) &= m;
777#elif defined(__alpha__)
778			writeb(p, readb(p) & (m >> 8));
779			writeb(p + 1, readb(p + 1) & (m >> 8));
780#endif
781			p += line_width;
782		}
783	} else {
784		for (i = y, j = 0; i < ymax; ++i, ++j) {
785			m = mouse_or_mask[j] >> xoff;
786#ifdef __i386__
787			*(u_char *)p &= m;
788#elif defined(__alpha__)
789			writeb(p, readb(p) & (m >> 8));
790#endif
791			p += line_width;
792		}
793	}
794	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
795	outw(GDCIDX, 0x0003);		/* data rotate/function select */
796}
797
798static void
799remove_pxlmouse(scr_stat *scp, int x, int y)
800{
801	vm_offset_t p;
802	int col, row;
803	int pos;
804	int line_width;
805	int ymax;
806	int i;
807
808	/* erase the mouse cursor image */
809	col = x/8 - scp->xoff;
810	row = y/scp->font_size - scp->yoff;
811	pos = row*scp->xsize + col;
812	i = (col < scp->xsize - 1) ? 2 : 1;
813	(*scp->rndr->draw)(scp, pos, i, FALSE);
814	if (row < scp->ysize - 1)
815		(*scp->rndr->draw)(scp, pos + scp->xsize, i, FALSE);
816
817	/* paint border if necessary */
818	line_width = scp->sc->adp->va_line_width;
819	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
820	outw(GDCIDX, 0x0003);		/* data rotate/function select */
821	outw(GDCIDX, 0x0f01);		/* set/reset enable */
822	outw(GDCIDX, 0xff08);		/* bit mask */
823	outw(GDCIDX, (scp->border << 8) | 0x00);	/* set/reset */
824	if (row == scp->ysize - 1) {
825		i = (scp->ysize + scp->yoff)*scp->font_size;
826		ymax = imin(i + scp->font_size, scp->ypixel);
827		p = scp->sc->adp->va_window + i*line_width + scp->xoff + col;
828		if (col < scp->xsize - 1) {
829			for (; i < ymax; ++i) {
830				writeb(p, 0);
831				writeb(p + 1, 0);
832				p += line_width;
833			}
834		} else {
835			for (; i < ymax; ++i) {
836				writeb(p, 0);
837				p += line_width;
838			}
839		}
840	}
841	if ((col == scp->xsize - 1) && (scp->xoff > 0)) {
842		i = (row + scp->yoff)*scp->font_size;
843		ymax = imin(i + scp->font_size*2, scp->ypixel);
844		p = scp->sc->adp->va_window + i*line_width
845			+ scp->xoff + scp->xsize;
846		for (; i < ymax; ++i) {
847			writeb(p, 0);
848			p += line_width;
849		}
850	}
851	outw(GDCIDX, 0x0000);		/* set/reset */
852	outw(GDCIDX, 0x0001);		/* set/reset enable */
853}
854
855static void
856vga_pxlmouse(scr_stat *scp, int x, int y, int on)
857{
858	if (on)
859		draw_pxlmouse(scp, x, y);
860	else
861		remove_pxlmouse(scp, x, y);
862}
863
864#endif /* SC_NO_CUTPASTE */
865#endif /* SC_PIXEL_MODE */
866
867#ifndef SC_NO_MODE_CHANGE
868
869/* graphics mode renderer */
870
871static void
872vga_grborder(scr_stat *scp, int color)
873{
874	(*vidsw[scp->sc->adapter]->set_border)(scp->sc->adp, color);
875}
876
877#endif
878