pcdisplay_subr.c revision 1.30 1 /* $NetBSD: pcdisplay_subr.c,v 1.30 2006/04/12 20:06:16 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 (at) 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.30 2006/04/12 20:06:16 jmmv Exp $");
32
33 #include "opt_wsdisplay_compat.h" /* for WSDISPLAY_CHARFUNCS */
34 #include "opt_wsmsgattrs.h" /* for WSDISPLAY_CUSTOM_OUTPUT */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <machine/bus.h>
40
41 #include <dev/ic/mc6845reg.h>
42 #include <dev/ic/pcdisplayvar.h>
43 #include <dev/wscons/wsconsio.h>
44
45 #include <dev/wscons/wsdisplayvar.h>
46
47 void
48 pcdisplay_cursor_init(struct pcdisplayscreen *scr, int existing)
49 {
50 #ifdef PCDISPLAY_SOFTCURSOR
51 bus_space_tag_t memt;
52 bus_space_handle_t memh;
53 int off;
54
55 pcdisplay_6845_write(scr->hdl, curstart, 0x10);
56 pcdisplay_6845_write(scr->hdl, curend, 0x10);
57
58 if (existing) {
59 /*
60 * This is the first screen. At this point, scr->mem is NULL
61 * (no backing store), so we can't use pcdisplay_cursor() to
62 * do this.
63 */
64 memt = scr->hdl->ph_memt;
65 memh = scr->hdl->ph_memh;
66 off = (scr->cursorrow * scr->type->ncols + scr->cursorcol) * 2
67 + scr->dispoffset;
68
69 scr->cursortmp = bus_space_read_2(memt, memh, off);
70 bus_space_write_2(memt, memh, off, scr->cursortmp ^ 0x7700);
71 } else
72 scr->cursortmp = 0;
73 #else
74 /*
75 * Firmware might not have initialized the cursor shape. Make
76 * sure there's something we can see.
77 * Don't touch the hardware if this is not the first screen.
78 */
79 if (existing) {
80 pcdisplay_6845_write(scr->hdl, curstart,
81 scr->type->fontheight - 2);
82 pcdisplay_6845_write(scr->hdl, curend,
83 scr->type->fontheight - 1);
84 }
85 #endif
86 scr->cursoron = 1;
87 }
88
89 void
90 pcdisplay_cursor(void *id, int on, int row, int col)
91 {
92 #ifdef PCDISPLAY_SOFTCURSOR
93 struct pcdisplayscreen *scr = id;
94 bus_space_tag_t memt = scr->hdl->ph_memt;
95 bus_space_handle_t memh = scr->hdl->ph_memh;
96 int off;
97
98 /* Remove old cursor image */
99 if (scr->cursoron) {
100 off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
101 if (scr->active)
102 bus_space_write_2(memt, memh, scr->dispoffset + off * 2,
103 scr->cursortmp);
104 else
105 scr->mem[off] = scr->cursortmp;
106 }
107
108 scr->cursorrow = row;
109 scr->cursorcol = col;
110
111 if ((scr->cursoron = on) == 0)
112 return;
113
114 off = (scr->cursorrow * scr->type->ncols + scr->cursorcol);
115 if (scr->active) {
116 off = off * 2 + scr->dispoffset;
117 scr->cursortmp = bus_space_read_2(memt, memh, off);
118 bus_space_write_2(memt, memh, off, scr->cursortmp ^ 0x7700);
119 } else {
120 scr->cursortmp = scr->mem[off];
121 scr->mem[off] = scr->cursortmp ^ 0x7700;
122 }
123 #else /* PCDISPLAY_SOFTCURSOR */
124 struct pcdisplayscreen *scr = id;
125 int pos;
126
127 scr->cursorrow = row;
128 scr->cursorcol = col;
129 scr->cursoron = on;
130
131 if (scr->active) {
132 if (!on)
133 pos = 0x3fff;
134 else
135 pos = scr->dispoffset / 2
136 + row * scr->type->ncols + col;
137
138 pcdisplay_6845_write(scr->hdl, cursorh, pos >> 8);
139 pcdisplay_6845_write(scr->hdl, cursorl, pos);
140 }
141 #endif /* PCDISPLAY_SOFTCURSOR */
142 }
143
144 #if 0
145 unsigned int
146 pcdisplay_mapchar_simple(void *id, int uni)
147 {
148 if (uni < 128)
149 return (uni);
150
151 return (1); /* XXX ??? smiley */
152 }
153 #endif
154
155 void
156 pcdisplay_putchar(void *id, int row, int col, unsigned int c, long attr)
157 {
158 struct pcdisplayscreen *scr = id;
159 bus_space_tag_t memt = scr->hdl->ph_memt;
160 bus_space_handle_t memh = scr->hdl->ph_memh;
161 int off;
162
163 off = row * scr->type->ncols + col;
164
165 if (scr->active)
166 bus_space_write_2(memt, memh, scr->dispoffset + off * 2,
167 c | (attr << 8));
168 else
169 scr->mem[off] = c | (attr << 8);
170
171 scr->visibleoffset = scr->dispoffset;
172 }
173
174 void
175 pcdisplay_copycols(void *id, int row, int srccol, int dstcol, int ncols)
176 {
177 struct pcdisplayscreen *scr = id;
178 bus_space_tag_t memt = scr->hdl->ph_memt;
179 bus_space_handle_t memh = scr->hdl->ph_memh;
180 bus_size_t srcoff, dstoff;
181
182 srcoff = dstoff = row * scr->type->ncols;
183 srcoff += srccol;
184 dstoff += dstcol;
185
186 if (scr->active)
187 bus_space_copy_region_2(memt, memh,
188 scr->dispoffset + srcoff * 2,
189 memh, scr->dispoffset + dstoff * 2,
190 ncols);
191 else
192 memcpy(&scr->mem[dstoff], &scr->mem[srcoff], ncols * 2);
193 }
194
195 void
196 pcdisplay_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
197 {
198 struct pcdisplayscreen *scr = id;
199 bus_space_tag_t memt = scr->hdl->ph_memt;
200 bus_space_handle_t memh = scr->hdl->ph_memh;
201 bus_size_t off;
202 u_int16_t val;
203 int i;
204
205 off = row * scr->type->ncols + startcol;
206
207 val = (fillattr << 8) | ' ';
208
209 if (scr->active)
210 bus_space_set_region_2(memt, memh, scr->dispoffset + off * 2,
211 val, ncols);
212 else
213 for (i = 0; i < ncols; i++)
214 scr->mem[off + i] = val;
215 }
216
217 void
218 pcdisplay_copyrows(void *id, int srcrow, int dstrow, int nrows)
219 {
220 struct pcdisplayscreen *scr = id;
221 bus_space_tag_t memt = scr->hdl->ph_memt;
222 bus_space_handle_t memh = scr->hdl->ph_memh;
223 int ncols = scr->type->ncols;
224 bus_size_t srcoff, dstoff;
225
226 srcoff = srcrow * ncols + 0;
227 dstoff = dstrow * ncols + 0;
228
229 if (scr->active)
230 bus_space_copy_region_2(memt, memh,
231 scr->dispoffset + srcoff * 2,
232 memh, scr->dispoffset + dstoff * 2,
233 nrows * ncols);
234 else
235 memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
236 nrows * ncols * 2);
237 }
238
239 void
240 pcdisplay_eraserows(void *id, int startrow, int nrows, long fillattr)
241 {
242 struct pcdisplayscreen *scr = id;
243 bus_space_tag_t memt = scr->hdl->ph_memt;
244 bus_space_handle_t memh = scr->hdl->ph_memh;
245 bus_size_t off, count;
246 u_int16_t val;
247 u_int i;
248
249 off = startrow * scr->type->ncols;
250 count = nrows * scr->type->ncols;
251
252 val = (fillattr << 8) | ' ';
253
254 if (scr->active)
255 bus_space_set_region_2(memt, memh, scr->dispoffset + off * 2,
256 val, count);
257 else
258 for (i = 0; i < count; i++)
259 scr->mem[off + i] = val;
260 }
261
262 #ifdef WSDISPLAY_CUSTOM_OUTPUT
263 void
264 pcdisplay_replaceattr(void *id, long oldattr, long newattr)
265 {
266 struct pcdisplayscreen *scr = id;
267 bus_space_tag_t memt = scr->hdl->ph_memt;
268 bus_space_handle_t memh = scr->hdl->ph_memh;
269 int off;
270 uint16_t chardata;
271
272 if (scr->active)
273 for (off = 0; off < scr->type->nrows * scr->type->ncols;
274 off++) {
275 chardata = bus_space_read_2(memt, memh,
276 scr->dispoffset + off * 2);
277 if ((long)(chardata >> 8) == oldattr)
278 bus_space_write_2(memt, memh,
279 scr->dispoffset + off * 2,
280 ((u_int16_t)(newattr << 8)) |
281 (chardata & 0x00FF));
282 }
283 else
284 for (off = 0; off < scr->type->nrows * scr->type->ncols;
285 off++) {
286 chardata = scr->mem[off];
287 if ((long)(chardata >> 8) == oldattr)
288 scr->mem[off] = ((u_int16_t)(newattr << 8)) |
289 (chardata & 0x00FF);
290 }
291 }
292 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
293
294 #ifdef WSDISPLAY_CHARFUNCS
295 int
296 pcdisplay_getwschar(void *id, struct wsdisplay_char *wschar)
297 {
298 struct pcdisplayscreen *scr = id;
299 bus_space_tag_t memt = scr->hdl->ph_memt;
300 bus_space_handle_t memh = scr->hdl->ph_memh;
301 int off;
302 uint16_t chardata;
303 uint8_t attrbyte;
304
305 off = wschar->row * scr->type->ncols + wschar->col;
306 if (off >= scr->type->ncols * scr->type->nrows)
307 return -1;
308
309 if (scr->active)
310 chardata = bus_space_read_2(memt, memh,
311 scr->dispoffset + off * 2);
312 else
313 chardata = scr->mem[off];
314
315 wschar->letter = (chardata & 0x00FF);
316 wschar->flags = 0;
317 attrbyte = (chardata & 0xFF00) >> 8;
318 if ((attrbyte & 0x08)) wschar->flags |= WSDISPLAY_CHAR_BRIGHT;
319 if ((attrbyte & 0x80)) wschar->flags |= WSDISPLAY_CHAR_BLINK;
320 wschar->foreground = attrbyte & 0x07;
321 wschar->background = (attrbyte >> 4) & 0x07;
322
323 return 0;
324 }
325
326 int
327 pcdisplay_putwschar(void *id, struct wsdisplay_char *wschar)
328 {
329 struct pcdisplayscreen *scr = id;
330 bus_space_tag_t memt = scr->hdl->ph_memt;
331 bus_space_handle_t memh = scr->hdl->ph_memh;
332 int off;
333 uint16_t chardata;
334 uint8_t attrbyte;
335
336 off = wschar->row * scr->type->ncols + wschar->col;
337 if (off >= (scr->type->ncols * scr->type->nrows))
338 return -1;
339
340 attrbyte = wschar->background & 0x07;
341 if (wschar->flags & WSDISPLAY_CHAR_BLINK) attrbyte |= 0x08;
342 attrbyte <<= 4;
343 attrbyte |= wschar->foreground & 0x07;
344 if (wschar->flags & WSDISPLAY_CHAR_BRIGHT) attrbyte |= 0x08;
345 chardata = (attrbyte << 8) | wschar->letter;
346
347 if (scr->active)
348 bus_space_write_2(memt, memh, scr->dispoffset + off * 2,
349 chardata);
350 else
351 scr->mem[off] = chardata;
352
353 return 0;
354 }
355 #endif /* WSDISPLAY_CHARFUNCS */
356