Home | History | Annotate | Line # | Download | only in dev
grfabs_tt.c revision 1.3
      1 /*	$NetBSD: grfabs_tt.c,v 1.3 1996/04/12 09:05:40 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/atari/stalloc.h>
     48 #include <atari/dev/grfabs_reg.h>
     49 
     50 /*
     51  * Function decls
     52  */
     53 static void       init_view __P((view_t *, bmap_t *, dmode_t *, box_t *));
     54 static bmap_t	  *alloc_bitmap __P((u_long, u_long, u_char));
     55 static colormap_t *alloc_colormap __P((dmode_t *));
     56 static void 	  free_bitmap __P((bmap_t *));
     57 static void	  tt_display_view __P((view_t *));
     58 static view_t	  *tt_alloc_view __P((dmode_t *, dimen_t *, u_char));
     59 static void	  tt_free_view __P((view_t *));
     60 static void	  tt_remove_view __P((view_t *));
     61 static int	  tt_use_colormap __P((view_t *, colormap_t *));
     62 
     63 /*
     64  * Our function switch table
     65  */
     66 struct grfabs_sw tt_vid_sw = {
     67 	tt_display_view,
     68 	tt_alloc_view,
     69 	tt_free_view,
     70 	tt_remove_view,
     71 	tt_use_colormap
     72 };
     73 
     74 static dmode_t vid_modes[] = {
     75 	{ { NULL, NULL }, "sthigh", { 640,  400 },  1, RES_STHIGH, &tt_vid_sw },
     76 	{ { NULL, NULL }, "tthigh", { 1280, 960 },  1, RES_TTHIGH, &tt_vid_sw },
     77 	{ { NULL, NULL }, "stmid",  { 640,  200 },  2, RES_STMID , &tt_vid_sw },
     78 	{ { NULL, NULL }, "stlow",  { 320,  200 },  4, RES_STLOW , &tt_vid_sw },
     79 	{ { NULL, NULL }, "ttmid",  { 640,  480 },  4, RES_TTMID , &tt_vid_sw },
     80 	{ { NULL, NULL }, "ttlow",  { 320,  480 },  8, RES_TTLOW , &tt_vid_sw },
     81 	{ { NULL, NULL }, NULL,  }
     82 };
     83 
     84 /*
     85  * XXX: called from ite console init routine.
     86  * Initialize list of posible video modes.
     87  */
     88 void
     89 tt_probe_video(modelp)
     90 MODES	*modelp;
     91 {
     92 	dmode_t	*dm;
     93 	int	i;
     94 	int	has_mono;
     95 
     96 	/*
     97 	 * First find out what kind of monitor is attached. Dma-sound
     98 	 * should be off because the 'sound-done' and 'monochrome-detect'
     99 	 * are xor-ed together. I think that shutting it down here is the
    100 	 * wrong place.
    101 	 */
    102 	has_mono = (MFP->mf_gpip & IA_MONO) == 0;
    103 
    104 	for (i = 0; (dm = &vid_modes[i])->name != NULL; i++) {
    105 		if (has_mono && (dm->vm_reg != RES_TTHIGH))
    106 			continue;
    107 		if (!has_mono && (dm->vm_reg == RES_TTHIGH))
    108 			continue;
    109 		LIST_INSERT_HEAD(modelp, dm, link);
    110 	}
    111 
    112 	for (i=0; i < 16; i++)
    113 		VIDEO->vd_tt_rgb[i] = CM_L2TT(gra_def_color16[i]);
    114 }
    115 
    116 static void
    117 tt_display_view(v)
    118 view_t *v;
    119 {
    120 	dmode_t	*dm = v->mode;
    121 	bmap_t	*bm = v->bitmap;
    122 
    123 	if (dm->current_view) {
    124 		/*
    125 		 * Mark current view for this mode as no longer displayed
    126 		 */
    127 		dm->current_view->flags &= ~VF_DISPLAY;
    128 	}
    129 	dm->current_view = v;
    130 	v->flags |= VF_DISPLAY;
    131 
    132 	tt_use_colormap(v, v->colormap);
    133 
    134 	/* XXX: should use vbl for this	*/
    135 	VIDEO->vd_tt_res = dm->vm_reg;
    136 	VIDEO->vd_raml   =  (u_long)bm->hw_address & 0xff;
    137 	VIDEO->vd_ramm   = ((u_long)bm->hw_address >>  8) & 0xff;
    138 	VIDEO->vd_ramh   = ((u_long)bm->hw_address >> 16) & 0xff;
    139 }
    140 
    141 void
    142 tt_remove_view(v)
    143 view_t *v;
    144 {
    145 	dmode_t *mode = v->mode;
    146 
    147 	if (mode->current_view == v) {
    148 #if 0
    149 		if (v->flags & VF_DISPLAY)
    150 			panic("Cannot shutdown display\n"); /* XXX */
    151 #endif
    152 		mode->current_view = NULL;
    153 	}
    154 	v->flags &= ~VF_DISPLAY;
    155 }
    156 
    157 void
    158 tt_free_view(v)
    159 view_t *v;
    160 {
    161 	if(v) {
    162 		tt_remove_view(v);
    163 		if (v->colormap != &gra_con_cmap)
    164 			free(v->colormap, M_DEVBUF);
    165 		free_bitmap(v->bitmap);
    166 		if (v != &gra_con_view)
    167 			free(v, M_DEVBUF);
    168 	}
    169 }
    170 
    171 static int
    172 tt_use_colormap(v, cm)
    173 view_t		*v;
    174 colormap_t	*cm;
    175 {
    176 	dmode_t			*dm;
    177 	volatile u_short	*creg;
    178 	u_long			*src;
    179 	colormap_t		*vcm;
    180 	u_long			*vcreg;
    181 	u_short			ncreg;
    182 	int			i;
    183 
    184 	dm  = v->mode;
    185 	vcm = v->colormap;
    186 
    187 	/*
    188 	 * I guess it seems reasonable to require the maps to be
    189 	 * of the same type...
    190 	 */
    191 	if (cm->type != vcm->type)
    192 		return(EINVAL);
    193 
    194 	/*
    195 	 * First figure out where the actual colormap resides and
    196 	 * howmany colors are in it.
    197 	 */
    198 	switch (dm->vm_reg) {
    199 		case RES_STLOW:
    200 			creg  = &VIDEO->vd_tt_rgb[0];
    201 			ncreg = 16;
    202 			break;
    203 		case RES_STMID:
    204 			creg  = &VIDEO->vd_tt_rgb[0];
    205 			ncreg = 4;
    206 			break;
    207 		case RES_STHIGH:
    208 			creg  = &VIDEO->vd_tt_rgb[254];
    209 			ncreg = 2;
    210 			break;
    211 		case RES_TTLOW:
    212 			creg  = &VIDEO->vd_tt_rgb[0];
    213 			ncreg = 256;
    214 			break;
    215 		case RES_TTMID:
    216 			creg  = &VIDEO->vd_tt_rgb[0];
    217 			ncreg = 16;
    218 			break;
    219 		case RES_TTHIGH:
    220 			return(0);	/* No colors	*/
    221 		default:
    222 			panic("grf_tt:use_colormap: wrong mode!?");
    223 	}
    224 
    225 	/* If first entry specified beyond capabilities -> error */
    226 	if (cm->first >= ncreg)
    227 		return(EINVAL);
    228 
    229 	/*
    230 	 * A little tricky, the actual colormap pointer will be NULL
    231 	 * when view is not displaying, valid otherwise.
    232 	 */
    233 	if (v->flags & VF_DISPLAY)
    234 		creg = &creg[cm->first];
    235 	else creg = NULL;
    236 
    237 	vcreg  = &vcm->entry[cm->first];
    238 	ncreg -= cm->first;
    239 	if (cm->size > ncreg)
    240 		return(EINVAL);
    241 	ncreg = cm->size;
    242 
    243 	for (i = 0, src = cm->entry; i < ncreg; i++, vcreg++) {
    244 		*vcreg = *src++;
    245 
    246 		/*
    247 		 * If displaying, also update actual color registers.
    248 		 */
    249 		if (creg != NULL)
    250 			*creg++ = CM_L2TT(*vcreg);
    251 	}
    252 	return (0);
    253 }
    254 
    255 static view_t *
    256 tt_alloc_view(mode, dim, depth)
    257 dmode_t	*mode;
    258 dimen_t	*dim;
    259 u_char   depth;
    260 {
    261 	view_t *v;
    262 	bmap_t *bm;
    263 
    264 	if (!atari_realconfig)
    265 		v = &gra_con_view;
    266 	else v = malloc(sizeof(*v), M_DEVBUF, M_NOWAIT);
    267 	if(v == NULL)
    268 		return (NULL);
    269 
    270 	bm = alloc_bitmap(mode->size.width, mode->size.height, mode->depth);
    271 	if (bm) {
    272 		box_t   box;
    273 
    274 		v->colormap = alloc_colormap(mode);
    275 		if (v->colormap) {
    276 			INIT_BOX(&box,0,0,mode->size.width,mode->size.height);
    277 			init_view(v, bm, mode, &box);
    278 			return (v);
    279 		}
    280 		free_bitmap(bm);
    281 	}
    282 	if (v != &gra_con_view)
    283 		free(v, M_DEVBUF);
    284 	return (NULL);
    285 }
    286 
    287 static void
    288 init_view(v, bm, mode, dbox)
    289 view_t	*v;
    290 bmap_t	*bm;
    291 dmode_t	*mode;
    292 box_t	*dbox;
    293 {
    294 	v->bitmap = bm;
    295 	v->mode   = mode;
    296 	v->flags  = 0;
    297 	bcopy(dbox, &v->display, sizeof(box_t));
    298 }
    299 
    300 /* bitmap functions */
    301 
    302 static bmap_t *
    303 alloc_bitmap(width, height, depth)
    304 u_long	width, height;
    305 u_char	depth;
    306 {
    307 	u_long  total_size, bm_size;
    308 	void	*hw_address;
    309 	bmap_t	*bm;
    310 
    311 	/*
    312 	 * Sigh, it seems for mapping to work we need the bitplane data to
    313 	 *  1: be aligned on a page boundry.
    314 	 *  2: be n pages large.
    315 	 *
    316 	 * why? because the user gets a page aligned address, if this is before
    317 	 * your allocation, too bad.  Also it seems that the mapping routines
    318 	 * do not watch to closely to the allowable length. so if you go over
    319 	 * n pages by less than another page, the user gets to write all over
    320 	 * the entire page. Since you did not allocate up to a page boundry
    321 	 * (or more) the user writes into someone elses memory. -ch
    322 	 */
    323 	bm_size    = atari_round_page((width * height * depth) / NBBY);
    324 	total_size = bm_size + sizeof(bmap_t) + NBPG;
    325 
    326 	if ((bm = (bmap_t*)alloc_stmem(total_size, &hw_address)) == NULL)
    327 		return(NULL);
    328 
    329 	bm->plane         = (u_char*)bm + sizeof(bmap_t);
    330 	bm->plane         = (u_char*)atari_round_page(bm->plane);
    331 	bm->hw_address    = (u_char*)hw_address + sizeof(bmap_t);
    332 	bm->hw_address    = (u_char*)atari_round_page(bm->hw_address);
    333 	bm->bytes_per_row = (width * depth) / NBBY;
    334 	bm->rows          = height;
    335 	bm->depth         = depth;
    336 
    337 	bzero(bm->plane, bm_size);
    338 	return (bm);
    339 }
    340 
    341 static void
    342 free_bitmap(bm)
    343 bmap_t *bm;
    344 {
    345 	if (bm)
    346 		free_stmem(bm);
    347 }
    348 
    349 static colormap_t *
    350 alloc_colormap(dm)
    351 dmode_t		*dm;
    352 {
    353 	int		nentries, i;
    354 	colormap_t	*cm;
    355 	u_char		type = CM_COLOR;
    356 
    357 	switch (dm->vm_reg) {
    358 		case RES_STLOW:
    359 		case RES_TTMID:
    360 			nentries = 16;
    361 			break;
    362 		case RES_STMID:
    363 			nentries = 4;
    364 			break;
    365 		case RES_STHIGH:
    366 			nentries = 2;
    367 			break;
    368 		case RES_TTLOW:
    369 			nentries = 256;
    370 			break;
    371 		case RES_TTHIGH:
    372 			type     = CM_MONO;
    373 			nentries = 0;
    374 			break;
    375 		default:
    376 			panic("grf_tt:alloc_colormap: wrong mode!?");
    377 	}
    378 	if (!atari_realconfig) {
    379 		cm = &gra_con_cmap;
    380 		cm->entry = gra_con_colors;
    381 	}
    382 	else {
    383 		int size;
    384 
    385 		size = sizeof(*cm) + (nentries * sizeof(cm->entry[0]));
    386 		cm   = malloc(size, M_DEVBUF, M_NOWAIT);
    387 		if (cm == NULL)
    388 			return (NULL);
    389 		cm->entry = (long *)&cm[1];
    390 
    391 	}
    392 	if ((cm->type = type) == CM_COLOR)
    393 		cm->red_mask = cm->green_mask = cm->blue_mask = 0xf;
    394 	cm->first = 0;
    395 	cm->size  = nentries;
    396 
    397 	for (i = 0; i < nentries; i++)
    398 		cm->entry[i] = gra_def_color16[i % 16];
    399 	return (cm);
    400 }
    401 #endif /* TT_VIDEO */
    402