xref: /netbsd-src/sys/dev/ic/pcdisplay_subr.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /* $NetBSD: pcdisplay_subr.c,v 1.32 2006/04/15 17:48:23 jmmv Exp $ */
2 
3 /*
4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5  * All rights reserved.
6  *
7  * Author: Chris G. Demetriou
8  *
9  * Permission to use, copy, modify and distribute this software and
10  * its documentation is hereby granted, provided that both the copyright
11  * notice and this permission notice appear in all copies of the
12  * software, derivative works or modified versions, and any portions
13  * thereof, and that both notices appear in supporting documentation.
14  *
15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18  *
19  * Carnegie Mellon requests users of this software to return to
20  *
21  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22  *  School of Computer Science
23  *  Carnegie Mellon University
24  *  Pittsburgh PA 15213-3890
25  *
26  * any improvements or extensions that they make and grant Carnegie the
27  * rights to redistribute these changes.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: pcdisplay_subr.c,v 1.32 2006/04/15 17:48:23 jmmv Exp $");
32 
33 #include "opt_wsmsgattrs.h" /* for WSDISPLAY_CUSTOM_OUTPUT */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <machine/bus.h>
39 
40 #include <dev/ic/mc6845reg.h>
41 #include <dev/ic/pcdisplayvar.h>
42 #include <dev/wscons/wsconsio.h>
43 
44 #include <dev/wscons/wsdisplayvar.h>
45 
46 void
47 pcdisplay_cursor_init(struct pcdisplayscreen *scr, int existing)
48 {
49 #ifdef PCDISPLAY_SOFTCURSOR
50 	bus_space_tag_t memt;
51 	bus_space_handle_t memh;
52 	int off;
53 
54 	pcdisplay_6845_write(scr->hdl, curstart, 0x10);
55 	pcdisplay_6845_write(scr->hdl, curend, 0x10);
56 
57 	if (existing) {
58 		/*
59 		 * This is the first screen. At this point, scr->mem is NULL
60 		 * (no backing store), so we can't use pcdisplay_cursor() to
61 		 * do this.
62 		 */
63 		memt = scr->hdl->ph_memt;
64 		memh = scr->hdl->ph_memh;
65 		off = (scr->cursorrow * scr->type->ncols + scr->cursorcol) * 2
66 		    + scr->dispoffset;
67 
68 		scr->cursortmp = bus_space_read_2(memt, memh, off);
69 		bus_space_write_2(memt, memh, off, scr->cursortmp ^ 0x7700);
70 	} else
71 		scr->cursortmp = 0;
72 #else
73 	/*
74 	 * Firmware might not have initialized the cursor shape.  Make
75 	 * sure there's something we can see.
76 	 * Don't touch the hardware if this is not the first screen.
77 	 */
78 	if (existing) {
79 		pcdisplay_6845_write(scr->hdl, curstart,
80 				     scr->type->fontheight - 2);
81 		pcdisplay_6845_write(scr->hdl, curend,
82 				     scr->type->fontheight - 1);
83 	}
84 #endif
85 	scr->cursoron = 1;
86 }
87 
88 void
89 pcdisplay_cursor(void *id, int on, int row, int col)
90 {
91 #ifdef PCDISPLAY_SOFTCURSOR
92 	struct pcdisplayscreen *scr = id;
93 	bus_space_tag_t memt = scr->hdl->ph_memt;
94 	bus_space_handle_t memh = scr->hdl->ph_memh;
95 	int off;
96 
97 	/* Remove old cursor image */
98 	if (scr->cursoron) {
99 		off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
100 		if (scr->active)
101 			bus_space_write_2(memt, memh, scr->dispoffset + off * 2,
102 			    scr->cursortmp);
103 		else
104 			scr->mem[off] = scr->cursortmp;
105 	}
106 
107 	scr->cursorrow = row;
108 	scr->cursorcol = col;
109 
110 	if ((scr->cursoron = on) == 0)
111 		return;
112 
113 	off = (scr->cursorrow * scr->type->ncols + scr->cursorcol);
114 	if (scr->active) {
115 		off = off * 2 + scr->dispoffset;
116 		scr->cursortmp = bus_space_read_2(memt, memh, off);
117 		bus_space_write_2(memt, memh, off, scr->cursortmp ^ 0x7700);
118 	} else {
119 		scr->cursortmp = scr->mem[off];
120 		scr->mem[off] = scr->cursortmp ^ 0x7700;
121 	}
122 #else 	/* PCDISPLAY_SOFTCURSOR */
123 	struct pcdisplayscreen *scr = id;
124 	int pos;
125 
126 	scr->cursorrow = row;
127 	scr->cursorcol = col;
128 	scr->cursoron = on;
129 
130 	if (scr->active) {
131 		if (!on)
132 			pos = 0x3fff;
133 		else
134 			pos = scr->dispoffset / 2
135 				+ row * scr->type->ncols + col;
136 
137 		pcdisplay_6845_write(scr->hdl, cursorh, pos >> 8);
138 		pcdisplay_6845_write(scr->hdl, cursorl, pos);
139 	}
140 #endif	/* PCDISPLAY_SOFTCURSOR */
141 }
142 
143 #if 0
144 unsigned int
145 pcdisplay_mapchar_simple(void *id, int uni)
146 {
147 	if (uni < 128)
148 		return (uni);
149 
150 	return (1); /* XXX ??? smiley */
151 }
152 #endif
153 
154 void
155 pcdisplay_putchar(void *id, int row, int col, unsigned int c, long attr)
156 {
157 	struct pcdisplayscreen *scr = id;
158 	bus_space_tag_t memt = scr->hdl->ph_memt;
159 	bus_space_handle_t memh = scr->hdl->ph_memh;
160 	int off;
161 
162 	off = row * scr->type->ncols + col;
163 
164 	if (scr->active)
165 		bus_space_write_2(memt, memh, scr->dispoffset + off * 2,
166 				  c | (attr << 8));
167 	else
168 		scr->mem[off] = c | (attr << 8);
169 
170 	scr->visibleoffset = scr->dispoffset;
171 }
172 
173 void
174 pcdisplay_copycols(void *id, int row, int srccol, int dstcol, int ncols)
175 {
176 	struct pcdisplayscreen *scr = id;
177 	bus_space_tag_t memt = scr->hdl->ph_memt;
178 	bus_space_handle_t memh = scr->hdl->ph_memh;
179 	bus_size_t srcoff, dstoff;
180 
181 	srcoff = dstoff = row * scr->type->ncols;
182 	srcoff += srccol;
183 	dstoff += dstcol;
184 
185 	if (scr->active)
186 		bus_space_copy_region_2(memt, memh,
187 					scr->dispoffset + srcoff * 2,
188 					memh, scr->dispoffset + dstoff * 2,
189 					ncols);
190 	else
191 		memcpy(&scr->mem[dstoff], &scr->mem[srcoff], ncols * 2);
192 }
193 
194 void
195 pcdisplay_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
196 {
197 	struct pcdisplayscreen *scr = id;
198 	bus_space_tag_t memt = scr->hdl->ph_memt;
199 	bus_space_handle_t memh = scr->hdl->ph_memh;
200 	bus_size_t off;
201 	u_int16_t val;
202 	int i;
203 
204 	off = row * scr->type->ncols + startcol;
205 
206 	val = (fillattr << 8) | ' ';
207 
208 	if (scr->active)
209 		bus_space_set_region_2(memt, memh, scr->dispoffset + off * 2,
210 				       val, ncols);
211 	else
212 		for (i = 0; i < ncols; i++)
213 			scr->mem[off + i] = val;
214 }
215 
216 void
217 pcdisplay_copyrows(void *id, int srcrow, int dstrow, int nrows)
218 {
219 	struct pcdisplayscreen *scr = id;
220 	bus_space_tag_t memt = scr->hdl->ph_memt;
221 	bus_space_handle_t memh = scr->hdl->ph_memh;
222 	int ncols = scr->type->ncols;
223 	bus_size_t srcoff, dstoff;
224 
225 	srcoff = srcrow * ncols + 0;
226 	dstoff = dstrow * ncols + 0;
227 
228 	if (scr->active)
229 		bus_space_copy_region_2(memt, memh,
230 					scr->dispoffset + srcoff * 2,
231 					memh, scr->dispoffset + dstoff * 2,
232 					nrows * ncols);
233 	else
234 		memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
235 		      nrows * ncols * 2);
236 }
237 
238 void
239 pcdisplay_eraserows(void *id, int startrow, int nrows, long fillattr)
240 {
241 	struct pcdisplayscreen *scr = id;
242 	bus_space_tag_t memt = scr->hdl->ph_memt;
243 	bus_space_handle_t memh = scr->hdl->ph_memh;
244 	bus_size_t off, count;
245 	u_int16_t val;
246 	u_int i;
247 
248 	off = startrow * scr->type->ncols;
249 	count = nrows * scr->type->ncols;
250 
251 	val = (fillattr << 8) | ' ';
252 
253 	if (scr->active)
254 		bus_space_set_region_2(memt, memh, scr->dispoffset + off * 2,
255 				       val, count);
256 	else
257 		for (i = 0; i < count; i++)
258 			scr->mem[off + i] = val;
259 }
260 
261 #ifdef WSDISPLAY_CUSTOM_OUTPUT
262 void
263 pcdisplay_replaceattr(void *id, long oldattr, long newattr)
264 {
265 	struct pcdisplayscreen *scr = id;
266 	bus_space_tag_t memt = scr->hdl->ph_memt;
267 	bus_space_handle_t memh = scr->hdl->ph_memh;
268 	int off;
269 	uint16_t chardata;
270 
271 	if (scr->active)
272 		for (off = 0; off < scr->type->nrows * scr->type->ncols;
273 		     off++) {
274 			chardata = bus_space_read_2(memt, memh,
275 						    scr->dispoffset + off * 2);
276 			if ((long)(chardata >> 8) == oldattr)
277 				bus_space_write_2(memt, memh,
278 				                  scr->dispoffset + off * 2,
279 				               	  ((u_int16_t)(newattr << 8)) |
280 				                  (chardata & 0x00FF));
281 		}
282 	else
283 		for (off = 0; off < scr->type->nrows * scr->type->ncols;
284 		     off++) {
285 			chardata = scr->mem[off];
286 			if ((long)(chardata >> 8) == oldattr)
287 				scr->mem[off] = ((u_int16_t)(newattr << 8)) |
288 				                (chardata & 0x00FF);
289 		}
290 }
291 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
292 
293 int
294 pcdisplay_getwschar(struct pcdisplayscreen *scr, struct wsdisplay_char *wschar)
295 {
296 	int off;
297 	uint16_t chardata;
298 	uint8_t attrbyte;
299 
300 	KASSERT(scr != NULL && wschar != NULL);
301 
302 	off = wschar->row * scr->type->ncols + wschar->col;
303 	if (off >= scr->type->ncols * scr->type->nrows)
304 		return -1;
305 
306 	if (scr->active)
307 		chardata = bus_space_read_2(scr->hdl->ph_memt,
308 		    scr->hdl->ph_memh, scr->dispoffset + off * 2);
309 	else
310 		chardata = scr->mem[off];
311 
312 	wschar->letter = (chardata & 0x00FF);
313 	wschar->flags = 0;
314 	attrbyte = (chardata & 0xFF00) >> 8;
315 	if ((attrbyte & 0x08)) wschar->flags |= WSDISPLAY_CHAR_BRIGHT;
316 	if ((attrbyte & 0x80)) wschar->flags |= WSDISPLAY_CHAR_BLINK;
317 	wschar->foreground = attrbyte & 0x07;
318 	wschar->background = (attrbyte >> 4) & 0x07;
319 
320 	return 0;
321 }
322 
323 int
324 pcdisplay_putwschar(struct pcdisplayscreen *scr, struct wsdisplay_char *wschar)
325 {
326 	int off;
327 	uint16_t chardata;
328 	uint8_t attrbyte;
329 
330 	KASSERT(scr != NULL && wschar != NULL);
331 
332 	off = wschar->row * scr->type->ncols + wschar->col;
333 	if (off >= (scr->type->ncols * scr->type->nrows))
334 		return -1;
335 
336 	attrbyte = wschar->background & 0x07;
337 	if (wschar->flags & WSDISPLAY_CHAR_BLINK) attrbyte |= 0x08;
338 	attrbyte <<= 4;
339 	attrbyte |= wschar->foreground & 0x07;
340 	if (wschar->flags & WSDISPLAY_CHAR_BRIGHT) attrbyte |= 0x08;
341 	chardata = (attrbyte << 8) | wschar->letter;
342 
343 	if (scr->active)
344 		bus_space_write_2(scr->hdl->ph_memt, scr->hdl->ph_memh,
345 		    scr->dispoffset + off * 2, chardata);
346 	else
347 		scr->mem[off] = chardata;
348 
349 	return 0;
350 }
351