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