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