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