xref: /netbsd-src/sys/arch/amiga/dev/amidisplaycc.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: amidisplaycc.c,v 1.38 2021/08/21 23:00:31 andvar 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.38 2021/08/21 23:00:31 andvar 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_setgfxview(struct amidisplaycc_softc *, int);
120 static void amidisplaycc_initgfxview(struct amidisplaycc_softc *);
121 static int amidisplaycc_getfbinfo(struct amidisplaycc_softc *, struct wsdisplayio_fbinfo *);
122 
123 static int amidisplaycc_setfont(struct amidisplaycc_screen *, const char *);
124 static const struct wsdisplay_font * amidisplaycc_getbuiltinfont(void);
125 
126 static void dprintf(const char *fmt, ...);
127 
128 /* end of private utility functions */
129 
130 /* emulops for wscons */
131 void amidisplaycc_cursor(void *, int, int, int);
132 int amidisplaycc_mapchar(void *, int, unsigned int *);
133 void amidisplaycc_putchar(void *, int, int, u_int, long);
134 void amidisplaycc_copycols(void *, int, int, int, int);
135 void amidisplaycc_erasecols(void *, int, int, int, long);
136 void amidisplaycc_copyrows(void *, int, int, int);
137 void amidisplaycc_eraserows(void *, int, int, long);
138 int  amidisplaycc_allocattr(void *, int, int, int, long *);
139 /* end of emulops for wscons */
140 
141 
142 /* accessops for wscons */
143 int amidisplaycc_ioctl(void *, void *, u_long, void *, int, struct lwp *);
144 paddr_t amidisplaycc_mmap(void *, void *, off_t, int);
145 int amidisplaycc_alloc_screen(void *, const struct wsscreen_descr *, void **,
146 			      int *, int *, long *);
147 void amidisplaycc_free_screen( void *, void *);
148 int amidisplaycc_show_screen(void *, void *, int, void (*)(void *, int, int),
149 			     void *);
150 int amidisplaycc_load_font(void *, void *, struct wsdisplay_font *);
151 void amidisplaycc_pollc(void *, int);
152 /* end of accessops for wscons */
153 
154 /*
155  * These structures are passed to wscons, and they contain the
156  * display-specific callback functions.
157  */
158 
159 const struct wsdisplay_accessops amidisplaycc_accessops = {
160 	amidisplaycc_ioctl,
161 	amidisplaycc_mmap,
162 	amidisplaycc_alloc_screen,
163 	amidisplaycc_free_screen,
164 	amidisplaycc_show_screen,
165 	amidisplaycc_load_font,
166 	amidisplaycc_pollc
167 };
168 
169 const struct wsdisplay_emulops amidisplaycc_emulops = {
170 	amidisplaycc_cursor,
171 	amidisplaycc_mapchar,
172 	amidisplaycc_putchar,
173 	amidisplaycc_copycols,
174 	amidisplaycc_erasecols,
175 	amidisplaycc_copyrows,
176 	amidisplaycc_eraserows,
177 	amidisplaycc_allocattr
178 };
179 
180 /* Add some of our own data to the wsscreen_descr */
181 struct amidisplaycc_screen_descr {
182 	struct wsscreen_descr  wsdescr;
183 	int                    depth;
184 };
185 
186 #define ADCC_SCREEN(name, width, height, depth, fontwidth, fontheight) \
187     /* CONSTCOND */ \
188     {{ \
189     name, \
190     width / fontwidth, \
191     height / fontheight, \
192     &amidisplaycc_emulops, fontwidth, fontheight, \
193     (depth > 1 ? WSSCREEN_WSCOLORS : 0) | WSSCREEN_REVERSE | \
194     WSSCREEN_HILIT | WSSCREEN_UNDERLINE }, \
195     depth }
196 
197 /*
198  * List of supported screen types.
199  *
200  * The first item in list is used for the console screen.
201  * A suitable screen size is guessed for it by looking
202  * at the GRF_* options.
203  */
204 struct amidisplaycc_screen_descr amidisplaycc_screentab[] = {
205 	/* name, width, height, depth, fontwidth==8, fontheight */
206 
207 #if defined(GRF_PAL) && !defined(GRF_NTSC)
208 	ADCC_SCREEN("default", 640, 512, 3, 8, 8),
209 #else
210 	ADCC_SCREEN("default", 640, 400, 3, 8, 8),
211 #endif
212 	ADCC_SCREEN("80x50", 640, 400, 3, 8, 8),
213 	ADCC_SCREEN("80x40", 640, 400, 3, 8, 10),
214 	ADCC_SCREEN("80x25", 640, 400, 3, 8, 16),
215 	ADCC_SCREEN("80x24", 640, 192, 3, 8, 8),
216 
217 	ADCC_SCREEN("80x64", 640, 512, 3, 8, 8),
218 	ADCC_SCREEN("80x51", 640, 510, 3, 8, 10),
219 	ADCC_SCREEN("80x32", 640, 512, 3, 8, 16),
220 	ADCC_SCREEN("80x31", 640, 248, 3, 8, 8),
221 
222 	ADCC_SCREEN("640x400x1", 640, 400, 1, 8, 8),
223 	ADCC_SCREEN("640x400x2", 640, 400, 2, 8, 8),
224 	ADCC_SCREEN("640x400x3", 640, 400, 3, 8, 8),
225 
226 	ADCC_SCREEN("640x200x1", 640, 200, 1, 8, 8),
227 	ADCC_SCREEN("640x200x2", 640, 200, 2, 8, 8),
228 	ADCC_SCREEN("640x200x3", 640, 200, 3, 8, 8),
229 };
230 
231 #define ADCC_SCREENPTR(index) &amidisplaycc_screentab[index].wsdescr
232 const struct wsscreen_descr *amidisplaycc_screens[] = {
233 	ADCC_SCREENPTR(0),
234 	ADCC_SCREENPTR(1),
235 	ADCC_SCREENPTR(2),
236 	ADCC_SCREENPTR(3),
237 	ADCC_SCREENPTR(4),
238 	ADCC_SCREENPTR(5),
239 	ADCC_SCREENPTR(6),
240 	ADCC_SCREENPTR(7),
241 	ADCC_SCREENPTR(8),
242 	ADCC_SCREENPTR(9),
243 	ADCC_SCREENPTR(10),
244 	ADCC_SCREENPTR(11),
245 	ADCC_SCREENPTR(12),
246 	ADCC_SCREENPTR(13),
247 	ADCC_SCREENPTR(14),
248 };
249 
250 #define NELEMS(arr) (sizeof(arr)/sizeof((arr)[0]))
251 
252 /*
253  * This structure is passed to wscons. It contains pointers
254  * to the available display modes.
255  */
256 
257 const struct wsscreen_list amidisplaycc_screenlist = {
258 	sizeof(amidisplaycc_screens)/sizeof(amidisplaycc_screens[0]),
259 	amidisplaycc_screens
260 };
261 
262 /*
263  * Our own screen structure. One will be created for each screen.
264  */
265 
266 struct amidisplaycc_screen
267 {
268 	struct amidisplaycc_softc *device;
269 
270 	int       isconsole;
271 	int       isvisible;
272 	view_t  * view;
273 
274 	int       ncols;
275 	int       nrows;
276 
277 	int       cursorrow;
278 	int       cursorcol;
279 
280 	/* Active bitplanes for each character row. */
281 	int       rowmasks[MAXROWS];
282 
283 	/* Mapping of colors to screen colors. */
284 	int       colormap[MAXCOLORS];
285 
286 	/* Copies of display parameters for convenience */
287 	int       width;
288 	int       height;
289 	int       depth;
290 
291 	int       widthbytes; /* bytes_per_row           */
292 	int       linebytes;  /* widthbytes + row_mod    */
293 	int       rowbytes;   /* linebytes * fontheight  */
294 
295 	u_char  * planes[MAXDEPTH];
296 
297 	const struct wsdisplay_font  * wsfont;
298 	int                      wsfontcookie; /* if -1, builtin font */
299 	int                      fontwidth;
300 	int                      fontheight;
301 };
302 
303 typedef struct amidisplaycc_screen adccscr_t;
304 
305 /*
306  * Need one statically allocated screen for early init.
307  * The rest are mallocated when needed.
308  */
309 adccscr_t amidisplaycc_consolescreen;
310 
311 /*
312  * Default palettes for 2, 4 and 8 color emulation displays.
313  */
314 
315 /* black, grey */
316 static u_char pal2red[] = { 0x00, 0xaa };
317 static u_char pal2grn[] = { 0x00, 0xaa };
318 static u_char pal2blu[] = { 0x00, 0xaa };
319 
320 /* black, red, green, grey */
321 static u_char pal4red[] = { 0x00, 0xaa, 0x00, 0xaa };
322 static u_char pal4grn[] = { 0x00, 0x00, 0xaa, 0xaa };
323 static u_char pal4blu[] = { 0x00, 0x00, 0x00, 0xaa };
324 
325 /* black, red, green, brown, blue, magenta, cyan, grey */
326 static u_char pal8red[] = { 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa};
327 static u_char pal8grn[] = { 0x00, 0x00, 0xaa, 0xaa, 0x00, 0x00, 0xaa, 0xaa};
328 static u_char pal8blu[] = { 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa};
329 
330 static struct wsdisplay_cmap pal2 = { 0, 2, pal2red, pal2grn, pal2blu };
331 static struct wsdisplay_cmap pal4 = { 0, 4, pal4red, pal4grn, pal4blu };
332 static struct wsdisplay_cmap pal8 = { 0, 8, pal8red, pal8grn, pal8blu };
333 
334 #ifdef GRF_AGA
335 extern int aga_enable;
336 #else
337 static int aga_enable = 0;
338 #endif
339 
340 /*
341  * This gets called at console init to determine the priority of
342  * this console device.
343  *
344  * Pointers to this and other functions must present
345  * in constab[] in conf.c for this to work.
346  */
347 void
348 amidisplaycc_cnprobe(struct consdev *cd)
349 {
350 	cd->cn_pri = CN_INTERNAL;
351 
352 	/*
353 	 * Yeah, real nice. But if we win the console then the wscons system
354 	 * does the proper initialization.
355 	 */
356 	cd->cn_dev = NODEV;
357 }
358 
359 /*
360  * This gets called if this device is used as the console.
361  */
362 void
363 amidisplaycc_cninit(struct consdev  * cd)
364 {
365 	void  * cookie;
366 	long    attr;
367 	int     x;
368 	int     y;
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, CFARGS_NONE);
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 		switch (INTDATA) {
1045 		case WSDISPLAYIO_MODE_EMUL:
1046 			return amidisplaycc_setgfxview(adp, 0);
1047 		case WSDISPLAYIO_MODE_MAPPED:
1048 		case WSDISPLAYIO_MODE_DUMBFB:
1049 			return amidisplaycc_setgfxview(adp, 1);
1050 		default:
1051 			return EINVAL;
1052 		}
1053 
1054 	case WSDISPLAYIO_GINFO:
1055 		FBINFO.width  = adp->gfxwidth;
1056 		FBINFO.height = adp->gfxheight;
1057 		FBINFO.depth  = adp->gfxdepth;
1058 		FBINFO.cmsize = 1 << FBINFO.depth;
1059 		return 0;
1060 
1061 	case WSDISPLAYIO_PUTCMAP:
1062 	case WSDISPLAYIO_GETCMAP:
1063 		return amidisplaycc_cmapioctl(adp->gfxview, cmd,
1064 					       (struct wsdisplay_cmap*)data);
1065 	case WSDISPLAYIO_GET_FBINFO:
1066 		amidisplaycc_initgfxview(adp);
1067 		return amidisplaycc_getfbinfo(adp, data);
1068 	}
1069 
1070 	return EPASSTHROUGH;
1071 
1072 #undef UINTDATA
1073 #undef INTDATA
1074 #undef FBINFO
1075 }
1076 
1077 static int
1078 amidisplaycc_getfbinfo(struct amidisplaycc_softc *adp, struct wsdisplayio_fbinfo *fbinfo)
1079 {
1080 	bmap_t *bm;
1081 
1082 	KASSERT(adp);
1083 
1084 	if (adp->gfxview == NULL) {
1085 		return ENOMEM;
1086 	}
1087 
1088 	bm = adp->gfxview->bitmap;
1089 	KASSERT(bm);
1090 
1091 	/* Depth 1 since current X wsfb driver doesn't support multiple bitplanes */
1092 	memset(fbinfo, 0, sizeof(*fbinfo));
1093 	fbinfo->fbi_fbsize = bm->bytes_per_row * bm->rows;
1094 	fbinfo->fbi_fboffset = 0;
1095 	fbinfo->fbi_width = bm->bytes_per_row * 8;
1096 	fbinfo->fbi_height = bm->rows;
1097 	fbinfo->fbi_stride = bm->bytes_per_row;
1098 	fbinfo->fbi_bitsperpixel = 1;
1099 	fbinfo->fbi_pixeltype = WSFB_CI;
1100 	fbinfo->fbi_flags = 0;
1101 	fbinfo->fbi_subtype.fbi_cmapinfo.cmap_entries = 1 << adp->gfxdepth;
1102 
1103 	return 0;
1104 }
1105 
1106 /*
1107  * Initialize (but not display) the view used for graphics.
1108  */
1109 static void
1110 amidisplaycc_initgfxview(struct amidisplaycc_softc *adp)
1111 {
1112 	dimen_t dimension;
1113 
1114 	if (adp->gfxview == NULL) {
1115 		/* First time here, create the screen */
1116 		dimension.width = adp->gfxwidth;
1117 		dimension.height = adp->gfxheight;
1118 		adp->gfxview = grf_alloc_view(NULL,
1119 			&dimension,
1120 			adp->gfxdepth);
1121 	}
1122 }
1123 
1124 /*
1125  * Switch to either emulation (text) or mapped (graphics) mode
1126  * We keep an extra screen for mapped mode so it does not
1127  * interfere with emulation screens.
1128  *
1129  * Once the extra screen is created, it never goes away.
1130  */
1131 static int
1132 amidisplaycc_setgfxview(struct amidisplaycc_softc *adp, int on)
1133 {
1134 	dprintf("amidisplaycc: switching to %s mode.\n",
1135 		on ? "mapped" : "emul");
1136 
1137 	/* Current mode same as requested mode? */
1138 	if ( (on > 0) == (adp->gfxon > 0) )
1139 		return 0;
1140 
1141 	if (!on) {
1142 		/*
1143 		 * Switch away from mapped mode. If there is
1144 		 * a emulation screen, switch to it, otherwise
1145 		 * just try to hide the mapped screen.
1146 		 */
1147 		adp->gfxon = 0;
1148 		if (adp->currentscreen)
1149 			grf_display_view(adp->currentscreen->view);
1150 		else if (adp->gfxview)
1151 			grf_remove_view(adp->gfxview);
1152 
1153 		return 0;
1154 	}
1155 
1156 	/* switch to mapped mode then */
1157 	amidisplaycc_initgfxview(adp);
1158 
1159 	if (adp->gfxview) {
1160 		adp->gfxon = 1;
1161 
1162 		grf_display_view(adp->gfxview);
1163 	} else {
1164 		printf("amidisplaycc: failed to make mapped screen\n");
1165 		return ENOMEM;
1166 	}
1167 	return 0;
1168 }
1169 
1170 /*
1171  * Map the graphics screen. It must have been created before
1172  * by switching to mapped mode by using an ioctl.
1173  */
1174 paddr_t
1175 amidisplaycc_mmap(void *dp, void *vs, off_t off, int prot)
1176 {
1177 	struct amidisplaycc_softc  * adp;
1178 	bmap_t                     * bm;
1179 	paddr_t                      rv;
1180 
1181 	adp = (struct amidisplaycc_softc*)dp;
1182 
1183 	/* Check we are in mapped mode */
1184 	if (adp->gfxon == 0 || adp->gfxview == NULL) {
1185 		dprintf("amidisplaycc_mmap: Not in mapped mode\n");
1186 		return (paddr_t)(-1);
1187 	}
1188 
1189 	/*
1190 	 * Screen reserved for graphics is used to avoid writing
1191 	 * over the text screens.
1192 	 */
1193 
1194 	bm = adp->gfxview->bitmap;
1195 
1196 	/* Check that the offset is valid */
1197 	if (off < 0 || off >= bm->depth * bm->bytes_per_row * bm->rows) {
1198 		dprintf("amidisplaycc_mmap: Offset out of range\n");
1199 		return (paddr_t)(-1);
1200 	}
1201 
1202 	rv = (paddr_t)bm->hardware_address;
1203 	rv += off;
1204 
1205 	return MD_BTOP(rv);
1206 }
1207 
1208 
1209 /*
1210  * Create a new screen.
1211  *
1212  * NULL dp signifies console and then memory is allocated statically
1213  * and the screen is automatically displayed.
1214  *
1215  * A font with suitable size is searched and if not found
1216  * the builtin 8x8 font is used.
1217  *
1218  * There are separate default palettes for 2, 4 and 8+ color
1219  * screens.
1220  */
1221 
1222 int
1223 amidisplaycc_alloc_screen(void *dp, const struct wsscreen_descr *screenp,
1224 			  void  **cookiep, int *curxp, int *curyp,
1225 			  long *defattrp)
1226 {
1227 	const struct amidisplaycc_screen_descr  * adccscreenp;
1228 	struct amidisplaycc_screen              * scr;
1229 	struct amidisplaycc_softc               * adp;
1230 	view_t                                  * view;
1231 
1232 	dimen_t  dimension;
1233 	int      fontheight;
1234 	int      fontwidth;
1235 	int      maxcolor;
1236 	int      depth;
1237 	int      i;
1238 	int      j;
1239 
1240 	adccscreenp = (const struct amidisplaycc_screen_descr *)screenp;
1241 	depth = adccscreenp->depth;
1242 
1243 	adp = dp;
1244 
1245 	maxcolor = (1 << depth) - 1;
1246 
1247 	/* Sanity checks because of fixed buffers */
1248 	if (depth > MAXDEPTH || maxcolor >= MAXCOLORS)
1249 		return ENOMEM;
1250 	if (screenp->nrows > MAXROWS)
1251 		return ENOMEM;
1252 
1253 	fontwidth = screenp->fontwidth;
1254 	fontheight = screenp->fontheight;
1255 
1256 	/*
1257 	 * The screen size is defined in characters.
1258 	 * Calculate the pixel size using the font size.
1259 	 */
1260 
1261 	dimension.width = screenp->ncols * fontwidth;
1262 	dimension.height = screenp->nrows * fontheight;
1263 
1264 	view = grf_alloc_view(NULL, &dimension, depth);
1265 	if (view == NULL)
1266 		return ENOMEM;
1267 
1268 	/*
1269 	 * First screen gets the statically allocated console screen.
1270 	 * Others are allocated dynamically.
1271 	 */
1272 	if (adp == NULL) {
1273 		scr = &amidisplaycc_consolescreen;
1274 		if (scr->isconsole)
1275 			panic("more than one console?");
1276 
1277 		scr->isconsole = 1;
1278 	} else {
1279 		scr = malloc(sizeof(adccscr_t), M_DEVBUF, M_WAITOK|M_ZERO);
1280 	}
1281 
1282 	scr->view = view;
1283 
1284 	scr->ncols = screenp->ncols;
1285 	scr->nrows = screenp->nrows;
1286 
1287 	/* Copies of most used values */
1288 	scr->width  = dimension.width;
1289 	scr->height = dimension.height;
1290 	scr->depth  = depth;
1291 	scr->widthbytes = view->bitmap->bytes_per_row;
1292 	scr->linebytes  = scr->widthbytes + view->bitmap->row_mod;
1293 	scr->rowbytes   = scr->linebytes * fontheight;
1294 
1295 	scr->device = adp;
1296 
1297 
1298 	/*
1299 	 * Try to find a suitable font.
1300 	 * Avoid everything but the builtin font for console screen.
1301 	 * Builtin font is used if no other is found, even if it
1302 	 * has the wrong size.
1303 	 */
1304 
1305 	KASSERT(fontwidth == 8);
1306 
1307 	scr->wsfont       = NULL;
1308 	scr->wsfontcookie = -1;
1309 	scr->fontwidth    = fontwidth;
1310 	scr->fontheight   = fontheight;
1311 
1312 	if (adp)
1313 		amidisplaycc_setfont(scr, NULL);
1314 
1315 	if (scr->wsfont == NULL)
1316 	{
1317 		scr->wsfont = amidisplaycc_getbuiltinfont();
1318 		scr->wsfontcookie = -1;
1319 	}
1320 
1321 	KASSERT(scr->wsfont);
1322 	KASSERT(scr->wsfont->stride == 1);
1323 
1324 	for (i = 0 ; i < depth ; i++) {
1325 		scr->planes[i] = view->bitmap->plane[i];
1326 	}
1327 
1328 	for (i = 0 ; i < MAXROWS ; i++)
1329 		scr->rowmasks[i] = 0;
1330 
1331 	/* Simple one-to-one mapping for most colors */
1332 	for (i = 0 ; i < MAXCOLORS ; i++)
1333 		scr->colormap[i] = i;
1334 
1335 	/*
1336 	 * Arrange the most used pens to quickest colors.
1337 	 * The default color for given depth is (1<<depth)-1.
1338 	 * It is assumed it is used most and it is mapped to
1339 	 * color that can be drawn by writing data to one bitplane
1340 	 * only.
1341 	 * So map colors 3->2, 7->4, 15->8 and so on.
1342 	 */
1343 	for (i = 2 ; i < MAXCOLORS ; i *= 2) {
1344 		j = i * 2 - 1;
1345 
1346 		if (j < MAXCOLORS) {
1347 			scr->colormap[i] = j;
1348 			scr->colormap[j] = i;
1349 		}
1350 	}
1351 
1352 	/*
1353 	 * Set the default colormap.
1354 	 */
1355 	if (depth == 1)
1356 		amidisplaycc_setemulcmap(scr, &pal2);
1357 	else if (depth == 2)
1358 		amidisplaycc_setemulcmap(scr, &pal4);
1359 	else
1360 		amidisplaycc_setemulcmap(scr, &pal8);
1361 
1362 	*cookiep = scr;
1363 
1364 	/* cursor initially at top left */
1365 	scr->cursorrow = -1;
1366 	scr->cursorcol = -1;
1367 	*curxp = 0;
1368 	*curyp = 0;
1369 	amidisplaycc_cursor(scr, 1, *curxp, *curyp);
1370 
1371 	*defattrp = MAKEATTR(maxcolor, 0, 0);
1372 
1373 	/* Show the console automatically */
1374 	if (adp == NULL)
1375 		grf_display_view(scr->view);
1376 
1377 	if (adp) {
1378 		dprintf("amidisplaycc: allocated screen; %dx%dx%d; font=%s\n",
1379 			dimension.width,
1380 			dimension.height,
1381 			depth,
1382 			scr->wsfont->name);
1383 	}
1384 
1385 	return 0;
1386 }
1387 
1388 
1389 /*
1390  * Destroy a screen.
1391  */
1392 
1393 void
1394 amidisplaycc_free_screen(void *dp, void *screen)
1395 {
1396 	struct amidisplaycc_screen  * scr;
1397 	struct amidisplaycc_softc   * adp;
1398 
1399 	scr = screen;
1400 	adp = (struct amidisplaycc_softc*)dp;
1401 
1402 	if (scr == NULL)
1403 		return;
1404 
1405 	/* Free the used font */
1406 	if (scr->wsfont && scr->wsfontcookie != -1)
1407 		wsfont_unlock(scr->wsfontcookie);
1408 	scr->wsfont = NULL;
1409 	scr->wsfontcookie = -1;
1410 
1411 	if (adp->currentscreen == scr)
1412 		adp->currentscreen = NULL;
1413 
1414 	if (scr->view)
1415 		grf_free_view(scr->view);
1416 	scr->view = NULL;
1417 
1418 	/* Take care not to free the statically allocated console screen */
1419 	if (scr != &amidisplaycc_consolescreen) {
1420 		free(scr, M_DEVBUF);
1421 	}
1422 }
1423 
1424 /*
1425  * Switch to another vt. Switch is always made immediately.
1426  */
1427 
1428 /* ARGSUSED2 */
1429 int
1430 amidisplaycc_show_screen(void *dp, void *screen, int waitok,
1431 			 void (*cb) (void *, int, int), void *cbarg)
1432 {
1433 	adccscr_t *scr;
1434 	struct amidisplaycc_softc *adp;
1435 
1436 	adp = (struct amidisplaycc_softc*)dp;
1437 	scr = screen;
1438 
1439 	if (adp == NULL) {
1440 		dprintf("amidisplaycc_show_screen: adp==NULL\n");
1441 		return EINVAL;
1442 	}
1443 	if (scr == NULL) {
1444 		dprintf("amidisplaycc_show_screen: scr==NULL\n");
1445 		return EINVAL;
1446 	}
1447 
1448 	if (adp->gfxon) {
1449 		dprintf("amidisplaycc: Screen shift while in gfx mode?");
1450 		adp->gfxon = 0;
1451 	}
1452 
1453 	adp->currentscreen = scr;
1454 	adp->ison = 1;
1455 
1456 	grf_display_view(scr->view);
1457 
1458 	return 0;
1459 }
1460 
1461 /*
1462  * Load/set a font.
1463  *
1464  * Only setting is supported, as the wsfont pseudo-device can
1465  * handle the loading of fonts for us.
1466  */
1467 int
1468 amidisplaycc_load_font(void *dp, void *cookie, struct wsdisplay_font *font)
1469 {
1470 	struct amidisplaycc_softc   * adp __diagused;
1471 	struct amidisplaycc_screen  * scr __diagused;
1472 
1473 	adp = dp;
1474 	scr = cookie;
1475 
1476 	KASSERT(adp);
1477 	KASSERT(scr);
1478 	KASSERT(font);
1479 	KASSERT(font->name);
1480 
1481 	if (font->data)
1482 	{
1483 		/* request to load the font, not supported */
1484 		return EINVAL;
1485 	}
1486 	else
1487 	{
1488 		/* request to use the given font on this screen */
1489 		return amidisplaycc_setfont(scr, font->name);
1490 	}
1491 }
1492 
1493 /*
1494  * Set display on/off.
1495  */
1496 static int
1497 amidisplaycc_setvideo(struct amidisplaycc_softc *adp, int mode)
1498 {
1499         view_t * view;
1500 
1501 	if (adp == NULL) {
1502 		dprintf("amidisplaycc_setvideo: adp==NULL\n");
1503 		return EINVAL;
1504 	}
1505 	if (adp->currentscreen == NULL) {
1506 		dprintf("amidisplaycc_setvideo: adp->currentscreen==NULL\n");
1507 		return EINVAL;
1508 	}
1509 
1510 	/* select graphics or emulation screen */
1511 	if (adp->gfxon && adp->gfxview)
1512 		view = adp->gfxview;
1513 	else
1514 		view = adp->currentscreen->view;
1515 
1516 	if (mode) {
1517 		/* on */
1518 
1519 		grf_display_view(view);
1520 		dprintf("amidisplaycc: video is now on\n");
1521 		adp->ison = 1;
1522 
1523 	} else {
1524 		/* off */
1525 
1526 		grf_remove_view(view);
1527 		dprintf("amidisplaycc: video is now off\n");
1528 		adp->ison = 0;
1529 	}
1530 
1531 	return 0;
1532 }
1533 
1534 /*
1535  * Handle the WSDISPLAY_[PUT/GET]CMAP ioctls.
1536  * Just handle the copying of data to/from userspace and
1537  * let the functions amidisplaycc_setcmap and amidisplaycc_putcmap
1538  * do the real work.
1539  */
1540 
1541 static int
1542 amidisplaycc_cmapioctl(view_t *view, u_long cmd, struct wsdisplay_cmap *cmap)
1543 {
1544 	struct wsdisplay_cmap  tmpcmap;
1545 	u_char                 cmred[MAXCOLORS];
1546 	u_char                 cmgrn[MAXCOLORS];
1547 	u_char                 cmblu[MAXCOLORS];
1548 
1549 	int                    err;
1550 
1551 	if (cmap->index >= MAXCOLORS ||
1552 	    cmap->count > MAXCOLORS ||
1553 	    cmap->index + cmap->count > MAXCOLORS)
1554 		return EINVAL;
1555 
1556 	if (cmap->count == 0)
1557 		return 0;
1558 
1559 	tmpcmap.index = cmap->index;
1560 	tmpcmap.count = cmap->count;
1561 	tmpcmap.red   = cmred;
1562 	tmpcmap.green = cmgrn;
1563 	tmpcmap.blue  = cmblu;
1564 
1565 	if (cmd == WSDISPLAYIO_PUTCMAP) {
1566 		/* copy the color data to kernel space */
1567 
1568 		err = copyin(cmap->red, cmred, cmap->count);
1569 		if (err)
1570 			return err;
1571 
1572 		err = copyin(cmap->green, cmgrn, cmap->count);
1573 		if (err)
1574 			return err;
1575 
1576 		err = copyin(cmap->blue, cmblu, cmap->count);
1577 		if (err)
1578 			return err;
1579 
1580 		return amidisplaycc_setcmap(view, &tmpcmap);
1581 
1582 	} else if (cmd == WSDISPLAYIO_GETCMAP) {
1583 
1584 		err = amidisplaycc_getcmap(view, &tmpcmap);
1585 		if (err)
1586 			return err;
1587 
1588 		/* copy data to user space */
1589 
1590 		err = copyout(cmred, cmap->red, cmap->count);
1591 		if (err)
1592 			return err;
1593 
1594 		err = copyout(cmgrn, cmap->green, cmap->count);
1595 		if (err)
1596 			return err;
1597 
1598 		err = copyout(cmblu, cmap->blue, cmap->count);
1599 		if (err)
1600 			return err;
1601 
1602 		return 0;
1603 
1604 	} else
1605 		return EPASSTHROUGH;
1606 }
1607 
1608 /*
1609  * Set the palette of a emulation screen.
1610  * Here we do only color remapping and then call
1611  * amidisplaycc_setcmap to do the work.
1612  */
1613 
1614 static int
1615 amidisplaycc_setemulcmap(struct amidisplaycc_screen *scr,
1616 			 struct wsdisplay_cmap *cmap)
1617 {
1618 	struct wsdisplay_cmap  tmpcmap;
1619 
1620 	u_char                 red [MAXCOLORS];
1621 	u_char                 grn [MAXCOLORS];
1622 	u_char                 blu [MAXCOLORS];
1623 
1624 	int                    rc;
1625 	int                    i;
1626 
1627 	/*
1628 	 * Get old palette first.
1629 	 * Because of the color mapping going on in the emulation
1630 	 * screen the color range may not be contiguous in the real
1631 	 * palette.
1632 	 * So get the whole palette, insert the new colors
1633 	 * at the appropriate places and then set the whole
1634 	 * palette back.
1635 	 */
1636 
1637 	tmpcmap.index = 0;
1638 	tmpcmap.count = 1 << scr->depth;
1639 	tmpcmap.red   = red;
1640 	tmpcmap.green = grn;
1641 	tmpcmap.blue  = blu;
1642 
1643 	rc = amidisplaycc_getcmap(scr->view, &tmpcmap);
1644 	if (rc)
1645 		return rc;
1646 
1647 	for (i = cmap->index ; i < cmap->index + cmap->count ; i++) {
1648 
1649 		tmpcmap.red   [ scr->colormap[ i ] ] = cmap->red   [ i ];
1650 		tmpcmap.green [ scr->colormap[ i ] ] = cmap->green [ i ];
1651 		tmpcmap.blue  [ scr->colormap[ i ] ] = cmap->blue  [ i ];
1652 	}
1653 
1654 	rc = amidisplaycc_setcmap(scr->view, &tmpcmap);
1655 	if (rc)
1656 		return rc;
1657 
1658 	return 0;
1659 }
1660 
1661 
1662 /*
1663  * Set the colormap for the given screen.
1664  */
1665 
1666 static int
1667 amidisplaycc_setcmap(view_t *view, struct wsdisplay_cmap *cmap)
1668 {
1669 	u_long      cmentries [MAXCOLORS];
1670 
1671 	u_int       colors;
1672 	int         index;
1673 	int         count;
1674 	int         err;
1675 	colormap_t  cm;
1676 
1677 	if (view == NULL)
1678 		return EINVAL;
1679 
1680 	if (!cmap || !cmap->red || !cmap->green || !cmap->blue) {
1681 		dprintf("amidisplaycc_setcmap: other==NULL\n");
1682 		return EINVAL;
1683 	}
1684 
1685 	index  = cmap->index;
1686 	count  = cmap->count;
1687 	colors = (1 << view->bitmap->depth);
1688 
1689 	if (count > colors || index >= colors || index + count > colors)
1690 		return EINVAL;
1691 
1692 	if (count == 0)
1693 		return 0;
1694 
1695 	cm.entry = cmentries;
1696 	cm.first = index;
1697 	cm.size  = count;
1698 
1699 	/*
1700 	 * Get the old colormap. We need to do this at least to know
1701 	 * how many bits to use with the color values.
1702 	 */
1703 
1704 	err = grf_get_colormap(view, &cm);
1705 	if (err)
1706 		return err;
1707 
1708 	/*
1709 	 * The palette entries from wscons contain 8 bits per gun.
1710 	 * We need to convert them to the number of bits the view
1711 	 * expects. That is typically 4 or 8. Here we calculate the
1712 	 * conversion constants with which we divide the color values.
1713 	 */
1714 
1715 	if (cm.type == CM_COLOR) {
1716 		int c, green_div, blue_div, red_div;
1717 
1718 		red_div = 256 / (cm.red_mask + 1);
1719 		green_div = 256 / (cm.green_mask + 1);
1720 		blue_div = 256 / (cm.blue_mask + 1);
1721 
1722 		for (c = 0 ; c < count ; c++)
1723 			cm.entry[c + index] = MAKE_COLOR_ENTRY(
1724 				cmap->red[c] / red_div,
1725 				cmap->green[c] / green_div,
1726 				cmap->blue[c] / blue_div);
1727 
1728 	} else if (cm.type == CM_GREYSCALE) {
1729 		int c, grey_div;
1730 
1731 		grey_div = 256 / (cm.grey_mask + 1);
1732 
1733 		/* Generate grey from average of r-g-b (?) */
1734 		for (c = 0 ; c < count ; c++)
1735 			cm.entry[c + index] = MAKE_COLOR_ENTRY(
1736 				0,
1737 				0,
1738 				(cmap->red[c] +
1739 				 cmap->green[c] +
1740 				 cmap->blue[c]) / 3 / grey_div);
1741 	} else
1742 		return EINVAL; /* Hmhh */
1743 
1744 	/*
1745 	 * Now we have a new colormap that contains all the entries. Set
1746 	 * it to the view.
1747 	 */
1748 
1749 	err = grf_use_colormap(view, &cm);
1750 	if (err)
1751 		return err;
1752 
1753 	return 0;
1754 }
1755 
1756 /*
1757  * Return the colormap of the given screen.
1758  */
1759 
1760 static int
1761 amidisplaycc_getcmap(view_t *view, struct wsdisplay_cmap *cmap)
1762 {
1763 	u_long      cmentries [MAXCOLORS];
1764 
1765 	u_int       colors;
1766 	int         index;
1767 	int         count;
1768 	int         err;
1769 	colormap_t  cm;
1770 
1771 	if (view == NULL)
1772 		return EINVAL;
1773 
1774 	if (!cmap || !cmap->red || !cmap->green || !cmap->blue)
1775 		return EINVAL;
1776 
1777 	index  = cmap->index;
1778 	count  = cmap->count;
1779 	colors = (1 << view->bitmap->depth);
1780 
1781 	if (count > colors || index >= colors || index + count > colors)
1782 		return EINVAL;
1783 
1784 	if (count == 0)
1785 		return 0;
1786 
1787 	cm.entry = cmentries;
1788 	cm.first = index;
1789 	cm.size  = count;
1790 
1791 	err = grf_get_colormap(view, &cm);
1792 	if (err)
1793 		return err;
1794 
1795 	/*
1796 	 * Copy color data to wscons-style structure. Translate to
1797 	 * 8 bits/gun from whatever resolution the color natively is.
1798 	 */
1799 	if (cm.type == CM_COLOR) {
1800 		int c, red_mul, green_mul, blue_mul;
1801 
1802 		red_mul   = 256 / (cm.red_mask + 1);
1803 		green_mul = 256 / (cm.green_mask + 1);
1804 		blue_mul  = 256 / (cm.blue_mask + 1);
1805 
1806 		for (c = 0 ; c < count ; c++) {
1807 			cmap->red[c] = red_mul *
1808 				CM_GET_RED(cm.entry[index+c]);
1809 			cmap->green[c] = green_mul *
1810 				CM_GET_GREEN(cm.entry[index+c]);
1811 			cmap->blue[c] = blue_mul *
1812 				CM_GET_BLUE(cm.entry[index+c]);
1813 		}
1814 	} else if (cm.type == CM_GREYSCALE) {
1815 		int c, grey_mul;
1816 
1817 		grey_mul = 256 / (cm.grey_mask + 1);
1818 
1819 		for (c = 0 ; c < count ; c++) {
1820 			cmap->red[c] = grey_mul *
1821 				CM_GET_GREY(cm.entry[index+c]);
1822 			cmap->green[c] = grey_mul *
1823 				CM_GET_GREY(cm.entry[index+c]);
1824 			cmap->blue[c] = grey_mul *
1825 				CM_GET_GREY(cm.entry[index+c]);
1826 		}
1827 	} else
1828 		return EINVAL;
1829 
1830 	return 0;
1831 }
1832 
1833 /*
1834  * Find and set a font for the given screen.
1835  *
1836  * If fontname is given, a font with that name and suitable
1837  * size (determined by the screen) is searched for.
1838  * If fontname is NULL, a font with suitable size is searched.
1839  *
1840  * On success, the found font is assigned to the screen and possible
1841  * old font is freed.
1842  */
1843 static int
1844 amidisplaycc_setfont(struct amidisplaycc_screen *scr, const char *fontname)
1845 {
1846 	struct wsdisplay_font *wsfont;
1847 	int wsfontcookie;
1848 
1849 	KASSERT(scr);
1850 
1851 	wsfontcookie = wsfont_find(fontname,
1852 		scr->fontwidth,
1853 		scr->fontheight,
1854 		1,
1855 		WSDISPLAY_FONTORDER_L2R,
1856 		WSDISPLAY_FONTORDER_L2R,
1857 		WSFONT_FIND_BITMAP);
1858 
1859 	if (wsfontcookie == -1)
1860 		return EINVAL;
1861 
1862 	/* Suitable font found. Now lock it. */
1863 	if (wsfont_lock(wsfontcookie, &wsfont))
1864 		return EINVAL;
1865 
1866 	KASSERT(wsfont);
1867 
1868 	if (scr->wsfont && scr->wsfontcookie != -1)
1869 		wsfont_unlock(scr->wsfontcookie);
1870 
1871 	scr->wsfont = wsfont;
1872 	scr->wsfontcookie = wsfontcookie;
1873 
1874 	return 0;
1875 }
1876 
1877 /*
1878  * Return a font that is guaranteed to exist.
1879  */
1880 static const struct wsdisplay_font *
1881 amidisplaycc_getbuiltinfont(void)
1882 {
1883 	static struct wsdisplay_font font;
1884 
1885 	extern unsigned char kernel_font_width_8x8;
1886 	extern unsigned char kernel_font_height_8x8;
1887 	extern unsigned char kernel_font_lo_8x8;
1888 	extern unsigned char kernel_font_hi_8x8;
1889 	extern unsigned char kernel_font_8x8[];
1890 
1891 	font.name = "kf8x8";
1892 	font.firstchar = kernel_font_lo_8x8;
1893 	font.numchars = kernel_font_hi_8x8 - kernel_font_lo_8x8 + 1;
1894 	font.fontwidth = kernel_font_width_8x8;
1895 	font.stride = 1;
1896 	font.fontheight = kernel_font_height_8x8;
1897 	font.data = kernel_font_8x8;
1898 
1899 	/* these values aren't really used for anything */
1900 	font.encoding = WSDISPLAY_FONTENC_ISO;
1901 	font.bitorder = WSDISPLAY_FONTORDER_KNOWN;
1902 	font.byteorder = WSDISPLAY_FONTORDER_KNOWN;
1903 
1904 	return &font;
1905 }
1906 
1907 /* ARGSUSED */
1908 void
1909 amidisplaycc_pollc(void *cookie, int on)
1910 {
1911 	if (amidisplaycc_consolescreen.isconsole)
1912 	{
1913 		if (on)
1914 		{
1915 			/* About to use console, so make it visible */
1916 			grf_display_view(amidisplaycc_consolescreen.view);
1917 		}
1918 		if (!on &&
1919 		    amidisplaycc_consolescreen.isconsole &&
1920 		    amidisplaycc_consolescreen.device != NULL &&
1921 		    amidisplaycc_consolescreen.device->currentscreen != NULL)
1922 		{
1923 			/* Restore the correct view after done with console use */
1924 			grf_display_view(amidisplaycc_consolescreen.device->currentscreen->view);
1925 		}
1926 	}
1927 }
1928 
1929 /*
1930  * These dummy functions are here just so that we can compete of
1931  * the console at init.
1932  * If we win the console then the wscons system will provide the
1933  * real ones which in turn will call the appropriate wskbd device.
1934  * These should never be called.
1935  */
1936 
1937 /* ARGSUSED */
1938 void
1939 amidisplaycc_cnputc(dev_t cd, int ch)
1940 {
1941 }
1942 
1943 /* ARGSUSED */
1944 int
1945 amidisplaycc_cngetc(dev_t cd)
1946 {
1947 	return 0;
1948 }
1949 
1950 /* ARGSUSED */
1951 void
1952 amidisplaycc_cnpollc(dev_t cd, int on)
1953 {
1954 }
1955 
1956 
1957 /*
1958  * Prints stuff if DEBUG is turned on.
1959  */
1960 
1961 /* ARGSUSED */
1962 static void
1963 dprintf(const char *fmt, ...)
1964 {
1965 #ifdef DEBUG
1966 	va_list ap;
1967 
1968 	va_start(ap, fmt);
1969 	vprintf(fmt, ap);
1970 	va_end(ap);
1971 #endif
1972 }
1973 
1974 #endif /* AMIDISPLAYCC */
1975