xref: /netbsd-src/sys/arch/atari/dev/ite_cc.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*	$NetBSD: ite_cc.c,v 1.1.1.1 1995/03/26 07:12:14 leo Exp $	*/
2 
3 /*
4  * Copyright (c) 1994 Christian E. Hopps
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Christian E. Hopps.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "grf.h"
34 #if NGRF > 0
35 
36 #include <sys/param.h>
37 #include <sys/conf.h>
38 #include <sys/proc.h>
39 #include <sys/device.h>
40 #include <sys/ioctl.h>
41 #include <sys/tty.h>
42 #include <sys/systm.h>
43 #include <sys/queue.h>
44 #include <sys/termios.h>
45 #include <sys/malloc.h>
46 #include <dev/cons.h>
47 #include <machine/cpu.h>
48 #include <atari/dev/itevar.h>
49 #include <atari/dev/iteioctl.h>
50 #include <atari/dev/grfioctl.h>
51 #include <atari/dev/grfabs_reg.h>
52 #include <atari/dev/grfvar.h>
53 #include <atari/dev/font.h>
54 #include <atari/dev/viewioctl.h>
55 #include <atari/dev/viewvar.h>
56 
57 /*
58  * This is what ip->priv points to;
59  * it contains local variables for custom-chip ites.
60  */
61 struct ite_priv {
62 	u_char	**row_ptr;	/* array of pointers into the bitmap	*/
63 	u_long	row_bytes;
64 	u_long	cursor_opt;
65 	u_short	*column_offset;	/* array of offsets for columns		*/
66 	u_int	row_offset;	/* the row offset			*/
67 	u_short	width;		/* the bitmap width			*/
68 	u_short	underline;	/* where the underline goes		*/
69 	u_short	ft_x;		/* the font width			*/
70 	u_short	ft_y;		/* the font height			*/
71 	u_char	*font_cell[256];/* the font pointer			*/
72 };
73 typedef struct ite_priv ipriv_t;
74 
75 /*
76  * We need the following space to get an ite-console setup before
77  * the VM-system is brought up. We setup for a 1280x960 monitor with
78  * an 8x8 font.
79  */
80 extern int	atari_realconfig;
81 
82 #define	CONS_MAXROW	120	/* Max. number of rows on console	*/
83 #define	CONS_MAXCOL	160	/* Max. number of columns on console	*/
84 static u_short	con_columns[CONS_MAXCOL];
85 static u_char	*con_rows[CONS_MAXROW];
86 static ipriv_t	con_ipriv;
87 
88 extern font_info	font_info_8x8;
89 extern font_info	font_info_8x16;
90 
91 static void view_init __P((struct ite_softc *));
92 static void view_deinit __P((struct ite_softc *));
93 static int  ite_newsize __P((struct ite_softc *, struct itewinsize *));
94 static void cursor32 __P((struct ite_softc *, int));
95 static void putc8 __P((struct ite_softc *, int, int, int, int));
96 static void clear8 __P((struct ite_softc *, int, int, int, int));
97 static void scroll8 __P((struct ite_softc *, int, int, int, int));
98 static void scrollbmap __P((bmap_t *, u_short, u_short, u_short, u_short,
99 							short, short));
100 
101 /*
102  * Patchable
103  */
104 int ite_default_x      = 0;	/* def leftedge offset	*/
105 int ite_default_y      = 0;	/* def topedge offset	*/
106 int ite_default_width  = 640;	/* def width		*/
107 int ite_default_depth  = 1;	/* def depth		*/
108 int ite_default_height = 400;	/* def height		*/
109 
110 /*
111  * called from grf_cc to return console priority
112  */
113 int
114 grfcc_cnprobe()
115 {
116 	return(CN_INTERNAL);
117 }
118 /*
119  * called from grf_cc to init ite portion of
120  * grf_softc struct
121  */
122 void
123 grfcc_iteinit(gp)
124 struct grf_softc *gp;
125 {
126 
127 	gp->g_itecursor = cursor32;
128 	gp->g_iteputc   = putc8;
129 	gp->g_iteclear  = clear8;
130 	gp->g_itescroll = scroll8;
131 	gp->g_iteinit   = view_init;
132 	gp->g_itedeinit = view_deinit;
133 }
134 
135 static void
136 view_deinit(ip)
137 struct ite_softc	*ip;
138 {
139 	ip->flags &= ~ITE_INITED;
140 }
141 
142 static void
143 view_init(ip)
144 register struct ite_softc *ip;
145 {
146 	struct itewinsize	wsz;
147 	ipriv_t			*cci;
148 
149 	if(cci = ip->priv)
150 		return;
151 
152 #if defined(KFONT_8X8)
153 	ip->font = font_info_8x8;
154 #else
155 	ip->font = font_info_8x16;
156 #endif
157 
158 	/* Find the correct set of rendering routines for this font.  */
159 	if(ip->font.width != 8)
160 		panic("kernel font size not supported");
161 
162 	if(!atari_realconfig)
163 		ip->priv = cci = &con_ipriv;
164 	else ip->priv = cci = (ipriv_t*)malloc(sizeof(*cci), M_DEVBUF,M_WAITOK);
165 	if(cci == NULL)
166 		panic("No memory for ite-view");
167 	bzero(cci, sizeof(*cci));
168 
169 	cci->cursor_opt    = 0;
170 	cci->row_ptr       = NULL;
171 	cci->column_offset = NULL;
172 
173 	wsz.x      = ite_default_x;
174 	wsz.y      = ite_default_y;
175 	wsz.width  = ite_default_width;
176 	wsz.height = ite_default_height;
177 	wsz.depth  = ite_default_depth;
178 
179 	ite_newsize (ip, &wsz);
180 
181 	/*
182 	 * Only console will be turned on by default..
183 	 */
184 	if(ip->flags & ITE_ISCONS)
185 		ip->grf->g_mode(ip->grf, GM_GRFON, NULL, 0, 0);
186 }
187 
188 static int
189 ite_newsize(ip, winsz)
190 struct ite_softc	*ip;
191 struct itewinsize	*winsz;
192 {
193 	struct view_size		vs;
194 	ipriv_t				*cci = ip->priv;
195 	u_long				fbp, i, j;
196 	int				error = 0;
197 	view_t				*view = ip->grf->g_view;
198 
199 	vs.x      = winsz->x;
200 	vs.y      = winsz->y;
201 	vs.width  = winsz->width;
202 	vs.height = winsz->height;
203 	vs.depth  = winsz->depth;
204 	error = viewioctl(ip->grf->g_viewdev, VIOCSSIZE, &vs, 0, -1);
205 
206 	/*
207 	 * Reinitialize our structs
208 	 */
209 	ip->cols = view->display.width  / ip->font.width;
210 	ip->rows = view->display.height / ip->font.height;
211 
212 	/*
213 	 * save new values so that future opens use them
214 	 * this may not be correct when we implement Virtual Consoles
215 	 */
216 	ite_default_height = view->display.height;
217 	ite_default_width  = view->display.width;
218 	ite_default_x      = view->display.x;
219 	ite_default_y      = view->display.y;
220 	ite_default_depth  = view->bitmap->depth;
221 
222 	if(cci->row_ptr && (cci->row_ptr != con_rows)) {
223 		free(cci->row_ptr, M_DEVBUF);
224 		cci->row_ptr = NULL;
225 	}
226 	if(cci->column_offset && (cci->column_offset != con_columns)) {
227 		free(cci->column_offset, M_DEVBUF);
228 		cci->column_offset = NULL;
229 	}
230 
231 	if(!atari_realconfig) {
232 		cci->row_ptr       = con_rows;
233 		cci->column_offset = con_columns;
234 	}
235 	else {
236 	  cci->row_ptr = malloc(sizeof(u_char *) * ip->rows,M_DEVBUF,M_WAITOK);
237 	  cci->column_offset = malloc(sizeof(u_int)*ip->cols,M_DEVBUF,M_WAITOK);
238 	}
239 
240 	if(!cci->row_ptr || !cci->column_offset)
241 		panic("No memory for ite-view");
242 
243 
244 	cci->width      = view->bitmap->bytes_per_row << 3;
245 	cci->underline  = ip->font.baseline + 1;
246 	cci->row_offset = view->bitmap->bytes_per_row;
247 	cci->ft_x       = ip->font.width;
248 	cci->ft_y       = ip->font.height;
249 	cci->row_bytes  = cci->row_offset * cci->ft_y;
250 	cci->row_ptr[0] = view->bitmap->plane;
251 	for(i = 1; i < ip->rows; i++)
252 		cci->row_ptr[i] = cci->row_ptr[i-1] + cci->row_bytes;
253 
254 	/*
255 	 * Initialize the column offsets to point at the correct location
256 	 * in the first plane. This definitely assumes a font width of 8!
257 	 */
258 	j = view->bitmap->depth * 2;
259 	cci->column_offset[0] = 0;
260 	for(i = 1; i < ip->cols; i++)
261 		cci->column_offset[i] = ((i >> 1) * j) + (i & 1);
262 
263 	/* initialize the font cell pointers */
264 	cci->font_cell[ip->font.font_lo] = ip->font.font_p;
265 	for(i = ip->font.font_lo+1; i <= ip->font.font_hi; i++)
266 		cci->font_cell[i] = cci->font_cell[i-1] + ip->font.height;
267 
268 	return(error);
269 }
270 
271 static void
272 cursor32(struct ite_softc *ip, int flag)
273 {
274 	int	cend;
275 	u_char	*pl;
276 	ipriv_t	*cci;
277 
278 	cci = ip->priv;
279 
280 	if(flag == END_CURSOROPT)
281 		cci->cursor_opt--;
282 	else if(flag == START_CURSOROPT) {
283 			if(!cci->cursor_opt)
284 				cursor32(ip, ERASE_CURSOR);
285 			cci->cursor_opt++;
286 			return;		  /* if we are already opted. */
287 	}
288 
289 	if(cci->cursor_opt)
290 		return;		  /* if we are still nested. */
291 				  /* else we draw the cursor. */
292 
293 	if(flag != DRAW_CURSOR && flag != END_CURSOROPT) {
294 		/*
295 		 * erase the cursor
296 		 */
297 		cend = ip->font.height-1;
298 		pl   = cci->column_offset[ip->cursorx]
299 				+ cci->row_ptr[ip->cursory];
300 		__asm__ __volatile__
301 			("1: notb  %0@ ; addaw %4,%0\n\t"
302 			 "dbra  %1,1b"
303 			 : "=a" (pl), "=d" (cend)
304 			 : "0" (pl), "1" (cend),
305 			 "d" (cci->row_offset)
306 			 );
307 	}
308 
309 	if(flag != DRAW_CURSOR && flag != MOVE_CURSOR && flag != END_CURSOROPT)
310 		return;
311 
312 	/*
313 	 * draw the cursor
314 	 */
315 	ip->cursorx = min(ip->curx, ip->cols-1);
316 	ip->cursory = ip->cury;
317 	cend        = ip->font.height-1;
318 	pl          = cci->column_offset[ip->cursorx]
319 			+ cci->row_ptr[ip->cursory];
320 
321 	__asm__ __volatile__
322 		("1: notb  %0@ ; addaw %4,%0\n\t"
323 		 "dbra  %1,1b"
324 		 : "=a" (pl), "=d" (cend)
325 		 : "0" (pl), "1" (cend),
326 		 "d" (cci->row_offset)
327 		 );
328 }
329 
330 static void
331 putc8(struct ite_softc *ip, int c, int dy, int dx, int mode)
332 {
333     register ipriv_t	*cci = (ipriv_t *)ip->priv;
334     register u_char	*pl  = cci->column_offset[dx] + cci->row_ptr[dy];
335     register u_int	fh   = cci->ft_y;
336     register u_int	ro   = cci->row_offset;
337     register u_char	eor_mask;
338     register u_char	bl, tmp, ul;
339     register u_char	*ft;
340 
341     if(c < ip->font.font_lo || c > ip->font.font_hi)
342 		return;
343 
344 	ft = cci->font_cell[c];
345 
346 	if(!mode) {
347 		while(fh--) {
348 			*pl = *ft++; pl += ro;
349 		}
350 		return;
351 	}
352 
353 	eor_mask = (mode & ATTR_INV) ? 0xff : 0x00;
354 	bl       = (mode & ATTR_BOLD) ? 1 : 0;
355 	ul       = fh - cci->underline;
356 	while(fh--) {
357 		tmp = *ft++ ^ eor_mask;
358 		if(bl)
359 			*pl = tmp | (tmp >> 1);
360 		else *pl = tmp;
361 		if(fh == ul)
362 			*pl = 0xff;
363 		pl += ro;
364 	}
365 }
366 
367 static void
368 clear8(struct ite_softc *ip, int sy, int sx, int h, int w)
369 {
370 	ipriv_t	*cci = (ipriv_t *) ip->priv;
371 	view_t	*v   = ip->grf->g_view;
372 	bmap_t	*bm  = v->bitmap;
373 
374 	if((sx == 0) && (w == ip->cols)) {
375 		/* common case: clearing whole lines */
376 		while (h--) {
377 			int		i;
378 			u_char	*ptr = cci->row_ptr[sy];
379 			for(i = 0; i < ip->font.height; i++) {
380 				bzero(ptr, bm->bytes_per_row);
381 				ptr += bm->bytes_per_row;
382 			}
383 			sy++;
384 		}
385 	}
386 	else {
387 		/*
388 		 * clearing only part of a line
389 		 * XXX could be optimized MUCH better, but is it worth the
390 		 * trouble?
391 		 */
392 
393 		int		i;
394 		u_char  *pls, *ple;
395 
396 		pls = cci->row_ptr[sy];
397 		ple = pls + cci->column_offset[sx + w-1];
398 		pls = pls + cci->column_offset[sx];
399 
400 		for(i = ((ip->font.height) * h)-1; i >= 0; i--) {
401 			u_char *p = pls;
402 			while(p <= ple)
403 				*p++ = 0;
404 			pls += bm->bytes_per_row;
405 			ple += bm->bytes_per_row;
406 		}
407 	}
408 }
409 
410 /* Note: sx is only relevant for SCROLL_LEFT or SCROLL_RIGHT.  */
411 static void
412 scroll8(ip, sy, sx, count, dir)
413 register struct ite_softc	*ip;
414 register int			sy;
415 int				dir, sx, count;
416 {
417 	bmap_t *bm = ip->grf->g_view->bitmap;
418 	u_char *pl = ((ipriv_t *)ip->priv)->row_ptr[sy];
419 
420 	if(dir == SCROLL_UP) {
421 		int	dy = sy - count;
422 		int	height = ip->bottom_margin - sy + 1;
423 		int	i;
424 
425 		cursor32(ip, ERASE_CURSOR);
426 		scrollbmap(bm, 0, dy*ip->font.height, bm->bytes_per_row >> 3,
427 				(ip->bottom_margin-dy+1)*ip->font.height,
428 				0, -(count*ip->font.height));
429 	}
430 	else if(dir == SCROLL_DOWN) {
431 		int	dy = sy + count;
432 		int	height = ip->bottom_margin - dy + 1;
433 		int	i;
434 
435         cursor32(ip, ERASE_CURSOR);
436 		scrollbmap(bm, 0, sy*ip->font.height, bm->bytes_per_row >> 3,
437 				(ip->bottom_margin-sy+1)*ip->font.height,
438 				0, count*ip->font.height);
439 	}
440 	else if(dir == SCROLL_RIGHT) {
441 		int	sofs = (ip->cols - count) * ip->font.width;
442 		int	dofs = (ip->cols) * ip->font.width;
443 		int	i, j;
444 
445 		cursor32(ip, ERASE_CURSOR);
446 		for(j = ip->font.height-1; j >= 0; j--) {
447 		    int sofs2 = sofs, dofs2 = dofs;
448 		    for (i = (ip->cols - (sx + count))-1; i >= 0; i--) {
449 			int	t;
450 			sofs2 -= ip->font.width;
451 			dofs2 -= ip->font.width;
452 			asm("bfextu %1@{%2:%3},%0" : "=d" (t)
453 				: "a" (pl), "d" (sofs2), "d" (ip->font.width));
454 			asm("bfins %3,%0@{%1:%2}" :
455 				: "a" (pl), "d" (dofs2), "d" (ip->font.width),
456 				  "d" (t));
457 		    }
458 			pl += bm->bytes_per_row;
459 		}
460 	}
461 	else { /* SCROLL_LEFT */
462 		int sofs = (sx) * ip->font.width;
463 		int dofs = (sx - count) * ip->font.width;
464 		int i, j;
465 
466 		cursor32(ip, ERASE_CURSOR);
467 		for(j = ip->font.height-1; j >= 0; j--) {
468 		    int sofs2 = sofs, dofs2 = dofs;
469 		    for(i = (ip->cols - sx)-1; i >= 0; i--) {
470 			int	t;
471 
472 			asm("bfextu %1@{%2:%3},%0" : "=d" (t)
473 				: "a" (pl), "d" (sofs2), "d" (ip->font.width));
474 			asm("bfins %3,%0@{%1:%2}"
475 				: : "a" (pl), "d" (dofs2),"d" (ip->font.width),
476 				    "d" (t));
477 			sofs2 += ip->font.width;
478 			dofs2 += ip->font.width;
479 		    }
480 		    pl += bm->bytes_per_row;
481 		}
482     }
483 }
484 
485 static void
486 scrollbmap (bmap_t *bm, u_short x, u_short y, u_short width, u_short height, short dx, short dy)
487 {
488     u_short	depth = bm->depth;
489     u_short lwpr  = bm->bytes_per_row >> 2;
490 
491     if(dx) {
492     	/* FIX: */ panic ("delta x not supported in scroll bitmap yet.");
493     }
494 
495     if(dy == 0) {
496         return;
497     }
498     if(dy > 0) {
499 		u_long *pl       = (u_long *)bm->plane;
500 		u_long *src_y    = pl + (lwpr*y);
501 		u_long *dest_y   = pl + (lwpr*(y+dy));
502 		u_long count     = lwpr*(height-dy);
503 		u_long *clr_y    = src_y;
504 		u_long clr_count = dest_y - src_y;
505 		u_long bc, cbc;
506 
507 		src_y  += count - 1;
508 		dest_y += count - 1;
509 
510 		bc = count >> 4;
511 		count &= 0xf;
512 
513 		while (bc--) {
514 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
515 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
516 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
517 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
518 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
519 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
520 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
521 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
522 		}
523 		while (count--)
524 		    *dest_y-- = *src_y--;
525 
526 		cbc = clr_count >> 4;
527 		clr_count &= 0xf;
528 
529 		while (cbc--) {
530 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
531 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
532 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
533 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
534 		}
535 		while(clr_count--)
536 		    *clr_y++ = 0;
537     }
538 	else {
539 		u_long	*pl       = (u_long *)bm->plane;
540 		u_long	*src_y    = pl + (lwpr*(y-dy));
541 		u_long	*dest_y   = pl + (lwpr*y);
542 		long	count     = lwpr*(height + dy);
543 		u_long	*clr_y    = dest_y + count;
544 		u_long	clr_count = src_y - dest_y;
545 		u_long	bc, cbc;
546 
547 		bc = count >> 4;
548 		count &= 0xf;
549 
550 		while(bc--) {
551 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
552 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
553 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
554 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
555 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
556 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
557 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
558 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
559 		}
560 		while(count--)
561 		    *dest_y++ = *src_y++;
562 
563 		cbc = clr_count >> 4;
564 		clr_count &= 0xf;
565 
566 		while (cbc--) {
567 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
568 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
569 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
570 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
571 		}
572 		while (clr_count--)
573 		    *clr_y++ = 0;
574     }
575 }
576 #else
577 #error Must be defined
578 #endif /* NGRF */
579