Home | History | Annotate | Line # | Download | only in dev
grfabs_fal.c revision 1.26.2.1
      1 /*	$NetBSD: grfabs_fal.c,v 1.26.2.1 2010/04/30 14:39:10 uebayasi Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Thomas Gerner.
      5  * Copyright (c) 1995 Leo Weppelman.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: grfabs_fal.c,v 1.26.2.1 2010/04/30 14:39:10 uebayasi Exp $");
     31 
     32 #ifdef FALCON_VIDEO
     33 /*
     34  *  atari abstract graphics driver: Falcon-interface
     35  */
     36 #include <sys/param.h>
     37 #include <sys/queue.h>
     38 #include <sys/malloc.h>
     39 #include <sys/device.h>
     40 #include <sys/systm.h>
     41 
     42 #include <uvm/uvm_extern.h>
     43 
     44 #include <machine/iomap.h>
     45 #include <machine/video.h>
     46 #include <machine/mfp.h>
     47 #include <atari/atari/device.h>
     48 #include <atari/atari/stalloc.h>
     49 #include <atari/dev/grfabs_reg.h>
     50 #include <atari/dev/grfabs_fal.h>
     51 
     52 /*
     53  * Function decls
     54  */
     55 static void       init_view(view_t *, bmap_t *, dmode_t *, box_t *);
     56 static bmap_t	  *alloc_bitmap(u_long, u_long, u_char);
     57 static colormap_t *alloc_colormap(dmode_t *);
     58 static void 	  free_bitmap(bmap_t *);
     59 static void	  falcon_display_view(view_t *);
     60 static view_t	  *falcon_alloc_view(dmode_t *, dimen_t *, u_char);
     61 static void	  falcon_free_view(view_t *);
     62 static void	  falcon_remove_view(view_t *);
     63 static void	  falcon_save_view(view_t *);
     64 static int	  falcon_use_colormap(view_t *, colormap_t *);
     65 static void	  falcon_detect(dmode_t *);
     66 static struct videl *falcon_getreg(u_short);
     67 
     68 /*
     69  * Our function switch table
     70  */
     71 struct grfabs_sw fal_vid_sw = {
     72 	falcon_display_view,
     73 	falcon_alloc_view,
     74 	falcon_free_view,
     75 	falcon_remove_view,
     76 	falcon_save_view,
     77 	falcon_use_colormap
     78 };
     79 
     80 struct falcon_hwregs {
     81 	u_short		fal_mode;	/* falcon mode		  */
     82 	struct videl	*fal_regs;	/* videl register values  */
     83 };
     84 #define vm_mode(dm)	(((struct falcon_hwregs*)(dm->data))->fal_mode)
     85 #define vm_regs(dm)	(((struct falcon_hwregs*)(dm->data))->fal_regs)
     86 
     87 /*
     88  * Note that the order of this table *must* match the order of
     89  * the table below!
     90  */
     91 static struct falcon_hwregs fal_hwregs[] = {
     92 	{ RES_FALAUTO    },
     93 	{ RES_FAL_STHIGH },
     94 	{ RES_FAL_STMID  },
     95 	{ RES_FAL_STLOW  },
     96 	{ RES_FAL_TTLOW  },
     97 	{ RES_VGA2       },
     98 	{ RES_VGA4       },
     99 	{ RES_VGA16      },
    100 	{ RES_VGA256     },
    101 	{ RES_DIRECT     }
    102 };
    103 
    104 
    105 static dmode_t vid_modes[] = {
    106     { {NULL,NULL}, "falauto", {   0,  0 },  0, NULL, &fal_vid_sw},
    107     { {NULL,NULL}, "sthigh",  { 640,400 },  1, NULL, &fal_vid_sw},
    108     { {NULL,NULL}, "stmid",   { 640,200 },  2, NULL, &fal_vid_sw},
    109     { {NULL,NULL}, "stlow",   { 320,200 },  4, NULL, &fal_vid_sw},
    110     { {NULL,NULL}, "ttlow",   { 320,480 },  8, NULL, &fal_vid_sw},
    111     { {NULL,NULL}, "vga2",    { 640,480 },  1, NULL, &fal_vid_sw},
    112     { {NULL,NULL}, "vga4",    { 640,480 },  2, NULL, &fal_vid_sw},
    113     { {NULL,NULL}, "vga16",   { 640,480 },  4, NULL, &fal_vid_sw},
    114     { {NULL,NULL}, "vga256",  { 640,480 },  8, NULL, &fal_vid_sw},
    115     { {NULL,NULL}, "highcol", { 320,200 }, 16, NULL, &fal_vid_sw},
    116     { {NULL,NULL},  NULL,  }
    117 };
    118 
    119 /*
    120  * The following table contains timing values for the various video modes.
    121  * I have only a multisync display, therefore I can not say if this values
    122  * are useful at other displays.
    123  * Use other video modes at YOUR OWN RISK.
    124  * THERE IS NO WARRENTY ABOUT THIS VALUES TO WORK WITH A PARTICULAR
    125  * DISPLAY. -- Thomas
    126  */
    127 static struct videl videlinit[] = {
    128 	{ RES_FALAUTO,		    /* autodedect			    */
    129 	0x0, 0x0, 0x0,   0x0, 0x0,   0x0,  0x0,  0x0,  0x0,   0x0,  0x0,  0x0,
    130 	0x0, 0x0,   0x0,   0x0,  0x0,  0x0,   0x0,   0x0,   0x0 },
    131 
    132 	{ FAL_VGA | RES_FAL_STHIGH, /* sthigh, 640x400, 2 colors	    */
    133 	0x2, 0x0, 0x28,  0x0, 0x400, 0xc6, 0x8d, 0x15, 0x273, 0x50, 0x96, 0x0,
    134 	0x0, 0x419, 0x3af, 0x8f, 0x8f, 0x3af, 0x415, 0x186, 0x8 },
    135 
    136 #if 0 /* not yet */
    137 	{ FAL_SM | RES_FAL_STHIGH,  /* sthigh, 640x400, 2 colors	    */
    138 	0x0, 0x0, 0x28,  0x2, 0x0,   0x1a, 0x0,  0x0,  0x20f, 0xc,  0x14, 0x0,
    139 	0x0, 0x3e9, 0x0,   0x0,  0x43, 0x363, 0x3e7, 0x80,  0x8 },
    140 #endif
    141 
    142 	{ FAL_VGA | RES_FAL_STMID,  /* stmid, 640x200, 4 colors		    */
    143 	0x2, 0x0, 0x50,  0x1, 0x0,   0x17, 0x12, 0x1,  0x20e, 0xd,  0x11, 0x0,
    144 	0x0, 0x419, 0x3af, 0x8f, 0x8f, 0x3af, 0x415, 0x186, 0x9 },
    145 
    146 	{ FAL_VGA | RES_FAL_STLOW,  /* stlow, 320x200, 16 colors	    */
    147 	0x2, 0x0, 0x50,  0x0, 0x0,   0x17, 0x12, 0x1,  0x20e, 0xd,  0x11, 0x0,
    148 	0x0, 0x419, 0x3af, 0x8f, 0x8f, 0x3af, 0x415, 0x186, 0x5 },
    149 
    150 	{ FAL_VGA | RES_FAL_TTLOW,  /* ttlow, 320x480, 256 colors	    */
    151 	0x2, 0x0, 0xa0,  0x0, 0x10,  0xc6, 0x8d, 0x15, 0x29a, 0x7b, 0x96, 0x0,
    152 	0x0, 0x419, 0x3ff, 0x3f, 0x3f, 0x3ff, 0x415, 0x186, 0x4 },
    153 
    154 	{ FAL_VGA | RES_VGA2,	    /* vga, 640x480, 2 colors		    */
    155 	0x2, 0x0, 0x28,  0x0, 0x400, 0xc6, 0x8d, 0x15, 0x273, 0x50, 0x96, 0x0,
    156 	0x0, 0x419, 0x3ff, 0x3f, 0x3f, 0x3ff, 0x415, 0x186, 0x8 },
    157 
    158 	{ FAL_VGA | RES_VGA4,	    /* vga, 640x480, 4 colors		    */
    159 	0x2, 0x0, 0x50,  0x1, 0x0,   0x17, 0x12, 0x1,  0x20e, 0xd,  0x11, 0x0,
    160 	0x0, 0x419, 0x3ff, 0x3f, 0x3f, 0x3ff, 0x415, 0x186, 0x8 },
    161 
    162 	{ FAL_VGA | RES_VGA16,	    /* vga, 640x480, 16 colors		    */
    163 	0x2, 0x0, 0xa0,  0x1, 0x0,   0xc6, 0x8d, 0x15, 0x2a3, 0x7c, 0x96, 0x0,
    164 	0x0, 0x419, 0x3ff, 0x3f, 0x3f, 0x3ff, 0x415, 0x186, 0x8 },
    165 
    166 	{ FAL_VGA | RES_VGA256,	    /* vga, 640x480, 256 colors		    */
    167 	0x2, 0x0, 0x140, 0x1, 0x10,  0xc6, 0x8d, 0x15, 0x2ab, 0x84, 0x96, 0x0,
    168 	0x0, 0x419, 0x3ff, 0x3f, 0x3f, 0x3ff, 0x415, 0x186, 0x8 },
    169 
    170 	{ FAL_VGA | RES_DIRECT,	    /* direct video, 320x200, 65536 colors  */
    171 	0x2, 0x0, 0x140, 0x0, 0x100, 0xc6, 0x8d, 0x15, 0x2ac, 0x91, 0x96, 0x0,
    172 	0x0, 0x419, 0x3ff, 0x3f, 0x3f, 0x3ff, 0x415, 0x186, 0x4 },
    173 
    174 	{ 0xffff }		    /* end of list			    */
    175 };
    176 
    177 static u_short mon_type;
    178 /*
    179  * XXX: called from ite console init routine.
    180  * Initialize list of possible video modes.
    181  */
    182 void
    183 falcon_probe_video(MODES *modelp)
    184 {
    185 	dmode_t	*dm;
    186 	struct videl *vregs;
    187 	int	i;
    188 
    189 	mon_type = *(volatile unsigned char *)(AD_FAL_MON_TYPE);
    190 	mon_type = (mon_type & 0xc0) << 2;
    191 
    192 	/*
    193 	 * get all possible modes
    194 	 */
    195 
    196 	for (i = 0; (dm = &vid_modes[i])->name != NULL; i++) {
    197 		dm->data = (void *)&fal_hwregs[i];
    198 		if (vm_mode(dm) == RES_FALAUTO) {
    199 			vm_regs(dm) = falcon_getreg(RES_FALAUTO);
    200 			falcon_detect(dm);
    201 			LIST_INSERT_HEAD(modelp, dm, link);
    202 		} else {
    203 			vregs = falcon_getreg(vm_mode(dm) | mon_type);
    204 			if (vregs) {
    205 				vm_regs(dm) = vregs;
    206 				LIST_INSERT_HEAD(modelp, dm, link);
    207 			}
    208 		}
    209 	}
    210 
    211 	/*
    212 	 * This seems to prevent bordered screens.
    213 	 */
    214 	for (i=0; i < 16; i++)
    215 		VIDEO->vd_fal_rgb[i] = CM_L2FAL(gra_def_color16[i]);
    216 }
    217 
    218 static struct videl *
    219 falcon_getreg(u_short mode)
    220 {
    221 	int i;
    222 	struct videl *vregs;
    223 
    224 	for (i = 0; (vregs = &videlinit[i])->video_mode != 0xffff; i++)
    225 		if ((vregs->video_mode) == mode)
    226 			return vregs;
    227 
    228 	return NULL; /* mode not found */
    229 }
    230 
    231 static void
    232 falcon_detect(dmode_t *dm)
    233 {
    234 	u_short	falshift, stshift;
    235 	struct videl *vregs = vm_regs(dm);
    236 
    237 	/*
    238 	 * First get the videl register values
    239 	 */
    240 
    241 	vregs->vd_syncmode	= VIDEO->vd_sync;
    242 	vregs->vd_line_wide	= VIDEO->vd_line_wide;
    243 	vregs->vd_vert_wrap	= VIDEO->vd_vert_wrap;
    244 	vregs->vd_st_res	= VIDEO->vd_st_res;
    245 	vregs->vd_fal_res	= VIDEO->vd_fal_res;
    246 	vregs->vd_h_hold_tim	= VIDEO->vd_h_hold_tim;
    247 	vregs->vd_h_bord_beg	= VIDEO->vd_h_bord_beg;
    248 	vregs->vd_h_bord_end	= VIDEO->vd_h_bord_end;
    249 	vregs->vd_h_dis_beg	= VIDEO->vd_h_dis_beg;
    250 	vregs->vd_h_dis_end	= VIDEO->vd_h_dis_end;
    251 	vregs->vd_h_ss		= VIDEO->vd_h_ss;
    252 	vregs->vd_h_fs		= VIDEO->vd_h_fs;
    253 	vregs->vd_h_hh		= VIDEO->vd_h_hh;
    254 	vregs->vd_v_freq_tim	= VIDEO->vd_v_freq_tim;
    255 	vregs->vd_v_bord_beg	= VIDEO->vd_v_bord_beg;
    256 	vregs->vd_v_bord_end	= VIDEO->vd_v_bord_end;
    257 	vregs->vd_v_dis_beg	= VIDEO->vd_v_dis_beg;
    258 	vregs->vd_v_dis_end	= VIDEO->vd_v_dis_end;
    259 	vregs->vd_v_ss		= VIDEO->vd_v_ss;
    260 	vregs->vd_fal_ctrl	= VIDEO->vd_fal_ctrl;
    261 	vregs->vd_fal_mode	= VIDEO->vd_fal_mode;
    262 
    263 
    264 	/*
    265 	 * Calculate the depth of the screen
    266 	 */
    267 
    268 	falshift = vregs->vd_fal_res;
    269 	stshift = vregs->vd_st_res;
    270 
    271 	if (falshift & 0x400)		/* 2 color */
    272 		dm->depth = 1;
    273 	else if (falshift & 0x100)	/* high color, direct */
    274 		dm->depth = 16;
    275 	else if (falshift & 0x10)	/* 256 color */
    276 		dm->depth = 8;
    277 	else if (stshift == 0)		/* 16 color */
    278 		dm->depth = 4;
    279 	else if (stshift == 1)		/* 4 color */
    280 		dm->depth = 2;
    281 	else
    282 		dm->depth = 1;		/* 2 color */
    283 
    284 	/*
    285 	 * Now calculate the screen hight
    286 	 */
    287 
    288 	dm->size.height = vregs->vd_v_dis_end - vregs->vd_v_dis_beg;
    289 	if (!((vregs->vd_fal_mode & 0x2) >> 1)) /* if not interlaced */
    290 		dm->size.height >>=1;
    291 	if (vregs->vd_fal_mode & 0x1)		/* if doublescan */
    292 		dm->size.height >>=1;
    293 
    294 	/*
    295 	 * And the width
    296 	 */
    297 
    298 	dm->size.width = vregs->vd_vert_wrap * 16 / dm->depth;
    299 
    300 }
    301 
    302 u_long	falcon_needs_vbl;
    303 void falcon_display_switch(void);
    304 
    305 static void
    306 falcon_display_view(view_t *v)
    307 {
    308 	dmode_t	*dm = v->mode;
    309 	bmap_t		*bm;
    310 	struct videl	*vregs;
    311 	static u_short last_mode = 0xffff;
    312 
    313 	if (dm->current_view) {
    314 		/*
    315 		 * Mark current view for this mode as no longer displayed
    316 		 */
    317 		dm->current_view->flags &= ~VF_DISPLAY;
    318 	}
    319 	dm->current_view = v;
    320 	v->flags |= VF_DISPLAY;
    321 	vregs = vm_regs(v->mode);
    322 
    323 	falcon_use_colormap(v, v->colormap);
    324 
    325 	bm = v->bitmap;
    326 	VIDEO->vd_ramh   = ((u_long)bm->hw_address >> 16) & 0xff;
    327 	VIDEO->vd_ramm   = ((u_long)bm->hw_address >>  8) & 0xff;
    328 	VIDEO->vd_raml   =  (u_long)bm->hw_address & 0xff;
    329 
    330 	if (last_mode != vregs->video_mode) {
    331 		last_mode = vregs->video_mode;
    332 
    333 		if (dm->depth == 1) {
    334 			/*
    335 			 * Set the resolution registers to a mode, which guarantee
    336 			 * no shifting when the register are written during vbl.
    337 			 */
    338 			VIDEO->vd_fal_res = 0;
    339 			VIDEO->vd_st_res = 0;
    340 		}
    341 
    342 		/*
    343 		 * Arrange for them to be activated
    344 		 * at the second vbl interrupt.
    345 		 */
    346 		falcon_needs_vbl = (u_long)v;
    347 	}
    348 }
    349 
    350 void
    351 falcon_display_switch(void)
    352 {
    353 	view_t		*v;
    354 	struct videl	*vregs;
    355 	static int vbl_count = 1;
    356 
    357 	if (vbl_count--)
    358 		return;
    359 
    360 	v = (view_t*)falcon_needs_vbl;
    361 
    362 	vbl_count = 1;
    363 	falcon_needs_vbl = 0;
    364 
    365 	/*
    366 	 * Write to videl registers only on VGA displays
    367 	 * This is only a hack. Must be fixed soon. XXX -- Thomas
    368 	 */
    369 	if (mon_type != FAL_VGA)
    370 		return;
    371 
    372 	vregs = vm_regs(v->mode);
    373 
    374 	VIDEO->vd_v_freq_tim    = vregs->vd_v_freq_tim;
    375 	VIDEO->vd_v_ss          = vregs->vd_v_ss;
    376 	VIDEO->vd_v_bord_beg    = vregs->vd_v_bord_beg;
    377 	VIDEO->vd_v_bord_end    = vregs->vd_v_bord_end;
    378 	VIDEO->vd_v_dis_beg     = vregs->vd_v_dis_beg;
    379 	VIDEO->vd_v_dis_end     = vregs->vd_v_dis_end;
    380 	VIDEO->vd_h_hold_tim    = vregs->vd_h_hold_tim;
    381 	VIDEO->vd_h_ss          = vregs->vd_h_ss;
    382 	VIDEO->vd_h_bord_beg    = vregs->vd_h_bord_beg;
    383 	VIDEO->vd_h_bord_end    = vregs->vd_h_bord_end;
    384 	VIDEO->vd_h_dis_beg     = vregs->vd_h_dis_beg;
    385 	VIDEO->vd_h_dis_end     = vregs->vd_h_dis_end;
    386 #if 0 /* This seems not to be necessary -- Thomas */
    387 	VIDEO->vd_h_fs		= vregs->vd_h_fs;
    388 	VIDEO->vd_h_hh		= vregs->vd_h_hh;
    389 #endif
    390 	VIDEO->vd_sync          = vregs->vd_syncmode;
    391 	VIDEO->vd_fal_res       = 0;
    392 	if (v->mode->depth == 2)
    393 		VIDEO->vd_st_res        = vregs->vd_st_res;
    394 	else {
    395 		VIDEO->vd_st_res        = 0;
    396 		VIDEO->vd_fal_res       = vregs->vd_fal_res;
    397 	}
    398 	VIDEO->vd_vert_wrap     = vregs->vd_vert_wrap;
    399 	VIDEO->vd_line_wide     = vregs->vd_line_wide;
    400 	VIDEO->vd_fal_ctrl      = vregs->vd_fal_ctrl;
    401 	VIDEO->vd_fal_mode      = vregs->vd_fal_mode;
    402 }
    403 
    404 static void
    405 falcon_remove_view(view_t *v)
    406 {
    407 	dmode_t *mode = v->mode;
    408 
    409 	if (mode->current_view == v) {
    410 #if 0
    411 		if (v->flags & VF_DISPLAY)
    412 			panic("Cannot shutdown display"); /* XXX */
    413 #endif
    414 		mode->current_view = NULL;
    415 	}
    416 	v->flags &= ~VF_DISPLAY;
    417 }
    418 
    419 void
    420 falcon_save_view(view_t *v)
    421 {
    422 }
    423 
    424 static void
    425 falcon_free_view(view_t *v)
    426 {
    427 	if (v) {
    428 		falcon_remove_view(v);
    429 		if (v->colormap != &gra_con_cmap)
    430 			free(v->colormap, M_DEVBUF);
    431 		free_bitmap(v->bitmap);
    432 		if (v != &gra_con_view)
    433 			free(v, M_DEVBUF);
    434 	}
    435 }
    436 
    437 static int
    438 falcon_use_colormap(view_t *v, colormap_t *cm)
    439 {
    440 	dmode_t			*dm;
    441 	volatile u_short	*creg;
    442 	volatile u_long		*fcreg;
    443 	u_long			*src;
    444 	colormap_t		*vcm;
    445 	u_long			*vcreg;
    446 	u_short			ncreg;
    447 	int			last_streg;
    448 	int			i;
    449 
    450 	dm  = v->mode;
    451 	vcm = v->colormap;
    452 
    453 	/*
    454 	 * I guess it seems reasonable to require the maps to be
    455 	 * of the same type...
    456 	 */
    457 	if (cm->type != vcm->type)
    458 		return (EINVAL);
    459 
    460 	/*
    461 	 * First get the colormap addresses an calculate
    462 	 * howmany colors are in it.
    463 	 */
    464 	if (dm->depth == 16) /* direct color, no colormap;
    465 				but also not (yet) supported */
    466 		return(0);
    467 	fcreg = &VIDEO->vd_fal_rgb[0];
    468 	creg  = &VIDEO->vd_st_rgb[0];
    469 	ncreg = 1 << dm->depth;
    470 
    471 	/* If first entry specified beyond capabilities -> error */
    472 	if (cm->first >= ncreg)
    473 		return (EINVAL);
    474 
    475 	/*
    476 	 * A little tricky, the actual colormap pointer will be NULL
    477 	 * when view is not displaying, valid otherwise.
    478 	 */
    479 	if (v->flags & VF_DISPLAY) {
    480 		creg = &creg[cm->first];
    481 		fcreg = &fcreg[cm->first];
    482 	} else {
    483 		creg = NULL;
    484 		fcreg = NULL;
    485 	}
    486 
    487 	vcreg  = &vcm->entry[cm->first];
    488 	ncreg -= cm->first;
    489 	last_streg = 16 - cm->first;
    490 	if (cm->size > ncreg)
    491 		return (EINVAL);
    492 	ncreg = cm->size;
    493 
    494 	for (i = 0, src = cm->entry; i < ncreg; i++, vcreg++) {
    495 		*vcreg = *src++;
    496 
    497 		/*
    498 		 * If displaying, also update actual color register.
    499 		 */
    500 		if (fcreg != NULL) {
    501 			*fcreg++ = CM_L2FAL(*vcreg);
    502 			if (i < last_streg)
    503 				*creg++ = CM_L2ST(*vcreg);
    504 		}
    505 	}
    506 	return (0);
    507 }
    508 
    509 static view_t *
    510 falcon_alloc_view(dmode_t *mode, dimen_t *dim, u_char depth)
    511 {
    512 	view_t *v;
    513 	bmap_t *bm;
    514 
    515 	if (!atari_realconfig)
    516 		v = &gra_con_view;
    517 	else
    518 		v = malloc(sizeof(*v), M_DEVBUF, M_NOWAIT);
    519 	if (v == NULL)
    520 		return(NULL);
    521 
    522 	bm = alloc_bitmap(mode->size.width, mode->size.height, mode->depth);
    523 	if (bm) {
    524 		box_t   box;
    525 
    526 		v->colormap = alloc_colormap(mode);
    527 		if (v->colormap) {
    528 			INIT_BOX(&box,0,0,mode->size.width,mode->size.height);
    529 			init_view(v, bm, mode, &box);
    530 			return(v);
    531 		}
    532 		free_bitmap(bm);
    533 	}
    534 	if (v != &gra_con_view)
    535 		free(v, M_DEVBUF);
    536 	return (NULL);
    537 }
    538 
    539 static void
    540 init_view(view_t *v, bmap_t *bm, dmode_t *mode, box_t *dbox)
    541 {
    542 	v->bitmap = bm;
    543 	v->mode   = mode;
    544 	v->flags  = 0;
    545 	memcpy(&v->display, dbox, sizeof(box_t));
    546 }
    547 
    548 /* bitmap functions */
    549 
    550 static bmap_t *
    551 alloc_bitmap(u_long width, u_long height, u_char depth)
    552 {
    553 	u_long  total_size, bm_size;
    554 	void	*hw_address;
    555 	bmap_t	*bm;
    556 
    557 	/*
    558 	 * Sigh, it seems for mapping to work we need the bitplane data to
    559 	 *  1: be aligned on a page boundry.
    560 	 *  2: be n pages large.
    561 	 *
    562 	 * why? because the user gets a page aligned address, if this is before
    563 	 * your allocation, too bad.  Also it seems that the mapping routines
    564 	 * do not watch to closely to the allowable length. so if you go over
    565 	 * n pages by less than another page, the user gets to write all over
    566 	 * the entire page. Since you did not allocate up to a page boundry
    567 	 * (or more) the user writes into someone elses memory. -ch
    568 	 */
    569 	bm_size    = m68k_round_page((width * height * depth) / NBBY);
    570 	total_size = bm_size + sizeof(bmap_t) + PAGE_SIZE;
    571 
    572 	if ((bm = (bmap_t*)alloc_stmem(total_size, &hw_address)) == NULL)
    573 		return(NULL);
    574 
    575 	bm->plane         = (u_char*)bm + sizeof(bmap_t);
    576 	bm->plane         = (u_char*)m68k_round_page(bm->plane);
    577 	bm->hw_address    = (u_char*)hw_address + sizeof(bmap_t);
    578 	bm->hw_address    = (u_char*)m68k_round_page(bm->hw_address);
    579 	bm->bytes_per_row = (width * depth) / NBBY;
    580 	bm->rows          = height;
    581 	bm->depth         = depth;
    582 	bm->phys_mappable = (depth * width * height) / NBBY;
    583 	bm->regs          = NULL;
    584 	bm->hw_regs       = NULL;
    585 	bm->reg_size      = 0;
    586 	bm->vga_address   = NULL;
    587 	bm->vga_mappable  = 0;
    588 	bm->lin_base      = 0;
    589 	bm->vga_base      = 0;
    590 
    591 	memset(bm->plane, 0, bm_size);
    592 	return (bm);
    593 }
    594 
    595 static void
    596 free_bitmap(bmap_t *bm)
    597 {
    598 	if (bm)
    599 		free_stmem(bm);
    600 }
    601 
    602 static colormap_t *
    603 alloc_colormap(dmode_t *dm)
    604 {
    605 	int		nentries, i;
    606 	colormap_t	*cm;
    607 	u_char		type = CM_COLOR;
    608 
    609 	if (dm->depth == 16) /* direct color, no colormap;
    610 				not (yet) supported */
    611 		nentries = 0;
    612 	else
    613 		nentries = 1 << dm->depth;
    614 
    615 	if (!atari_realconfig) {
    616 		cm = &gra_con_cmap;
    617 		cm->entry = gra_con_colors;
    618 	} else {
    619 		int size;
    620 
    621 		size = sizeof(*cm) + (nentries * sizeof(cm->entry[0]));
    622 		cm   = malloc(size, M_DEVBUF, M_NOWAIT);
    623 		if (cm == NULL)
    624 			return(NULL);
    625 		cm->entry = (long *)&cm[1];
    626 
    627 	}
    628 
    629 	if ((cm->type = type) == CM_COLOR)
    630 		cm->red_mask = cm->green_mask = cm->blue_mask = 0x3f;
    631 
    632 	cm->first = 0;
    633 	cm->size  = nentries;
    634 
    635 	for (i = 0; i < nentries; i++)
    636 		cm->entry[i] = gra_def_color16[i % 16];
    637 	return (cm);
    638 }
    639 #endif /* FALCON_VIDEO */
    640