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