Home | History | Annotate | Line # | Download | only in dev
grfabs_tt.c revision 1.2
      1 /*	$NetBSD: grfabs_tt.c,v 1.2 1996/02/22 10:11:25 leo Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Leo Weppelman.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Leo Weppelman.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #ifdef TT_VIDEO
     34 /*
     35  *  atari abstract graphics driver: TT-interface
     36  */
     37 #include <sys/param.h>
     38 #include <sys/queue.h>
     39 #include <sys/malloc.h>
     40 #include <sys/device.h>
     41 #include <sys/systm.h>
     42 
     43 #include <machine/iomap.h>
     44 #include <machine/video.h>
     45 #include <machine/mfp.h>
     46 #include <atari/atari/device.h>
     47 #include <atari/dev/grfabs_reg.h>
     48 
     49 /*
     50  * Function decls
     51  */
     52 static void       init_view __P((view_t *, bmap_t *, dmode_t *, box_t *));
     53 static bmap_t	  *alloc_bitmap __P((u_long, u_long, u_char));
     54 static colormap_t *alloc_colormap __P((dmode_t *));
     55 static void 	  free_bitmap __P((bmap_t *));
     56 static void	  tt_display_view __P((view_t *));
     57 static view_t	  *tt_alloc_view __P((dmode_t *, dimen_t *, u_char));
     58 static void	  tt_free_view __P((view_t *));
     59 static void	  tt_remove_view __P((view_t *));
     60 static int	  tt_use_colormap __P((view_t *, colormap_t *));
     61 
     62 /*
     63  * Our function switch table
     64  */
     65 struct grfabs_sw tt_vid_sw = {
     66 	tt_display_view,
     67 	tt_alloc_view,
     68 	tt_free_view,
     69 	tt_remove_view,
     70 	tt_use_colormap
     71 };
     72 
     73 static dmode_t vid_modes[] = {
     74 	{ { NULL, NULL }, "sthigh", { 640,  400 },  1, RES_STHIGH, &tt_vid_sw },
     75 	{ { NULL, NULL }, "tthigh", { 1280, 960 },  1, RES_TTHIGH, &tt_vid_sw },
     76 	{ { NULL, NULL }, "stmid",  { 640,  200 },  2, RES_STMID , &tt_vid_sw },
     77 	{ { NULL, NULL }, "stlow",  { 320,  200 },  4, RES_STLOW , &tt_vid_sw },
     78 	{ { NULL, NULL }, "ttmid",  { 640,  480 },  4, RES_TTMID , &tt_vid_sw },
     79 	{ { NULL, NULL }, "ttlow",  { 320,  480 },  8, RES_TTLOW , &tt_vid_sw },
     80 	{ { NULL, NULL }, NULL,  }
     81 };
     82 
     83 /*
     84  * XXX: called from ite console init routine.
     85  * Initialize list of posible video modes.
     86  */
     87 void
     88 tt_probe_video(modelp)
     89 MODES	*modelp;
     90 {
     91 	dmode_t	*dm;
     92 	int	i;
     93 	int	has_mono;
     94 
     95 	/*
     96 	 * First find out what kind of monitor is attached. Dma-sound
     97 	 * should be off because the 'sound-done' and 'monochrome-detect'
     98 	 * are xor-ed together. I think that shutting it down here is the
     99 	 * wrong place.
    100 	 */
    101 	has_mono = (MFP->mf_gpip & IA_MONO) == 0;
    102 
    103 	for (i = 0; (dm = &vid_modes[i])->name != NULL; i++) {
    104 		if (has_mono && (dm->vm_reg != RES_TTHIGH))
    105 			continue;
    106 		if (!has_mono && (dm->vm_reg == RES_TTHIGH))
    107 			continue;
    108 		LIST_INSERT_HEAD(modelp, dm, link);
    109 	}
    110 
    111 	for (i=0; i < 16; i++)
    112 		VIDEO->vd_tt_rgb[i] = CM_L2TT(gra_def_color16[i]);
    113 }
    114 
    115 static void
    116 tt_display_view(v)
    117 view_t *v;
    118 {
    119 	dmode_t	*dm = v->mode;
    120 	bmap_t	*bm = v->bitmap;
    121 
    122 	if (dm->current_view) {
    123 		/*
    124 		 * Mark current view for this mode as no longer displayed
    125 		 */
    126 		dm->current_view->flags &= ~VF_DISPLAY;
    127 	}
    128 	dm->current_view = v;
    129 	v->flags |= VF_DISPLAY;
    130 
    131 	tt_use_colormap(v, v->colormap);
    132 
    133 	/* XXX: should use vbl for this	*/
    134 	VIDEO->vd_tt_res = dm->vm_reg;
    135 	VIDEO->vd_raml   =  (u_long)bm->hw_address & 0xff;
    136 	VIDEO->vd_ramm   = ((u_long)bm->hw_address >>  8) & 0xff;
    137 	VIDEO->vd_ramh   = ((u_long)bm->hw_address >> 16) & 0xff;
    138 }
    139 
    140 void
    141 tt_remove_view(v)
    142 view_t *v;
    143 {
    144 	dmode_t *mode = v->mode;
    145 
    146 	if (mode->current_view == v) {
    147 #if 0
    148 		if (v->flags & VF_DISPLAY)
    149 			panic("Cannot shutdown display\n"); /* XXX */
    150 #endif
    151 		mode->current_view = NULL;
    152 	}
    153 	v->flags &= ~VF_DISPLAY;
    154 }
    155 
    156 void
    157 tt_free_view(v)
    158 view_t *v;
    159 {
    160 	if(v) {
    161 		tt_remove_view(v);
    162 		if (v->colormap != &gra_con_cmap)
    163 			free(v->colormap, M_DEVBUF);
    164 		free_bitmap(v->bitmap);
    165 		if (v != &gra_con_view)
    166 			free(v, M_DEVBUF);
    167 	}
    168 }
    169 
    170 static int
    171 tt_use_colormap(v, cm)
    172 view_t		*v;
    173 colormap_t	*cm;
    174 {
    175 	dmode_t			*dm;
    176 	volatile u_short	*creg;
    177 	u_long			*src;
    178 	colormap_t		*vcm;
    179 	u_long			*vcreg;
    180 	u_short			ncreg;
    181 	int			i;
    182 
    183 	dm  = v->mode;
    184 	vcm = v->colormap;
    185 
    186 	/*
    187 	 * I guess it seems reasonable to require the maps to be
    188 	 * of the same type...
    189 	 */
    190 	if (cm->type != vcm->type)
    191 		return(EINVAL);
    192 
    193 	/*
    194 	 * First figure out where the actual colormap resides and
    195 	 * howmany colors are in it.
    196 	 */
    197 	switch (dm->vm_reg) {
    198 		case RES_STLOW:
    199 			creg  = &VIDEO->vd_tt_rgb[0];
    200 			ncreg = 16;
    201 			break;
    202 		case RES_STMID:
    203 			creg  = &VIDEO->vd_tt_rgb[0];
    204 			ncreg = 4;
    205 			break;
    206 		case RES_STHIGH:
    207 			creg  = &VIDEO->vd_tt_rgb[254];
    208 			ncreg = 2;
    209 			break;
    210 		case RES_TTLOW:
    211 			creg  = &VIDEO->vd_tt_rgb[0];
    212 			ncreg = 256;
    213 			break;
    214 		case RES_TTMID:
    215 			creg  = &VIDEO->vd_tt_rgb[0];
    216 			ncreg = 16;
    217 			break;
    218 		case RES_TTHIGH:
    219 			return(0);	/* No colors	*/
    220 		default:
    221 			panic("grf_tt:use_colormap: wrong mode!?");
    222 	}
    223 
    224 	/* If first entry specified beyond capabilities -> error */
    225 	if (cm->first >= ncreg)
    226 		return(EINVAL);
    227 
    228 	/*
    229 	 * A little tricky, the actual colormap pointer will be NULL
    230 	 * when view is not displaying, valid otherwise.
    231 	 */
    232 	if (v->flags & VF_DISPLAY)
    233 		creg = &creg[cm->first];
    234 	else creg = NULL;
    235 
    236 	vcreg  = &vcm->entry[cm->first];
    237 	ncreg -= cm->first;
    238 	if (cm->size > ncreg)
    239 		return(EINVAL);
    240 	ncreg = cm->size;
    241 
    242 	for (i = 0, src = cm->entry; i < ncreg; i++, vcreg++) {
    243 		*vcreg = *src++;
    244 
    245 		/*
    246 		 * If displaying, also update actual color registers.
    247 		 */
    248 		if (creg != NULL)
    249 			*creg++ = CM_L2TT(*vcreg);
    250 	}
    251 	return (0);
    252 }
    253 
    254 static view_t *
    255 tt_alloc_view(mode, dim, depth)
    256 dmode_t	*mode;
    257 dimen_t	*dim;
    258 u_char   depth;
    259 {
    260 	view_t *v;
    261 	bmap_t *bm;
    262 
    263 	if (!atari_realconfig)
    264 		v = &gra_con_view;
    265 	else v = malloc(sizeof(*v), M_DEVBUF, M_NOWAIT);
    266 	if(v == NULL)
    267 		return (NULL);
    268 
    269 	bm = alloc_bitmap(mode->size.width, mode->size.height, mode->depth);
    270 	if (bm) {
    271 		box_t   box;
    272 
    273 		v->colormap = alloc_colormap(mode);
    274 		if (v->colormap) {
    275 			INIT_BOX(&box,0,0,mode->size.width,mode->size.height);
    276 			init_view(v, bm, mode, &box);
    277 			return (v);
    278 		}
    279 		free_bitmap(bm);
    280 	}
    281 	if (v != &gra_con_view)
    282 		free(v, M_DEVBUF);
    283 	return (NULL);
    284 }
    285 
    286 static void
    287 init_view(v, bm, mode, dbox)
    288 view_t	*v;
    289 bmap_t	*bm;
    290 dmode_t	*mode;
    291 box_t	*dbox;
    292 {
    293 	v->bitmap = bm;
    294 	v->mode   = mode;
    295 	v->flags  = 0;
    296 	bcopy(dbox, &v->display, sizeof(box_t));
    297 }
    298 
    299 /* bitmap functions */
    300 
    301 static bmap_t *
    302 alloc_bitmap(width, height, depth)
    303 u_long	width, height;
    304 u_char	depth;
    305 {
    306 	u_long  total_size, bm_size;
    307 	void	*hw_address;
    308 	bmap_t	*bm;
    309 
    310 	/*
    311 	 * Sigh, it seems for mapping to work we need the bitplane data to
    312 	 *  1: be aligned on a page boundry.
    313 	 *  2: be n pages large.
    314 	 *
    315 	 * why? because the user gets a page aligned address, if this is before
    316 	 * your allocation, too bad.  Also it seems that the mapping routines
    317 	 * do not watch to closely to the allowable length. so if you go over
    318 	 * n pages by less than another page, the user gets to write all over
    319 	 * the entire page. Since you did not allocate up to a page boundry
    320 	 * (or more) the user writes into someone elses memory. -ch
    321 	 */
    322 	bm_size    = atari_round_page((width * height * depth) / NBBY);
    323 	total_size = bm_size + sizeof(bmap_t) + NBPG;
    324 
    325 	if ((bm = (bmap_t*)alloc_stmem(total_size, &hw_address)) == NULL)
    326 		return(NULL);
    327 
    328 	bm->plane         = (u_char*)bm + sizeof(bmap_t);
    329 	bm->plane         = (u_char*)atari_round_page(bm->plane);
    330 	bm->hw_address    = (u_char*)hw_address + sizeof(bmap_t);
    331 	bm->hw_address    = (u_char*)atari_round_page(bm->hw_address);
    332 	bm->bytes_per_row = (width * depth) / NBBY;
    333 	bm->rows          = height;
    334 	bm->depth         = depth;
    335 
    336 	bzero(bm->plane, bm_size);
    337 	return (bm);
    338 }
    339 
    340 static void
    341 free_bitmap(bm)
    342 bmap_t *bm;
    343 {
    344 	if (bm)
    345 		free_stmem(bm);
    346 }
    347 
    348 static colormap_t *
    349 alloc_colormap(dm)
    350 dmode_t		*dm;
    351 {
    352 	int		nentries, i;
    353 	colormap_t	*cm;
    354 	u_char		type = CM_COLOR;
    355 
    356 	switch (dm->vm_reg) {
    357 		case RES_STLOW:
    358 		case RES_TTMID:
    359 			nentries = 16;
    360 			break;
    361 		case RES_STMID:
    362 			nentries = 4;
    363 			break;
    364 		case RES_STHIGH:
    365 			nentries = 2;
    366 			break;
    367 		case RES_TTLOW:
    368 			nentries = 256;
    369 			break;
    370 		case RES_TTHIGH:
    371 			type     = CM_MONO;
    372 			nentries = 0;
    373 			break;
    374 		default:
    375 			panic("grf_tt:alloc_colormap: wrong mode!?");
    376 	}
    377 	if (!atari_realconfig) {
    378 		cm = &gra_con_cmap;
    379 		cm->entry = gra_con_colors;
    380 	}
    381 	else {
    382 		int size;
    383 
    384 		size = sizeof(*cm) + (nentries * sizeof(cm->entry[0]));
    385 		cm   = malloc(size, M_DEVBUF, M_NOWAIT);
    386 		if (cm == NULL)
    387 			return (NULL);
    388 		cm->entry = (long *)&cm[1];
    389 
    390 	}
    391 	if ((cm->type = type) == CM_COLOR)
    392 		cm->red_mask = cm->green_mask = cm->blue_mask = 0xf;
    393 	cm->first = 0;
    394 	cm->size  = nentries;
    395 
    396 	for (i = 0; i < nentries; i++)
    397 		cm->entry[i] = gra_def_color16[i % 16];
    398 	return (cm);
    399 }
    400 #endif /* TT_VIDEO */
    401