xref: /netbsd/sys/arch/atari/dev/grfabs_fal.c (revision 60e2ec70)
1 /*	$NetBSD: grfabs_fal.c,v 1.29 2023/01/06 10:28:28 tsutsui 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.29 2023/01/06 10:28:28 tsutsui 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 static dmode_t vid_modes[] = {
105     { {NULL,NULL}, "falauto", {   0,  0 },  0, NULL, &fal_vid_sw},
106     { {NULL,NULL}, "sthigh",  { 640,400 },  1, NULL, &fal_vid_sw},
107     { {NULL,NULL}, "stmid",   { 640,200 },  2, NULL, &fal_vid_sw},
108     { {NULL,NULL}, "stlow",   { 320,200 },  4, NULL, &fal_vid_sw},
109     { {NULL,NULL}, "ttlow",   { 320,480 },  8, NULL, &fal_vid_sw},
110     { {NULL,NULL}, "vga2",    { 640,480 },  1, NULL, &fal_vid_sw},
111     { {NULL,NULL}, "vga4",    { 640,480 },  2, NULL, &fal_vid_sw},
112     { {NULL,NULL}, "vga16",   { 640,480 },  4, NULL, &fal_vid_sw},
113     { {NULL,NULL}, "vga256",  { 640,480 },  8, NULL, &fal_vid_sw},
114     { {NULL,NULL}, "highcol", { 320,200 }, 16, NULL, &fal_vid_sw},
115     { {NULL,NULL},  NULL,  }
116 };
117 
118 /*
119  * The following table contains timing values for the various video modes.
120  * I have only a multisync display, therefore I can not say if this values
121  * are useful at other displays.
122  * Use other video modes at YOUR OWN RISK.
123  * THERE IS NO WARRENTY ABOUT THIS VALUES TO WORK WITH A PARTICULAR
124  * DISPLAY. -- Thomas
125  */
126 static struct videl videlinit[] = {
127 	{ RES_FALAUTO,		    /* autodedect			    */
128 	0x0, 0x0, 0x0,   0x0, 0x0,   0x0,  0x0,  0x0,  0x0,   0x0,  0x0,  0x0,
129 	0x0, 0x0,   0x0,   0x0,  0x0,  0x0,   0x0,   0x0,   0x0 },
130 
131 	{ FAL_VGA | RES_FAL_STHIGH, /* sthigh, 640x400, 2 colors	    */
132 	0x2, 0x0, 0x28,  0x0, 0x400, 0xc6, 0x8d, 0x15, 0x273, 0x50, 0x96, 0x0,
133 	0x0, 0x419, 0x3af, 0x8f, 0x8f, 0x3af, 0x415, 0x186, 0x8 },
134 
135 #if 0 /* not yet */
136 	{ FAL_SM | RES_FAL_STHIGH,  /* sthigh, 640x400, 2 colors	    */
137 	0x0, 0x0, 0x28,  0x2, 0x0,   0x1a, 0x0,  0x0,  0x20f, 0xc,  0x14, 0x0,
138 	0x0, 0x3e9, 0x0,   0x0,  0x43, 0x363, 0x3e7, 0x80,  0x8 },
139 #endif
140 
141 	{ FAL_VGA | RES_FAL_STMID,  /* stmid, 640x200, 4 colors		    */
142 	0x2, 0x0, 0x50,  0x1, 0x0,   0x17, 0x12, 0x1,  0x20e, 0xd,  0x11, 0x0,
143 	0x0, 0x419, 0x3af, 0x8f, 0x8f, 0x3af, 0x415, 0x186, 0x9 },
144 
145 	{ FAL_VGA | RES_FAL_STLOW,  /* stlow, 320x200, 16 colors	    */
146 	0x2, 0x0, 0x50,  0x0, 0x0,   0x17, 0x12, 0x1,  0x20e, 0xd,  0x11, 0x0,
147 	0x0, 0x419, 0x3af, 0x8f, 0x8f, 0x3af, 0x415, 0x186, 0x5 },
148 
149 	{ FAL_VGA | RES_FAL_TTLOW,  /* ttlow, 320x480, 256 colors	    */
150 	0x2, 0x0, 0xa0,  0x0, 0x10,  0xc6, 0x8d, 0x15, 0x29a, 0x7b, 0x96, 0x0,
151 	0x0, 0x419, 0x3ff, 0x3f, 0x3f, 0x3ff, 0x415, 0x186, 0x4 },
152 
153 	{ FAL_VGA | RES_VGA2,	    /* vga, 640x480, 2 colors		    */
154 	0x2, 0x0, 0x28,  0x0, 0x400, 0xc6, 0x8d, 0x15, 0x273, 0x50, 0x96, 0x0,
155 	0x0, 0x419, 0x3ff, 0x3f, 0x3f, 0x3ff, 0x415, 0x186, 0x8 },
156 
157 	{ FAL_VGA | RES_VGA4,	    /* vga, 640x480, 4 colors		    */
158 	0x2, 0x0, 0x50,  0x1, 0x0,   0x17, 0x12, 0x1,  0x20e, 0xd,  0x11, 0x0,
159 	0x0, 0x419, 0x3ff, 0x3f, 0x3f, 0x3ff, 0x415, 0x186, 0x8 },
160 
161 	{ FAL_VGA | RES_VGA16,	    /* vga, 640x480, 16 colors		    */
162 	0x2, 0x0, 0xa0,  0x1, 0x0,   0xc6, 0x8d, 0x15, 0x2a3, 0x7c, 0x96, 0x0,
163 	0x0, 0x419, 0x3ff, 0x3f, 0x3f, 0x3ff, 0x415, 0x186, 0x8 },
164 
165 	{ FAL_VGA | RES_VGA256,	    /* vga, 640x480, 256 colors		    */
166 	0x2, 0x0, 0x140, 0x1, 0x10,  0xc6, 0x8d, 0x15, 0x2ab, 0x84, 0x96, 0x0,
167 	0x0, 0x419, 0x3ff, 0x3f, 0x3f, 0x3ff, 0x415, 0x186, 0x8 },
168 
169 	{ FAL_VGA | RES_DIRECT,	    /* direct video, 320x200, 65536 colors  */
170 	0x2, 0x0, 0x140, 0x0, 0x100, 0xc6, 0x8d, 0x15, 0x2ac, 0x91, 0x96, 0x0,
171 	0x0, 0x419, 0x3ff, 0x3f, 0x3f, 0x3ff, 0x415, 0x186, 0x4 },
172 
173 	{ 0xffff }		    /* end of list			    */
174 };
175 
176 static u_short mon_type;
177 /*
178  * XXX: called from ite console init routine.
179  * Initialize list of possible video modes.
180  */
181 void
falcon_probe_video(MODES * modelp)182 falcon_probe_video(MODES *modelp)
183 {
184 	dmode_t	*dm;
185 	struct videl *vregs;
186 	int	i;
187 
188 	mon_type = *(volatile unsigned char *)(AD_FAL_MON_TYPE);
189 	mon_type = (mon_type & 0xc0) << 2;
190 
191 	/*
192 	 * get all possible modes
193 	 */
194 
195 	for (i = 0; (dm = &vid_modes[i])->name != NULL; i++) {
196 		dm->data = (void *)&fal_hwregs[i];
197 		if (vm_mode(dm) == RES_FALAUTO) {
198 			vm_regs(dm) = falcon_getreg(RES_FALAUTO);
199 			falcon_detect(dm);
200 			LIST_INSERT_HEAD(modelp, dm, link);
201 		} else {
202 			vregs = falcon_getreg(vm_mode(dm) | mon_type);
203 			if (vregs) {
204 				vm_regs(dm) = vregs;
205 				LIST_INSERT_HEAD(modelp, dm, link);
206 			}
207 		}
208 	}
209 
210 	/*
211 	 * This seems to prevent bordered screens.
212 	 */
213 	for (i=0; i < 16; i++)
214 		VIDEO->vd_fal_rgb[i] = CM_L2FAL(gra_def_color16[i]);
215 }
216 
217 static struct videl *
falcon_getreg(u_short mode)218 falcon_getreg(u_short mode)
219 {
220 	int i;
221 	struct videl *vregs;
222 
223 	for (i = 0; (vregs = &videlinit[i])->video_mode != 0xffff; i++)
224 		if ((vregs->video_mode) == mode)
225 			return vregs;
226 
227 	return NULL; /* mode not found */
228 }
229 
230 static void
falcon_detect(dmode_t * dm)231 falcon_detect(dmode_t *dm)
232 {
233 	u_short	falshift, stshift;
234 	struct videl *vregs = vm_regs(dm);
235 
236 	/*
237 	 * First get the videl register values
238 	 */
239 
240 	vregs->vd_syncmode	= VIDEO->vd_sync;
241 	vregs->vd_line_wide	= VIDEO->vd_line_wide;
242 	vregs->vd_vert_wrap	= VIDEO->vd_vert_wrap;
243 	vregs->vd_st_res	= VIDEO->vd_st_res;
244 	vregs->vd_fal_res	= VIDEO->vd_fal_res;
245 	vregs->vd_h_hold_tim	= VIDEO->vd_h_hold_tim;
246 	vregs->vd_h_bord_beg	= VIDEO->vd_h_bord_beg;
247 	vregs->vd_h_bord_end	= VIDEO->vd_h_bord_end;
248 	vregs->vd_h_dis_beg	= VIDEO->vd_h_dis_beg;
249 	vregs->vd_h_dis_end	= VIDEO->vd_h_dis_end;
250 	vregs->vd_h_ss		= VIDEO->vd_h_ss;
251 	vregs->vd_h_fs		= VIDEO->vd_h_fs;
252 	vregs->vd_h_hh		= VIDEO->vd_h_hh;
253 	vregs->vd_v_freq_tim	= VIDEO->vd_v_freq_tim;
254 	vregs->vd_v_bord_beg	= VIDEO->vd_v_bord_beg;
255 	vregs->vd_v_bord_end	= VIDEO->vd_v_bord_end;
256 	vregs->vd_v_dis_beg	= VIDEO->vd_v_dis_beg;
257 	vregs->vd_v_dis_end	= VIDEO->vd_v_dis_end;
258 	vregs->vd_v_ss		= VIDEO->vd_v_ss;
259 	vregs->vd_fal_ctrl	= VIDEO->vd_fal_ctrl;
260 	vregs->vd_fal_mode	= VIDEO->vd_fal_mode;
261 
262 	/*
263 	 * Calculate the depth of the screen
264 	 */
265 
266 	falshift = vregs->vd_fal_res;
267 	stshift = vregs->vd_st_res;
268 
269 	if (falshift & 0x400)		/* 2 color */
270 		dm->depth = 1;
271 	else if (falshift & 0x100)	/* high color, direct */
272 		dm->depth = 16;
273 	else if (falshift & 0x10)	/* 256 color */
274 		dm->depth = 8;
275 	else if (stshift == 0)		/* 16 color */
276 		dm->depth = 4;
277 	else if (stshift == 1)		/* 4 color */
278 		dm->depth = 2;
279 	else
280 		dm->depth = 1;		/* 2 color */
281 
282 	/*
283 	 * Now calculate the screen hight
284 	 */
285 
286 	dm->size.height = vregs->vd_v_dis_end - vregs->vd_v_dis_beg;
287 	if (!((vregs->vd_fal_mode & 0x2) >> 1)) /* if not interlaced */
288 		dm->size.height >>=1;
289 	if (vregs->vd_fal_mode & 0x1)		/* if doublescan */
290 		dm->size.height >>=1;
291 
292 	/*
293 	 * And the width
294 	 */
295 
296 	dm->size.width = vregs->vd_vert_wrap * 16 / dm->depth;
297 
298 }
299 
300 u_long	falcon_needs_vbl;
301 void falcon_display_switch(void);
302 
303 static void
falcon_display_view(view_t * v)304 falcon_display_view(view_t *v)
305 {
306 	dmode_t	*dm = v->mode;
307 	bmap_t		*bm;
308 	struct videl	*vregs;
309 	static u_short last_mode = 0xffff;
310 
311 	if (dm->current_view) {
312 		/*
313 		 * Mark current view for this mode as no longer displayed
314 		 */
315 		dm->current_view->flags &= ~VF_DISPLAY;
316 	}
317 	dm->current_view = v;
318 	v->flags |= VF_DISPLAY;
319 	vregs = vm_regs(v->mode);
320 
321 	falcon_use_colormap(v, v->colormap);
322 
323 	bm = v->bitmap;
324 	VIDEO->vd_ramh   = ((u_long)bm->hw_address >> 16) & 0xff;
325 	VIDEO->vd_ramm   = ((u_long)bm->hw_address >>  8) & 0xff;
326 	VIDEO->vd_raml   =  (u_long)bm->hw_address & 0xff;
327 
328 	if (last_mode != vregs->video_mode) {
329 		last_mode = vregs->video_mode;
330 
331 		if (dm->depth == 1) {
332 			/*
333 			 * Set the resolution registers to a mode, which guarantee
334 			 * no shifting when the register are written during vbl.
335 			 */
336 			VIDEO->vd_fal_res = 0;
337 			VIDEO->vd_st_res = 0;
338 		}
339 
340 		/*
341 		 * Arrange for them to be activated
342 		 * at the second vbl interrupt.
343 		 */
344 		falcon_needs_vbl = (u_long)v;
345 	}
346 }
347 
348 void
falcon_display_switch(void)349 falcon_display_switch(void)
350 {
351 	view_t		*v;
352 	struct videl	*vregs;
353 	static int vbl_count = 1;
354 
355 	if (vbl_count--)
356 		return;
357 
358 	v = (view_t*)falcon_needs_vbl;
359 
360 	vbl_count = 1;
361 	falcon_needs_vbl = 0;
362 
363 	/*
364 	 * Write to videl registers only on VGA displays
365 	 * This is only a hack. Must be fixed soon. XXX -- Thomas
366 	 */
367 	if (mon_type != FAL_VGA)
368 		return;
369 
370 	vregs = vm_regs(v->mode);
371 
372 	VIDEO->vd_v_freq_tim    = vregs->vd_v_freq_tim;
373 	VIDEO->vd_v_ss          = vregs->vd_v_ss;
374 	VIDEO->vd_v_bord_beg    = vregs->vd_v_bord_beg;
375 	VIDEO->vd_v_bord_end    = vregs->vd_v_bord_end;
376 	VIDEO->vd_v_dis_beg     = vregs->vd_v_dis_beg;
377 	VIDEO->vd_v_dis_end     = vregs->vd_v_dis_end;
378 	VIDEO->vd_h_hold_tim    = vregs->vd_h_hold_tim;
379 	VIDEO->vd_h_ss          = vregs->vd_h_ss;
380 	VIDEO->vd_h_bord_beg    = vregs->vd_h_bord_beg;
381 	VIDEO->vd_h_bord_end    = vregs->vd_h_bord_end;
382 	VIDEO->vd_h_dis_beg     = vregs->vd_h_dis_beg;
383 	VIDEO->vd_h_dis_end     = vregs->vd_h_dis_end;
384 #if 0 /* This seems not to be necessary -- Thomas */
385 	VIDEO->vd_h_fs		= vregs->vd_h_fs;
386 	VIDEO->vd_h_hh		= vregs->vd_h_hh;
387 #endif
388 	VIDEO->vd_sync          = vregs->vd_syncmode;
389 	VIDEO->vd_fal_res       = 0;
390 	if (v->mode->depth == 2)
391 		VIDEO->vd_st_res        = vregs->vd_st_res;
392 	else {
393 		VIDEO->vd_st_res        = 0;
394 		VIDEO->vd_fal_res       = vregs->vd_fal_res;
395 	}
396 	VIDEO->vd_vert_wrap     = vregs->vd_vert_wrap;
397 	VIDEO->vd_line_wide     = vregs->vd_line_wide;
398 	VIDEO->vd_fal_ctrl      = vregs->vd_fal_ctrl;
399 	VIDEO->vd_fal_mode      = vregs->vd_fal_mode;
400 }
401 
402 static void
falcon_remove_view(view_t * v)403 falcon_remove_view(view_t *v)
404 {
405 	dmode_t *mode = v->mode;
406 
407 	if (mode->current_view == v) {
408 #if 0
409 		if (v->flags & VF_DISPLAY)
410 			panic("Cannot shutdown display"); /* XXX */
411 #endif
412 		mode->current_view = NULL;
413 	}
414 	v->flags &= ~VF_DISPLAY;
415 }
416 
417 void
falcon_save_view(view_t * v)418 falcon_save_view(view_t *v)
419 {
420 }
421 
422 static void
falcon_free_view(view_t * v)423 falcon_free_view(view_t *v)
424 {
425 	if (v) {
426 		falcon_remove_view(v);
427 		if (v->colormap != &gra_con_cmap)
428 			free(v->colormap, M_DEVBUF);
429 		free_bitmap(v->bitmap);
430 		if (v != &gra_con_view)
431 			free(v, M_DEVBUF);
432 	}
433 }
434 
435 static int
falcon_use_colormap(view_t * v,colormap_t * cm)436 falcon_use_colormap(view_t *v, colormap_t *cm)
437 {
438 	dmode_t			*dm;
439 	volatile u_short	*creg;
440 	volatile u_long		*fcreg;
441 	u_long			*src;
442 	colormap_t		*vcm;
443 	u_long			*vcreg;
444 	u_short			ncreg;
445 	int			last_streg;
446 	int			i;
447 
448 	dm  = v->mode;
449 	vcm = v->colormap;
450 
451 	/*
452 	 * I guess it seems reasonable to require the maps to be
453 	 * of the same type...
454 	 */
455 	if (cm->type != vcm->type)
456 		return (EINVAL);
457 
458 	/*
459 	 * First get the colormap addresses an calculate
460 	 * howmany colors are in it.
461 	 */
462 	if (dm->depth == 16) /* direct color, no colormap;
463 				but also not (yet) supported */
464 		return(0);
465 	fcreg = &VIDEO->vd_fal_rgb[0];
466 	creg  = &VIDEO->vd_st_rgb[0];
467 	ncreg = 1 << dm->depth;
468 
469 	/* If first entry specified beyond capabilities -> error */
470 	if (cm->first >= ncreg)
471 		return (EINVAL);
472 
473 	/*
474 	 * A little tricky, the actual colormap pointer will be NULL
475 	 * when view is not displaying, valid otherwise.
476 	 */
477 	if (v->flags & VF_DISPLAY) {
478 		creg = &creg[cm->first];
479 		fcreg = &fcreg[cm->first];
480 	} else {
481 		creg = NULL;
482 		fcreg = NULL;
483 	}
484 
485 	vcreg  = &vcm->entry[cm->first];
486 	ncreg -= cm->first;
487 	last_streg = 16 - cm->first;
488 	if (cm->size > ncreg)
489 		return (EINVAL);
490 	ncreg = cm->size;
491 
492 	for (i = 0, src = cm->entry; i < ncreg; i++, vcreg++) {
493 		*vcreg = *src++;
494 
495 		/*
496 		 * If displaying, also update actual color register.
497 		 */
498 		if (fcreg != NULL) {
499 			*fcreg++ = CM_L2FAL(*vcreg);
500 			if (i < last_streg)
501 				*creg++ = CM_L2ST(*vcreg);
502 		}
503 	}
504 	return (0);
505 }
506 
507 static view_t *
falcon_alloc_view(dmode_t * mode,dimen_t * dim,u_char depth)508 falcon_alloc_view(dmode_t *mode, dimen_t *dim, u_char depth)
509 {
510 	view_t *v;
511 	bmap_t *bm;
512 
513 	if (!atari_realconfig)
514 		v = &gra_con_view;
515 	else
516 		v = malloc(sizeof(*v), M_DEVBUF, M_NOWAIT);
517 	if (v == NULL)
518 		return(NULL);
519 
520 	bm = alloc_bitmap(mode->size.width, mode->size.height, mode->depth);
521 	if (bm) {
522 		box_t   box;
523 
524 		v->colormap = alloc_colormap(mode);
525 		if (v->colormap) {
526 			INIT_BOX(&box,0,0,mode->size.width,mode->size.height);
527 			init_view(v, bm, mode, &box);
528 			return(v);
529 		}
530 		free_bitmap(bm);
531 	}
532 	if (v != &gra_con_view)
533 		free(v, M_DEVBUF);
534 	return (NULL);
535 }
536 
537 static void
init_view(view_t * v,bmap_t * bm,dmode_t * mode,box_t * dbox)538 init_view(view_t *v, bmap_t *bm, dmode_t *mode, box_t *dbox)
539 {
540 	v->bitmap = bm;
541 	v->mode   = mode;
542 	v->flags  = 0;
543 	memcpy(&v->display, dbox, sizeof(box_t));
544 }
545 
546 /* bitmap functions */
547 
548 static bmap_t *
alloc_bitmap(u_long width,u_long height,u_char depth)549 alloc_bitmap(u_long width, u_long height, u_char depth)
550 {
551 	u_long  total_size, bm_size;
552 	void	*hw_address;
553 	bmap_t	*bm;
554 
555 	/*
556 	 * Sigh, it seems for mapping to work we need the bitplane data to
557 	 *  1: be aligned on a page boundary.
558 	 *  2: be n pages large.
559 	 *
560 	 * why? because the user gets a page aligned address, if this is before
561 	 * your allocation, too bad.  Also it seems that the mapping routines
562 	 * do not watch to closely to the allowable length. so if you go over
563 	 * n pages by less than another page, the user gets to write all over
564 	 * the entire page. Since you did not allocate up to a page boundary
565 	 * (or more) the user writes into someone elses memory. -ch
566 	 */
567 	bm_size    = m68k_round_page((width * height * depth) / NBBY);
568 	total_size = bm_size + sizeof(bmap_t) + PAGE_SIZE;
569 
570 	if ((bm = (bmap_t*)alloc_stmem(total_size, &hw_address)) == NULL)
571 		return(NULL);
572 
573 	bm->plane         = (u_char*)bm + sizeof(bmap_t);
574 	bm->plane         = (u_char*)m68k_round_page(bm->plane);
575 	bm->hw_address    = (u_char*)hw_address + sizeof(bmap_t);
576 	bm->hw_address    = (u_char*)m68k_round_page(bm->hw_address);
577 	bm->bytes_per_row = (width * depth) / NBBY;
578 	bm->rows          = height;
579 	bm->depth         = depth;
580 	bm->phys_mappable = (depth * width * height) / NBBY;
581 	bm->regs          = NULL;
582 	bm->hw_regs       = NULL;
583 	bm->reg_size      = 0;
584 	bm->vga_address   = NULL;
585 	bm->vga_mappable  = 0;
586 	bm->lin_base      = 0;
587 	bm->vga_base      = 0;
588 
589 	memset(bm->plane, 0, bm_size);
590 	return (bm);
591 }
592 
593 static void
free_bitmap(bmap_t * bm)594 free_bitmap(bmap_t *bm)
595 {
596 	if (bm)
597 		free_stmem(bm);
598 }
599 
600 static colormap_t *
alloc_colormap(dmode_t * dm)601 alloc_colormap(dmode_t *dm)
602 {
603 	int		nentries, i;
604 	colormap_t	*cm;
605 	u_char		type = CM_COLOR;
606 
607 	if (dm->depth == 16) /* direct color, no colormap;
608 				not (yet) supported */
609 		nentries = 0;
610 	else
611 		nentries = 1 << dm->depth;
612 
613 	if (!atari_realconfig) {
614 		cm = &gra_con_cmap;
615 		cm->entry = gra_con_colors;
616 	} else {
617 		int size;
618 
619 		size = sizeof(*cm) + (nentries * sizeof(cm->entry[0]));
620 		cm   = malloc(size, M_DEVBUF, M_NOWAIT);
621 		if (cm == NULL)
622 			return(NULL);
623 		cm->entry = (long *)&cm[1];
624 
625 	}
626 
627 	if ((cm->type = type) == CM_COLOR)
628 		cm->red_mask = cm->green_mask = cm->blue_mask = 0x3f;
629 
630 	cm->first = 0;
631 	cm->size  = nentries;
632 
633 	for (i = 0; i < nentries; i++)
634 		cm->entry[i] = gra_def_color16[i % 16];
635 	return (cm);
636 }
637 #endif /* FALCON_VIDEO */
638