summaryrefslogtreecommitdiff
path: root/sys/arch/atari/dev/grfabs.c
blob: 4f2d60d680f6709c9fb10befde8046aa87fa5295 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*	$NetBSD: grfabs.c,v 1.4 1995/05/21 10:54:44 leo Exp $	*/

/*
 * Copyright (c) 1995 Leo Weppelman.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Leo Weppelman.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *  atari abstract graphics driver.
 */
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/malloc.h>

#include <machine/iomap.h>
#include <machine/video.h>
#include <machine/mfp.h>
#include <atari/dev/grfabs_reg.h>

/*
 * Function decls
 */
static dmode_t    *get_best_display_mode __P((dimen_t *, int, dmode_t *));
static view_t     *alloc_view __P((dmode_t *, dimen_t *, u_char));
static void       init_view __P((view_t *, bmap_t *, dmode_t *, box_t *));
static bmap_t	  *alloc_bitmap __P((u_long, u_long, u_char));
static void 	  free_bitmap __P((bmap_t *));
static colormap_t *alloc_colormap __P((dmode_t *));

/*
 * Ugh.. Stuff needed to allocate console structures before the VM-system
 * is running. There is no malloc() available at that time.
 */
extern int		atari_realconfig;	/* 0 -> no malloc	*/
static view_t		con_view;
static colormap_t	con_cmap;
static long		con_colors[MAX_CENTRIES];

/*
 * List of available graphic modes
 */
static LIST_HEAD(modelist, display_mode) modes;

static dmode_t vid_modes[] = {
	{ { NULL, NULL }, "sthigh", { 640,  400 },  1, RES_STHIGH },
	{ { NULL, NULL }, "tthigh", { 1280, 960 },  1, RES_TTHIGH },
	{ { NULL, NULL }, "stmid",  { 640,  200 },  2, RES_STMID  },
	{ { NULL, NULL }, "stlow",  { 320,  200 },  4, RES_STLOW  },
	{ { NULL, NULL }, "ttmid",  { 640,  480 },  4, RES_TTMID  },
	{ { NULL, NULL }, "ttlow",  { 320,  480 },  8, RES_TTLOW  },
	{ { NULL, NULL }, NULL,  }
};

/*
 * Default colors.....
 * Currently the TT-low (256 colors) just uses 16 time the 16-color default.
 * If you have a sensible 256 scale, feel free to add.....
 * The first 2 colors in all maps are {black,white}, so ite (text) displays
 * are initially readable. Also, this enables me to supply only 1 map. The
 * 4 color mode uses the first four entries of the 16 color mode thus giving
 * a gray scale display. (Maybe we can add an intensity bit to the ite...)
 */
static u_short def_color16[16] = {
	0x000,	/* black		*/
	0xfff,	/* white		*/
	0xccc,	/* light gray		*/
	0x888,	/* gray			*/
	0x00c,	/* blue			*/
	0x0c0,	/* green		*/
	0x0cc,	/* cyan			*/
	0xc00,	/* red			*/
	0xc0c,	/* magenta		*/
	0xcc0,	/* brown		*/
	0x00f,	/* light blue		*/
	0x0f0,	/* light green		*/
	0x0ff,	/* light cyan		*/
	0xf00,	/* light red		*/
	0xf0f,	/* light magenta	*/
	0xff0	/* light brown		*/
};

/*
 * XXX: called from ite console init routine.
 * Initialize list of posible video modes.
 */
int
grfabs_probe()
{
		dmode_t	*dm;
		int	i;
		int	has_mono;
	static  int	inited = 0;

	if(inited)
		return(1);	/* Has to be done only once */
	inited++;

	/*
	 * First find out what kind of monitor is attached. Dma-sound
	 * should be off because the 'sound-done' and 'monochrome-detect'
	 * are xor-ed together. I think that shutting it down here is the
	 * wrong place.
	 */
	has_mono = (MFP->mf_gpip & IA_MONO) == 0;

	LIST_INIT(&modes);
	for(i = 0; (dm = &vid_modes[i])->name != NULL; i++) {
		if(has_mono && (dm->vm_reg != RES_TTHIGH))
			continue;
		if(!has_mono && (dm->vm_reg == RES_TTHIGH))
			continue;
		LIST_INSERT_HEAD(&modes, dm, link);
	}

	/*
	 * This seems to prevent bordered screens.
	 */
	for(i=0; i < 16; i++)
		VIDEO->vd_tt_rgb[i] = def_color16[i];
	return(1);
}

view_t *
grf_alloc_view(d, dim, depth)
dmode_t	*d;
dimen_t	*dim;
u_char	depth;
{
	if(!d)
		d = get_best_display_mode(dim, depth, NULL);
	if(d)
		return(alloc_view(d, dim, depth));
	return(NULL);
}

dmode_t	*
grf_get_best_mode(dim, depth)
dimen_t	*dim;
u_char	depth;
{
		return(get_best_display_mode(dim, depth, NULL));
}

void grf_display_view(v)
view_t *v;
{
	dmode_t	*dm = v->mode;
	bmap_t	*bm = v->bitmap;

	if(dm->current_view) {
		/*
		 * Mark current view for this mode as no longer displayed
		 */
		dm->current_view->flags &= ~VF_DISPLAY;
	}
	dm->current_view = v;
	v->flags |= VF_DISPLAY;

	grf_use_colormap(v, v->colormap);

	/* XXX: should use vbl for this	*/
	VIDEO->vd_tt_res = dm->vm_reg;
	VIDEO->vd_raml   =  (u_long)bm->hw_address & 0xff;
	VIDEO->vd_ramm   = ((u_long)bm->hw_address >>  8) & 0xff;
	VIDEO->vd_ramh   = ((u_long)bm->hw_address >> 16) & 0xff;
}

void grf_remove_view(v)
view_t *v;
{
	dmode_t *mode = v->mode;

	if(mode->current_view == v) {
#if 0
		if(v->flags & VF_DISPLAY)
			panic("Cannot shutdown display\n"); /* XXX */
#endif
		mode->current_view = NULL;
	}
	v->flags &= ~VF_DISPLAY;
}

void grf_free_view(v)
view_t *v;
{
	if(v) {
		dmode_t *md = v->mode;

		grf_remove_view(v);
		if(v->colormap != &con_cmap)
			free(v->colormap, M_DEVBUF);
		free_bitmap(v->bitmap);
		if(v != &con_view)
			free(v, M_DEVBUF);
	}
}

int
grf_get_colormap(v, cm)
view_t		*v;
colormap_t	*cm;
{
	colormap_t	*gcm;
	int		i, n;
	u_long		*sv_entry;

	gcm = v->colormap;
	n   = cm->size < gcm->size ? cm->size : gcm->size;

	/*
	 * Copy struct from view but be carefull to keep 'entry'
	 */
	sv_entry = cm->entry;
	*cm = *gcm;
	cm->entry = sv_entry;

	/*
	 * Copy the colors
	 */
	bzero(cm->entry, cm->size * sizeof(long));
	for(i = 0; i < n; i++)
		cm->entry[i] = gcm->entry[i];
	return(0);
}

int
grf_use_colormap(v, cm)
view_t		*v;
colormap_t	*cm;
{
	dmode_t			*dm;
	volatile u_short	*creg;
	u_long			*src;
	colormap_t		*vcm;
	u_long			*vcreg;
	u_short			ncreg;
	int			i;

	dm  = v->mode;
	vcm = v->colormap;

	/*
	 * I guess it seems reasonable to require the maps to be
	 * of the same type...
	 */
	if(cm->type != vcm->type)
		return(EINVAL);

	/*
	 * First figure out where the actual colormap resides and
	 * howmany colors are in it.
	 */
	switch(dm->vm_reg) {
		case RES_STLOW:
			creg  = &VIDEO->vd_tt_rgb[0];
			ncreg = 16;
			break;
		case RES_STMID:
			creg  = &VIDEO->vd_tt_rgb[0];
			ncreg = 4;
			break;
		case RES_STHIGH:
			creg  = &VIDEO->vd_tt_rgb[254];
			ncreg = 2;
			break;
		case RES_TTLOW:
			creg  = &VIDEO->vd_tt_rgb[0];
			ncreg = 256;
			break;
		case RES_TTMID:
			creg  = &VIDEO->vd_tt_rgb[0];
			ncreg = 16;
			break;
		case RES_TTHIGH:
			return(0);	/* No colors	*/
		default:
			panic("grf_get_colormap: wrong mode!?");
	}

	/* If first entry specified beyond capabilities -> error */
	if(cm->first >= ncreg)
		return(EINVAL);

	/*
	 * A little tricky, the actual colormap pointer will be NULL
	 * when view is not displaying, valid otherwise.
	 */
	if(v->flags & VF_DISPLAY)
		creg = &creg[cm->first];
	else creg = NULL;

	vcreg  = &vcm->entry[cm->first];
	ncreg -= cm->first;
	if(cm->size > ncreg)
		return(EINVAL);
	ncreg = cm->size;

	for(i = 0, src = cm->entry; i < ncreg; i++, vcreg++) {
		*vcreg = *src++;

		/*
		 * If displaying, also update actual color register.
		 */
		if(creg != NULL)
			*creg++ = CM_LTOW(*vcreg);
	}
	return(0);
}

static dmode_t *
get_best_display_mode(dim, depth, curr_mode)
int	depth;
dimen_t	*dim;
dmode_t	*curr_mode;
{
	dmode_t		*save;
	dmode_t		*dm;
	long   		dt, dx, dy, ct;

	save = NULL;
	dm = modes.lh_first;
	while(dm != NULL) {
		dx = abs(dm->size.width  - dim->width);
		dy = abs(dm->size.height - dim->height);
		ct = dx + dy;

		if (ct < dt || save == NULL) {
			save = dm;
			dt = ct;
		}
		dm = dm->link.le_next;
	}
	/*
	 * Did we do better than the current mode?
	 */
	if((save != NULL) && (curr_mode != NULL)) {
		dx = abs(curr_mode->size.width  - dim->width);
		dy = abs(curr_mode->size.height - dim->height);
		ct = dx + dy;
		if(ct <= dt)
			return(NULL);
	}
	return (save);
}

static view_t *
alloc_view(mode, dim, depth)
dmode_t	*mode;
dimen_t	*dim;
u_char   depth;
{
	view_t *v;
	bmap_t *bm;

	if(!atari_realconfig)
		v = &con_view;
	else v = malloc(sizeof(*v), M_DEVBUF, M_NOWAIT);
	if(v == NULL)
		return(NULL);
	
	bm = alloc_bitmap(mode->size.width, mode->size.height, mode->depth);
	if(bm) {
		box_t   box;

		v->colormap = alloc_colormap(mode);
		if(v->colormap) {
			INIT_BOX(&box,0,0,mode->size.width,mode->size.height);
			init_view(v, bm, mode, &box);
			return(v);
		}
		free_bitmap(bm);
	}
	if(v != &con_view)
		free(v, M_DEVBUF);
	return (NULL);
}

static void
init_view(v, bm, mode, dbox)
view_t	*v;
bmap_t	*bm;
dmode_t	*mode;
box_t	*dbox;
{
	v->bitmap = bm;
	v->mode   = mode;
	v->flags  = 0;
	bcopy(dbox, &v->display, sizeof(box_t));
}

/* bitmap functions */

static bmap_t *
alloc_bitmap(width, height, depth)
u_long	width, height;
u_char	depth;
{
	int     i;
	u_long  total_size, bm_size;
	void	*hw_address;
	bmap_t	*bm;

	/*
	 * Sigh, it seems for mapping to work we need the bitplane data to
	 *  1: be aligned on a page boundry.
	 *  2: be n pages large.
	 * 
	 * why? because the user gets a page aligned address, if this is before
	 * your allocation, too bad.  Also it seems that the mapping routines
	 * do not watch to closely to the allowable length. so if you go over
	 * n pages by less than another page, the user gets to write all over
	 * the entire page. Since you did not allocate up to a page boundry
	 * (or more) the user writes into someone elses memory. -ch
	 */
	bm_size    = atari_round_page((width * height * depth) / NBBY);
	total_size = bm_size + sizeof(bmap_t) + NBPG;

	if((bm = (bmap_t*)alloc_stmem(total_size, &hw_address)) == NULL)
		return(NULL);

	bm->plane         = (u_char*)bm + sizeof(bmap_t);
	bm->plane         = (u_char*)atari_round_page(bm->plane);
	bm->hw_address    = (u_char*)hw_address + sizeof(bmap_t);
	bm->hw_address    = (u_char*)atari_round_page(bm->hw_address);
	bm->bytes_per_row = (width * depth) / NBBY;
	bm->rows          = height;
	bm->depth         = depth;

	bzero(bm->plane, bm_size);
	return(bm);
}

static void
free_bitmap(bm)
bmap_t *bm;
{
	if(bm)
		free_stmem(bm);
}

static colormap_t *
alloc_colormap(dm)
dmode_t		*dm;
{
	int		nentries, i;
	colormap_t	*cm;
	u_char		type = CM_COLOR;

	switch(dm->vm_reg) {
		case RES_STLOW:
		case RES_TTMID:
			nentries = 16;
			break;
		case RES_STMID:
			nentries = 4;
			break;
		case RES_STHIGH:
			nentries = 2;
			break;
		case RES_TTLOW:
			nentries = 256;
			break;
		case RES_TTHIGH:
			type     = CM_MONO;
			nentries = 0;
			break;
		default:
			panic("grf:alloc_colormap: wrong mode!?");
	}
	if(!atari_realconfig) {
		cm = &con_cmap;
		cm->entry = con_colors;
	}
	else {
		int size;

		size = sizeof(*cm) + (nentries * sizeof(cm->entry[0]));
		cm   = malloc(size, M_DEVBUF, M_NOWAIT);
		if(cm == NULL)
			return(NULL);
		cm->entry = (long *)&cm[1];

	}
	if((cm->type = type) == CM_COLOR)
		cm->red_mask = cm->green_mask = cm->blue_mask = 0xf;
	cm->first = 0;
	cm->size  = nentries;

	for(i = 0; i < nentries; i++) {
		register short	val = def_color16[i % 16];
		cm->entry[i] = CM_WTOL(val);
	}
	return(cm);
}