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