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