Home | History | Annotate | Line # | Download | only in dev
amidisplaycc.c revision 1.16
      1 /*	$NetBSD: amidisplaycc.c,v 1.16 2004/12/13 02:14:13 chs 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.16 2004/12/13 02:14:13 chs 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 
     47 #if NAMIDISPLAYCC>0
     48 
     49 #include <sys/param.h>
     50 #include <sys/types.h>
     51 #include <sys/device.h>
     52 #include <sys/malloc.h>
     53 #include <sys/systm.h>
     54 
     55 #include <sys/conf.h>
     56 
     57 #include <amiga/dev/grfabs_reg.h>
     58 #include <amiga/dev/viewioctl.h>
     59 #include <amiga/amiga/device.h>
     60 #include <dev/wscons/wsconsio.h>
     61 #include <dev/rcons/raster.h>
     62 #include <dev/wscons/wscons_raster.h>
     63 #include <dev/wscons/wsdisplayvar.h>
     64 #include <dev/cons.h>
     65 #include <dev/wsfont/wsfont.h>
     66 
     67 #include <machine/stdarg.h>
     68 
     69 /* These can be lowered if you are sure you dont need that much colors. */
     70 #define MAXDEPTH 8
     71 #define MAXROWS 128
     72 
     73 #define ADJUSTCOLORS
     74 
     75 #define MAXCOLORS (1<<MAXDEPTH)
     76 
     77 struct amidisplaycc_screen;
     78 struct amidisplaycc_softc
     79 {
     80 	struct device dev;
     81 
     82 	struct amidisplaycc_screen  * currentscreen;
     83 
     84 	/* display turned on? */
     85 	int       ison;
     86 
     87 	/* stuff relating to the mapped screen */
     88 	view_t  * gfxview;
     89 	int       gfxwidth;
     90 	int       gfxheight;
     91 	int       gfxdepth;
     92 	int       gfxon;
     93 };
     94 
     95 
     96 /*
     97  * Configuration stuff.
     98  */
     99 
    100 static int  amidisplaycc_match(struct device *, struct cfdata *, void *);
    101 static void amidisplaycc_attach(struct device *, struct device *, void *);
    102 
    103 CFATTACH_DECL(amidisplaycc, sizeof(struct amidisplaycc_softc),
    104     amidisplaycc_match, amidisplaycc_attach, NULL, NULL);
    105 
    106 static int amidisplaycc_attached;
    107 
    108 cons_decl(amidisplaycc_);
    109 
    110 /* end of configuration stuff */
    111 
    112 /* private utility functions */
    113 
    114 static int amidisplaycc_setvideo(struct amidisplaycc_softc *, int);
    115 
    116 static int amidisplaycc_setemulcmap(struct amidisplaycc_screen *,
    117 				    struct wsdisplay_cmap *);
    118 
    119 static int amidisplaycc_cmapioctl(view_t *, u_long, struct wsdisplay_cmap *);
    120 static int amidisplaycc_setcmap(view_t *, struct wsdisplay_cmap *);
    121 static int amidisplaycc_getcmap(view_t *, struct wsdisplay_cmap *);
    122 static int amidisplaycc_gfxscreen(struct amidisplaycc_softc *, int);
    123 
    124 static int amidisplaycc_setfont(struct amidisplaycc_screen *, const char *);
    125 static const struct wsdisplay_font * amidisplaycc_getbuiltinfont(void);
    126 
    127 static void dprintf(const char *fmt, ...);
    128 
    129 /* end of private utility functions */
    130 
    131 /* emulops for wscons */
    132 void amidisplaycc_cursor(void *, int, int, int);
    133 int amidisplaycc_mapchar(void *, int, unsigned int *);
    134 void amidisplaycc_putchar(void *, int, int, u_int, long);
    135 void amidisplaycc_copycols(void *, int, int, int, int);
    136 void amidisplaycc_erasecols(void *, int, int, int, long);
    137 void amidisplaycc_copyrows(void *, int, int, int);
    138 void amidisplaycc_eraserows(void *, int, int, long);
    139 int  amidisplaycc_allocattr(void *, int, int, int, long *);
    140 /* end of emulops for wscons */
    141 
    142 
    143 /* accessops for wscons */
    144 int amidisplaycc_ioctl(void *, u_long, caddr_t, int, struct proc *);
    145 paddr_t amidisplaycc_mmap(void *, off_t, int);
    146 int amidisplaycc_alloc_screen(void *, const struct wsscreen_descr *, void **,
    147 			      int *, int *, long *);
    148 void amidisplaycc_free_screen( void *, void *);
    149 int amidisplaycc_show_screen(void *, void *, int, void (*)(void *, int, int),
    150 			     void *);
    151 int amidisplaycc_load_font(void *, void *, struct wsdisplay_font *);
    152 void amidisplaycc_pollc(void *, int);
    153 /* end of accessops for wscons */
    154 
    155 /*
    156  * These structures are passed to wscons, and they contain the
    157  * display-specific callback functions.
    158  */
    159 
    160 const struct wsdisplay_accessops amidisplaycc_accessops = {
    161 	amidisplaycc_ioctl,
    162 	amidisplaycc_mmap,
    163 	amidisplaycc_alloc_screen,
    164 	amidisplaycc_free_screen,
    165 	amidisplaycc_show_screen,
    166 	amidisplaycc_load_font,
    167 	amidisplaycc_pollc
    168 };
    169 
    170 const struct wsdisplay_emulops amidisplaycc_emulops = {
    171 	amidisplaycc_cursor,
    172 	amidisplaycc_mapchar,
    173 	amidisplaycc_putchar,
    174 	amidisplaycc_copycols,
    175 	amidisplaycc_erasecols,
    176 	amidisplaycc_copyrows,
    177 	amidisplaycc_eraserows,
    178 	amidisplaycc_allocattr
    179 };
    180 
    181 /* add some of our own data to the wsscreen_descr */
    182 struct amidisplaycc_screen_descr {
    183 	struct wsscreen_descr  wsdescr;
    184 	int                    depth;
    185 };
    186 
    187 /*
    188  * List of supported screenmodes. Almost anything can be given here.
    189  */
    190 
    191 #define ADCC_SCREEN(name, width, height, depth, fontwidth, fontheight) \
    192     /* CONSTCOND */ \
    193     {{ \
    194     name, \
    195     width / fontwidth, \
    196     height / fontheight, \
    197     &amidisplaycc_emulops, fontwidth, fontheight, \
    198     (depth > 1 ? WSSCREEN_WSCOLORS : 0) | WSSCREEN_REVERSE | \
    199     WSSCREEN_HILIT | WSSCREEN_UNDERLINE }, \
    200     depth }
    201 
    202 /*
    203  * Screen types.
    204  *
    205  * The first in list is used for the console screen.
    206  * A suitable screen mode is guessed for it by looking
    207  * at the GRF_* options.
    208  */
    209 struct amidisplaycc_screen_descr amidisplaycc_screentab[] = {
    210 	/* name, width, height, depth, fontwidth==8, fontheight */
    211 
    212 #if defined(GRF_PAL) && !defined(GRF_NTSC)
    213 	ADCC_SCREEN("default", 640, 512, 3, 8, 8),
    214 #else
    215 	ADCC_SCREEN("default", 640, 400, 3, 8, 8),
    216 #endif
    217 	ADCC_SCREEN("80x50", 640, 400, 3, 8, 8),
    218 	ADCC_SCREEN("80x40", 640, 400, 3, 8, 10),
    219 	ADCC_SCREEN("80x25", 640, 400, 3, 8, 16),
    220 	ADCC_SCREEN("80x24", 640, 192, 3, 8, 8),
    221 
    222 	ADCC_SCREEN("80x64", 640, 512, 3, 8, 8),
    223 	ADCC_SCREEN("80x51", 640, 510, 3, 8, 10),
    224 	ADCC_SCREEN("80x32", 640, 512, 3, 8, 16),
    225 	ADCC_SCREEN("80x31", 640, 248, 3, 8, 8),
    226 
    227 	ADCC_SCREEN("640x400x1", 640, 400, 1, 8, 8),
    228 	ADCC_SCREEN("640x400x2", 640, 400, 2, 8, 8),
    229 	ADCC_SCREEN("640x400x3", 640, 400, 3, 8, 8),
    230 
    231 	ADCC_SCREEN("640x200x1", 640, 200, 1, 8, 8),
    232 	ADCC_SCREEN("640x200x2", 640, 200, 2, 8, 8),
    233 	ADCC_SCREEN("640x200x3", 640, 200, 3, 8, 8),
    234 };
    235 
    236 #define ADCC_SCREENPTR(index) &amidisplaycc_screentab[index].wsdescr
    237 const struct wsscreen_descr *amidisplaycc_screens[] = {
    238 	ADCC_SCREENPTR(0),
    239 	ADCC_SCREENPTR(1),
    240 	ADCC_SCREENPTR(2),
    241 	ADCC_SCREENPTR(3),
    242 	ADCC_SCREENPTR(4),
    243 	ADCC_SCREENPTR(5),
    244 	ADCC_SCREENPTR(6),
    245 	ADCC_SCREENPTR(7),
    246 	ADCC_SCREENPTR(8),
    247 	ADCC_SCREENPTR(9),
    248 	ADCC_SCREENPTR(10),
    249 	ADCC_SCREENPTR(11),
    250 	ADCC_SCREENPTR(12),
    251 	ADCC_SCREENPTR(13),
    252 	ADCC_SCREENPTR(14),
    253 };
    254 
    255 #define NELEMS(arr) (sizeof(arr)/sizeof((arr)[0]))
    256 
    257 /*
    258  * This structure also is passed to wscons. It contains pointers
    259  * to the available display modes.
    260  */
    261 
    262 const struct wsscreen_list amidisplaycc_screenlist = {
    263 	sizeof(amidisplaycc_screens)/sizeof(amidisplaycc_screens[0]),
    264 	amidisplaycc_screens
    265 };
    266 
    267 /*
    268  * Our own screen structure. One will be created for each screen.
    269  */
    270 
    271 struct amidisplaycc_screen
    272 {
    273 	struct amidisplaycc_softc *device;
    274 
    275 	int       isconsole;
    276 	int       isvisible;
    277 	view_t  * view;
    278 
    279 	int       ncols;
    280 	int       nrows;
    281 
    282 	int       cursorrow;
    283 	int       cursorcol;
    284 
    285 	/* Active bitplanes for each character row. */
    286 	int       rowmasks[MAXROWS];
    287 
    288 	/* Mapping of colors to screen colors. */
    289 	int       colormap[MAXCOLORS];
    290 
    291 	/* Copies of display parameters for convenience */
    292 	int       width;
    293 	int       height;
    294 	int       depth;
    295 
    296 	int       widthbytes; /* bytes_per_row           */
    297 	int       linebytes;  /* widthbytes + row_mod    */
    298 	int       rowbytes;   /* linebytes * fontheight  */
    299 
    300 	u_char  * planes[MAXDEPTH];
    301 
    302 	const struct wsdisplay_font  * wsfont;
    303 	int                      wsfontcookie; /* if -1, builtin font */
    304 	int                      fontwidth;
    305 	int                      fontheight;
    306 };
    307 
    308 typedef struct amidisplaycc_screen adccscr_t;
    309 
    310 /*
    311  * Need one statically allocated screen for early init.
    312  * The rest are mallocated when needed.
    313  */
    314 adccscr_t amidisplaycc_consolescreen;
    315 
    316 /*
    317  * Default palettes for 2, 4 and 8 color emulation displays.
    318  */
    319 
    320 /* black, grey */
    321 static u_char pal2red[] = { 0x00, 0xaa };
    322 static u_char pal2grn[] = { 0x00, 0xaa };
    323 static u_char pal2blu[] = { 0x00, 0xaa };
    324 
    325 /* black, red, green, grey */
    326 static u_char pal4red[] = { 0x00, 0xaa, 0x00, 0xaa };
    327 static u_char pal4grn[] = { 0x00, 0x00, 0xaa, 0xaa };
    328 static u_char pal4blu[] = { 0x00, 0x00, 0x00, 0xaa };
    329 
    330 /* black, red, green, brown, blue, magenta, cyan, grey */
    331 static u_char pal8red[] = { 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa};
    332 static u_char pal8grn[] = { 0x00, 0x00, 0xaa, 0xaa, 0x00, 0x00, 0xaa, 0xaa};
    333 static u_char pal8blu[] = { 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa};
    334 
    335 static struct wsdisplay_cmap pal2 = { 0, 2, pal2red, pal2grn, pal2blu };
    336 static struct wsdisplay_cmap pal4 = { 0, 4, pal4red, pal4grn, pal4blu };
    337 static struct wsdisplay_cmap pal8 = { 0, 8, pal8red, pal8grn, pal8blu };
    338 
    339 #ifdef GRF_AGA
    340 extern int aga_enable;
    341 #else
    342 static int aga_enable = 0;
    343 #endif
    344 
    345 /*
    346  * This gets called at console init to determine the priority of
    347  * this console device.
    348  *
    349  * Of course pointers to this and other functions must present
    350  * in constab[] in conf.c for this to work.
    351  */
    352 void
    353 amidisplaycc_cnprobe(struct consdev *cd)
    354 {
    355 	cd->cn_pri = CN_INTERNAL;
    356 
    357 	/*
    358 	 * Yeah, real nice. But if we win the console then the wscons system
    359 	 * does the proper initialization.
    360 	 */
    361 	cd->cn_dev = NODEV;
    362 }
    363 
    364 /*
    365  * This gets called if this device is used as the console.
    366  */
    367 void
    368 amidisplaycc_cninit(struct consdev  * cd)
    369 {
    370 	void  * cookie;
    371 	long    attr;
    372 	int     x;
    373 	int     y;
    374 
    375 	/* Yeah, we got the console! */
    376 
    377 	/*
    378 	 * This will do the basic stuff we also need.
    379 	 */
    380 	config_console();
    381 
    382 	grfcc_probe();
    383 
    384 #if NVIEW>0
    385 	viewprobe();
    386 #endif
    387 
    388 	/*
    389 	 * Set up wscons to handle the details.
    390 	 * It will then call us back when it needs something
    391 	 * display-specific. It will also set up cn_tab properly,
    392 	 * something which we failed to do at amidisplaycc_cnprobe().
    393 	 */
    394 
    395 	/*
    396 	 * The alloc_screen knows to allocate the first screen statically.
    397 	 */
    398 	amidisplaycc_alloc_screen(NULL, &amidisplaycc_screentab[0].wsdescr,
    399 				  &cookie, &x, &y, &attr);
    400 	wsdisplay_cnattach(&amidisplaycc_screentab[0].wsdescr,
    401 			   cookie, x, y, attr);
    402 }
    403 
    404 static int
    405 amidisplaycc_match(struct device *pdp, struct cfdata *cfp, void *auxp)
    406 {
    407 	char *name = auxp;
    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(struct device *pdp, struct device *dp, void *auxp)
    422 {
    423 	struct wsemuldisplaydev_attach_args    waa;
    424 	struct amidisplaycc_softc            * adp;
    425 
    426 	amidisplaycc_attached = 1;
    427 
    428 	adp = (struct amidisplaycc_softc*)dp;
    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 = dp;
    478 		config_found(dp, &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 					bzero(dst, copysize);
    890 				} else {
    891 					for (i = 0 ; i < fontheight ; i++) {
    892 						bzero(dst, 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, u_long cmd, caddr_t data, int flag, struct proc *p)
   1018 {
   1019 	struct amidisplaycc_softc *adp;
   1020 
   1021 	adp = dp;
   1022 
   1023 	if (adp == NULL) {
   1024 		printf("amidisplaycc_ioctl: adp==NULL\n");
   1025 		return (EINVAL);
   1026 	}
   1027 
   1028 #define UINTDATA (*(u_int*)data)
   1029 #define INTDATA (*(int*)data)
   1030 #define FBINFO (*(struct wsdisplay_fbinfo*)data)
   1031 
   1032 	switch (cmd)
   1033 	{
   1034 	case WSDISPLAYIO_GTYPE:
   1035 		UINTDATA = WSDISPLAY_TYPE_AMIGACC;
   1036 		return (0);
   1037 
   1038 	case WSDISPLAYIO_SVIDEO:
   1039 		dprintf("amidisplaycc: WSDISPLAYIO_SVIDEO %s\n",
   1040 			UINTDATA ? "On" : "Off");
   1041 
   1042 		return (amidisplaycc_setvideo(adp, UINTDATA));
   1043 
   1044 	case WSDISPLAYIO_GVIDEO:
   1045 		dprintf("amidisplaycc: WSDISPLAYIO_GVIDEO\n");
   1046 		UINTDATA = adp->ison ?
   1047 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
   1048 
   1049 		return (0);
   1050 
   1051 	case WSDISPLAYIO_SMODE:
   1052 		if (INTDATA == WSDISPLAYIO_MODE_EMUL)
   1053 			return amidisplaycc_gfxscreen(adp, 0);
   1054 		if (INTDATA == WSDISPLAYIO_MODE_MAPPED)
   1055 			return amidisplaycc_gfxscreen(adp, 1);
   1056 		return (EINVAL);
   1057 
   1058 	case WSDISPLAYIO_GINFO:
   1059 		FBINFO.width  = adp->gfxwidth;
   1060 		FBINFO.height = adp->gfxheight;
   1061 		FBINFO.depth  = adp->gfxdepth;
   1062 		FBINFO.cmsize = 1 << FBINFO.depth;
   1063 		return (0);
   1064 
   1065 	case WSDISPLAYIO_PUTCMAP:
   1066 	case WSDISPLAYIO_GETCMAP:
   1067 		return (amidisplaycc_cmapioctl(adp->gfxview,
   1068 					       cmd,
   1069 					       (struct wsdisplay_cmap*)data));
   1070 	}
   1071 
   1072 	dprintf("amidisplaycc: unknown ioctl %lx (grp:'%c' num:%d)\n",
   1073 		(long)cmd,
   1074 		(char)((cmd&0xff00)>>8),
   1075 		(int)(cmd&0xff));
   1076 
   1077 	return (EPASSTHROUGH);
   1078 
   1079 #undef UINTDATA
   1080 #undef INTDATA
   1081 #undef FBINFO
   1082 }
   1083 
   1084 
   1085 /*
   1086  * Switch to either emulation (text) or mapped (graphics) mode
   1087  * We keep an extra screen for mapped mode so it does not
   1088  * interfere with emulation screens.
   1089  *
   1090  * Once the extra screen is created, it never goes away.
   1091  */
   1092 
   1093 static int
   1094 amidisplaycc_gfxscreen(struct amidisplaycc_softc *adp, int on)
   1095 {
   1096 	dimen_t  dimension;
   1097 
   1098 	dprintf("amidisplaycc: switching to %s mode.\n",
   1099 		on ? "mapped" : "emul");
   1100 
   1101 	/* Current mode same as requested mode? */
   1102 	if ( (on > 0) == (adp->gfxon > 0) )
   1103 		return (0);
   1104 
   1105 	if (!on) {
   1106 		/*
   1107 		 * Switch away from mapped mode. If there is
   1108 		 * a emulation screen, switch to it, otherwise
   1109 		 * just try to hide the mapped screen.
   1110 		 */
   1111 		adp->gfxon = 0;
   1112 		if (adp->currentscreen)
   1113 			grf_display_view(adp->currentscreen->view);
   1114 		else if (adp->gfxview)
   1115 			grf_remove_view(adp->gfxview);
   1116 
   1117 		return (0);
   1118 	}
   1119 
   1120 	/* switch to mapped mode then */
   1121 
   1122 	if (adp->gfxview == NULL) {
   1123 		/* First time here, create the screen */
   1124 
   1125 		dimension.width = adp->gfxwidth;
   1126 		dimension.height = adp->gfxheight;
   1127 
   1128 		dprintf("amidisplaycc: preparing mapped screen %dx%dx%d\n",
   1129 			dimension.width,
   1130 			dimension.height,
   1131 			adp->gfxdepth);
   1132 
   1133 		adp->gfxview = grf_alloc_view(NULL,
   1134 					      &dimension,
   1135 					      adp->gfxdepth);
   1136 	}
   1137 
   1138 	if (adp->gfxview) {
   1139 		adp->gfxon = 1;
   1140 
   1141 		grf_display_view(adp->gfxview);
   1142 	} else {
   1143 		printf("amidisplaycc: failed to make mapped screen\n");
   1144 		return (ENOMEM);
   1145 	}
   1146 	return (0);
   1147 }
   1148 
   1149 /*
   1150  * Map the graphics screen. It must have been created before
   1151  * by switching to mapped mode by using an ioctl.
   1152  */
   1153 paddr_t
   1154 amidisplaycc_mmap(void *dp, off_t off, int prot)
   1155 {
   1156 	struct amidisplaycc_softc  * adp;
   1157 	bmap_t                     * bm;
   1158 	paddr_t                      rv;
   1159 
   1160 	adp = (struct amidisplaycc_softc*)dp;
   1161 
   1162 	/* Check we are in mapped mode */
   1163 	if (adp->gfxon == 0 || adp->gfxview == NULL) {
   1164 		dprintf("amidisplaycc_mmap: Not in mapped mode\n");
   1165 		return (paddr_t)(-1);
   1166 	}
   1167 
   1168 	/*
   1169 	 * As we all know by now, we are mapping our special
   1170 	 * screen here so our pretty text consoles are left
   1171 	 * untouched.
   1172 	 */
   1173 
   1174 	bm = adp->gfxview->bitmap;
   1175 
   1176 	/* Check that the offset is valid */
   1177 	if (off < 0 || off >= bm->depth * bm->bytes_per_row * bm->rows) {
   1178 		dprintf("amidisplaycc_mmap: Offset out of range\n");
   1179 		return (paddr_t)(-1);
   1180 	}
   1181 
   1182 	rv = (paddr_t)bm->hardware_address;
   1183 	rv += off;
   1184 
   1185 	return (rv >> PGSHIFT);
   1186 }
   1187 
   1188 
   1189 /*
   1190  * Create a new screen.
   1191  * NULL dp signifies console and then memory is allocated statically
   1192  * and the screen is automatically displayed.
   1193  *
   1194  * A font with suitable size is searched and if not found
   1195  * the builtin 8x8 font is used.
   1196  *
   1197  * There are separate default palettes for 2, 4 and 8+ color
   1198  * screens.
   1199  */
   1200 
   1201 int
   1202 amidisplaycc_alloc_screen(void *dp, const struct wsscreen_descr *screenp,
   1203 			  void  **cookiep, int *curxp, int *curyp,
   1204 			  long *defattrp)
   1205 {
   1206 	const struct amidisplaycc_screen_descr  * adccscreenp;
   1207 	struct amidisplaycc_screen              * scr;
   1208 	struct amidisplaycc_softc               * adp;
   1209 	view_t                                  * view;
   1210 
   1211 	dimen_t  dimension;
   1212 	int      fontheight;
   1213 	int      fontwidth;
   1214 	int      maxcolor;
   1215 	int      depth;
   1216 	int      i;
   1217 	int      j;
   1218 
   1219 	adccscreenp = (const struct amidisplaycc_screen_descr *)screenp;
   1220 	depth = adccscreenp->depth;
   1221 
   1222 	adp = dp;
   1223 
   1224 	maxcolor = (1 << depth) - 1;
   1225 
   1226 	/* Sanity checks because of fixed buffers */
   1227 	if (depth > MAXDEPTH || maxcolor >= MAXCOLORS)
   1228 		return (ENOMEM);
   1229 	if (screenp->nrows > MAXROWS)
   1230 		return (ENOMEM);
   1231 
   1232 	fontwidth = screenp->fontwidth;
   1233 	fontheight = screenp->fontheight;
   1234 
   1235 	/*
   1236 	 * The screen size is defined in characters.
   1237 	 * Calculate the pixel size using the font size.
   1238 	 */
   1239 
   1240 	dimension.width = screenp->ncols * fontwidth;
   1241 	dimension.height = screenp->nrows * fontheight;
   1242 
   1243 	view = grf_alloc_view(NULL, &dimension, depth);
   1244 	if (view == NULL)
   1245 		return (ENOMEM);
   1246 
   1247 	/*
   1248 	 * First screen gets the statically allocated console screen.
   1249 	 * Others are allocated dynamically.
   1250 	 */
   1251 	if (adp == NULL) {
   1252 		scr = &amidisplaycc_consolescreen;
   1253 		if (scr->isconsole)
   1254 			panic("more than one console?");
   1255 
   1256 		scr->isconsole = 1;
   1257 	} else {
   1258 		scr = malloc(sizeof(adccscr_t), M_DEVBUF, M_WAITOK);
   1259 		bzero(scr, sizeof(adccscr_t));
   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;
   1451 	struct amidisplaycc_screen  * scr;
   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 
   1838 	if (wsfontcookie == -1)
   1839 		return (EINVAL);
   1840 
   1841 	/* Suitable font found. Now lock it. */
   1842 	if (wsfont_lock(wsfontcookie, &wsfont))
   1843 		return (EINVAL);
   1844 
   1845 	KASSERT(wsfont);
   1846 
   1847 	if (scr->wsfont && scr->wsfontcookie != -1)
   1848 		wsfont_unlock(scr->wsfontcookie);
   1849 
   1850 	scr->wsfont = wsfont;
   1851 	scr->wsfontcookie = wsfontcookie;
   1852 
   1853 	return (0);
   1854 }
   1855 
   1856 /*
   1857  * Return a font that is guaranteed to exist.
   1858  */
   1859 static const struct wsdisplay_font *
   1860 amidisplaycc_getbuiltinfont(void)
   1861 {
   1862 	static struct wsdisplay_font font;
   1863 
   1864 	extern unsigned char kernel_font_width_8x8;
   1865 	extern unsigned char kernel_font_height_8x8;
   1866 	extern unsigned char kernel_font_lo_8x8;
   1867 	extern unsigned char kernel_font_hi_8x8;
   1868 	extern unsigned char kernel_font_8x8[];
   1869 
   1870 	font.name = "kf8x8";
   1871 	font.firstchar = kernel_font_lo_8x8;
   1872 	font.numchars = kernel_font_hi_8x8 - kernel_font_lo_8x8 + 1;
   1873 	font.fontwidth = kernel_font_width_8x8;
   1874 	font.stride = 1;
   1875 	font.fontheight = kernel_font_height_8x8;
   1876 	font.data = kernel_font_8x8;
   1877 
   1878 	/* these values aren't really used for anything */
   1879 	font.encoding = WSDISPLAY_FONTENC_ISO;
   1880 	font.bitorder = WSDISPLAY_FONTORDER_KNOWN;
   1881 	font.byteorder = WSDISPLAY_FONTORDER_KNOWN;
   1882 
   1883 	return &font;
   1884 }
   1885 
   1886 /* ARGSUSED */
   1887 void
   1888 amidisplaycc_pollc(void *cookie, int on)
   1889 {
   1890 }
   1891 
   1892 /*
   1893  * These dummy functions are here just so that we can compete of
   1894  * the console at init.
   1895  * If we win the console then the wscons system will provide the
   1896  * real ones which in turn will call the apropriate wskbd device.
   1897  * These should never be called.
   1898  */
   1899 
   1900 /* ARGSUSED */
   1901 void
   1902 amidisplaycc_cnputc(dev_t cd, int ch)
   1903 {
   1904 }
   1905 
   1906 /* ARGSUSED */
   1907 int
   1908 amidisplaycc_cngetc(dev_t cd)
   1909 {
   1910 	return (0);
   1911 }
   1912 
   1913 /* ARGSUSED */
   1914 void
   1915 amidisplaycc_cnpollc(dev_t cd, int on)
   1916 {
   1917 }
   1918 
   1919 
   1920 /*
   1921  * Prints stuff if DEBUG is turned on.
   1922  */
   1923 
   1924 /* ARGSUSED */
   1925 static void
   1926 dprintf(const char *fmt, ...)
   1927 {
   1928 #ifdef DEBUG
   1929 	va_list ap;
   1930 
   1931 	va_start(ap, fmt);
   1932 	vprintf(fmt, ap);
   1933 	va_end(ap);
   1934 #endif
   1935 }
   1936 
   1937 #endif /* AMIDISPLAYCC */
   1938