xref: /netbsd-src/sys/arch/amiga/dev/amidisplaycc.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: amidisplaycc.c,v 1.19 2006/04/12 19:38:22 jmmv Exp $ */
2 
3 /*-
4  * Copyright (c) 2000 Jukka Andberg.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: amidisplaycc.c,v 1.19 2006/04/12 19:38:22 jmmv Exp $");
32 
33 /*
34  * wscons interface to amiga custom chips. Contains the necessary functions
35  * to render text on bitmapped screens. Uses the functions defined in
36  * grfabs_reg.h for display creation/destruction and low level setup.
37  *
38  * For each virtual terminal a new screen ('view') is allocated.
39  * Also one more is allocated for the mapped screen on demand.
40  */
41 
42 #include "amidisplaycc.h"
43 #include "grfcc.h"
44 #include "view.h"
45 #include "opt_amigaccgrf.h"
46 #include "kbd.h"
47 
48 #if NAMIDISPLAYCC>0
49 
50 #include <sys/param.h>
51 #include <sys/types.h>
52 #include <sys/device.h>
53 #include <sys/malloc.h>
54 #include <sys/systm.h>
55 
56 #include <sys/conf.h>
57 
58 #include <amiga/dev/grfabs_reg.h>
59 #include <amiga/dev/kbdvar.h>
60 #include <amiga/dev/viewioctl.h>
61 #include <amiga/amiga/device.h>
62 #include <dev/wscons/wsconsio.h>
63 #include <dev/rcons/raster.h>
64 #include <dev/wscons/wscons_raster.h>
65 #include <dev/wscons/wsdisplayvar.h>
66 #include <dev/cons.h>
67 #include <dev/wsfont/wsfont.h>
68 
69 #include <machine/stdarg.h>
70 
71 /* These can be lowered if you are sure you dont need that much colors. */
72 #define MAXDEPTH 8
73 #define MAXROWS 128
74 
75 #define ADJUSTCOLORS
76 
77 #define MAXCOLORS (1<<MAXDEPTH)
78 
79 struct amidisplaycc_screen;
80 struct amidisplaycc_softc
81 {
82 	struct device dev;
83 
84 	struct amidisplaycc_screen  * currentscreen;
85 
86 	/* display turned on? */
87 	int       ison;
88 
89 	/* stuff relating to the mapped screen */
90 	view_t  * gfxview;
91 	int       gfxwidth;
92 	int       gfxheight;
93 	int       gfxdepth;
94 	int       gfxon;
95 };
96 
97 
98 /*
99  * Configuration stuff.
100  */
101 
102 static int  amidisplaycc_match(struct device *, struct cfdata *, void *);
103 static void amidisplaycc_attach(struct device *, struct device *, void *);
104 
105 CFATTACH_DECL(amidisplaycc, sizeof(struct amidisplaycc_softc),
106     amidisplaycc_match, amidisplaycc_attach, NULL, NULL);
107 
108 static int amidisplaycc_attached;
109 
110 cons_decl(amidisplaycc_);
111 
112 /* end of configuration stuff */
113 
114 /* private utility functions */
115 
116 static int amidisplaycc_setvideo(struct amidisplaycc_softc *, int);
117 
118 static int amidisplaycc_setemulcmap(struct amidisplaycc_screen *,
119 				    struct wsdisplay_cmap *);
120 
121 static int amidisplaycc_cmapioctl(view_t *, u_long, struct wsdisplay_cmap *);
122 static int amidisplaycc_setcmap(view_t *, struct wsdisplay_cmap *);
123 static int amidisplaycc_getcmap(view_t *, struct wsdisplay_cmap *);
124 static int amidisplaycc_gfxscreen(struct amidisplaycc_softc *, int);
125 
126 static int amidisplaycc_setfont(struct amidisplaycc_screen *, const char *);
127 static const struct wsdisplay_font * amidisplaycc_getbuiltinfont(void);
128 
129 static void dprintf(const char *fmt, ...);
130 
131 /* end of private utility functions */
132 
133 /* emulops for wscons */
134 void amidisplaycc_cursor(void *, int, int, int);
135 int amidisplaycc_mapchar(void *, int, unsigned int *);
136 void amidisplaycc_putchar(void *, int, int, u_int, long);
137 void amidisplaycc_copycols(void *, int, int, int, int);
138 void amidisplaycc_erasecols(void *, int, int, int, long);
139 void amidisplaycc_copyrows(void *, int, int, int);
140 void amidisplaycc_eraserows(void *, int, int, long);
141 int  amidisplaycc_allocattr(void *, int, int, int, long *);
142 /* end of emulops for wscons */
143 
144 
145 /* accessops for wscons */
146 int amidisplaycc_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
147 paddr_t amidisplaycc_mmap(void *, void *, off_t, int);
148 int amidisplaycc_alloc_screen(void *, const struct wsscreen_descr *, void **,
149 			      int *, int *, long *);
150 void amidisplaycc_free_screen( void *, void *);
151 int amidisplaycc_show_screen(void *, void *, int, void (*)(void *, int, int),
152 			     void *);
153 int amidisplaycc_load_font(void *, void *, struct wsdisplay_font *);
154 void amidisplaycc_pollc(void *, int);
155 /* end of accessops for wscons */
156 
157 /*
158  * These structures are passed to wscons, and they contain the
159  * display-specific callback functions.
160  */
161 
162 const struct wsdisplay_accessops amidisplaycc_accessops = {
163 	amidisplaycc_ioctl,
164 	amidisplaycc_mmap,
165 	amidisplaycc_alloc_screen,
166 	amidisplaycc_free_screen,
167 	amidisplaycc_show_screen,
168 	amidisplaycc_load_font,
169 	amidisplaycc_pollc
170 };
171 
172 const struct wsdisplay_emulops amidisplaycc_emulops = {
173 	amidisplaycc_cursor,
174 	amidisplaycc_mapchar,
175 	amidisplaycc_putchar,
176 	amidisplaycc_copycols,
177 	amidisplaycc_erasecols,
178 	amidisplaycc_copyrows,
179 	amidisplaycc_eraserows,
180 	amidisplaycc_allocattr
181 };
182 
183 /* add some of our own data to the wsscreen_descr */
184 struct amidisplaycc_screen_descr {
185 	struct wsscreen_descr  wsdescr;
186 	int                    depth;
187 };
188 
189 /*
190  * List of supported screenmodes. Almost anything can be given here.
191  */
192 
193 #define ADCC_SCREEN(name, width, height, depth, fontwidth, fontheight) \
194     /* CONSTCOND */ \
195     {{ \
196     name, \
197     width / fontwidth, \
198     height / fontheight, \
199     &amidisplaycc_emulops, fontwidth, fontheight, \
200     (depth > 1 ? WSSCREEN_WSCOLORS : 0) | WSSCREEN_REVERSE | \
201     WSSCREEN_HILIT | WSSCREEN_UNDERLINE }, \
202     depth }
203 
204 /*
205  * Screen types.
206  *
207  * The first in list is used for the console screen.
208  * A suitable screen mode is guessed for it by looking
209  * at the GRF_* options.
210  */
211 struct amidisplaycc_screen_descr amidisplaycc_screentab[] = {
212 	/* name, width, height, depth, fontwidth==8, fontheight */
213 
214 #if defined(GRF_PAL) && !defined(GRF_NTSC)
215 	ADCC_SCREEN("default", 640, 512, 3, 8, 8),
216 #else
217 	ADCC_SCREEN("default", 640, 400, 3, 8, 8),
218 #endif
219 	ADCC_SCREEN("80x50", 640, 400, 3, 8, 8),
220 	ADCC_SCREEN("80x40", 640, 400, 3, 8, 10),
221 	ADCC_SCREEN("80x25", 640, 400, 3, 8, 16),
222 	ADCC_SCREEN("80x24", 640, 192, 3, 8, 8),
223 
224 	ADCC_SCREEN("80x64", 640, 512, 3, 8, 8),
225 	ADCC_SCREEN("80x51", 640, 510, 3, 8, 10),
226 	ADCC_SCREEN("80x32", 640, 512, 3, 8, 16),
227 	ADCC_SCREEN("80x31", 640, 248, 3, 8, 8),
228 
229 	ADCC_SCREEN("640x400x1", 640, 400, 1, 8, 8),
230 	ADCC_SCREEN("640x400x2", 640, 400, 2, 8, 8),
231 	ADCC_SCREEN("640x400x3", 640, 400, 3, 8, 8),
232 
233 	ADCC_SCREEN("640x200x1", 640, 200, 1, 8, 8),
234 	ADCC_SCREEN("640x200x2", 640, 200, 2, 8, 8),
235 	ADCC_SCREEN("640x200x3", 640, 200, 3, 8, 8),
236 };
237 
238 #define ADCC_SCREENPTR(index) &amidisplaycc_screentab[index].wsdescr
239 const struct wsscreen_descr *amidisplaycc_screens[] = {
240 	ADCC_SCREENPTR(0),
241 	ADCC_SCREENPTR(1),
242 	ADCC_SCREENPTR(2),
243 	ADCC_SCREENPTR(3),
244 	ADCC_SCREENPTR(4),
245 	ADCC_SCREENPTR(5),
246 	ADCC_SCREENPTR(6),
247 	ADCC_SCREENPTR(7),
248 	ADCC_SCREENPTR(8),
249 	ADCC_SCREENPTR(9),
250 	ADCC_SCREENPTR(10),
251 	ADCC_SCREENPTR(11),
252 	ADCC_SCREENPTR(12),
253 	ADCC_SCREENPTR(13),
254 	ADCC_SCREENPTR(14),
255 };
256 
257 #define NELEMS(arr) (sizeof(arr)/sizeof((arr)[0]))
258 
259 /*
260  * This structure also is passed to wscons. It contains pointers
261  * to the available display modes.
262  */
263 
264 const struct wsscreen_list amidisplaycc_screenlist = {
265 	sizeof(amidisplaycc_screens)/sizeof(amidisplaycc_screens[0]),
266 	amidisplaycc_screens
267 };
268 
269 /*
270  * Our own screen structure. One will be created for each screen.
271  */
272 
273 struct amidisplaycc_screen
274 {
275 	struct amidisplaycc_softc *device;
276 
277 	int       isconsole;
278 	int       isvisible;
279 	view_t  * view;
280 
281 	int       ncols;
282 	int       nrows;
283 
284 	int       cursorrow;
285 	int       cursorcol;
286 
287 	/* Active bitplanes for each character row. */
288 	int       rowmasks[MAXROWS];
289 
290 	/* Mapping of colors to screen colors. */
291 	int       colormap[MAXCOLORS];
292 
293 	/* Copies of display parameters for convenience */
294 	int       width;
295 	int       height;
296 	int       depth;
297 
298 	int       widthbytes; /* bytes_per_row           */
299 	int       linebytes;  /* widthbytes + row_mod    */
300 	int       rowbytes;   /* linebytes * fontheight  */
301 
302 	u_char  * planes[MAXDEPTH];
303 
304 	const struct wsdisplay_font  * wsfont;
305 	int                      wsfontcookie; /* if -1, builtin font */
306 	int                      fontwidth;
307 	int                      fontheight;
308 };
309 
310 typedef struct amidisplaycc_screen adccscr_t;
311 
312 /*
313  * Need one statically allocated screen for early init.
314  * The rest are mallocated when needed.
315  */
316 adccscr_t amidisplaycc_consolescreen;
317 
318 /*
319  * Default palettes for 2, 4 and 8 color emulation displays.
320  */
321 
322 /* black, grey */
323 static u_char pal2red[] = { 0x00, 0xaa };
324 static u_char pal2grn[] = { 0x00, 0xaa };
325 static u_char pal2blu[] = { 0x00, 0xaa };
326 
327 /* black, red, green, grey */
328 static u_char pal4red[] = { 0x00, 0xaa, 0x00, 0xaa };
329 static u_char pal4grn[] = { 0x00, 0x00, 0xaa, 0xaa };
330 static u_char pal4blu[] = { 0x00, 0x00, 0x00, 0xaa };
331 
332 /* black, red, green, brown, blue, magenta, cyan, grey */
333 static u_char pal8red[] = { 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa};
334 static u_char pal8grn[] = { 0x00, 0x00, 0xaa, 0xaa, 0x00, 0x00, 0xaa, 0xaa};
335 static u_char pal8blu[] = { 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa};
336 
337 static struct wsdisplay_cmap pal2 = { 0, 2, pal2red, pal2grn, pal2blu };
338 static struct wsdisplay_cmap pal4 = { 0, 4, pal4red, pal4grn, pal4blu };
339 static struct wsdisplay_cmap pal8 = { 0, 8, pal8red, pal8grn, pal8blu };
340 
341 #ifdef GRF_AGA
342 extern int aga_enable;
343 #else
344 static int aga_enable = 0;
345 #endif
346 
347 /*
348  * This gets called at console init to determine the priority of
349  * this console device.
350  *
351  * Of course pointers to this and other functions must present
352  * in constab[] in conf.c for this to work.
353  */
354 void
355 amidisplaycc_cnprobe(struct consdev *cd)
356 {
357 	cd->cn_pri = CN_INTERNAL;
358 
359 	/*
360 	 * Yeah, real nice. But if we win the console then the wscons system
361 	 * does the proper initialization.
362 	 */
363 	cd->cn_dev = NODEV;
364 }
365 
366 /*
367  * This gets called if this device is used as the console.
368  */
369 void
370 amidisplaycc_cninit(struct consdev  * cd)
371 {
372 	void  * cookie;
373 	long    attr;
374 	int     x;
375 	int     y;
376 
377 	/* Yeah, we got the console! */
378 
379 	/*
380 	 * This will do the basic stuff we also need.
381 	 */
382 	config_console();
383 
384 	grfcc_probe();
385 
386 #if NVIEW>0
387 	viewprobe();
388 #endif
389 
390 	/*
391 	 * Set up wscons to handle the details.
392 	 * It will then call us back when it needs something
393 	 * display-specific. It will also set up cn_tab properly,
394 	 * something which we failed to do at amidisplaycc_cnprobe().
395 	 */
396 
397 	/*
398 	 * The alloc_screen knows to allocate the first screen statically.
399 	 */
400 	amidisplaycc_alloc_screen(NULL, &amidisplaycc_screentab[0].wsdescr,
401 				  &cookie, &x, &y, &attr);
402 	wsdisplay_cnattach(&amidisplaycc_screentab[0].wsdescr,
403 			   cookie, x, y, attr);
404 
405 #if NKBD>0
406 	/* tell kbd device it is used as console keyboard */
407 	kbd_cnattach();
408 #endif
409 }
410 
411 static int
412 amidisplaycc_match(struct device *pdp, struct cfdata *cfp, void *auxp)
413 {
414 	char *name = auxp;
415 
416 	if (matchname("amidisplaycc", name) == 0)
417 		return (0);
418 
419 	/* Allow only one of us now. Not sure about that. */
420 	if (amidisplaycc_attached)
421 		return (0);
422 
423 	return 1;
424 }
425 
426 /* ARGSUSED */
427 static void
428 amidisplaycc_attach(struct device *pdp, struct device *dp, void *auxp)
429 {
430 	struct wsemuldisplaydev_attach_args    waa;
431 	struct amidisplaycc_softc            * adp;
432 
433 	amidisplaycc_attached = 1;
434 
435 	adp = (struct amidisplaycc_softc*)dp;
436 
437 	grfcc_probe();
438 
439 #if NVIEW>0
440 	viewprobe();
441 #endif
442 
443 	/*
444 	 * Attach only at real configuration time. Console init is done at
445 	 * the amidisplaycc_cninit function above.
446 	 */
447 	if (adp) {
448 		printf(": Amiga custom chip graphics %s",
449 		       aga_enable ? "(AGA)" : "");
450 
451 		if (amidisplaycc_consolescreen.isconsole) {
452 			adp->currentscreen = &amidisplaycc_consolescreen;
453 			printf(" (console)");
454 		} else
455 			adp->currentscreen = NULL;
456 
457 		printf("\n");
458 
459 		adp->ison = 1;
460 
461 		/*
462 		 * Mapped screen properties.
463 		 * Would need a way to configure.
464 		 */
465 		adp->gfxview = NULL;
466 		adp->gfxon = 0;
467 		adp->gfxwidth = amidisplaycc_screentab[0].wsdescr.ncols *
468 			amidisplaycc_screentab[0].wsdescr.fontwidth;
469 		adp->gfxheight = amidisplaycc_screentab[0].wsdescr.nrows *
470 			amidisplaycc_screentab[0].wsdescr.fontheight;
471 
472 		if (aga_enable)
473 			adp->gfxdepth = 8;
474 		else
475 			adp->gfxdepth = 4;
476 
477 		if (NELEMS(amidisplaycc_screentab) !=
478 		    NELEMS(amidisplaycc_screens))
479 			panic("invalid screen definitions");
480 
481 		waa.scrdata = &amidisplaycc_screenlist;
482 		waa.console = amidisplaycc_consolescreen.isconsole;
483 		waa.accessops = &amidisplaycc_accessops;
484 		waa.accesscookie = dp;
485 		config_found(dp, &waa, wsemuldisplaydevprint);
486 
487 		wsfont_init();
488 	}
489 }
490 
491 
492 /*
493  * Color, bgcolor and style are packed into one long attribute.
494  * These macros are used to create/split the attribute
495  */
496 
497 #define MAKEATTR(fg, bg, mode) (((fg)<<16) | ((bg)<<8) | (mode))
498 #define ATTRFG(attr) (((attr)>>16) & 255)
499 #define ATTRBG(attr) (((attr)>>8) & 255)
500 #define ATTRMO(attr) ((attr) & 255)
501 
502 /*
503  * Called by wscons to draw/clear the cursor.
504  * We do this by xorring the block to the screen.
505  *
506  * This simple implementation will break if the screen is modified
507  * under the cursor before clearing it.
508  */
509 void
510 amidisplaycc_cursor(void *screen, int on, int row, int col)
511 {
512 	adccscr_t  * scr;
513 	u_char     * dst;
514 	int  i;
515 
516 	scr = screen;
517 
518 	if (row < 0 || col < 0 || row >= scr->nrows || col >= scr->ncols)
519 		return;
520 
521 	/* was off, turning off again? */
522 	if (!on && scr->cursorrow == -1 && scr->cursorcol == -1)
523 		return;
524 
525 	/* was on, and turning on again? */
526 	if (on && scr->cursorrow >= 0 && scr->cursorcol >= 0)
527 	{
528 		/* clear from old location first */
529 		amidisplaycc_cursor (screen, 0, scr->cursorrow, scr->cursorcol);
530 	}
531 
532 	dst = scr->planes[0];
533 	dst += row * scr->rowbytes;
534 	dst += col;
535 
536 	if (on) {
537 		scr->cursorrow = row;
538 		scr->cursorcol = col;
539 	} else {
540 		scr->cursorrow = -1;
541 		scr->cursorcol = -1;
542 	}
543 
544 	for (i = scr->fontheight ; i > 0 ; i--) {
545 		*dst ^= 255;
546 		dst += scr->linebytes;
547 	}
548 }
549 
550 
551 /*
552  * This obviously does something important, don't ask me what.
553  */
554 int
555 amidisplaycc_mapchar(void *screen, int ch, unsigned int *chp)
556 {
557 	if (ch > 0 && ch < 256) {
558 		*chp = ch;
559 		return (5);
560 	}
561 	*chp = ' ';
562 	return (0);
563 }
564 
565 /*
566  * Write a character to screen with color / bgcolor / hilite(bold) /
567  * underline / reverse.
568  * Surely could be made faster but I'm not sure if its worth the
569  * effort as scrolling is at least a magnitude slower.
570  */
571 void
572 amidisplaycc_putchar(void *screen, int row, int col, u_int ch, long attr)
573 {
574 	adccscr_t  * scr;
575 	u_char     * dst;
576 	u_char     * font;
577 
578 	int         fontheight;
579 	u_int8_t  * fontreal;
580 	int         fontlow;
581 	int         fonthigh;
582 
583 	int     bmapoffset;
584 	int     linebytes;
585 	int     underline;
586 	int     fgcolor;
587 	int     bgcolor;
588 	int     plane;
589 	int     depth;
590 	int     mode;
591 	int     bold;
592 	u_char  f;
593 	int     j;
594 
595 	scr = screen;
596 
597 	if (row < 0 || col < 0 || row >= scr->nrows || col >= scr->ncols)
598 		return;
599 
600 	/* Extract the colors from the attribute */
601 	fgcolor = ATTRFG(attr);
602 	bgcolor = ATTRBG(attr);
603 	mode    = ATTRMO(attr);
604 
605 	/* Translate to screen colors */
606 	fgcolor = scr->colormap[fgcolor];
607 	bgcolor = scr->colormap[bgcolor];
608 
609 	if (mode & WSATTR_REVERSE) {
610 		j = fgcolor;
611 		fgcolor = bgcolor;
612 		bgcolor = j;
613 	}
614 
615 	bold      = (mode & WSATTR_HILIT) > 0;
616 	underline = (mode & WSATTR_UNDERLINE) > 0;
617 
618 	fontreal = scr->wsfont->data;
619 	fontlow  = scr->wsfont->firstchar;
620 	fonthigh = fontlow + scr->wsfont->numchars - 1;
621 
622 	fontheight = min(scr->fontheight, scr->wsfont->fontheight);
623 	depth      = scr->depth;
624 	linebytes  = scr->linebytes;
625 
626 	if (ch < fontlow || ch > fonthigh)
627 		ch = fontlow;
628 
629 	/* Find the location where the wanted char is in the font data */
630 	fontreal += scr->wsfont->fontheight * (ch - fontlow);
631 
632 	bmapoffset = row * scr->rowbytes + col;
633 
634 	scr->rowmasks[row] |= fgcolor | bgcolor;
635 
636 	for (plane = 0 ; plane < depth ; plane++) {
637 		dst = scr->planes[plane] + bmapoffset;
638 
639 		if (fgcolor & 1) {
640 			if (bgcolor & 1) {
641 				/* fg=on bg=on (fill) */
642 
643 				for (j = 0 ; j < fontheight ; j++) {
644 					*dst = 255;
645 					dst += linebytes;
646 				}
647 			} else {
648 				/* fg=on bg=off (normal) */
649 
650 				font = fontreal;
651 				for (j = 0 ; j < fontheight ; j++) {
652 					f = *(font++);
653 					f |= f >> bold;
654 					*dst = f;
655 					dst += linebytes;
656 				}
657 
658 				if (underline)
659 					*(dst - linebytes) = 255;
660 			}
661 		} else {
662 			if (bgcolor & 1) {
663 				/* fg=off bg=on (inverted) */
664 
665 				font = fontreal;
666 				for (j = 0 ; j < fontheight ; j++) {
667 					f = *(font++);
668 					f |= f >> bold;
669 					*dst = ~f;
670 					dst += linebytes;
671 				}
672 
673 				if (underline)
674 					*(dst - linebytes) = 0;
675 			} else {
676 				/* fg=off bg=off (clear) */
677 
678 				for (j = 0 ; j < fontheight ; j++) {
679 					*dst = 0;
680 					dst += linebytes;
681 				}
682 			}
683 		}
684 		fgcolor >>= 1;
685 		bgcolor >>= 1;
686 	}
687 }
688 
689 /*
690  * Copy characters on a row to another position on the same row.
691  */
692 
693 void
694 amidisplaycc_copycols(void *screen, int row, int srccol, int dstcol, int ncols)
695 {
696 	adccscr_t  * scr;
697 	u_char     * src;
698 	u_char     * dst;
699 
700 	int  bmapoffset;
701 	int  linebytes;
702 	int  depth;
703 	int  plane;
704 	int  i;
705 	int  j;
706 
707 	scr = screen;
708 
709 	if (srccol < 0 || srccol + ncols > scr->ncols ||
710 	    dstcol < 0 || dstcol + ncols > scr->ncols ||
711 	    row < 0 || row >= scr->nrows)
712 		return;
713 
714 	depth = scr->depth;
715 	linebytes = scr->linebytes;
716 	bmapoffset = row * scr->rowbytes;
717 
718 	for (plane = 0 ; plane < depth ; plane++) {
719 		src = scr->planes[plane] + bmapoffset;
720 
721 		for (j = 0 ; j < scr->fontheight ; j++) {
722 			dst = src;
723 
724 			if (srccol < dstcol) {
725 
726 				for (i = ncols - 1 ; i >= 0 ; i--)
727 					dst[dstcol + i] = src[srccol + i];
728 
729 			} else {
730 
731 				for (i = 0 ; i < ncols ; i++)
732 					dst[dstcol + i] = src[srccol + i];
733 
734 			}
735 			src += linebytes;
736 		}
737 	}
738 }
739 
740 /*
741  * Erase part of a row.
742  */
743 
744 void
745 amidisplaycc_erasecols(void *screen, int row, int startcol, int ncols,
746 		       long attr)
747 {
748 	adccscr_t  * scr;
749 	u_char     * dst;
750 
751 	int  bmapoffset;
752 	int  linebytes;
753 	int  bgcolor;
754 	int  depth;
755 	int  plane;
756 	int  fill;
757 	int  j;
758 
759 	scr = screen;
760 
761 	if (row < 0 || row >= scr->nrows ||
762 	    startcol < 0 || startcol + ncols > scr->ncols)
763 		return;
764 
765 	depth = scr->depth;
766 	linebytes = scr->linebytes;
767 	bmapoffset = row * scr->rowbytes + startcol;
768 
769 	/* Erase will be done using the set background color. */
770 	bgcolor = ATTRBG(attr);
771 	bgcolor = scr->colormap[bgcolor];
772 
773 	for(plane = 0 ; plane < depth ; plane++) {
774 
775 		fill = (bgcolor & 1) ? 255 : 0;
776 
777 		dst = scr->planes[plane] + bmapoffset;
778 
779 		for (j = 0 ; j < scr->fontheight ; j++) {
780 			memset(dst, fill, ncols);
781 			dst += linebytes;
782 		}
783 	}
784 }
785 
786 /*
787  * Copy a number of rows to another location on the screen.
788  * Combined with eraserows it can be used to perform operation
789  * also known as 'scrolling'.
790  */
791 
792 void
793 amidisplaycc_copyrows(void *screen, int srcrow, int dstrow, int nrows)
794 {
795 	adccscr_t  * scr;
796 	u_char     * src;
797 	u_char     * dst;
798 
799 	int  srcbmapoffset;
800 	int  dstbmapoffset;
801 	int  widthbytes;
802 	int  fontheight;
803 	int  linebytes;
804 	u_int copysize;
805 	int  rowdelta;
806 	int  rowbytes;
807 	int  srcmask;
808 	int  dstmask;
809 	int  bmdelta;
810 	int  depth;
811 	int  plane;
812 	int  i;
813 	int  j;
814 
815 	scr = screen;
816 
817 	if (srcrow < 0 || srcrow + nrows > scr->nrows ||
818 	    dstrow < 0 || dstrow + nrows > scr->nrows)
819 		return;
820 
821 	depth = scr->depth;
822 
823 	widthbytes = scr->widthbytes;
824 	rowbytes   = scr->rowbytes;
825 	linebytes  = scr->linebytes;
826 	fontheight = scr->fontheight;
827 
828 	srcbmapoffset = rowbytes * srcrow;
829 	dstbmapoffset = rowbytes * dstrow;
830 
831 	if (srcrow < dstrow) {
832 		/* Move data downwards, need to copy from down to up */
833 		bmdelta = -rowbytes;
834 		rowdelta = -1;
835 
836 		srcbmapoffset += rowbytes * (nrows - 1);
837 		srcrow += nrows - 1;
838 
839 		dstbmapoffset += rowbytes * (nrows - 1);
840 		dstrow += nrows - 1;
841 	} else {
842 		/* Move data upwards, copy up to down */
843 		bmdelta = rowbytes;
844 		rowdelta = 1;
845 	}
846 
847 	if (widthbytes == linebytes)
848 		copysize = rowbytes;
849 	else
850 		copysize = 0;
851 
852 	for (j = 0 ; j < nrows ; j++) {
853 		/* Need to copy only planes that have data on src or dst */
854 		srcmask = scr->rowmasks[srcrow];
855 		dstmask = scr->rowmasks[dstrow];
856 		scr->rowmasks[dstrow] = srcmask;
857 
858 		for (plane = 0 ; plane < depth ; plane++) {
859 
860 			if (srcmask & 1) {
861 				/*
862 				 * Source row has data on this
863 				 * plane, copy it.
864 				 */
865 
866 				src = scr->planes[plane] + srcbmapoffset;
867 				dst = scr->planes[plane] + dstbmapoffset;
868 
869 				if (copysize > 0) {
870 
871 					memcpy(dst, src, copysize);
872 
873 				} else {
874 
875 					/*
876 					 * Data not continuous,
877 					 * must do in pieces
878 					 */
879 					for (i=0 ; i < fontheight ; i++) {
880 						memcpy(dst, src, widthbytes);
881 
882 						src += linebytes;
883 						dst += linebytes;
884 					}
885 				}
886 			} else if (dstmask & 1) {
887 				/*
888 				 * Source plane is empty, but dest is not.
889 				 * so all we need to is clear it.
890 				 */
891 
892 				dst = scr->planes[plane] + dstbmapoffset;
893 
894 				if (copysize > 0) {
895 					/* Do it all */
896 					bzero(dst, copysize);
897 				} else {
898 					for (i = 0 ; i < fontheight ; i++) {
899 						bzero(dst, widthbytes);
900 						dst += linebytes;
901 					}
902 				}
903 			}
904 
905 			srcmask >>= 1;
906 			dstmask >>= 1;
907 		}
908 		srcbmapoffset += bmdelta;
909 		dstbmapoffset += bmdelta;
910 
911 		srcrow += rowdelta;
912 		dstrow += rowdelta;
913 	}
914 }
915 
916 /*
917  * Erase some rows.
918  */
919 
920 void
921 amidisplaycc_eraserows(void *screen, int row, int nrows, long attr)
922 {
923 	adccscr_t  * scr;
924 	u_char     * dst;
925 
926 	int  bmapoffset;
927 	int  fillsize;
928 	int  bgcolor;
929 	int  depth;
930 	int  plane;
931 	int  fill;
932 	int  j;
933 
934 	int  widthbytes;
935 	int  linebytes;
936 	int  rowbytes;
937 
938 
939 	scr = screen;
940 
941 	if (row < 0 || row + nrows > scr->nrows)
942 		return;
943 
944 	depth      = scr->depth;
945 	widthbytes = scr->widthbytes;
946 	linebytes  = scr->linebytes;
947 	rowbytes   = scr->rowbytes;
948 
949 	bmapoffset = row * rowbytes;
950 
951 	if (widthbytes == linebytes)
952 		fillsize = rowbytes * nrows;
953 	else
954 		fillsize = 0;
955 
956 	bgcolor = ATTRBG(attr);
957 	bgcolor = scr->colormap[bgcolor];
958 
959 	for (j = 0 ; j < nrows ; j++)
960 		scr->rowmasks[row+j] = bgcolor;
961 
962 	for (plane = 0 ; plane < depth ; plane++) {
963 		dst = scr->planes[plane] + bmapoffset;
964 		fill = (bgcolor & 1) ? 255 : 0;
965 
966 		if (fillsize > 0) {
967 			/* If the rows are continuous, write them all. */
968 			memset(dst, fill, fillsize);
969 		} else {
970 			for (j = 0 ; j < scr->fontheight * nrows ; j++) {
971 				memset(dst, fill, widthbytes);
972 				dst += linebytes;
973 			}
974 		}
975 		bgcolor >>= 1;
976 	}
977 }
978 
979 
980 /*
981  * Compose an attribute value from foreground color,
982  * background color, and flags.
983  */
984 int
985 amidisplaycc_allocattr(void *screen, int fg, int bg, int flags, long *attrp)
986 {
987 	adccscr_t  * scr;
988 	int          maxcolor;
989 	int          newfg;
990 	int          newbg;
991 
992 	scr = screen;
993 	maxcolor = (1 << scr->view->bitmap->depth) - 1;
994 
995 	/* Ensure the colors are displayable. */
996 	newfg = fg & maxcolor;
997 	newbg = bg & maxcolor;
998 
999 #ifdef ADJUSTCOLORS
1000 	/*
1001 	 * Hack for low-color screens, if background color is nonzero
1002 	 * but would be displayed as one, adjust it.
1003 	 */
1004 	if (bg > 0 && newbg == 0)
1005 		newbg = maxcolor;
1006 
1007 	/*
1008 	 * If foreground and background colors are different but would
1009 	 * display the same fix them by modifying the foreground.
1010 	 */
1011 	if (fg != bg && newfg == newbg) {
1012 		if (newbg > 0)
1013 			newfg = 0;
1014 		else
1015 			newfg = maxcolor;
1016 	}
1017 #endif
1018 	*attrp = MAKEATTR(newfg, newbg, flags);
1019 
1020 	return (0);
1021 }
1022 
1023 int
1024 amidisplaycc_ioctl(void *dp, void *vs, u_long cmd, caddr_t data, int flag,
1025 	struct lwp *l)
1026 {
1027 	struct amidisplaycc_softc *adp;
1028 
1029 	adp = dp;
1030 
1031 	if (adp == NULL) {
1032 		printf("amidisplaycc_ioctl: adp==NULL\n");
1033 		return (EINVAL);
1034 	}
1035 
1036 #define UINTDATA (*(u_int*)data)
1037 #define INTDATA (*(int*)data)
1038 #define FBINFO (*(struct wsdisplay_fbinfo*)data)
1039 
1040 	switch (cmd)
1041 	{
1042 	case WSDISPLAYIO_GTYPE:
1043 		UINTDATA = WSDISPLAY_TYPE_AMIGACC;
1044 		return (0);
1045 
1046 	case WSDISPLAYIO_SVIDEO:
1047 		dprintf("amidisplaycc: WSDISPLAYIO_SVIDEO %s\n",
1048 			UINTDATA ? "On" : "Off");
1049 
1050 		return (amidisplaycc_setvideo(adp, UINTDATA));
1051 
1052 	case WSDISPLAYIO_GVIDEO:
1053 		dprintf("amidisplaycc: WSDISPLAYIO_GVIDEO\n");
1054 		UINTDATA = adp->ison ?
1055 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
1056 
1057 		return (0);
1058 
1059 	case WSDISPLAYIO_SMODE:
1060 		if (INTDATA == WSDISPLAYIO_MODE_EMUL)
1061 			return amidisplaycc_gfxscreen(adp, 0);
1062 		if (INTDATA == WSDISPLAYIO_MODE_MAPPED)
1063 			return amidisplaycc_gfxscreen(adp, 1);
1064 		return (EINVAL);
1065 
1066 	case WSDISPLAYIO_GINFO:
1067 		FBINFO.width  = adp->gfxwidth;
1068 		FBINFO.height = adp->gfxheight;
1069 		FBINFO.depth  = adp->gfxdepth;
1070 		FBINFO.cmsize = 1 << FBINFO.depth;
1071 		return (0);
1072 
1073 	case WSDISPLAYIO_PUTCMAP:
1074 	case WSDISPLAYIO_GETCMAP:
1075 		return (amidisplaycc_cmapioctl(adp->gfxview,
1076 					       cmd,
1077 					       (struct wsdisplay_cmap*)data));
1078 	}
1079 
1080 	dprintf("amidisplaycc: unknown ioctl %lx (grp:'%c' num:%d)\n",
1081 		(long)cmd,
1082 		(char)((cmd&0xff00)>>8),
1083 		(int)(cmd&0xff));
1084 
1085 	return (EPASSTHROUGH);
1086 
1087 #undef UINTDATA
1088 #undef INTDATA
1089 #undef FBINFO
1090 }
1091 
1092 
1093 /*
1094  * Switch to either emulation (text) or mapped (graphics) mode
1095  * We keep an extra screen for mapped mode so it does not
1096  * interfere with emulation screens.
1097  *
1098  * Once the extra screen is created, it never goes away.
1099  */
1100 
1101 static int
1102 amidisplaycc_gfxscreen(struct amidisplaycc_softc *adp, int on)
1103 {
1104 	dimen_t  dimension;
1105 
1106 	dprintf("amidisplaycc: switching to %s mode.\n",
1107 		on ? "mapped" : "emul");
1108 
1109 	/* Current mode same as requested mode? */
1110 	if ( (on > 0) == (adp->gfxon > 0) )
1111 		return (0);
1112 
1113 	if (!on) {
1114 		/*
1115 		 * Switch away from mapped mode. If there is
1116 		 * a emulation screen, switch to it, otherwise
1117 		 * just try to hide the mapped screen.
1118 		 */
1119 		adp->gfxon = 0;
1120 		if (adp->currentscreen)
1121 			grf_display_view(adp->currentscreen->view);
1122 		else if (adp->gfxview)
1123 			grf_remove_view(adp->gfxview);
1124 
1125 		return (0);
1126 	}
1127 
1128 	/* switch to mapped mode then */
1129 
1130 	if (adp->gfxview == NULL) {
1131 		/* First time here, create the screen */
1132 
1133 		dimension.width = adp->gfxwidth;
1134 		dimension.height = adp->gfxheight;
1135 
1136 		dprintf("amidisplaycc: preparing mapped screen %dx%dx%d\n",
1137 			dimension.width,
1138 			dimension.height,
1139 			adp->gfxdepth);
1140 
1141 		adp->gfxview = grf_alloc_view(NULL,
1142 					      &dimension,
1143 					      adp->gfxdepth);
1144 	}
1145 
1146 	if (adp->gfxview) {
1147 		adp->gfxon = 1;
1148 
1149 		grf_display_view(adp->gfxview);
1150 	} else {
1151 		printf("amidisplaycc: failed to make mapped screen\n");
1152 		return (ENOMEM);
1153 	}
1154 	return (0);
1155 }
1156 
1157 /*
1158  * Map the graphics screen. It must have been created before
1159  * by switching to mapped mode by using an ioctl.
1160  */
1161 paddr_t
1162 amidisplaycc_mmap(void *dp, void *vs, off_t off, int prot)
1163 {
1164 	struct amidisplaycc_softc  * adp;
1165 	bmap_t                     * bm;
1166 	paddr_t                      rv;
1167 
1168 	adp = (struct amidisplaycc_softc*)dp;
1169 
1170 	/* Check we are in mapped mode */
1171 	if (adp->gfxon == 0 || adp->gfxview == NULL) {
1172 		dprintf("amidisplaycc_mmap: Not in mapped mode\n");
1173 		return (paddr_t)(-1);
1174 	}
1175 
1176 	/*
1177 	 * As we all know by now, we are mapping our special
1178 	 * screen here so our pretty text consoles are left
1179 	 * untouched.
1180 	 */
1181 
1182 	bm = adp->gfxview->bitmap;
1183 
1184 	/* Check that the offset is valid */
1185 	if (off < 0 || off >= bm->depth * bm->bytes_per_row * bm->rows) {
1186 		dprintf("amidisplaycc_mmap: Offset out of range\n");
1187 		return (paddr_t)(-1);
1188 	}
1189 
1190 	rv = (paddr_t)bm->hardware_address;
1191 	rv += off;
1192 
1193 	return (rv >> PGSHIFT);
1194 }
1195 
1196 
1197 /*
1198  * Create a new screen.
1199  * NULL dp signifies console and then memory is allocated statically
1200  * and the screen is automatically displayed.
1201  *
1202  * A font with suitable size is searched and if not found
1203  * the builtin 8x8 font is used.
1204  *
1205  * There are separate default palettes for 2, 4 and 8+ color
1206  * screens.
1207  */
1208 
1209 int
1210 amidisplaycc_alloc_screen(void *dp, const struct wsscreen_descr *screenp,
1211 			  void  **cookiep, int *curxp, int *curyp,
1212 			  long *defattrp)
1213 {
1214 	const struct amidisplaycc_screen_descr  * adccscreenp;
1215 	struct amidisplaycc_screen              * scr;
1216 	struct amidisplaycc_softc               * adp;
1217 	view_t                                  * view;
1218 
1219 	dimen_t  dimension;
1220 	int      fontheight;
1221 	int      fontwidth;
1222 	int      maxcolor;
1223 	int      depth;
1224 	int      i;
1225 	int      j;
1226 
1227 	adccscreenp = (const struct amidisplaycc_screen_descr *)screenp;
1228 	depth = adccscreenp->depth;
1229 
1230 	adp = dp;
1231 
1232 	maxcolor = (1 << depth) - 1;
1233 
1234 	/* Sanity checks because of fixed buffers */
1235 	if (depth > MAXDEPTH || maxcolor >= MAXCOLORS)
1236 		return (ENOMEM);
1237 	if (screenp->nrows > MAXROWS)
1238 		return (ENOMEM);
1239 
1240 	fontwidth = screenp->fontwidth;
1241 	fontheight = screenp->fontheight;
1242 
1243 	/*
1244 	 * The screen size is defined in characters.
1245 	 * Calculate the pixel size using the font size.
1246 	 */
1247 
1248 	dimension.width = screenp->ncols * fontwidth;
1249 	dimension.height = screenp->nrows * fontheight;
1250 
1251 	view = grf_alloc_view(NULL, &dimension, depth);
1252 	if (view == NULL)
1253 		return (ENOMEM);
1254 
1255 	/*
1256 	 * First screen gets the statically allocated console screen.
1257 	 * Others are allocated dynamically.
1258 	 */
1259 	if (adp == NULL) {
1260 		scr = &amidisplaycc_consolescreen;
1261 		if (scr->isconsole)
1262 			panic("more than one console?");
1263 
1264 		scr->isconsole = 1;
1265 	} else {
1266 		scr = malloc(sizeof(adccscr_t), M_DEVBUF, M_WAITOK);
1267 		bzero(scr, sizeof(adccscr_t));
1268 	}
1269 
1270 	scr->view = view;
1271 
1272 	scr->ncols = screenp->ncols;
1273 	scr->nrows = screenp->nrows;
1274 
1275 	/* Copies of most used values */
1276 	scr->width  = dimension.width;
1277 	scr->height = dimension.height;
1278 	scr->depth  = depth;
1279 	scr->widthbytes = view->bitmap->bytes_per_row;
1280 	scr->linebytes  = scr->widthbytes + view->bitmap->row_mod;
1281 	scr->rowbytes   = scr->linebytes * fontheight;
1282 
1283 	scr->device = adp;
1284 
1285 
1286 	/*
1287 	 * Try to find a suitable font.
1288 	 * Avoid everything but the builtin font for console screen.
1289 	 * Builtin font is used if no other is found, even if it
1290 	 * has the wrong size.
1291 	 */
1292 
1293 	KASSERT(fontwidth == 8);
1294 
1295 	scr->wsfont       = NULL;
1296 	scr->wsfontcookie = -1;
1297 	scr->fontwidth    = fontwidth;
1298 	scr->fontheight   = fontheight;
1299 
1300 	if (adp)
1301 		amidisplaycc_setfont(scr, NULL);
1302 
1303 	if (scr->wsfont == NULL)
1304 	{
1305 		scr->wsfont = amidisplaycc_getbuiltinfont();
1306 		scr->wsfontcookie = -1;
1307 	}
1308 
1309 	KASSERT(scr->wsfont);
1310 	KASSERT(scr->wsfont->stride == 1);
1311 
1312 	for (i = 0 ; i < depth ; i++) {
1313 		scr->planes[i] = view->bitmap->plane[i];
1314 	}
1315 
1316 	for (i = 0 ; i < MAXROWS ; i++)
1317 		scr->rowmasks[i] = 0;
1318 
1319 	/* Simple one-to-one mapping for most colors */
1320 	for (i = 0 ; i < MAXCOLORS ; i++)
1321 		scr->colormap[i] = i;
1322 
1323 	/*
1324 	 * Arrange the most used pens to quickest colors.
1325 	 * The default color for given depth is (1<<depth)-1.
1326 	 * It is assumed it is used most and it is mapped to
1327 	 * color that can be drawn by writing data to one bitplane
1328 	 * only.
1329 	 * So map colors 3->2, 7->4, 15->8 and so on.
1330 	 */
1331 	for (i = 2 ; i < MAXCOLORS ; i *= 2) {
1332 		j = i * 2 - 1;
1333 
1334 		if (j < MAXCOLORS) {
1335 			scr->colormap[i] = j;
1336 			scr->colormap[j] = i;
1337 		}
1338 	}
1339 
1340 	/*
1341 	 * Set the default colormap.
1342 	 */
1343 	if (depth == 1)
1344 		amidisplaycc_setemulcmap(scr, &pal2);
1345 	else if (depth == 2)
1346 		amidisplaycc_setemulcmap(scr, &pal4);
1347 	else
1348 		amidisplaycc_setemulcmap(scr, &pal8);
1349 
1350 	*cookiep = scr;
1351 
1352 	/* cursor initially at top left */
1353 	scr->cursorrow = -1;
1354 	scr->cursorcol = -1;
1355 	*curxp = 0;
1356 	*curyp = 0;
1357 	amidisplaycc_cursor(scr, 1, *curxp, *curyp);
1358 
1359 	*defattrp = MAKEATTR(maxcolor, 0, 0);
1360 
1361 	/* Show the console automatically */
1362 	if (adp == NULL)
1363 		grf_display_view(scr->view);
1364 
1365 	if (adp) {
1366 		dprintf("amidisplaycc: allocated screen; %dx%dx%d; font=%s\n",
1367 			dimension.width,
1368 			dimension.height,
1369 			depth,
1370 			scr->wsfont->name);
1371 	}
1372 
1373 	return (0);
1374 }
1375 
1376 
1377 /*
1378  * Destroy a screen.
1379  */
1380 
1381 void
1382 amidisplaycc_free_screen(void *dp, void *screen)
1383 {
1384 	struct amidisplaycc_screen  * scr;
1385 	struct amidisplaycc_softc   * adp;
1386 
1387 	scr = screen;
1388 	adp = (struct amidisplaycc_softc*)dp;
1389 
1390 	if (scr == NULL)
1391 		return;
1392 
1393 	/* Free the used font */
1394 	if (scr->wsfont && scr->wsfontcookie != -1)
1395 		wsfont_unlock(scr->wsfontcookie);
1396 	scr->wsfont = NULL;
1397 	scr->wsfontcookie = -1;
1398 
1399 	if (adp->currentscreen == scr)
1400 		adp->currentscreen = NULL;
1401 
1402 	if (scr->view)
1403 		grf_free_view(scr->view);
1404 	scr->view = NULL;
1405 
1406 	/* Take care not to free the statically allocated console screen */
1407 	if (scr != &amidisplaycc_consolescreen) {
1408 		free(scr, M_DEVBUF);
1409 	}
1410 }
1411 
1412 /*
1413  * Switch to another vt. Switch is always made immediately.
1414  */
1415 
1416 /* ARGSUSED2 */
1417 int
1418 amidisplaycc_show_screen(void *dp, void *screen, int waitok,
1419 			 void (*cb) (void *, int, int), void *cbarg)
1420 {
1421 	adccscr_t *scr;
1422 	struct amidisplaycc_softc *adp;
1423 
1424 	adp = (struct amidisplaycc_softc*)dp;
1425 	scr = screen;
1426 
1427 	if (adp == NULL) {
1428 		dprintf("amidisplaycc_show_screen: adp==NULL\n");
1429 		return (EINVAL);
1430 	}
1431 	if (scr == NULL) {
1432 		dprintf("amidisplaycc_show_screen: scr==NULL\n");
1433 		return (EINVAL);
1434 	}
1435 
1436 	if (adp->gfxon) {
1437 		dprintf("amidisplaycc: Screen shift while in gfx mode?");
1438 		adp->gfxon = 0;
1439 	}
1440 
1441 	adp->currentscreen = scr;
1442 	adp->ison = 1;
1443 
1444 	grf_display_view(scr->view);
1445 
1446 	return (0);
1447 }
1448 
1449 /*
1450  * Load/set a font.
1451  *
1452  * Only setting is supported, as the wsfont pseudo-device can
1453  * handle the loading of fonts for us.
1454  */
1455 int
1456 amidisplaycc_load_font(void *dp, void *cookie, struct wsdisplay_font *font)
1457 {
1458 	struct amidisplaycc_softc   * adp;
1459 	struct amidisplaycc_screen  * scr;
1460 
1461 	adp = dp;
1462 	scr = cookie;
1463 
1464 	KASSERT(adp);
1465 	KASSERT(scr);
1466 	KASSERT(font);
1467 	KASSERT(font->name);
1468 
1469 	if (font->data)
1470 	{
1471 		/* request to load the font, not supported */
1472 		return (EINVAL);
1473 	}
1474 	else
1475 	{
1476 		/* request to use the given font on this screen */
1477 		return amidisplaycc_setfont(scr, font->name);
1478 	}
1479 }
1480 
1481 /*
1482  * Set display on/off.
1483  */
1484 static int
1485 amidisplaycc_setvideo(struct amidisplaycc_softc *adp, int mode)
1486 {
1487         view_t * view;
1488 
1489 	if (adp == NULL) {
1490 		dprintf("amidisplaycc_setvideo: adp==NULL\n");
1491 		return (EINVAL);
1492 	}
1493 	if (adp->currentscreen == NULL) {
1494 		dprintf("amidisplaycc_setvideo: adp->currentscreen==NULL\n");
1495 		return (EINVAL);
1496 	}
1497 
1498 	/* select graphics or emulation screen */
1499 	if (adp->gfxon && adp->gfxview)
1500 		view = adp->gfxview;
1501 	else
1502 		view = adp->currentscreen->view;
1503 
1504 	if (mode) {
1505 		/* on */
1506 
1507 		grf_display_view(view);
1508 		dprintf("amidisplaycc: video is now on\n");
1509 		adp->ison = 1;
1510 
1511 	} else {
1512 		/* off */
1513 
1514 		grf_remove_view(view);
1515 		dprintf("amidisplaycc: video is now off\n");
1516 		adp->ison = 0;
1517 	}
1518 
1519 	return (0);
1520 }
1521 
1522 /*
1523  * Handle the WSDISPLAY_[PUT/GET]CMAP ioctls.
1524  * Just handle the copying of data to/from userspace and
1525  * let the functions amidisplaycc_setcmap and amidisplaycc_putcmap
1526  * do the real work.
1527  */
1528 
1529 static int
1530 amidisplaycc_cmapioctl(view_t *view, u_long cmd, struct wsdisplay_cmap *cmap)
1531 {
1532 	struct wsdisplay_cmap  tmpcmap;
1533 	u_char                 cmred[MAXCOLORS];
1534 	u_char                 cmgrn[MAXCOLORS];
1535 	u_char                 cmblu[MAXCOLORS];
1536 
1537 	int                    err;
1538 
1539 	if (cmap->index >= MAXCOLORS ||
1540 	    cmap->count > MAXCOLORS ||
1541 	    cmap->index + cmap->count > MAXCOLORS)
1542 		return (EINVAL);
1543 
1544 	if (cmap->count == 0)
1545 		return (0);
1546 
1547 	tmpcmap.index = cmap->index;
1548 	tmpcmap.count = cmap->count;
1549 	tmpcmap.red   = cmred;
1550 	tmpcmap.green = cmgrn;
1551 	tmpcmap.blue  = cmblu;
1552 
1553 	if (cmd == WSDISPLAYIO_PUTCMAP) {
1554 		/* copy the color data to kernel space */
1555 
1556 		err = copyin(cmap->red, cmred, cmap->count);
1557 		if (err)
1558 			return (err);
1559 
1560 		err = copyin(cmap->green, cmgrn, cmap->count);
1561 		if (err)
1562 			return (err);
1563 
1564 		err = copyin(cmap->blue, cmblu, cmap->count);
1565 		if (err)
1566 			return (err);
1567 
1568 		return amidisplaycc_setcmap(view, &tmpcmap);
1569 
1570 	} else if (cmd == WSDISPLAYIO_GETCMAP) {
1571 
1572 		err = amidisplaycc_getcmap(view, &tmpcmap);
1573 		if (err)
1574 			return (err);
1575 
1576 		/* copy data to user space */
1577 
1578 		err = copyout(cmred, cmap->red, cmap->count);
1579 		if (err)
1580 			return (err);
1581 
1582 		err = copyout(cmgrn, cmap->green, cmap->count);
1583 		if (err)
1584 			return (err);
1585 
1586 		err = copyout(cmblu, cmap->blue, cmap->count);
1587 		if (err)
1588 			return (err);
1589 
1590 		return (0);
1591 
1592 	} else
1593 		return (EPASSTHROUGH);
1594 }
1595 
1596 /*
1597  * Set the palette of a emulation screen.
1598  * Here we do only color remapping and then call
1599  * amidisplaycc_setcmap to do the work.
1600  */
1601 
1602 static int
1603 amidisplaycc_setemulcmap(struct amidisplaycc_screen *scr,
1604 			 struct wsdisplay_cmap *cmap)
1605 {
1606 	struct wsdisplay_cmap  tmpcmap;
1607 
1608 	u_char                 red [MAXCOLORS];
1609 	u_char                 grn [MAXCOLORS];
1610 	u_char                 blu [MAXCOLORS];
1611 
1612 	int                    rc;
1613 	int                    i;
1614 
1615 	/*
1616 	 * Get old palette first.
1617 	 * Because of the color mapping going on in the emulation
1618 	 * screen the color range may not be contiguous in the real
1619 	 * palette.
1620 	 * So get the whole palette, insert the new colors
1621 	 * at the appropriate places and then set the whole
1622 	 * palette back.
1623 	 */
1624 
1625 	tmpcmap.index = 0;
1626 	tmpcmap.count = 1 << scr->depth;
1627 	tmpcmap.red   = red;
1628 	tmpcmap.green = grn;
1629 	tmpcmap.blue  = blu;
1630 
1631 	rc = amidisplaycc_getcmap(scr->view, &tmpcmap);
1632 	if (rc)
1633 		return (rc);
1634 
1635 	for (i = cmap->index ; i < cmap->index + cmap->count ; i++) {
1636 
1637 		tmpcmap.red   [ scr->colormap[ i ] ] = cmap->red   [ i ];
1638 		tmpcmap.green [ scr->colormap[ i ] ] = cmap->green [ i ];
1639 		tmpcmap.blue  [ scr->colormap[ i ] ] = cmap->blue  [ i ];
1640 	}
1641 
1642 	rc = amidisplaycc_setcmap(scr->view, &tmpcmap);
1643 	if (rc)
1644 		return (rc);
1645 
1646 	return (0);
1647 }
1648 
1649 
1650 /*
1651  * Set the colormap for the given screen.
1652  */
1653 
1654 static int
1655 amidisplaycc_setcmap(view_t *view, struct wsdisplay_cmap *cmap)
1656 {
1657 	u_long      cmentries [MAXCOLORS];
1658 
1659 	u_int       colors;
1660 	int         index;
1661 	int         count;
1662 	int         err;
1663 	colormap_t  cm;
1664 
1665 	if (view == NULL)
1666 		return (EINVAL);
1667 
1668 	if (!cmap || !cmap->red || !cmap->green || !cmap->blue) {
1669 		dprintf("amidisplaycc_setcmap: other==NULL\n");
1670 		return (EINVAL);
1671 	}
1672 
1673 	index  = cmap->index;
1674 	count  = cmap->count;
1675 	colors = (1 << view->bitmap->depth);
1676 
1677 	if (count > colors || index >= colors || index + count > colors)
1678 		return (EINVAL);
1679 
1680 	if (count == 0)
1681 		return (0);
1682 
1683 	cm.entry = cmentries;
1684 	cm.first = index;
1685 	cm.size  = count;
1686 
1687 	/*
1688 	 * Get the old colormap. We need to do this at least to know
1689 	 * how many bits to use with the color values.
1690 	 */
1691 
1692 	err = grf_get_colormap(view, &cm);
1693 	if (err)
1694 		return (err);
1695 
1696 	/*
1697 	 * The palette entries from wscons contain 8 bits per gun.
1698 	 * We need to convert them to the number of bits the view
1699 	 * expects. That is typically 4 or 8. Here we calculate the
1700 	 * conversion constants with which we divide the color values.
1701 	 */
1702 
1703 	if (cm.type == CM_COLOR) {
1704 		int c, green_div, blue_div, red_div;
1705 
1706 		red_div = 256 / (cm.red_mask + 1);
1707 		green_div = 256 / (cm.green_mask + 1);
1708 		blue_div = 256 / (cm.blue_mask + 1);
1709 
1710 		for (c = 0 ; c < count ; c++)
1711 			cm.entry[c + index] = MAKE_COLOR_ENTRY(
1712 				cmap->red[c] / red_div,
1713 				cmap->green[c] / green_div,
1714 				cmap->blue[c] / blue_div);
1715 
1716 	} else if (cm.type == CM_GREYSCALE) {
1717 		int c, grey_div;
1718 
1719 		grey_div = 256 / (cm.grey_mask + 1);
1720 
1721 		/* Generate grey from average of r-g-b (?) */
1722 		for (c = 0 ; c < count ; c++)
1723 			cm.entry[c + index] = MAKE_COLOR_ENTRY(
1724 				0,
1725 				0,
1726 				(cmap->red[c] +
1727 				 cmap->green[c] +
1728 				 cmap->blue[c]) / 3 / grey_div);
1729 	} else
1730 		return (EINVAL); /* Hmhh */
1731 
1732 	/*
1733 	 * Now we have a new colormap that contains all the entries. Set
1734 	 * it to the view.
1735 	 */
1736 
1737 	err = grf_use_colormap(view, &cm);
1738 	if (err)
1739 		return err;
1740 
1741 	return (0);
1742 }
1743 
1744 /*
1745  * Return the colormap of the given screen.
1746  */
1747 
1748 static int
1749 amidisplaycc_getcmap(view_t *view, struct wsdisplay_cmap *cmap)
1750 {
1751 	u_long      cmentries [MAXCOLORS];
1752 
1753 	u_int       colors;
1754 	int         index;
1755 	int         count;
1756 	int         err;
1757 	colormap_t  cm;
1758 
1759 	if (view == NULL)
1760 		return (EINVAL);
1761 
1762 	if (!cmap || !cmap->red || !cmap->green || !cmap->blue)
1763 		return (EINVAL);
1764 
1765 	index  = cmap->index;
1766 	count  = cmap->count;
1767 	colors = (1 << view->bitmap->depth);
1768 
1769 	if (count > colors || index >= colors || index + count > colors)
1770 		return (EINVAL);
1771 
1772 	if (count == 0)
1773 		return (0);
1774 
1775 	cm.entry = cmentries;
1776 	cm.first = index;
1777 	cm.size  = count;
1778 
1779 	err = grf_get_colormap(view, &cm);
1780 	if (err)
1781 		return (err);
1782 
1783 	/*
1784 	 * Copy color data to wscons-style structure. Translate to
1785 	 * 8 bits/gun from whatever resolution the color natively is.
1786 	 */
1787 	if (cm.type == CM_COLOR) {
1788 		int c, red_mul, green_mul, blue_mul;
1789 
1790 		red_mul   = 256 / (cm.red_mask + 1);
1791 		green_mul = 256 / (cm.green_mask + 1);
1792 		blue_mul  = 256 / (cm.blue_mask + 1);
1793 
1794 		for (c = 0 ; c < count ; c++) {
1795 			cmap->red[c] = red_mul *
1796 				CM_GET_RED(cm.entry[index+c]);
1797 			cmap->green[c] = green_mul *
1798 				CM_GET_GREEN(cm.entry[index+c]);
1799 			cmap->blue[c] = blue_mul *
1800 				CM_GET_BLUE(cm.entry[index+c]);
1801 		}
1802 	} else if (cm.type == CM_GREYSCALE) {
1803 		int c, grey_mul;
1804 
1805 		grey_mul = 256 / (cm.grey_mask + 1);
1806 
1807 		for (c = 0 ; c < count ; c++) {
1808 			cmap->red[c] = grey_mul *
1809 				CM_GET_GREY(cm.entry[index+c]);
1810 			cmap->green[c] = grey_mul *
1811 				CM_GET_GREY(cm.entry[index+c]);
1812 			cmap->blue[c] = grey_mul *
1813 				CM_GET_GREY(cm.entry[index+c]);
1814 		}
1815 	} else
1816 		return (EINVAL);
1817 
1818 	return (0);
1819 }
1820 
1821 /*
1822  * Find and set a font for the given screen.
1823  *
1824  * If fontname is given, a font with that name and suitable
1825  * size (determined by the screen) is searched for.
1826  * If fontname is NULL, a font with suitable size is searched.
1827  *
1828  * On success, the found font is assigned to the screen and possible
1829  * old font is freed.
1830  */
1831 static int
1832 amidisplaycc_setfont(struct amidisplaycc_screen *scr, const char *fontname)
1833 {
1834 	struct wsdisplay_font *wsfont;
1835 	int wsfontcookie;
1836 
1837 	KASSERT(scr);
1838 
1839 	wsfontcookie = wsfont_find(fontname,
1840 		scr->fontwidth,
1841 		scr->fontheight,
1842 		1,
1843 		WSDISPLAY_FONTORDER_L2R,
1844 		WSDISPLAY_FONTORDER_L2R);
1845 
1846 	if (wsfontcookie == -1)
1847 		return (EINVAL);
1848 
1849 	/* Suitable font found. Now lock it. */
1850 	if (wsfont_lock(wsfontcookie, &wsfont))
1851 		return (EINVAL);
1852 
1853 	KASSERT(wsfont);
1854 
1855 	if (scr->wsfont && scr->wsfontcookie != -1)
1856 		wsfont_unlock(scr->wsfontcookie);
1857 
1858 	scr->wsfont = wsfont;
1859 	scr->wsfontcookie = wsfontcookie;
1860 
1861 	return (0);
1862 }
1863 
1864 /*
1865  * Return a font that is guaranteed to exist.
1866  */
1867 static const struct wsdisplay_font *
1868 amidisplaycc_getbuiltinfont(void)
1869 {
1870 	static struct wsdisplay_font font;
1871 
1872 	extern unsigned char kernel_font_width_8x8;
1873 	extern unsigned char kernel_font_height_8x8;
1874 	extern unsigned char kernel_font_lo_8x8;
1875 	extern unsigned char kernel_font_hi_8x8;
1876 	extern unsigned char kernel_font_8x8[];
1877 
1878 	font.name = "kf8x8";
1879 	font.firstchar = kernel_font_lo_8x8;
1880 	font.numchars = kernel_font_hi_8x8 - kernel_font_lo_8x8 + 1;
1881 	font.fontwidth = kernel_font_width_8x8;
1882 	font.stride = 1;
1883 	font.fontheight = kernel_font_height_8x8;
1884 	font.data = kernel_font_8x8;
1885 
1886 	/* these values aren't really used for anything */
1887 	font.encoding = WSDISPLAY_FONTENC_ISO;
1888 	font.bitorder = WSDISPLAY_FONTORDER_KNOWN;
1889 	font.byteorder = WSDISPLAY_FONTORDER_KNOWN;
1890 
1891 	return &font;
1892 }
1893 
1894 /* ARGSUSED */
1895 void
1896 amidisplaycc_pollc(void *cookie, int on)
1897 {
1898 }
1899 
1900 /*
1901  * These dummy functions are here just so that we can compete of
1902  * the console at init.
1903  * If we win the console then the wscons system will provide the
1904  * real ones which in turn will call the apropriate wskbd device.
1905  * These should never be called.
1906  */
1907 
1908 /* ARGSUSED */
1909 void
1910 amidisplaycc_cnputc(dev_t cd, int ch)
1911 {
1912 }
1913 
1914 /* ARGSUSED */
1915 int
1916 amidisplaycc_cngetc(dev_t cd)
1917 {
1918 	return (0);
1919 }
1920 
1921 /* ARGSUSED */
1922 void
1923 amidisplaycc_cnpollc(dev_t cd, int on)
1924 {
1925 }
1926 
1927 
1928 /*
1929  * Prints stuff if DEBUG is turned on.
1930  */
1931 
1932 /* ARGSUSED */
1933 static void
1934 dprintf(const char *fmt, ...)
1935 {
1936 #ifdef DEBUG
1937 	va_list ap;
1938 
1939 	va_start(ap, fmt);
1940 	vprintf(fmt, ap);
1941 	va_end(ap);
1942 #endif
1943 }
1944 
1945 #endif /* AMIDISPLAYCC */
1946