xref: /netbsd-src/sys/arch/atari/dev/ite_cc.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: ite_cc.c,v 1.11 1996/10/13 04:11:00 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 Leo Weppelman
5  * Copyright (c) 1994 Christian E. Hopps
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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Christian E. Hopps.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/conf.h>
36 #include <sys/proc.h>
37 #include <sys/ioctl.h>
38 #include <sys/tty.h>
39 #include <sys/systm.h>
40 #include <sys/queue.h>
41 #include <sys/termios.h>
42 #include <sys/malloc.h>
43 #include <sys/device.h>
44 #include <dev/cons.h>
45 #include <machine/cpu.h>
46 #include <atari/atari/device.h>
47 #include <atari/dev/itevar.h>
48 #include <atari/dev/iteioctl.h>
49 #include <atari/dev/grfioctl.h>
50 #include <atari/dev/grfabs_reg.h>
51 #include <atari/dev/grfvar.h>
52 #include <atari/dev/font.h>
53 #include <atari/dev/viewioctl.h>
54 #include <atari/dev/viewvar.h>
55 
56 #include "grfcc.h"
57 
58 /*
59  * This is what ip->priv points to;
60  * it contains local variables for custom-chip ites.
61  */
62 struct ite_priv {
63 	u_char	**row_ptr;	/* array of pointers into the bitmap	*/
64 	u_long	row_bytes;
65 	u_long	cursor_opt;
66 	u_short	*column_offset;	/* array of offsets for columns		*/
67 	u_int	row_offset;	/* the row offset			*/
68 	u_short	width;		/* the bitmap width			*/
69 	u_short	underline;	/* where the underline goes		*/
70 	u_short	ft_x;		/* the font width			*/
71 	u_short	ft_y;		/* the font height			*/
72 	u_char	*font_cell[256];/* the font pointer			*/
73 };
74 typedef struct ite_priv ipriv_t;
75 
76 /*
77  * We need the following space to get an ite-console setup before
78  * the VM-system is brought up. We setup for a 1280x960 monitor with
79  * an 8x8 font.
80  */
81 #define	CONS_MAXROW	120	/* Max. number of rows on console	*/
82 #define	CONS_MAXCOL	160	/* Max. number of columns on console	*/
83 static u_short	con_columns[CONS_MAXCOL];
84 static u_char	*con_rows[CONS_MAXROW];
85 static ipriv_t	con_ipriv;
86 
87 extern font_info	font_info_8x8;
88 extern font_info	font_info_8x16;
89 
90 static void view_init __P((struct ite_softc *));
91 static void view_deinit __P((struct ite_softc *));
92 static int  itecc_ioctl __P((struct ite_softc *, u_long, caddr_t, int,
93 							struct proc *));
94 static int  ite_newsize __P((struct ite_softc *, struct itewinsize *));
95 static void cursor32 __P((struct ite_softc *, int));
96 static void putc8 __P((struct ite_softc *, int, int, int, int));
97 static void clear8 __P((struct ite_softc *, int, int, int, int));
98 static void scroll8 __P((struct ite_softc *, int, int, int, int));
99 static void scrollbmap __P((bmap_t *, u_short, u_short, u_short, u_short,
100 							short, short));
101 
102 /*
103  * Patchable
104  */
105 int ite_default_x      = 0;	/* def leftedge offset	*/
106 int ite_default_y      = 0;	/* def topedge offset	*/
107 int ite_default_width  = 640;	/* def width		*/
108 int ite_default_depth  = 1;	/* def depth		*/
109 int ite_default_height = 400;	/* def height		*/
110 
111 /*
112  * grfcc config stuff
113  */
114 void grfccattach __P((struct device *, struct device *, void *));
115 int  grfccmatch __P((struct device *, void *, void *));
116 int  grfccprint __P((void *, const char *));
117 
118 struct cfattach grfcc_ca = {
119 	sizeof(struct grf_softc), grfccmatch, grfccattach
120 };
121 
122 struct cfdriver grfcc_cd = {
123 	NULL, "grfcc", DV_DULL
124 };
125 
126 /*
127  * only used in console init.
128  */
129 static struct cfdata *cfdata_grf   = NULL;
130 
131 /*
132  * Probe functions we can use:
133  */
134 #ifdef TT_VIDEO
135 void	tt_probe_video __P((MODES *));
136 #endif /* TT_VIDEO */
137 #ifdef FALCON_VIDEO
138 void	falcon_probe_video __P((MODES *));
139 #endif /* FALCON_VIDEO */
140 
141 int
142 grfccmatch(pdp, match, auxp)
143 struct device	*pdp;
144 void	*match, *auxp;
145 {
146 	static int	must_probe = 1;
147 	grf_auxp_t	*grf_auxp = auxp;
148 	struct cfdata	*cfp = match;
149 
150 	if (must_probe) {
151 		/*
152 		 * Check if the layers we depend on exist
153 		 */
154 		if (!(machineid & (ATARI_TT|ATARI_FALCON)))
155 			return (0);
156 #ifdef TT_VIDEO
157 		if ((machineid & ATARI_TT) && !grfabs_probe(&tt_probe_video))
158 			return (0);
159 #endif /* TT_VIDEO */
160 #ifdef FALCON_VIDEO
161 		if ((machineid & ATARI_FALCON)
162 		    && !grfabs_probe(&falcon_probe_video))
163 			return (0);
164 #endif /* FALCON_VIDEO */
165 
166 		viewprobe();
167 		must_probe = 0;
168 	}
169 
170 	if (atari_realconfig == 0) {
171 		/*
172 		 * Early console init. Only match unit 0.
173 		 */
174 		if (cfp->cf_unit != 0)
175 			return(0);
176 		if (viewopen(0, 0, 0, NULL))
177 			return(0);
178 		cfdata_grf = cfp;
179 		return (1);
180 	}
181 
182 	/*
183 	 * Normal config. When we are called directly from the grfbus,
184 	 * we only match unit 0. The attach function will call us for
185 	 * the other configured units.
186 	 */
187 	if (grf_auxp->from_bus_match && (cfp->cf_unit != 0))
188 		return (0);
189 
190 	if (!grf_auxp->from_bus_match && (grf_auxp->unit != cfp->cf_unit))
191 		return (0);
192 
193 	/*
194 	 * Final constraint: each grf needs a view....
195 	 */
196 	if((cfdata_grf == NULL) || (cfp->cf_unit != 0)) {
197 	    if(viewopen(cfp->cf_unit, 0, 0, NULL))
198 		return(0);
199 	}
200 	return(1);
201 }
202 
203 /*
204  * attach: initialize the grf-structure and try to attach an ite to us.
205  * note  : dp is NULL during early console init.
206  */
207 void
208 grfccattach(pdp, dp, auxp)
209 struct device	*pdp, *dp;
210 void		*auxp;
211 {
212 	static struct grf_softc		congrf;
213 	       grf_auxp_t		*grf_bus_auxp = auxp;
214 	       grf_auxp_t		grf_auxp;
215 	       struct grf_softc		*gp;
216 	       int			maj;
217 
218 	/*
219 	 * find our major device number
220 	 */
221 	for(maj = 0; maj < nchrdev; maj++)
222 		if (cdevsw[maj].d_open == grfopen)
223 			break;
224 
225 	/*
226 	 * Handle exeption case: early console init
227 	 */
228 	if(dp == NULL) {
229 		congrf.g_unit    = 0;
230 		congrf.g_itedev  = (dev_t)-1;
231 		congrf.g_grfdev  = makedev(maj, 0);
232 		congrf.g_flags   = GF_ALIVE;
233 		congrf.g_mode    = grf_mode;
234 		congrf.g_conpri  = grfcc_cnprobe();
235 		congrf.g_viewdev = congrf.g_unit;
236 		grfcc_iteinit(&congrf);
237 		grf_viewsync(&congrf);
238 
239 		/* Attach console ite */
240 		atari_config_found(cfdata_grf, NULL, &congrf, grfccprint);
241 		return;
242 	}
243 
244 	gp = (struct grf_softc *)dp;
245 	gp->g_unit = gp->g_device.dv_unit;
246 	grfsp[gp->g_unit] = gp;
247 
248 	if((cfdata_grf != NULL) && (gp->g_unit == 0)) {
249 		/*
250 		 * We inited earlier just copy the info, take care
251 		 * not to copy the device struct though.
252 		 */
253 		bcopy(&congrf.g_display, &gp->g_display,
254 			(char *)&gp[1] - (char *)&gp->g_display);
255 	}
256 	else {
257 		gp->g_grfdev  = makedev(maj, gp->g_unit);
258 		gp->g_itedev  = (dev_t)-1;
259 		gp->g_flags   = GF_ALIVE;
260 		gp->g_mode    = grf_mode;
261 		gp->g_conpri  = 0;
262 		gp->g_viewdev = gp->g_unit;
263 		grfcc_iteinit(gp);
264 		grf_viewsync(gp);
265 	}
266 
267 	printf(": width %d height %d", gp->g_display.gd_dwidth,
268 		    gp->g_display.gd_dheight);
269 	if(gp->g_display.gd_colors == 2)
270 		printf(" monochrome\n");
271 	else printf(" colors %d\n", gp->g_display.gd_colors);
272 
273 	/*
274 	 * try and attach an ite
275 	 */
276 	config_found(dp, gp, grfccprint);
277 
278 	/*
279 	 * If attaching unit 0, go ahead and 'find' the rest of us
280 	 */
281 	if (gp->g_unit == 0) {
282 		grf_auxp.from_bus_match = 0;
283 		for (grf_auxp.unit=1; grf_auxp.unit < NGRFCC; grf_auxp.unit++) {
284 		    config_found(pdp, (void*)&grf_auxp, grf_bus_auxp->busprint);
285 		}
286 	}
287 }
288 
289 int
290 grfccprint(auxp, pnp)
291 void		*auxp;
292 const char	*pnp;
293 {
294 	if(pnp)
295 		printf("ite at %s", pnp);
296 	return(UNCONF);
297 }
298 
299 /*
300  * called from grf_cc to return console priority
301  */
302 int
303 grfcc_cnprobe()
304 {
305 	return(CN_INTERNAL);
306 }
307 /*
308  * called from grf_cc to init ite portion of
309  * grf_softc struct
310  */
311 void
312 grfcc_iteinit(gp)
313 struct grf_softc *gp;
314 {
315 
316 	gp->g_itecursor = cursor32;
317 	gp->g_iteputc   = putc8;
318 	gp->g_iteclear  = clear8;
319 	gp->g_itescroll = scroll8;
320 	gp->g_iteinit   = view_init;
321 	gp->g_itedeinit = view_deinit;
322 }
323 
324 static void
325 view_deinit(ip)
326 struct ite_softc	*ip;
327 {
328 	ip->flags &= ~ITE_INITED;
329 }
330 
331 static void
332 view_init(ip)
333 register struct ite_softc *ip;
334 {
335 	struct itewinsize	wsz;
336 	ipriv_t			*cci;
337 
338 	if((cci = ip->priv) != NULL)
339 		return;
340 
341 	ip->itexx_ioctl = itecc_ioctl;
342 
343 #if defined(KFONT_8X8)
344 	ip->font = font_info_8x8;
345 #else
346 	ip->font = font_info_8x16;
347 #endif
348 
349 	/* Find the correct set of rendering routines for this font.  */
350 	if(ip->font.width != 8)
351 		panic("kernel font size not supported");
352 
353 	if(!atari_realconfig)
354 		ip->priv = cci = &con_ipriv;
355 	else ip->priv = cci = (ipriv_t*)malloc(sizeof(*cci), M_DEVBUF,M_WAITOK);
356 	if(cci == NULL)
357 		panic("No memory for ite-view");
358 	bzero(cci, sizeof(*cci));
359 
360 	cci->cursor_opt    = 0;
361 	cci->row_ptr       = NULL;
362 	cci->column_offset = NULL;
363 
364 	wsz.x      = ite_default_x;
365 	wsz.y      = ite_default_y;
366 	wsz.width  = ite_default_width;
367 	wsz.height = ite_default_height;
368 	wsz.depth  = ite_default_depth;
369 
370 	ite_newsize (ip, &wsz);
371 
372 	/*
373 	 * Only console will be turned on by default..
374 	 */
375 	if(ip->flags & ITE_ISCONS)
376 		ip->grf->g_mode(ip->grf, GM_GRFON, NULL, 0, 0);
377 }
378 
379 static int
380 ite_newsize(ip, winsz)
381 struct ite_softc	*ip;
382 struct itewinsize	*winsz;
383 {
384 	struct view_size	vs;
385 	ipriv_t			*cci = ip->priv;
386 	u_long			i, j;
387 	int			error = 0;
388 	view_t			*view;
389 
390 	vs.x      = winsz->x;
391 	vs.y      = winsz->y;
392 	vs.width  = winsz->width;
393 	vs.height = winsz->height;
394 	vs.depth  = winsz->depth;
395 
396 	error = viewioctl(ip->grf->g_viewdev, VIOCSSIZE, (caddr_t)&vs, 0,
397 								NOPROC);
398 	view  = viewview(ip->grf->g_viewdev);
399 
400 	/*
401 	 * Reinitialize our structs
402 	 */
403 	ip->cols = view->display.width  / ip->font.width;
404 	ip->rows = view->display.height / ip->font.height;
405 
406 	/*
407 	 * save new values so that future opens use them
408 	 * this may not be correct when we implement Virtual Consoles
409 	 */
410 	ite_default_height = view->display.height;
411 	ite_default_width  = view->display.width;
412 	ite_default_x      = view->display.x;
413 	ite_default_y      = view->display.y;
414 	ite_default_depth  = view->bitmap->depth;
415 
416 	if(cci->row_ptr && (cci->row_ptr != con_rows)) {
417 		free(cci->row_ptr, M_DEVBUF);
418 		cci->row_ptr = NULL;
419 	}
420 	if(cci->column_offset && (cci->column_offset != con_columns)) {
421 		free(cci->column_offset, M_DEVBUF);
422 		cci->column_offset = NULL;
423 	}
424 
425 	if(!atari_realconfig) {
426 		cci->row_ptr       = con_rows;
427 		cci->column_offset = con_columns;
428 	}
429 	else {
430 	  cci->row_ptr = malloc(sizeof(u_char *) * ip->rows,M_DEVBUF,M_NOWAIT);
431 	  cci->column_offset = malloc(sizeof(u_int)*ip->cols,M_DEVBUF,M_NOWAIT);
432 	}
433 
434 	if(!cci->row_ptr || !cci->column_offset)
435 		panic("No memory for ite-view");
436 
437 	cci->width      = view->bitmap->bytes_per_row << 3;
438 	cci->underline  = ip->font.baseline + 1;
439 	cci->row_offset = view->bitmap->bytes_per_row;
440 	cci->ft_x       = ip->font.width;
441 	cci->ft_y       = ip->font.height;
442 	cci->row_bytes  = cci->row_offset * cci->ft_y;
443 	cci->row_ptr[0] = view->bitmap->plane;
444 	for(i = 1; i < ip->rows; i++)
445 		cci->row_ptr[i] = cci->row_ptr[i-1] + cci->row_bytes;
446 
447 	/*
448 	 * Initialize the column offsets to point at the correct location
449 	 * in the first plane. This definitely assumes a font width of 8!
450 	 */
451 	j = view->bitmap->depth * 2;
452 	cci->column_offset[0] = 0;
453 	for(i = 1; i < ip->cols; i++)
454 		cci->column_offset[i] = ((i >> 1) * j) + (i & 1);
455 
456 	/* initialize the font cell pointers */
457 	cci->font_cell[ip->font.font_lo] = ip->font.font_p;
458 	for(i = ip->font.font_lo+1; i <= ip->font.font_hi; i++)
459 		cci->font_cell[i] = cci->font_cell[i-1] + ip->font.height;
460 
461 	return(error);
462 }
463 
464 int
465 itecc_ioctl(ip, cmd, addr, flag, p)
466 struct ite_softc	*ip;
467 u_long			cmd;
468 caddr_t			addr;
469 int			flag;
470 struct proc		*p;
471 {
472 	struct winsize		ws;
473 	struct itewinsize	*is;
474 	int			error = 0;
475 	view_t			*view = viewview(ip->grf->g_viewdev);
476 #if 0 /* LWP: notyet */
477 	struct itebell		*ib;
478 #endif
479 
480 	switch (cmd) {
481 	case ITEIOCSWINSZ:
482 		is = (struct itewinsize *)addr;
483 
484 		if(ite_newsize(ip, is))
485 			error = ENOMEM;
486 		else {
487 			view         = viewview(ip->grf->g_viewdev);
488 			ws.ws_row    = ip->rows;
489 			ws.ws_col    = ip->cols;
490 			ws.ws_xpixel = view->display.width;
491 			ws.ws_ypixel = view->display.height;
492 			ite_reset(ip);
493 			/*
494 			 * XXX tell tty about the change
495 			 * XXX this is messy, but works
496 			 */
497 			iteioctl(ip->grf->g_itedev,TIOCSWINSZ,(caddr_t)&ws,0,p);
498 		}
499 		break;
500 	case ITEIOCGBELL:
501 #if 0 /* LWP */
502 		/* XXX This won't work now			*/
503 		/* XXX Should the bell be device dependent?	*/
504 		ib         = (struct itebell *)addr;
505 		ib->volume = bvolume;
506 		ib->pitch  = bpitch;
507 		ib->msec   = bmsec;
508 #endif
509 		break;
510 	case ITEIOCSBELL:
511 #if 0 /* LWP */
512 		/* XXX See above				*/
513 		ib = (struct itebell *)addr;
514 		/* bounds check */
515 		if(ib->pitch > MAXBPITCH || ib->pitch < MINBPITCH ||
516 		    ib->volume > MAXBVOLUME || ib->msec > MAXBTIME)
517 			error = EINVAL;
518 		else {
519 			bvolume = ib->volume;
520 			bpitch  = ib->pitch;
521 			bmsec   = ib->msec;
522 		}
523 #endif
524 		break;
525 	case VIOCSCMAP:
526 	case VIOCGCMAP:
527 		/*
528 		 * XXX watchout for that NOPROC. its not really the kernel
529 		 * XXX talking these two commands don't use the proc pointer
530 		 * XXX though.
531 		 */
532 		error = viewioctl(ip->grf->g_viewdev, cmd, addr, flag, NOPROC);
533 		break;
534 	default:
535 		error = -1;
536 		break;
537 	}
538 	return (error);
539 }
540 
541 static void
542 cursor32(struct ite_softc *ip, int flag)
543 {
544 	int	cend;
545 	u_char	*pl;
546 	ipriv_t	*cci;
547 
548 	cci = ip->priv;
549 
550 	if(flag == END_CURSOROPT)
551 		cci->cursor_opt--;
552 	else if(flag == START_CURSOROPT) {
553 			if(!cci->cursor_opt)
554 				cursor32(ip, ERASE_CURSOR);
555 			cci->cursor_opt++;
556 			return;		  /* if we are already opted. */
557 	}
558 
559 	if(cci->cursor_opt)
560 		return;		  /* if we are still nested. */
561 				  /* else we draw the cursor. */
562 
563 	if(flag != DRAW_CURSOR && flag != END_CURSOROPT) {
564 		/*
565 		 * erase the cursor
566 		 */
567 		cend = ip->font.height-1;
568 		pl   = cci->column_offset[ip->cursorx]
569 				+ cci->row_ptr[ip->cursory];
570 		__asm__ __volatile__
571 			("1: notb  %0@ ; addaw %4,%0\n\t"
572 			 "dbra  %1,1b"
573 			 : "=a" (pl), "=d" (cend)
574 			 : "0" (pl), "1" (cend),
575 			 "d" (cci->row_offset)
576 			 );
577 	}
578 
579 	if(flag != DRAW_CURSOR && flag != MOVE_CURSOR && flag != END_CURSOROPT)
580 		return;
581 
582 	/*
583 	 * draw the cursor
584 	 */
585 	cend = min(ip->curx, ip->cols-1);
586 	if (flag == DRAW_CURSOR
587 		&& ip->cursorx == cend && ip->cursory == ip->cury)
588 		return;
589 	ip->cursorx = cend;
590 	ip->cursory = ip->cury;
591 	cend        = ip->font.height-1;
592 	pl          = cci->column_offset[ip->cursorx]
593 			+ cci->row_ptr[ip->cursory];
594 
595 	__asm__ __volatile__
596 		("1: notb  %0@ ; addaw %4,%0\n\t"
597 		 "dbra  %1,1b"
598 		 : "=a" (pl), "=d" (cend)
599 		 : "0" (pl), "1" (cend),
600 		 "d" (cci->row_offset)
601 		 );
602 }
603 
604 static void
605 putc8(struct ite_softc *ip, int c, int dy, int dx, int mode)
606 {
607     register ipriv_t	*cci = (ipriv_t *)ip->priv;
608     register u_char	*pl  = cci->column_offset[dx] + cci->row_ptr[dy];
609     register u_int	fh   = cci->ft_y;
610     register u_int	ro   = cci->row_offset;
611     register u_char	eor_mask;
612     register u_char	bl, tmp, ul;
613     register u_char	*ft;
614 
615     if(c < ip->font.font_lo || c > ip->font.font_hi)
616 		return;
617 
618 	ft = cci->font_cell[c];
619 
620 	if(!mode) {
621 		while(fh--) {
622 			*pl = *ft++; pl += ro;
623 		}
624 		return;
625 	}
626 
627 	eor_mask = (mode & ATTR_INV) ? 0xff : 0x00;
628 	bl       = (mode & ATTR_BOLD) ? 1 : 0;
629 	ul       = (mode & ATTR_UL) ? fh - cci->underline : fh;
630 	for(; fh--; pl += ro) {
631 		if(fh != ul) {
632 			tmp = *ft++;
633 			if(bl)
634 				tmp |= (tmp >> 1);
635 			*pl = tmp ^ eor_mask;
636 		}
637 		else {
638 			*pl = 0xff ^ eor_mask;
639 			ft++;
640 		}
641 	}
642 }
643 
644 static void
645 clear8(struct ite_softc *ip, int sy, int sx, int h, int w)
646 {
647 	ipriv_t	*cci = (ipriv_t *) ip->priv;
648 	view_t	*v   = viewview(ip->grf->g_viewdev);
649 	bmap_t	*bm  = v->bitmap;
650 
651 	if((sx == 0) && (w == ip->cols)) {
652 		/* common case: clearing whole lines */
653 		while (h--) {
654 			int		i;
655 			u_char	*ptr = cci->row_ptr[sy];
656 			for(i = 0; i < ip->font.height; i++) {
657 				bzero(ptr, bm->bytes_per_row);
658 				ptr += bm->bytes_per_row;
659 			}
660 			sy++;
661 		}
662 	}
663 	else {
664 		/*
665 		 * clearing only part of a line
666 		 * XXX could be optimized MUCH better, but is it worth the
667 		 * trouble?
668 		 */
669 
670 		int		i;
671 		u_char  *pls, *ple;
672 
673 		pls = cci->row_ptr[sy];
674 		ple = pls + cci->column_offset[sx + w-1];
675 		pls = pls + cci->column_offset[sx];
676 
677 		for(i = ((ip->font.height) * h)-1; i >= 0; i--) {
678 			u_char *p = pls;
679 			while(p <= ple)
680 				*p++ = 0;
681 			pls += bm->bytes_per_row;
682 			ple += bm->bytes_per_row;
683 		}
684 	}
685 }
686 
687 /* Note: sx is only relevant for SCROLL_LEFT or SCROLL_RIGHT.  */
688 static void
689 scroll8(ip, sy, sx, count, dir)
690 register struct ite_softc	*ip;
691 register int			sy;
692 int				dir, sx, count;
693 {
694 	bmap_t *bm = viewview(ip->grf->g_viewdev)->bitmap;
695 	u_char *pl = ((ipriv_t *)ip->priv)->row_ptr[sy];
696 
697 	if(dir == SCROLL_UP) {
698 		int	dy = sy - count;
699 
700 		cursor32(ip, ERASE_CURSOR);
701 		scrollbmap(bm, 0, dy*ip->font.height, bm->bytes_per_row >> 3,
702 				(ip->bottom_margin-dy+1)*ip->font.height,
703 				0, -(count*ip->font.height));
704 	}
705 	else if(dir == SCROLL_DOWN) {
706         	cursor32(ip, ERASE_CURSOR);
707 		scrollbmap(bm, 0, sy*ip->font.height, bm->bytes_per_row >> 3,
708 				(ip->bottom_margin-sy+1)*ip->font.height,
709 				0, count*ip->font.height);
710 	}
711 	else if(dir == SCROLL_RIGHT) {
712 		int	sofs = (ip->cols - count) * ip->font.width;
713 		int	dofs = (ip->cols) * ip->font.width;
714 		int	i, j;
715 
716 		cursor32(ip, ERASE_CURSOR);
717 		for(j = ip->font.height-1; j >= 0; j--) {
718 		    int sofs2 = sofs, dofs2 = dofs;
719 		    for (i = (ip->cols - (sx + count))-1; i >= 0; i--) {
720 			int	t;
721 			sofs2 -= ip->font.width;
722 			dofs2 -= ip->font.width;
723 			asm("bfextu %1@{%2:%3},%0" : "=d" (t)
724 				: "a" (pl), "d" (sofs2), "d" (ip->font.width));
725 			asm("bfins %3,%0@{%1:%2}" :
726 				: "a" (pl), "d" (dofs2), "d" (ip->font.width),
727 				  "d" (t));
728 		    }
729 			pl += bm->bytes_per_row;
730 		}
731 	}
732 	else { /* SCROLL_LEFT */
733 		int sofs = (sx) * ip->font.width;
734 		int dofs = (sx - count) * ip->font.width;
735 		int i, j;
736 
737 		cursor32(ip, ERASE_CURSOR);
738 		for(j = ip->font.height-1; j >= 0; j--) {
739 		    int sofs2 = sofs, dofs2 = dofs;
740 		    for(i = (ip->cols - sx)-1; i >= 0; i--) {
741 			int	t;
742 
743 			asm("bfextu %1@{%2:%3},%0" : "=d" (t)
744 				: "a" (pl), "d" (sofs2), "d" (ip->font.width));
745 			asm("bfins %3,%0@{%1:%2}"
746 				: : "a" (pl), "d" (dofs2),"d" (ip->font.width),
747 				    "d" (t));
748 			sofs2 += ip->font.width;
749 			dofs2 += ip->font.width;
750 		    }
751 		    pl += bm->bytes_per_row;
752 		}
753     }
754 }
755 
756 static void
757 scrollbmap (bmap_t *bm, u_short x, u_short y, u_short width, u_short height, short dx, short dy)
758 {
759     u_short lwpr  = bm->bytes_per_row >> 2;
760 
761     if(dx) {
762     	/* FIX: */ panic ("delta x not supported in scroll bitmap yet.");
763     }
764 
765     if(dy == 0) {
766         return;
767     }
768     if(dy > 0) {
769 		u_long *pl       = (u_long *)bm->plane;
770 		u_long *src_y    = pl + (lwpr*y);
771 		u_long *dest_y   = pl + (lwpr*(y+dy));
772 		u_long count     = lwpr*(height-dy);
773 		u_long *clr_y    = src_y;
774 		u_long clr_count = dest_y - src_y;
775 		u_long bc, cbc;
776 
777 		src_y  += count - 1;
778 		dest_y += count - 1;
779 
780 		bc = count >> 4;
781 		count &= 0xf;
782 
783 		while (bc--) {
784 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
785 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
786 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
787 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
788 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
789 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
790 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
791 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
792 		}
793 		while (count--)
794 		    *dest_y-- = *src_y--;
795 
796 		cbc = clr_count >> 4;
797 		clr_count &= 0xf;
798 
799 		while (cbc--) {
800 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
801 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
802 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
803 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
804 		}
805 		while(clr_count--)
806 		    *clr_y++ = 0;
807     }
808 	else {
809 		u_long	*pl       = (u_long *)bm->plane;
810 		u_long	*src_y    = pl + (lwpr*(y-dy));
811 		u_long	*dest_y   = pl + (lwpr*y);
812 		long	count     = lwpr*(height + dy);
813 		u_long	*clr_y    = dest_y + count;
814 		u_long	clr_count = src_y - dest_y;
815 		u_long	bc, cbc;
816 
817 		bc = count >> 4;
818 		count &= 0xf;
819 
820 		while(bc--) {
821 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
822 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
823 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
824 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
825 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
826 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
827 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
828 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
829 		}
830 		while(count--)
831 		    *dest_y++ = *src_y++;
832 
833 		cbc = clr_count >> 4;
834 		clr_count &= 0xf;
835 
836 		while (cbc--) {
837 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
838 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
839 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
840 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
841 		}
842 		while (clr_count--)
843 		    *clr_y++ = 0;
844     }
845 }
846