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