grfabs.c revision 1.4 1 1.4 leo /* $NetBSD: grfabs.c,v 1.4 1995/05/21 10:54:44 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 /*
34 1.1 leo * atari abstract graphics driver.
35 1.1 leo */
36 1.1 leo #include <sys/param.h>
37 1.1 leo #include <sys/queue.h>
38 1.1 leo #include <sys/malloc.h>
39 1.1 leo
40 1.1 leo #include <machine/iomap.h>
41 1.1 leo #include <machine/video.h>
42 1.1 leo #include <machine/mfp.h>
43 1.1 leo #include <atari/dev/grfabs_reg.h>
44 1.1 leo
45 1.1 leo /*
46 1.1 leo * Function decls
47 1.1 leo */
48 1.3 leo static dmode_t *get_best_display_mode __P((dimen_t *, int, dmode_t *));
49 1.1 leo static view_t *alloc_view __P((dmode_t *, dimen_t *, u_char));
50 1.1 leo static void init_view __P((view_t *, bmap_t *, dmode_t *, box_t *));
51 1.4 leo static bmap_t *alloc_bitmap __P((u_long, u_long, u_char));
52 1.1 leo static void free_bitmap __P((bmap_t *));
53 1.1 leo static colormap_t *alloc_colormap __P((dmode_t *));
54 1.1 leo
55 1.1 leo /*
56 1.1 leo * Ugh.. Stuff needed to allocate console structures before the VM-system
57 1.1 leo * is running. There is no malloc() available at that time.
58 1.1 leo */
59 1.1 leo extern int atari_realconfig; /* 0 -> no malloc */
60 1.1 leo static view_t con_view;
61 1.1 leo static colormap_t con_cmap;
62 1.2 leo static long con_colors[MAX_CENTRIES];
63 1.1 leo
64 1.1 leo /*
65 1.1 leo * List of available graphic modes
66 1.1 leo */
67 1.1 leo static LIST_HEAD(modelist, display_mode) modes;
68 1.1 leo
69 1.1 leo static dmode_t vid_modes[] = {
70 1.3 leo { { NULL, NULL }, "sthigh", { 640, 400 }, 1, RES_STHIGH },
71 1.3 leo { { NULL, NULL }, "tthigh", { 1280, 960 }, 1, RES_TTHIGH },
72 1.3 leo { { NULL, NULL }, "stmid", { 640, 200 }, 2, RES_STMID },
73 1.2 leo { { NULL, NULL }, "stlow", { 320, 200 }, 4, RES_STLOW },
74 1.3 leo { { NULL, NULL }, "ttmid", { 640, 480 }, 4, RES_TTMID },
75 1.1 leo { { NULL, NULL }, "ttlow", { 320, 480 }, 8, RES_TTLOW },
76 1.1 leo { { NULL, NULL }, NULL, }
77 1.1 leo };
78 1.1 leo
79 1.1 leo /*
80 1.1 leo * Default colors.....
81 1.1 leo * Currently the TT-low (256 colors) just uses 16 time the 16-color default.
82 1.1 leo * If you have a sensible 256 scale, feel free to add.....
83 1.1 leo * The first 2 colors in all maps are {black,white}, so ite (text) displays
84 1.1 leo * are initially readable. Also, this enables me to supply only 1 map. The
85 1.1 leo * 4 color mode uses the first four entries of the 16 color mode thus giving
86 1.1 leo * a gray scale display. (Maybe we can add an intensity bit to the ite...)
87 1.1 leo */
88 1.1 leo static u_short def_color16[16] = {
89 1.1 leo 0x000, /* black */
90 1.1 leo 0xfff, /* white */
91 1.1 leo 0xccc, /* light gray */
92 1.1 leo 0x888, /* gray */
93 1.1 leo 0x00c, /* blue */
94 1.1 leo 0x0c0, /* green */
95 1.1 leo 0x0cc, /* cyan */
96 1.1 leo 0xc00, /* red */
97 1.1 leo 0xc0c, /* magenta */
98 1.1 leo 0xcc0, /* brown */
99 1.1 leo 0x00f, /* light blue */
100 1.1 leo 0x0f0, /* light green */
101 1.1 leo 0x0ff, /* light cyan */
102 1.1 leo 0xf00, /* light red */
103 1.1 leo 0xf0f, /* light magenta */
104 1.1 leo 0xff0 /* light brown */
105 1.1 leo };
106 1.1 leo
107 1.1 leo /*
108 1.1 leo * XXX: called from ite console init routine.
109 1.1 leo * Initialize list of posible video modes.
110 1.1 leo */
111 1.1 leo int
112 1.1 leo grfabs_probe()
113 1.1 leo {
114 1.1 leo dmode_t *dm;
115 1.1 leo int i;
116 1.1 leo int has_mono;
117 1.1 leo static int inited = 0;
118 1.1 leo
119 1.1 leo if(inited)
120 1.4 leo return(1); /* Has to be done only once */
121 1.1 leo inited++;
122 1.1 leo
123 1.1 leo /*
124 1.1 leo * First find out what kind of monitor is attached. Dma-sound
125 1.1 leo * should be off because the 'sound-done' and 'monochrome-detect'
126 1.1 leo * are xor-ed together. I think that shutting it down here is the
127 1.1 leo * wrong place.
128 1.1 leo */
129 1.1 leo has_mono = (MFP->mf_gpip & IA_MONO) == 0;
130 1.1 leo
131 1.1 leo LIST_INIT(&modes);
132 1.1 leo for(i = 0; (dm = &vid_modes[i])->name != NULL; i++) {
133 1.1 leo if(has_mono && (dm->vm_reg != RES_TTHIGH))
134 1.1 leo continue;
135 1.1 leo if(!has_mono && (dm->vm_reg == RES_TTHIGH))
136 1.1 leo continue;
137 1.1 leo LIST_INSERT_HEAD(&modes, dm, link);
138 1.1 leo }
139 1.2 leo
140 1.2 leo /*
141 1.2 leo * This seems to prevent bordered screens.
142 1.2 leo */
143 1.2 leo for(i=0; i < 16; i++)
144 1.2 leo VIDEO->vd_tt_rgb[i] = def_color16[i];
145 1.1 leo return(1);
146 1.1 leo }
147 1.1 leo
148 1.1 leo view_t *
149 1.1 leo grf_alloc_view(d, dim, depth)
150 1.1 leo dmode_t *d;
151 1.1 leo dimen_t *dim;
152 1.1 leo u_char depth;
153 1.1 leo {
154 1.1 leo if(!d)
155 1.3 leo d = get_best_display_mode(dim, depth, NULL);
156 1.4 leo if(d)
157 1.1 leo return(alloc_view(d, dim, depth));
158 1.1 leo return(NULL);
159 1.1 leo }
160 1.1 leo
161 1.4 leo dmode_t *
162 1.4 leo grf_get_best_mode(dim, depth)
163 1.4 leo dimen_t *dim;
164 1.4 leo u_char depth;
165 1.4 leo {
166 1.4 leo return(get_best_display_mode(dim, depth, NULL));
167 1.4 leo }
168 1.4 leo
169 1.1 leo void grf_display_view(v)
170 1.1 leo view_t *v;
171 1.1 leo {
172 1.1 leo dmode_t *dm = v->mode;
173 1.1 leo bmap_t *bm = v->bitmap;
174 1.1 leo
175 1.1 leo if(dm->current_view) {
176 1.1 leo /*
177 1.1 leo * Mark current view for this mode as no longer displayed
178 1.1 leo */
179 1.1 leo dm->current_view->flags &= ~VF_DISPLAY;
180 1.1 leo }
181 1.1 leo dm->current_view = v;
182 1.1 leo v->flags |= VF_DISPLAY;
183 1.1 leo
184 1.1 leo grf_use_colormap(v, v->colormap);
185 1.1 leo
186 1.1 leo /* XXX: should use vbl for this */
187 1.1 leo VIDEO->vd_tt_res = dm->vm_reg;
188 1.1 leo VIDEO->vd_raml = (u_long)bm->hw_address & 0xff;
189 1.1 leo VIDEO->vd_ramm = ((u_long)bm->hw_address >> 8) & 0xff;
190 1.1 leo VIDEO->vd_ramh = ((u_long)bm->hw_address >> 16) & 0xff;
191 1.1 leo }
192 1.1 leo
193 1.1 leo void grf_remove_view(v)
194 1.1 leo view_t *v;
195 1.1 leo {
196 1.1 leo dmode_t *mode = v->mode;
197 1.1 leo
198 1.1 leo if(mode->current_view == v) {
199 1.1 leo #if 0
200 1.1 leo if(v->flags & VF_DISPLAY)
201 1.1 leo panic("Cannot shutdown display\n"); /* XXX */
202 1.1 leo #endif
203 1.1 leo mode->current_view = NULL;
204 1.1 leo }
205 1.1 leo v->flags &= ~VF_DISPLAY;
206 1.1 leo }
207 1.1 leo
208 1.1 leo void grf_free_view(v)
209 1.1 leo view_t *v;
210 1.1 leo {
211 1.1 leo if(v) {
212 1.1 leo dmode_t *md = v->mode;
213 1.1 leo
214 1.1 leo grf_remove_view(v);
215 1.1 leo if(v->colormap != &con_cmap)
216 1.1 leo free(v->colormap, M_DEVBUF);
217 1.1 leo free_bitmap(v->bitmap);
218 1.1 leo if(v != &con_view)
219 1.1 leo free(v, M_DEVBUF);
220 1.1 leo }
221 1.1 leo }
222 1.1 leo
223 1.1 leo int
224 1.1 leo grf_get_colormap(v, cm)
225 1.1 leo view_t *v;
226 1.1 leo colormap_t *cm;
227 1.1 leo {
228 1.1 leo colormap_t *gcm;
229 1.1 leo int i, n;
230 1.4 leo u_long *sv_entry;
231 1.1 leo
232 1.1 leo gcm = v->colormap;
233 1.2 leo n = cm->size < gcm->size ? cm->size : gcm->size;
234 1.4 leo
235 1.4 leo /*
236 1.4 leo * Copy struct from view but be carefull to keep 'entry'
237 1.4 leo */
238 1.4 leo sv_entry = cm->entry;
239 1.4 leo *cm = *gcm;
240 1.4 leo cm->entry = sv_entry;
241 1.4 leo
242 1.4 leo /*
243 1.4 leo * Copy the colors
244 1.4 leo */
245 1.4 leo bzero(cm->entry, cm->size * sizeof(long));
246 1.1 leo for(i = 0; i < n; i++)
247 1.2 leo cm->entry[i] = gcm->entry[i];
248 1.1 leo return(0);
249 1.1 leo }
250 1.1 leo
251 1.1 leo int
252 1.1 leo grf_use_colormap(v, cm)
253 1.1 leo view_t *v;
254 1.1 leo colormap_t *cm;
255 1.1 leo {
256 1.1 leo dmode_t *dm;
257 1.1 leo volatile u_short *creg;
258 1.4 leo u_long *src;
259 1.4 leo colormap_t *vcm;
260 1.4 leo u_long *vcreg;
261 1.1 leo u_short ncreg;
262 1.1 leo int i;
263 1.1 leo
264 1.4 leo dm = v->mode;
265 1.4 leo vcm = v->colormap;
266 1.4 leo
267 1.4 leo /*
268 1.4 leo * I guess it seems reasonable to require the maps to be
269 1.4 leo * of the same type...
270 1.4 leo */
271 1.4 leo if(cm->type != vcm->type)
272 1.4 leo return(EINVAL);
273 1.1 leo
274 1.4 leo /*
275 1.4 leo * First figure out where the actual colormap resides and
276 1.4 leo * howmany colors are in it.
277 1.4 leo */
278 1.1 leo switch(dm->vm_reg) {
279 1.1 leo case RES_STLOW:
280 1.1 leo creg = &VIDEO->vd_tt_rgb[0];
281 1.1 leo ncreg = 16;
282 1.2 leo break;
283 1.1 leo case RES_STMID:
284 1.1 leo creg = &VIDEO->vd_tt_rgb[0];
285 1.1 leo ncreg = 4;
286 1.1 leo break;
287 1.1 leo case RES_STHIGH:
288 1.1 leo creg = &VIDEO->vd_tt_rgb[254];
289 1.1 leo ncreg = 2;
290 1.1 leo break;
291 1.1 leo case RES_TTLOW:
292 1.1 leo creg = &VIDEO->vd_tt_rgb[0];
293 1.1 leo ncreg = 256;
294 1.1 leo break;
295 1.1 leo case RES_TTMID:
296 1.1 leo creg = &VIDEO->vd_tt_rgb[0];
297 1.1 leo ncreg = 16;
298 1.1 leo break;
299 1.1 leo case RES_TTHIGH:
300 1.1 leo return(0); /* No colors */
301 1.1 leo default:
302 1.1 leo panic("grf_get_colormap: wrong mode!?");
303 1.1 leo }
304 1.1 leo
305 1.4 leo /* If first entry specified beyond capabilities -> error */
306 1.2 leo if(cm->first >= ncreg)
307 1.2 leo return(EINVAL);
308 1.4 leo
309 1.4 leo /*
310 1.4 leo * A little tricky, the actual colormap pointer will be NULL
311 1.4 leo * when view is not displaying, valid otherwise.
312 1.4 leo */
313 1.4 leo if(v->flags & VF_DISPLAY)
314 1.4 leo creg = &creg[cm->first];
315 1.4 leo else creg = NULL;
316 1.4 leo
317 1.4 leo vcreg = &vcm->entry[cm->first];
318 1.2 leo ncreg -= cm->first;
319 1.2 leo if(cm->size > ncreg)
320 1.2 leo return(EINVAL);
321 1.2 leo ncreg = cm->size;
322 1.2 leo
323 1.4 leo for(i = 0, src = cm->entry; i < ncreg; i++, vcreg++) {
324 1.4 leo *vcreg = *src++;
325 1.4 leo
326 1.4 leo /*
327 1.4 leo * If displaying, also update actual color register.
328 1.4 leo */
329 1.4 leo if(creg != NULL)
330 1.4 leo *creg++ = CM_LTOW(*vcreg);
331 1.2 leo }
332 1.1 leo return(0);
333 1.1 leo }
334 1.1 leo
335 1.1 leo static dmode_t *
336 1.3 leo get_best_display_mode(dim, depth, curr_mode)
337 1.3 leo int depth;
338 1.3 leo dimen_t *dim;
339 1.3 leo dmode_t *curr_mode;
340 1.1 leo {
341 1.1 leo dmode_t *save;
342 1.1 leo dmode_t *dm;
343 1.1 leo long dt, dx, dy, ct;
344 1.1 leo
345 1.1 leo save = NULL;
346 1.1 leo dm = modes.lh_first;
347 1.1 leo while(dm != NULL) {
348 1.3 leo dx = abs(dm->size.width - dim->width);
349 1.3 leo dy = abs(dm->size.height - dim->height);
350 1.1 leo ct = dx + dy;
351 1.1 leo
352 1.1 leo if (ct < dt || save == NULL) {
353 1.1 leo save = dm;
354 1.1 leo dt = ct;
355 1.1 leo }
356 1.1 leo dm = dm->link.le_next;
357 1.3 leo }
358 1.3 leo /*
359 1.3 leo * Did we do better than the current mode?
360 1.3 leo */
361 1.3 leo if((save != NULL) && (curr_mode != NULL)) {
362 1.3 leo dx = abs(curr_mode->size.width - dim->width);
363 1.3 leo dy = abs(curr_mode->size.height - dim->height);
364 1.3 leo ct = dx + dy;
365 1.3 leo if(ct <= dt)
366 1.3 leo return(NULL);
367 1.1 leo }
368 1.1 leo return (save);
369 1.1 leo }
370 1.1 leo
371 1.1 leo static view_t *
372 1.1 leo alloc_view(mode, dim, depth)
373 1.1 leo dmode_t *mode;
374 1.1 leo dimen_t *dim;
375 1.1 leo u_char depth;
376 1.1 leo {
377 1.1 leo view_t *v;
378 1.1 leo bmap_t *bm;
379 1.1 leo
380 1.1 leo if(!atari_realconfig)
381 1.1 leo v = &con_view;
382 1.2 leo else v = malloc(sizeof(*v), M_DEVBUF, M_NOWAIT);
383 1.4 leo if(v == NULL)
384 1.4 leo return(NULL);
385 1.4 leo
386 1.4 leo bm = alloc_bitmap(mode->size.width, mode->size.height, mode->depth);
387 1.1 leo if(bm) {
388 1.1 leo box_t box;
389 1.1 leo
390 1.1 leo v->colormap = alloc_colormap(mode);
391 1.1 leo if(v->colormap) {
392 1.1 leo INIT_BOX(&box,0,0,mode->size.width,mode->size.height);
393 1.1 leo init_view(v, bm, mode, &box);
394 1.1 leo return(v);
395 1.1 leo }
396 1.1 leo free_bitmap(bm);
397 1.1 leo }
398 1.1 leo if(v != &con_view)
399 1.1 leo free(v, M_DEVBUF);
400 1.1 leo return (NULL);
401 1.1 leo }
402 1.1 leo
403 1.1 leo static void
404 1.1 leo init_view(v, bm, mode, dbox)
405 1.1 leo view_t *v;
406 1.1 leo bmap_t *bm;
407 1.1 leo dmode_t *mode;
408 1.1 leo box_t *dbox;
409 1.1 leo {
410 1.1 leo v->bitmap = bm;
411 1.1 leo v->mode = mode;
412 1.4 leo v->flags = 0;
413 1.1 leo bcopy(dbox, &v->display, sizeof(box_t));
414 1.1 leo }
415 1.1 leo
416 1.1 leo /* bitmap functions */
417 1.1 leo
418 1.1 leo static bmap_t *
419 1.4 leo alloc_bitmap(width, height, depth)
420 1.1 leo u_long width, height;
421 1.1 leo u_char depth;
422 1.1 leo {
423 1.1 leo int i;
424 1.1 leo u_long total_size, bm_size;
425 1.1 leo void *hw_address;
426 1.1 leo bmap_t *bm;
427 1.1 leo
428 1.1 leo /*
429 1.1 leo * Sigh, it seems for mapping to work we need the bitplane data to
430 1.1 leo * 1: be aligned on a page boundry.
431 1.1 leo * 2: be n pages large.
432 1.1 leo *
433 1.1 leo * why? because the user gets a page aligned address, if this is before
434 1.1 leo * your allocation, too bad. Also it seems that the mapping routines
435 1.1 leo * do not watch to closely to the allowable length. so if you go over
436 1.1 leo * n pages by less than another page, the user gets to write all over
437 1.1 leo * the entire page. Since you did not allocate up to a page boundry
438 1.1 leo * (or more) the user writes into someone elses memory. -ch
439 1.1 leo */
440 1.1 leo bm_size = atari_round_page((width * height * depth) / NBBY);
441 1.1 leo total_size = bm_size + sizeof(bmap_t) + NBPG;
442 1.1 leo
443 1.1 leo if((bm = (bmap_t*)alloc_stmem(total_size, &hw_address)) == NULL)
444 1.1 leo return(NULL);
445 1.1 leo
446 1.1 leo bm->plane = (u_char*)bm + sizeof(bmap_t);
447 1.1 leo bm->plane = (u_char*)atari_round_page(bm->plane);
448 1.1 leo bm->hw_address = (u_char*)hw_address + sizeof(bmap_t);
449 1.1 leo bm->hw_address = (u_char*)atari_round_page(bm->hw_address);
450 1.1 leo bm->bytes_per_row = (width * depth) / NBBY;
451 1.1 leo bm->rows = height;
452 1.1 leo bm->depth = depth;
453 1.1 leo
454 1.4 leo bzero(bm->plane, bm_size);
455 1.1 leo return(bm);
456 1.1 leo }
457 1.1 leo
458 1.1 leo static void
459 1.1 leo free_bitmap(bm)
460 1.1 leo bmap_t *bm;
461 1.1 leo {
462 1.1 leo if(bm)
463 1.1 leo free_stmem(bm);
464 1.1 leo }
465 1.1 leo
466 1.1 leo static colormap_t *
467 1.1 leo alloc_colormap(dm)
468 1.1 leo dmode_t *dm;
469 1.1 leo {
470 1.1 leo int nentries, i;
471 1.1 leo colormap_t *cm;
472 1.2 leo u_char type = CM_COLOR;
473 1.1 leo
474 1.1 leo switch(dm->vm_reg) {
475 1.1 leo case RES_STLOW:
476 1.1 leo case RES_TTMID:
477 1.1 leo nentries = 16;
478 1.1 leo break;
479 1.1 leo case RES_STMID:
480 1.1 leo nentries = 4;
481 1.1 leo break;
482 1.1 leo case RES_STHIGH:
483 1.1 leo nentries = 2;
484 1.1 leo break;
485 1.1 leo case RES_TTLOW:
486 1.1 leo nentries = 256;
487 1.1 leo break;
488 1.1 leo case RES_TTHIGH:
489 1.2 leo type = CM_MONO;
490 1.1 leo nentries = 0;
491 1.4 leo break;
492 1.1 leo default:
493 1.1 leo panic("grf:alloc_colormap: wrong mode!?");
494 1.1 leo }
495 1.1 leo if(!atari_realconfig) {
496 1.1 leo cm = &con_cmap;
497 1.2 leo cm->entry = con_colors;
498 1.1 leo }
499 1.1 leo else {
500 1.1 leo int size;
501 1.1 leo
502 1.2 leo size = sizeof(*cm) + (nentries * sizeof(cm->entry[0]));
503 1.2 leo cm = malloc(size, M_DEVBUF, M_NOWAIT);
504 1.1 leo if(cm == NULL)
505 1.1 leo return(NULL);
506 1.2 leo cm->entry = (long *)&cm[1];
507 1.1 leo
508 1.1 leo }
509 1.2 leo if((cm->type = type) == CM_COLOR)
510 1.2 leo cm->red_mask = cm->green_mask = cm->blue_mask = 0xf;
511 1.2 leo cm->first = 0;
512 1.2 leo cm->size = nentries;
513 1.2 leo
514 1.2 leo for(i = 0; i < nentries; i++) {
515 1.2 leo register short val = def_color16[i % 16];
516 1.2 leo cm->entry[i] = CM_WTOL(val);
517 1.2 leo }
518 1.1 leo return(cm);
519 1.1 leo }
520