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