xref: /netbsd-src/sys/arch/atari/dev/grfabs_tt.c (revision 9573504567626934c7ee01c7dce0c4bb1dfe7403)
1 /*	$NetBSD: grfabs_tt.c,v 1.1 1995/08/20 18:17:34 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 
42 #include <machine/iomap.h>
43 #include <machine/video.h>
44 #include <machine/mfp.h>
45 #include <atari/atari/device.h>
46 #include <atari/dev/grfabs_reg.h>
47 
48 /*
49  * Function decls
50  */
51 static void       init_view __P((view_t *, bmap_t *, dmode_t *, box_t *));
52 static bmap_t	  *alloc_bitmap __P((u_long, u_long, u_char));
53 static colormap_t *alloc_colormap __P((dmode_t *));
54 static void 	  free_bitmap __P((bmap_t *));
55 static void	  tt_display_view __P((view_t *));
56 static view_t	  *tt_alloc_view __P((dmode_t *, dimen_t *, u_char));
57 static void	  tt_free_view __P((view_t *));
58 static void	  tt_remove_view __P((view_t *));
59 static int	  tt_use_colormap __P((view_t *, colormap_t *));
60 
61 /*
62  * Our function switch table
63  */
64 struct grfabs_sw tt_vid_sw = {
65 	tt_display_view,
66 	tt_alloc_view,
67 	tt_free_view,
68 	tt_remove_view,
69 	tt_use_colormap
70 };
71 
72 static dmode_t vid_modes[] = {
73 	{ { NULL, NULL }, "sthigh", { 640,  400 },  1, RES_STHIGH, &tt_vid_sw },
74 	{ { NULL, NULL }, "tthigh", { 1280, 960 },  1, RES_TTHIGH, &tt_vid_sw },
75 	{ { NULL, NULL }, "stmid",  { 640,  200 },  2, RES_STMID , &tt_vid_sw },
76 	{ { NULL, NULL }, "stlow",  { 320,  200 },  4, RES_STLOW , &tt_vid_sw },
77 	{ { NULL, NULL }, "ttmid",  { 640,  480 },  4, RES_TTMID , &tt_vid_sw },
78 	{ { NULL, NULL }, "ttlow",  { 320,  480 },  8, RES_TTLOW , &tt_vid_sw },
79 	{ { NULL, NULL }, NULL,  }
80 };
81 
82 /*
83  * XXX: called from ite console init routine.
84  * Initialize list of posible video modes.
85  */
86 void
87 tt_probe_video(modelp)
88 MODES	*modelp;
89 {
90 	dmode_t	*dm;
91 	int	i;
92 	int	has_mono;
93 
94 	/*
95 	 * First find out what kind of monitor is attached. Dma-sound
96 	 * should be off because the 'sound-done' and 'monochrome-detect'
97 	 * are xor-ed together. I think that shutting it down here is the
98 	 * wrong place.
99 	 */
100 	has_mono = (MFP->mf_gpip & IA_MONO) == 0;
101 
102 	for (i = 0; (dm = &vid_modes[i])->name != NULL; i++) {
103 		if (has_mono && (dm->vm_reg != RES_TTHIGH))
104 			continue;
105 		if (!has_mono && (dm->vm_reg == RES_TTHIGH))
106 			continue;
107 		LIST_INSERT_HEAD(modelp, dm, link);
108 	}
109 
110 	for (i=0; i < 16; i++)
111 		VIDEO->vd_tt_rgb[i] = CM_L2TT(gra_def_color16[i]);
112 }
113 
114 static void
115 tt_display_view(v)
116 view_t *v;
117 {
118 	dmode_t	*dm = v->mode;
119 	bmap_t	*bm = v->bitmap;
120 
121 	if (dm->current_view) {
122 		/*
123 		 * Mark current view for this mode as no longer displayed
124 		 */
125 		dm->current_view->flags &= ~VF_DISPLAY;
126 	}
127 	dm->current_view = v;
128 	v->flags |= VF_DISPLAY;
129 
130 	tt_use_colormap(v, v->colormap);
131 
132 	/* XXX: should use vbl for this	*/
133 	VIDEO->vd_tt_res = dm->vm_reg;
134 	VIDEO->vd_raml   =  (u_long)bm->hw_address & 0xff;
135 	VIDEO->vd_ramm   = ((u_long)bm->hw_address >>  8) & 0xff;
136 	VIDEO->vd_ramh   = ((u_long)bm->hw_address >> 16) & 0xff;
137 }
138 
139 void
140 tt_remove_view(v)
141 view_t *v;
142 {
143 	dmode_t *mode = v->mode;
144 
145 	if (mode->current_view == v) {
146 #if 0
147 		if (v->flags & VF_DISPLAY)
148 			panic("Cannot shutdown display\n"); /* XXX */
149 #endif
150 		mode->current_view = NULL;
151 	}
152 	v->flags &= ~VF_DISPLAY;
153 }
154 
155 void
156 tt_free_view(v)
157 view_t *v;
158 {
159 	if(v) {
160 		dmode_t *md = v->mode;
161 
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 	int     i;
308 	u_long  total_size, bm_size;
309 	void	*hw_address;
310 	bmap_t	*bm;
311 
312 	/*
313 	 * Sigh, it seems for mapping to work we need the bitplane data to
314 	 *  1: be aligned on a page boundry.
315 	 *  2: be n pages large.
316 	 *
317 	 * why? because the user gets a page aligned address, if this is before
318 	 * your allocation, too bad.  Also it seems that the mapping routines
319 	 * do not watch to closely to the allowable length. so if you go over
320 	 * n pages by less than another page, the user gets to write all over
321 	 * the entire page. Since you did not allocate up to a page boundry
322 	 * (or more) the user writes into someone elses memory. -ch
323 	 */
324 	bm_size    = atari_round_page((width * height * depth) / NBBY);
325 	total_size = bm_size + sizeof(bmap_t) + NBPG;
326 
327 	if ((bm = (bmap_t*)alloc_stmem(total_size, &hw_address)) == NULL)
328 		return(NULL);
329 
330 	bm->plane         = (u_char*)bm + sizeof(bmap_t);
331 	bm->plane         = (u_char*)atari_round_page(bm->plane);
332 	bm->hw_address    = (u_char*)hw_address + sizeof(bmap_t);
333 	bm->hw_address    = (u_char*)atari_round_page(bm->hw_address);
334 	bm->bytes_per_row = (width * depth) / NBBY;
335 	bm->rows          = height;
336 	bm->depth         = depth;
337 
338 	bzero(bm->plane, bm_size);
339 	return (bm);
340 }
341 
342 static void
343 free_bitmap(bm)
344 bmap_t *bm;
345 {
346 	if (bm)
347 		free_stmem(bm);
348 }
349 
350 static colormap_t *
351 alloc_colormap(dm)
352 dmode_t		*dm;
353 {
354 	int		nentries, i;
355 	colormap_t	*cm;
356 	u_char		type = CM_COLOR;
357 
358 	switch (dm->vm_reg) {
359 		case RES_STLOW:
360 		case RES_TTMID:
361 			nentries = 16;
362 			break;
363 		case RES_STMID:
364 			nentries = 4;
365 			break;
366 		case RES_STHIGH:
367 			nentries = 2;
368 			break;
369 		case RES_TTLOW:
370 			nentries = 256;
371 			break;
372 		case RES_TTHIGH:
373 			type     = CM_MONO;
374 			nentries = 0;
375 			break;
376 		default:
377 			panic("grf_tt:alloc_colormap: wrong mode!?");
378 	}
379 	if (!atari_realconfig) {
380 		cm = &gra_con_cmap;
381 		cm->entry = gra_con_colors;
382 	}
383 	else {
384 		int size;
385 
386 		size = sizeof(*cm) + (nentries * sizeof(cm->entry[0]));
387 		cm   = malloc(size, M_DEVBUF, M_NOWAIT);
388 		if (cm == NULL)
389 			return (NULL);
390 		cm->entry = (long *)&cm[1];
391 
392 	}
393 	if ((cm->type = type) == CM_COLOR)
394 		cm->red_mask = cm->green_mask = cm->blue_mask = 0xf;
395 	cm->first = 0;
396 	cm->size  = nentries;
397 
398 	for (i = 0; i < nentries; i++)
399 		cm->entry[i] = gra_def_color16[i % 16];
400 	return (cm);
401 }
402 #endif /* TT_VIDEO */
403