scgfbrndr.c revision 85809
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 81030 2001-08-02 08:30:40Z yokota $
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#include <sys/fbio.h>
36#include <sys/consio.h>
37
38#include <machine/bus.h>
39
40#include <dev/fb/fbreg.h>
41#include <dev/fb/vgareg.h>
42#include <dev/syscons/syscons.h>
43
44#include <isa/isareg.h>
45
46#ifndef SC_RENDER_DEBUG
47#define SC_RENDER_DEBUG		0
48#endif
49
50static vr_clear_t		vga_txtclear;
51static vr_draw_border_t		vga_txtborder;
52static vr_draw_t		vga_txtdraw;
53static vr_set_cursor_t		vga_txtcursor_shape;
54static vr_draw_cursor_t		vga_txtcursor;
55static vr_blink_cursor_t	vga_txtblink;
56#ifndef SC_NO_CUTPASTE
57static vr_draw_mouse_t		vga_txtmouse;
58#else
59#define vga_txtmouse		(vr_draw_mouse_t *)vga_nop
60#endif
61
62#ifdef SC_PIXEL_MODE
63static vr_clear_t		vga_pxlclear;
64static vr_draw_border_t		vga_pxlborder;
65static vr_draw_t		vga_egadraw;
66static vr_draw_t		vga_vgadraw;
67static vr_set_cursor_t		vga_pxlcursor_shape;
68static vr_draw_cursor_t		vga_pxlcursor;
69static vr_blink_cursor_t	vga_pxlblink;
70#ifndef SC_NO_CUTPASTE
71static vr_draw_mouse_t		vga_pxlmouse;
72#else
73#define vga_pxlmouse		(vr_draw_mouse_t *)vga_nop
74#endif
75#endif /* SC_PIXEL_MODE */
76
77#ifndef SC_NO_MODE_CHANGE
78static vr_draw_border_t		vga_grborder;
79#endif
80
81static void			vga_nop(scr_stat *scp, ...);
82
83static sc_rndr_sw_t txtrndrsw = {
84	vga_txtclear,
85	vga_txtborder,
86	vga_txtdraw,
87	vga_txtcursor_shape,
88	vga_txtcursor,
89	vga_txtblink,
90	(vr_set_mouse_t *)vga_nop,
91	vga_txtmouse,
92};
93RENDERER(mda, 0, txtrndrsw, vga_set);
94RENDERER(cga, 0, txtrndrsw, vga_set);
95RENDERER(ega, 0, txtrndrsw, vga_set);
96RENDERER(vga, 0, txtrndrsw, vga_set);
97
98#ifdef SC_PIXEL_MODE
99static sc_rndr_sw_t egarndrsw = {
100	vga_pxlclear,
101	vga_pxlborder,
102	vga_egadraw,
103	vga_pxlcursor_shape,
104	vga_pxlcursor,
105	vga_pxlblink,
106	(vr_set_mouse_t *)vga_nop,
107	vga_pxlmouse,
108};
109RENDERER(ega, PIXEL_MODE, egarndrsw, vga_set);
110
111static sc_rndr_sw_t vgarndrsw = {
112	vga_pxlclear,
113	vga_pxlborder,
114	vga_vgadraw,
115	vga_pxlcursor_shape,
116	vga_pxlcursor,
117	vga_pxlblink,
118	(vr_set_mouse_t *)vga_nop,
119	vga_pxlmouse,
120};
121RENDERER(vga, PIXEL_MODE, vgarndrsw, vga_set);
122#endif /* SC_PIXEL_MODE */
123
124#ifndef SC_NO_MODE_CHANGE
125static sc_rndr_sw_t grrndrsw = {
126	(vr_clear_t *)vga_nop,
127	vga_grborder,
128	(vr_draw_t *)vga_nop,
129	(vr_set_cursor_t *)vga_nop,
130	(vr_draw_cursor_t *)vga_nop,
131	(vr_blink_cursor_t *)vga_nop,
132	(vr_set_mouse_t *)vga_nop,
133	(vr_draw_mouse_t *)vga_nop,
134};
135RENDERER(cga, GRAPHICS_MODE, grrndrsw, vga_set);
136RENDERER(ega, GRAPHICS_MODE, grrndrsw, vga_set);
137RENDERER(vga, GRAPHICS_MODE, grrndrsw, vga_set);
138#endif /* SC_NO_MODE_CHANGE */
139
140RENDERER_MODULE(vga, vga_set);
141
142#ifndef SC_NO_CUTPASTE
143static u_short mouse_and_mask[16] = {
144	0xc000, 0xe000, 0xf000, 0xf800, 0xfc00, 0xfe00, 0xff00, 0xff80,
145	0xfe00, 0x1e00, 0x1f00, 0x0f00, 0x0f00, 0x0000, 0x0000, 0x0000
146};
147static u_short mouse_or_mask[16] = {
148	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7c00, 0x7e00, 0x6800,
149	0x0c00, 0x0c00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000
150};
151#endif
152
153static void
154vga_nop(scr_stat *scp, ...)
155{
156}
157
158/* text mode renderer */
159
160static void
161vga_txtclear(scr_stat *scp, int c, int attr)
162{
163	sc_vtb_clear(&scp->scr, c, attr);
164}
165
166static void
167vga_txtborder(scr_stat *scp, int color)
168{
169	(*vidsw[scp->sc->adapter]->set_border)(scp->sc->adp, color);
170}
171
172static void
173vga_txtdraw(scr_stat *scp, int from, int count, int flip)
174{
175	vm_offset_t p;
176	int c;
177	int a;
178
179	if (from + count > scp->xsize*scp->ysize)
180		count = scp->xsize*scp->ysize - from;
181
182	if (flip) {
183		for (p = sc_vtb_pointer(&scp->scr, from); count-- > 0; ++from) {
184			c = sc_vtb_getc(&scp->vtb, from);
185			a = sc_vtb_geta(&scp->vtb, from);
186			a = (a & 0x8800) | ((a & 0x7000) >> 4)
187				| ((a & 0x0700) << 4);
188			p = sc_vtb_putchar(&scp->scr, p, c, a);
189		}
190	} else {
191		sc_vtb_copy(&scp->vtb, from, &scp->scr, from, count);
192	}
193}
194
195static void
196vga_txtcursor_shape(scr_stat *scp, int base, int height, int blink)
197{
198	if (base < 0 || base >= scp->font_size)
199		return;
200	/* the caller may set height <= 0 in order to disable the cursor */
201#if 0
202	scp->curs_attr.base = base;
203	scp->curs_attr.height = height;
204#endif
205	(*vidsw[scp->sc->adapter]->set_hw_cursor_shape)(scp->sc->adp,
206							base, height,
207							scp->font_size, blink);
208}
209
210static void
211draw_txtcharcursor(scr_stat *scp, int at, u_short c, u_short a, int flip)
212{
213	sc_softc_t *sc;
214
215	sc = scp->sc;
216	scp->cursor_saveunder_char = c;
217	scp->cursor_saveunder_attr = a;
218
219#ifndef SC_NO_FONT_LOADING
220	if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
221		unsigned char *font;
222		int h;
223		int i;
224
225		if (scp->font_size < 14) {
226			font = sc->font_8;
227			h = 8;
228		} else if (scp->font_size >= 16) {
229			font = sc->font_16;
230			h = 16;
231		} else {
232			font = sc->font_14;
233			h = 14;
234		}
235		if (scp->curs_attr.base >= h)
236			return;
237		if (flip)
238			a = (a & 0x8800)
239				| ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
240		bcopy(font + c*h, font + sc->cursor_char*h, h);
241		font = font + sc->cursor_char*h;
242		for (i = imax(h - scp->curs_attr.base - scp->curs_attr.height, 0);
243			i < h - scp->curs_attr.base; ++i) {
244			font[i] ^= 0xff;
245		}
246		/* XXX */
247		(*vidsw[sc->adapter]->load_font)(sc->adp, 0, h, font,
248						 sc->cursor_char, 1);
249		sc_vtb_putc(&scp->scr, at, sc->cursor_char, a);
250	} else
251#endif /* SC_NO_FONT_LOADING */
252	{
253		if ((a & 0x7000) == 0x7000) {
254			a &= 0x8f00;
255			if ((a & 0x0700) == 0)
256				a |= 0x0700;
257		} else {
258			a |= 0x7000;
259			if ((a & 0x0700) == 0x0700)
260				a &= 0xf000;
261		}
262		if (flip)
263			a = (a & 0x8800)
264				| ((a & 0x7000) >> 4) | ((a & 0x0700) << 4);
265		sc_vtb_putc(&scp->scr, at, c, a);
266	}
267}
268
269static void
270vga_txtcursor(scr_stat *scp, int at, int blink, int on, int flip)
271{
272	video_adapter_t *adp;
273	int cursor_attr;
274
275	if (scp->curs_attr.height <= 0)	/* the text cursor is disabled */
276		return;
277
278	adp = scp->sc->adp;
279	if (blink) {
280		scp->status |= VR_CURSOR_BLINK;
281		if (on) {
282			scp->status |= VR_CURSOR_ON;
283			(*vidsw[adp->va_index]->set_hw_cursor)(adp,
284							       at%scp->xsize,
285							       at/scp->xsize);
286		} else {
287			if (scp->status & VR_CURSOR_ON)
288				(*vidsw[adp->va_index]->set_hw_cursor)(adp,
289								       -1, -1);
290			scp->status &= ~VR_CURSOR_ON;
291		}
292	} else {
293		scp->status &= ~VR_CURSOR_BLINK;
294		if (on) {
295			scp->status |= VR_CURSOR_ON;
296			draw_txtcharcursor(scp, at,
297					   sc_vtb_getc(&scp->scr, at),
298					   sc_vtb_geta(&scp->scr, at),
299					   flip);
300		} else {
301			cursor_attr = scp->cursor_saveunder_attr;
302			if (flip)
303				cursor_attr = (cursor_attr & 0x8800)
304					| ((cursor_attr & 0x7000) >> 4)
305					| ((cursor_attr & 0x0700) << 4);
306			if (scp->status & VR_CURSOR_ON)
307				sc_vtb_putc(&scp->scr, at,
308					    scp->cursor_saveunder_char,
309					    cursor_attr);
310			scp->status &= ~VR_CURSOR_ON;
311		}
312	}
313}
314
315static void
316vga_txtblink(scr_stat *scp, int at, int flip)
317{
318}
319
320#ifndef SC_NO_CUTPASTE
321
322static void
323draw_txtmouse(scr_stat *scp, int x, int y)
324{
325#ifndef SC_ALT_MOUSE_IMAGE
326    if (ISMOUSEAVAIL(scp->sc->adp->va_flags)) {
327	u_char font_buf[128];
328	u_short cursor[32];
329	u_char c;
330	int pos;
331	int xoffset, yoffset;
332	int crtc_addr;
333	int i;
334
335	/* prepare mousepointer char's bitmaps */
336	pos = (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
337	bcopy(scp->font + sc_vtb_getc(&scp->scr, pos)*scp->font_size,
338	      &font_buf[0], scp->font_size);
339	bcopy(scp->font + sc_vtb_getc(&scp->scr, pos + 1)*scp->font_size,
340	      &font_buf[32], scp->font_size);
341	bcopy(scp->font
342		 + sc_vtb_getc(&scp->scr, pos + scp->xsize)*scp->font_size,
343	      &font_buf[64], scp->font_size);
344	bcopy(scp->font
345		 + sc_vtb_getc(&scp->scr, pos + scp->xsize + 1)*scp->font_size,
346	      &font_buf[96], scp->font_size);
347	for (i = 0; i < scp->font_size; ++i) {
348		cursor[i] = font_buf[i]<<8 | font_buf[i+32];
349		cursor[i + scp->font_size] = font_buf[i+64]<<8 | font_buf[i+96];
350	}
351
352	/* now and-or in the mousepointer image */
353	xoffset = x%8;
354	yoffset = y%scp->font_size;
355	for (i = 0; i < 16; ++i) {
356		cursor[i + yoffset] =
357	    		(cursor[i + yoffset] & ~(mouse_and_mask[i] >> xoffset))
358	    		| (mouse_or_mask[i] >> xoffset);
359	}
360	for (i = 0; i < scp->font_size; ++i) {
361		font_buf[i] = (cursor[i] & 0xff00) >> 8;
362		font_buf[i + 32] = cursor[i] & 0xff;
363		font_buf[i + 64] = (cursor[i + scp->font_size] & 0xff00) >> 8;
364		font_buf[i + 96] = cursor[i + scp->font_size] & 0xff;
365	}
366
367#if 1
368	/* wait for vertical retrace to avoid jitter on some videocards */
369	crtc_addr = scp->sc->adp->va_crtc_addr;
370	while (!(inb(crtc_addr + 6) & 0x08)) /* idle */ ;
371#endif
372	c = scp->sc->mouse_char;
373	(*vidsw[scp->sc->adapter]->load_font)(scp->sc->adp, 0, 32, font_buf,
374					      c, 4);
375
376	sc_vtb_putc(&scp->scr, pos, c, sc_vtb_geta(&scp->scr, pos));
377	/* FIXME: may be out of range! */
378	sc_vtb_putc(&scp->scr, pos + scp->xsize, c + 2,
379		    sc_vtb_geta(&scp->scr, pos + scp->xsize));
380	if (x < (scp->xsize - 1)*8) {
381		sc_vtb_putc(&scp->scr, pos + 1, c + 1,
382			    sc_vtb_geta(&scp->scr, pos + 1));
383		sc_vtb_putc(&scp->scr, pos + scp->xsize + 1, c + 3,
384			    sc_vtb_geta(&scp->scr, pos + scp->xsize + 1));
385	}
386    } else
387#endif /* SC_ALT_MOUSE_IMAGE */
388    {
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    }
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->curs_attr.base = base;
623	scp->curs_attr.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->curs_attr.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->curs_attr.base - 1]);
668	height = imin(scp->curs_attr.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->curs_attr.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