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