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