smg.c revision 1.7 1 /* $NetBSD: smg.c,v 1.7 1998/08/05 16:50:39 kleink Exp $ */
2 /*
3 * Copyright (c) 1998 Ludd, University of Lule}, Sweden.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed at Ludd, University of
17 * Lule}, Sweden and its contributors.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/param.h>
34 #include <sys/device.h>
35 #include <sys/systm.h>
36 #include <sys/time.h>
37 #include <sys/malloc.h>
38 #include <sys/conf.h>
39 #include <sys/kernel.h>
40
41 #include <dev/cons.h>
42
43 #include <dev/wscons/wsdisplayvar.h>
44 #include <dev/wscons/wsconsio.h>
45 #include <dev/wscons/wscons_callbacks.h>
46
47 #include <machine/vsbus.h>
48 #include <machine/sid.h>
49
50 #include "lkc.h"
51
52 #define SM_COLS 128 /* char width of screen */
53 #define SM_ROWS 57 /* rows of char on screen */
54 #define SM_CHEIGHT 15 /* lines a char consists of */
55 #define SM_NEXTROW (SM_COLS * SM_CHEIGHT)
56
57 static int smg_match __P((struct device *, struct cfdata *, void *));
58 static void smg_attach __P((struct device *, struct device *, void *));
59
60 struct smg_softc {
61 struct device ss_dev;
62 };
63
64 struct cfattach smg_ca = {
65 sizeof(struct smg_softc), smg_match, smg_attach,
66 };
67
68 static void smg_cursor __P((void *, int, int, int));
69 unsigned int smg_mapchar __P((void *, int));
70 static void smg_putchar __P((void *, int, int, u_int, long));
71 static void smg_copycols __P((void *, int, int, int,int));
72 static void smg_erasecols __P((void *, int, int, int, long));
73 static void smg_copyrows __P((void *, int, int, int));
74 static void smg_eraserows __P((void *, int, int, long));
75 static int smg_alloc_attr __P((void *, int, int, int, long *));
76
77 const struct wsdisplay_emulops smg_emulops = {
78 smg_cursor,
79 smg_mapchar,
80 smg_putchar,
81 smg_copycols,
82 smg_erasecols,
83 smg_copyrows,
84 smg_eraserows,
85 smg_alloc_attr
86 };
87
88 const struct wsscreen_descr smg_stdscreen = {
89 "128x57", SM_COLS, SM_ROWS,
90 &smg_emulops,
91 8, SM_CHEIGHT,
92 WSSCREEN_UNDERLINE|WSSCREEN_REVERSE,
93 };
94
95 const struct wsscreen_descr *_smg_scrlist[] = {
96 &smg_stdscreen,
97 };
98
99 const struct wsscreen_list smg_screenlist = {
100 sizeof(_smg_scrlist) / sizeof(struct wsscreen_descr *),
101 _smg_scrlist,
102 };
103
104 extern char q_font[];
105 #define QCHAR(c) (c < 32 ? 32 : (c > 127 ? c - 66 : c - 32))
106 #define QFONT(c,line) q_font[QCHAR(c) * 15 + line]
107 #define SM_ADDR(row, col, line) \
108 sm_addr[col + (row * SM_CHEIGHT * SM_COLS) + line * SM_COLS]
109
110
111 static int smg_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
112 static int smg_mmap __P((void *, off_t, int));
113 static int smg_alloc_screen __P((void *, const struct wsscreen_descr *,
114 void **, int *, int *, long *));
115 static void smg_free_screen __P((void *, void *));
116 static void smg_show_screen __P((void *, void *));
117 static int smg_load_font __P((void *, void *, int, int, int, void *));
118 static void smg_crsr_blink __P((void *));
119
120 const struct wsdisplay_accessops smg_accessops = {
121 smg_ioctl,
122 smg_mmap,
123 smg_alloc_screen,
124 smg_free_screen,
125 smg_show_screen,
126 smg_load_font
127 };
128
129 struct smg_screen {
130 int ss_curx;
131 int ss_cury;
132 u_char ss_image[SM_ROWS][SM_COLS]; /* Image of current screen */
133 u_char ss_attr[SM_ROWS][SM_COLS]; /* Reversed etc... */
134 };
135
136 static struct smg_screen smg_conscreen;
137 static struct smg_screen *curscr;
138
139 int
140 smg_match(parent, match, aux)
141 struct device *parent;
142 struct cfdata *match;
143 void *aux;
144 {
145 struct vsbus_attach_args *va = aux;
146
147 if (va->va_type != INR_VF)
148 return 0;
149 #ifdef DIAGNOSTIC
150 if (sm_addr == 0)
151 panic("smg: inconsistency in smg address mapping");
152 #endif
153 return 1;
154 }
155
156 void
157 smg_attach(parent, self, aux)
158 struct device *parent, *self;
159 void *aux;
160 {
161 struct wsemuldisplaydev_attach_args aa;
162
163 printf("\n");
164 curscr = &smg_conscreen;
165 aa.console = !(vax_confdata & 0x20);
166 aa.scrdata = &smg_screenlist;
167 aa.accessops = &smg_accessops;
168 timeout(smg_crsr_blink, 0, hz/2);
169
170 config_found(self, &aa, wsemuldisplaydevprint);
171 }
172
173 static u_char *cursor;
174 static int cur_on;
175
176 static void
177 smg_crsr_blink(arg)
178 void *arg;
179 {
180 if (cur_on)
181 *cursor ^= 255;
182 timeout(smg_crsr_blink, 0, hz/2);
183 }
184
185 void
186 smg_cursor(id, on, row, col)
187 void *id;
188 int on, row, col;
189 {
190 struct smg_screen *ss = id;
191
192 if (ss == curscr) {
193 SM_ADDR(ss->ss_cury, ss->ss_curx, 14) =
194 QFONT(ss->ss_image[ss->ss_cury][ss->ss_curx], 14);
195 cursor = &SM_ADDR(row, col, 14);
196 if ((cur_on = on))
197 *cursor ^= 255;
198 }
199 ss->ss_curx = col;
200 ss->ss_cury = row;
201 }
202
203 unsigned int
204 smg_mapchar(id, uni)
205 void *id;
206 int uni;
207 {
208 if (uni < 256)
209 return (uni);
210 return (0);
211 }
212
213 static void
214 smg_putchar(id, row, col, c, attr)
215 void *id;
216 int row, col;
217 u_int c;
218 long attr;
219 {
220 struct smg_screen *ss = id;
221 int i;
222
223 c &= 0xff;
224
225 ss->ss_image[row][col] = c;
226 ss->ss_attr[row][col] = attr;
227 if (ss != curscr)
228 return;
229 for (i = 0; i < 15; i++) {
230 unsigned char ch = QFONT(c, i);
231
232 SM_ADDR(row, col, i) = (attr & WSATTR_REVERSE ? ~ch : ch);
233
234 }
235 if (attr & WSATTR_UNDERLINE)
236 SM_ADDR(row, col, 14) ^= SM_ADDR(row, col, 14);
237 }
238
239 /*
240 * copies columns inside a row.
241 */
242 static void
243 smg_copycols(id, row, srccol, dstcol, ncols)
244 void *id;
245 int row, srccol, dstcol, ncols;
246 {
247 struct smg_screen *ss = id;
248 int i;
249
250 bcopy(&ss->ss_image[row][srccol], &ss->ss_image[row][dstcol], ncols);
251 bcopy(&ss->ss_attr[row][srccol], &ss->ss_attr[row][dstcol], ncols);
252 if (ss != curscr)
253 return;
254 for (i = 0; i < SM_CHEIGHT; i++)
255 bcopy(&SM_ADDR(row,srccol, i), &SM_ADDR(row, dstcol, i),ncols);
256 }
257
258 /*
259 * Erases a bunch of chars inside one row.
260 */
261 static void
262 smg_erasecols(id, row, startcol, ncols, fillattr)
263 void *id;
264 int row, startcol, ncols;
265 long fillattr;
266 {
267 struct smg_screen *ss = id;
268 int i;
269
270 bzero(&ss->ss_image[row][startcol], ncols);
271 bzero(&ss->ss_attr[row][startcol], ncols);
272 if (ss != curscr)
273 return;
274 for (i = 0; i < SM_CHEIGHT; i++)
275 bzero(&SM_ADDR(row, startcol, i), ncols);
276 }
277
278 static void
279 smg_copyrows(id, srcrow, dstrow, nrows)
280 void *id;
281 int srcrow, dstrow, nrows;
282 {
283 struct smg_screen *ss = id;
284 int frows;
285
286 bcopy(&ss->ss_image[srcrow][0], &ss->ss_image[dstrow][0],
287 nrows * SM_COLS);
288 bcopy(&ss->ss_attr[srcrow][0], &ss->ss_attr[dstrow][0],
289 nrows * SM_COLS);
290 if (ss != curscr)
291 return;
292 if (nrows > 25) {
293 frows = nrows >> 1;
294 if (srcrow > dstrow) {
295 bcopy(&sm_addr[(srcrow * SM_NEXTROW)],
296 &sm_addr[(dstrow * SM_NEXTROW)],
297 frows * SM_NEXTROW);
298 bcopy(&sm_addr[((srcrow + frows) * SM_NEXTROW)],
299 &sm_addr[((dstrow + frows) * SM_NEXTROW)],
300 (nrows - frows) * SM_NEXTROW);
301 } else {
302 bcopy(&sm_addr[((srcrow + frows) * SM_NEXTROW)],
303 &sm_addr[((dstrow + frows) * SM_NEXTROW)],
304 (nrows - frows) * SM_NEXTROW);
305 bcopy(&sm_addr[(srcrow * SM_NEXTROW)],
306 &sm_addr[(dstrow * SM_NEXTROW)],
307 frows * SM_NEXTROW);
308 }
309 } else
310 bcopy(&sm_addr[(srcrow * SM_NEXTROW)],
311 &sm_addr[(dstrow * SM_NEXTROW)], nrows * SM_NEXTROW);
312 }
313
314 static void
315 smg_eraserows(id, startrow, nrows, fillattr)
316 void *id;
317 int startrow, nrows;
318 long fillattr;
319 {
320 struct smg_screen *ss = id;
321 int frows;
322
323 bzero(&ss->ss_image[startrow][0], nrows * SM_COLS);
324 bzero(&ss->ss_attr[startrow][0], nrows * SM_COLS);
325 if (ss != curscr)
326 return;
327 if (nrows > 25) {
328 frows = nrows >> 1;
329 bzero(&sm_addr[(startrow * SM_NEXTROW)], frows * SM_NEXTROW);
330 bzero(&sm_addr[((startrow + frows) * SM_NEXTROW)],
331 (nrows - frows) * SM_NEXTROW);
332 } else
333 bzero(&sm_addr[(startrow * SM_NEXTROW)], nrows * SM_NEXTROW);
334 }
335
336 static int
337 smg_alloc_attr(id, fg, bg, flags, attrp)
338 void *id;
339 int fg, bg;
340 int flags;
341 long *attrp;
342 {
343 *attrp = flags;
344 return 0;
345 }
346
347 int
348 smg_ioctl(v, cmd, data, flag, p)
349 void *v;
350 u_long cmd;
351 caddr_t data;
352 int flag;
353 struct proc *p;
354 {
355 return -1;
356 }
357
358 static int
359 smg_mmap(v, offset, prot)
360 void *v;
361 off_t offset;
362 int prot;
363 {
364 return -1;
365 }
366
367 int
368 smg_alloc_screen(v, type, cookiep, curxp, curyp, defattrp)
369 void *v;
370 const struct wsscreen_descr *type;
371 void **cookiep;
372 int *curxp, *curyp;
373 long *defattrp;
374 {
375 *cookiep = malloc(sizeof(struct smg_screen), M_DEVBUF, M_WAITOK);
376 bzero(*cookiep, sizeof(struct smg_screen));
377 *curxp = *curyp = *defattrp = 0;
378 return 0;
379 }
380
381 void
382 smg_free_screen(v, cookie)
383 void *v;
384 void *cookie;
385 {
386 }
387
388 void
389 smg_show_screen(v, cookie)
390 void *v;
391 void *cookie;
392 {
393 struct smg_screen *ss = cookie;
394 int row, col, line;
395
396 if (ss == curscr)
397 return;
398
399 for (row = 0; row < SM_ROWS; row++)
400 for (line = 0; line < SM_CHEIGHT; line++)
401 for (col = 0; col < SM_COLS; col++) {
402 u_char s, c = ss->ss_image[row][col];
403
404 if (c < 32)
405 c = 32;
406 s = QFONT(c, line);
407 if (ss->ss_attr[row][col] & WSATTR_REVERSE)
408 s ^= 255;
409 SM_ADDR(row, col, line) = s;
410 }
411 cursor = &sm_addr[(ss->ss_cury * SM_CHEIGHT * SM_COLS) + ss->ss_curx +
412 ((SM_CHEIGHT - 1) * SM_COLS)];
413 curscr = ss;
414 }
415
416 static int
417 smg_load_font(v, cookie, first, num, stride, data)
418 void *v;
419 void *cookie;
420 int first, num, stride;
421 void *data;
422 {
423 return 0;
424 }
425
426 cons_decl(smg);
427
428 #define WSCONSOLEMAJOR 68
429
430 void
431 smgcninit(cndev)
432 struct consdev *cndev;
433 {
434 extern void lkccninit __P((struct consdev *));
435 extern int lkccngetc __P((dev_t));
436 /* Clear screen */
437 blkclr(sm_addr, 128*864);
438
439 curscr = &smg_conscreen;
440 wsdisplay_cnattach(&smg_stdscreen, &smg_conscreen, 0, 0, 0);
441 cn_tab->cn_dev = makedev(WSCONSOLEMAJOR, 0);
442 #if NLKC
443 lkccninit(cndev);
444 wsdisplay_set_cons_kbd(lkccngetc, nullcnpollc);
445 #endif
446 }
447
448 int smgprobe(void);
449 int
450 smgprobe()
451 {
452 switch (vax_boardtype) {
453 case VAX_BTYP_410:
454 case VAX_BTYP_420:
455 case VAX_BTYP_43:
456 if (vax_confdata & 0x20) /* doesn't use graphics console */
457 break;
458 if (sm_addr == 0) /* Haven't mapped graphic area */
459 break;
460
461 return 1;
462
463 default:
464 break;
465 }
466 return 0;
467 }
468