xref: /netbsd/sys/arch/atari/dev/ite_cc.c (revision c4a72b64)
1 /*	$NetBSD: ite_cc.c,v 1.20 2002/10/02 05:04:25 thorpej 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  * grfcc config stuff
104  */
105 void grfccattach __P((struct device *, struct device *, void *));
106 int  grfccmatch __P((struct device *, struct cfdata *, void *));
107 int  grfccprint __P((void *, const char *));
108 
109 CFATTACH_DECL(grfcc, sizeof(struct grf_softc),
110     grfccmatch, grfccattach, NULL, NULL);
111 
112 /*
113  * only used in console init.
114  */
115 static struct cfdata *cfdata_grf   = NULL;
116 
117 /*
118  * Probe functions we can use:
119  */
120 #ifdef TT_VIDEO
121 void	tt_probe_video __P((MODES *));
122 #endif /* TT_VIDEO */
123 #ifdef FALCON_VIDEO
124 void	falcon_probe_video __P((MODES *));
125 #endif /* FALCON_VIDEO */
126 
127 int
128 grfccmatch(pdp, cfp, auxp)
129 struct device	*pdp;
130 struct cfdata	*cfp;
131 void		*auxp;
132 {
133 	static int	did_consinit = 0;
134 	static int	must_probe = 1;
135 	grf_auxp_t	*grf_auxp = auxp;
136 	extern const struct cdevsw view_cdevsw;
137 
138 	if (must_probe) {
139 		/*
140 		 * Check if the layers we depend on exist
141 		 */
142 		if (!(machineid & (ATARI_TT|ATARI_FALCON)))
143 			return (0);
144 #ifdef TT_VIDEO
145 		if ((machineid & ATARI_TT) && !grfabs_probe(&tt_probe_video))
146 			return (0);
147 #endif /* TT_VIDEO */
148 #ifdef FALCON_VIDEO
149 		if ((machineid & ATARI_FALCON)
150 		    && !grfabs_probe(&falcon_probe_video))
151 			return (0);
152 #endif /* FALCON_VIDEO */
153 
154 		viewprobe();
155 		must_probe = 0;
156 	}
157 
158 	if (atari_realconfig == 0) {
159 		/*
160 		 * Early console init. Only match first unit.
161 		 */
162 		if (did_consinit)
163 			return(0);
164 		if ((*view_cdevsw.d_open)(cfp->cf_unit, 0, 0, NULL))
165 			return(0);
166 		cfdata_grf = cfp;
167 		did_consinit = 1;
168 		return (1);
169 	}
170 
171 	/*
172 	 * Normal config. When we are called directly from the grfbus,
173 	 * we only match the first unit. The attach function will call us for
174 	 * the other configured units.
175 	 */
176 	if (grf_auxp->from_bus_match && (did_consinit > 1))
177 		return (0);
178 
179 	if (!grf_auxp->from_bus_match && (grf_auxp->unit != cfp->cf_unit))
180 		return (0);
181 
182 	/*
183 	 * Final constraint: each grf needs a view....
184 	 */
185 	if((cfdata_grf == NULL) || (did_consinit > 1)) {
186 	    if((*view_cdevsw.d_open)(cfp->cf_unit, 0, 0, NULL))
187 		return(0);
188 	}
189 	did_consinit = 2;
190 	return(1);
191 }
192 
193 /*
194  * attach: initialize the grf-structure and try to attach an ite to us.
195  * note  : dp is NULL during early console init.
196  */
197 void
198 grfccattach(pdp, dp, auxp)
199 struct device	*pdp, *dp;
200 void		*auxp;
201 {
202 	static struct grf_softc		congrf;
203 	static int			first_attach = 1;
204 	       grf_auxp_t		*grf_bus_auxp = auxp;
205 	       grf_auxp_t		grf_auxp;
206 	       struct grf_softc		*gp;
207 	       int			maj;
208 	extern const struct cdevsw grf_cdevsw;
209 
210 	/*
211 	 * find our major device number
212 	 */
213 	maj = cdevsw_lookup_major(&grf_cdevsw);
214 
215 	/*
216 	 * Handle exeption case: early console init
217 	 */
218 	if(dp == NULL) {
219 		congrf.g_unit    = cfdata_grf->cf_unit;
220 		congrf.g_grfdev  = makedev(maj, congrf.g_unit);
221 		congrf.g_itedev  = (dev_t)-1;
222 		congrf.g_flags   = GF_ALIVE;
223 		congrf.g_mode    = grf_mode;
224 		congrf.g_conpri  = grfcc_cnprobe();
225 		congrf.g_viewdev = congrf.g_unit;
226 		grfcc_iteinit(&congrf);
227 		grf_viewsync(&congrf);
228 
229 		/* Attach console ite */
230 		atari_config_found(cfdata_grf, NULL, &congrf, grfccprint);
231 		return;
232 	}
233 
234 	gp = (struct grf_softc *)dp;
235 	gp->g_unit = gp->g_device.dv_unit;
236 	grfsp[gp->g_unit] = gp;
237 
238 	if((cfdata_grf != NULL) && (gp->g_unit == congrf.g_unit)) {
239 		/*
240 		 * We inited earlier just copy the info, take care
241 		 * not to copy the device struct though.
242 		 */
243 		bcopy(&congrf.g_display, &gp->g_display,
244 			(char *)&gp[1] - (char *)&gp->g_display);
245 	}
246 	else {
247 		gp->g_grfdev  = makedev(maj, gp->g_unit);
248 		gp->g_itedev  = (dev_t)-1;
249 		gp->g_flags   = GF_ALIVE;
250 		gp->g_mode    = grf_mode;
251 		gp->g_conpri  = 0;
252 		gp->g_viewdev = gp->g_unit;
253 		grfcc_iteinit(gp);
254 		grf_viewsync(gp);
255 	}
256 
257 	printf(": width %d height %d", gp->g_display.gd_dwidth,
258 		    gp->g_display.gd_dheight);
259 	if(gp->g_display.gd_colors == 2)
260 		printf(" monochrome\n");
261 	else printf(" colors %d\n", gp->g_display.gd_colors);
262 
263 	/*
264 	 * try and attach an ite
265 	 */
266 	config_found(dp, gp, grfccprint);
267 
268 	/*
269 	 * If attaching the first unit, go ahead and 'find' the rest of us
270 	 */
271 	if (first_attach) {
272 		first_attach = 0;
273 		grf_auxp.from_bus_match = 0;
274 		for (grf_auxp.unit=1; grf_auxp.unit < NGRFCC; grf_auxp.unit++) {
275 		    config_found(pdp, (void*)&grf_auxp, grf_bus_auxp->busprint);
276 		}
277 	}
278 }
279 
280 int
281 grfccprint(auxp, pnp)
282 void		*auxp;
283 const char	*pnp;
284 {
285 	if(pnp)
286 		printf("ite at %s", pnp);
287 	return(UNCONF);
288 }
289 
290 /*
291  * called from grf_cc to return console priority
292  */
293 int
294 grfcc_cnprobe()
295 {
296 	return(CN_INTERNAL);
297 }
298 /*
299  * called from grf_cc to init ite portion of
300  * grf_softc struct
301  */
302 void
303 grfcc_iteinit(gp)
304 struct grf_softc *gp;
305 {
306 
307 	gp->g_itecursor = cursor32;
308 	gp->g_iteputc   = putc8;
309 	gp->g_iteclear  = clear8;
310 	gp->g_itescroll = scroll8;
311 	gp->g_iteinit   = view_init;
312 	gp->g_itedeinit = view_deinit;
313 }
314 
315 static void
316 view_deinit(ip)
317 struct ite_softc	*ip;
318 {
319 	ip->flags &= ~ITE_INITED;
320 }
321 
322 static void
323 view_init(ip)
324 register struct ite_softc *ip;
325 {
326 	struct itewinsize	wsz;
327 	ipriv_t			*cci;
328 
329 	if((cci = ip->priv) != NULL)
330 		return;
331 
332 	ip->itexx_ioctl = itecc_ioctl;
333 
334 #if defined(KFONT_8X8)
335 	ip->font = font_info_8x8;
336 #else
337 	ip->font = font_info_8x16;
338 #endif
339 
340 	/* Find the correct set of rendering routines for this font.  */
341 	if(ip->font.width != 8)
342 		panic("kernel font size not supported");
343 
344 	if(!atari_realconfig)
345 		ip->priv = cci = &con_ipriv;
346 	else ip->priv = cci = (ipriv_t*)malloc(sizeof(*cci), M_DEVBUF,M_WAITOK);
347 	if(cci == NULL)
348 		panic("No memory for ite-view");
349 	bzero(cci, sizeof(*cci));
350 
351 	cci->cursor_opt    = 0;
352 	cci->row_ptr       = NULL;
353 	cci->column_offset = NULL;
354 
355 	wsz.x      = ite_default_x;
356 	wsz.y      = ite_default_y;
357 	wsz.width  = ite_default_width;
358 	wsz.height = ite_default_height;
359 	wsz.depth  = ite_default_depth;
360 
361 	ite_newsize (ip, &wsz);
362 
363 	/*
364 	 * Only console will be turned on by default..
365 	 */
366 	if(ip->flags & ITE_ISCONS)
367 		ip->grf->g_mode(ip->grf, GM_GRFON, NULL, 0, 0);
368 }
369 
370 static int
371 ite_newsize(ip, winsz)
372 struct ite_softc	*ip;
373 struct itewinsize	*winsz;
374 {
375 	struct view_size	vs;
376 	ipriv_t			*cci = ip->priv;
377 	u_long			i, j;
378 	int			error = 0;
379 	view_t			*view;
380 	extern const struct cdevsw view_cdevsw;
381 
382 	vs.x      = winsz->x;
383 	vs.y      = winsz->y;
384 	vs.width  = winsz->width;
385 	vs.height = winsz->height;
386 	vs.depth  = winsz->depth;
387 
388 	error = (*view_cdevsw.d_ioctl)(ip->grf->g_viewdev, VIOCSSIZE,
389 				       (caddr_t)&vs, 0, NOPROC);
390 	view  = viewview(ip->grf->g_viewdev);
391 
392 	/*
393 	 * Reinitialize our structs
394 	 */
395 	ip->cols = view->display.width  / ip->font.width;
396 	ip->rows = view->display.height / ip->font.height;
397 
398 	/*
399 	 * save new values so that future opens use them
400 	 * this may not be correct when we implement Virtual Consoles
401 	 */
402 	ite_default_height = view->display.height;
403 	ite_default_width  = view->display.width;
404 	ite_default_x      = view->display.x;
405 	ite_default_y      = view->display.y;
406 	ite_default_depth  = view->bitmap->depth;
407 
408 	if(cci->row_ptr && (cci->row_ptr != con_rows)) {
409 		free(cci->row_ptr, M_DEVBUF);
410 		cci->row_ptr = NULL;
411 	}
412 	if(cci->column_offset && (cci->column_offset != con_columns)) {
413 		free(cci->column_offset, M_DEVBUF);
414 		cci->column_offset = NULL;
415 	}
416 
417 	if(!atari_realconfig) {
418 		cci->row_ptr       = con_rows;
419 		cci->column_offset = con_columns;
420 	}
421 	else {
422 	  cci->row_ptr = malloc(sizeof(u_char *) * ip->rows,M_DEVBUF,M_NOWAIT);
423 	  cci->column_offset = malloc(sizeof(u_int)*ip->cols,M_DEVBUF,M_NOWAIT);
424 	}
425 
426 	if(!cci->row_ptr || !cci->column_offset)
427 		panic("No memory for ite-view");
428 
429 	cci->width      = view->bitmap->bytes_per_row << 3;
430 	cci->underline  = ip->font.baseline + 1;
431 	cci->row_offset = view->bitmap->bytes_per_row;
432 	cci->ft_x       = ip->font.width;
433 	cci->ft_y       = ip->font.height;
434 	cci->row_bytes  = cci->row_offset * cci->ft_y;
435 	cci->row_ptr[0] = view->bitmap->plane;
436 	for(i = 1; i < ip->rows; i++)
437 		cci->row_ptr[i] = cci->row_ptr[i-1] + cci->row_bytes;
438 
439 	/*
440 	 * Initialize the column offsets to point at the correct location
441 	 * in the first plane. This definitely assumes a font width of 8!
442 	 */
443 	j = view->bitmap->depth * 2;
444 	cci->column_offset[0] = 0;
445 	for(i = 1; i < ip->cols; i++)
446 		cci->column_offset[i] = ((i >> 1) * j) + (i & 1);
447 
448 	/* initialize the font cell pointers */
449 	cci->font_cell[ip->font.font_lo] = ip->font.font_p;
450 	for(i = ip->font.font_lo+1; i <= ip->font.font_hi; i++)
451 		cci->font_cell[i] = cci->font_cell[i-1] + ip->font.height;
452 
453 	return(error);
454 }
455 
456 int
457 itecc_ioctl(ip, cmd, addr, flag, p)
458 struct ite_softc	*ip;
459 u_long			cmd;
460 caddr_t			addr;
461 int			flag;
462 struct proc		*p;
463 {
464 	struct winsize		ws;
465 	struct itewinsize	*is;
466 	int			error = 0;
467 	view_t			*view = viewview(ip->grf->g_viewdev);
468 	extern const struct cdevsw ite_cdevsw;
469 	extern const struct cdevsw view_cdevsw;
470 
471 	switch (cmd) {
472 	case ITEIOCSWINSZ:
473 		is = (struct itewinsize *)addr;
474 
475 		if(ite_newsize(ip, is))
476 			error = ENOMEM;
477 		else {
478 			view         = viewview(ip->grf->g_viewdev);
479 			ws.ws_row    = ip->rows;
480 			ws.ws_col    = ip->cols;
481 			ws.ws_xpixel = view->display.width;
482 			ws.ws_ypixel = view->display.height;
483 			ite_reset(ip);
484 			/*
485 			 * XXX tell tty about the change
486 			 * XXX this is messy, but works
487 			 */
488 			(*ite_cdevsw.d_ioctl)(ip->grf->g_itedev, TIOCSWINSZ,
489 					      (caddr_t)&ws, 0, p);
490 		}
491 		break;
492 	case VIOCSCMAP:
493 	case VIOCGCMAP:
494 		/*
495 		 * XXX watchout for that NOPROC. its not really the kernel
496 		 * XXX talking these two commands don't use the proc pointer
497 		 * XXX though.
498 		 */
499 		error = (*view_cdevsw.d_ioctl)(ip->grf->g_viewdev, cmd, addr,
500 					       flag, NOPROC);
501 		break;
502 	default:
503 		error = EPASSTHROUGH;
504 		break;
505 	}
506 	return (error);
507 }
508 
509 static void
510 cursor32(struct ite_softc *ip, int flag)
511 {
512 	int	cend;
513 	u_char	*pl;
514 	ipriv_t	*cci;
515 
516 	cci = ip->priv;
517 
518 	if(flag == END_CURSOROPT)
519 		cci->cursor_opt--;
520 	else if(flag == START_CURSOROPT) {
521 			if(!cci->cursor_opt)
522 				cursor32(ip, ERASE_CURSOR);
523 			cci->cursor_opt++;
524 			return;		  /* if we are already opted. */
525 	}
526 
527 	if(cci->cursor_opt)
528 		return;		  /* if we are still nested. */
529 				  /* else we draw the cursor. */
530 
531 	if(flag != DRAW_CURSOR && flag != END_CURSOROPT) {
532 		/*
533 		 * erase the cursor
534 		 */
535 		cend = ip->font.height-1;
536 		pl   = cci->column_offset[ip->cursorx]
537 				+ cci->row_ptr[ip->cursory];
538 		__asm__ __volatile__
539 			("1: notb  %0@ ; addaw %4,%0\n\t"
540 			 "dbra  %1,1b"
541 			 : "=a" (pl), "=d" (cend)
542 			 : "0" (pl), "1" (cend),
543 			 "d" (cci->row_offset)
544 			 );
545 	}
546 
547 	if(flag != DRAW_CURSOR && flag != MOVE_CURSOR && flag != END_CURSOROPT)
548 		return;
549 
550 	/*
551 	 * draw the cursor
552 	 */
553 	cend = min(ip->curx, ip->cols-1);
554 	if (flag == DRAW_CURSOR
555 		&& ip->cursorx == cend && ip->cursory == ip->cury)
556 		return;
557 	ip->cursorx = cend;
558 	ip->cursory = ip->cury;
559 	cend        = ip->font.height-1;
560 	pl          = cci->column_offset[ip->cursorx]
561 			+ cci->row_ptr[ip->cursory];
562 
563 	__asm__ __volatile__
564 		("1: notb  %0@ ; addaw %4,%0\n\t"
565 		 "dbra  %1,1b"
566 		 : "=a" (pl), "=d" (cend)
567 		 : "0" (pl), "1" (cend),
568 		 "d" (cci->row_offset)
569 		 );
570 }
571 
572 static void
573 putc8(struct ite_softc *ip, int c, int dy, int dx, int mode)
574 {
575     register ipriv_t	*cci = (ipriv_t *)ip->priv;
576     register u_char	*pl  = cci->column_offset[dx] + cci->row_ptr[dy];
577     register u_int	fh   = cci->ft_y;
578     register u_int	ro   = cci->row_offset;
579     register u_char	eor_mask;
580     register u_char	bl, tmp, ul;
581     register u_char	*ft;
582 
583     if(c < ip->font.font_lo || c > ip->font.font_hi)
584 		return;
585 
586 	ft = cci->font_cell[c];
587 
588 	if(!mode) {
589 		while(fh--) {
590 			*pl = *ft++; pl += ro;
591 		}
592 		return;
593 	}
594 
595 	eor_mask = (mode & ATTR_INV) ? 0xff : 0x00;
596 	bl       = (mode & ATTR_BOLD) ? 1 : 0;
597 	ul       = (mode & ATTR_UL) ? fh - cci->underline : fh;
598 	for(; fh--; pl += ro) {
599 		if(fh != ul) {
600 			tmp = *ft++;
601 			if(bl)
602 				tmp |= (tmp >> 1);
603 			*pl = tmp ^ eor_mask;
604 		}
605 		else {
606 			*pl = 0xff ^ eor_mask;
607 			ft++;
608 		}
609 	}
610 }
611 
612 static void
613 clear8(struct ite_softc *ip, int sy, int sx, int h, int w)
614 {
615 	ipriv_t	*cci = (ipriv_t *) ip->priv;
616 	view_t	*v   = viewview(ip->grf->g_viewdev);
617 	bmap_t	*bm  = v->bitmap;
618 
619 	if((sx == 0) && (w == ip->cols)) {
620 		/* common case: clearing whole lines */
621 		while (h--) {
622 			int		i;
623 			u_char	*ptr = cci->row_ptr[sy];
624 			for(i = 0; i < ip->font.height; i++) {
625 				bzero(ptr, bm->bytes_per_row);
626 				ptr += bm->bytes_per_row;
627 			}
628 			sy++;
629 		}
630 	}
631 	else {
632 		/*
633 		 * clearing only part of a line
634 		 * XXX could be optimized MUCH better, but is it worth the
635 		 * trouble?
636 		 */
637 
638 		int		i;
639 		u_char  *pls, *ple;
640 
641 		pls = cci->row_ptr[sy];
642 		ple = pls + cci->column_offset[sx + w-1];
643 		pls = pls + cci->column_offset[sx];
644 
645 		for(i = ((ip->font.height) * h)-1; i >= 0; i--) {
646 			u_char *p = pls;
647 			while(p <= ple)
648 				*p++ = 0;
649 			pls += bm->bytes_per_row;
650 			ple += bm->bytes_per_row;
651 		}
652 	}
653 }
654 
655 /* Note: sx is only relevant for SCROLL_LEFT or SCROLL_RIGHT.  */
656 static void
657 scroll8(ip, sy, sx, count, dir)
658 register struct ite_softc	*ip;
659 register int			sy;
660 int				dir, sx, count;
661 {
662 	bmap_t *bm = viewview(ip->grf->g_viewdev)->bitmap;
663 	u_char *pl = ((ipriv_t *)ip->priv)->row_ptr[sy];
664 
665 	if(dir == SCROLL_UP) {
666 		int	dy = sy - count;
667 
668 		cursor32(ip, ERASE_CURSOR);
669 		scrollbmap(bm, 0, dy*ip->font.height, bm->bytes_per_row >> 3,
670 				(ip->bottom_margin-dy+1)*ip->font.height,
671 				0, -(count*ip->font.height));
672 	}
673 	else if(dir == SCROLL_DOWN) {
674         	cursor32(ip, ERASE_CURSOR);
675 		scrollbmap(bm, 0, sy*ip->font.height, bm->bytes_per_row >> 3,
676 				(ip->bottom_margin-sy+1)*ip->font.height,
677 				0, count*ip->font.height);
678 	}
679 	else if(dir == SCROLL_RIGHT) {
680 		int	sofs = (ip->cols - count) * ip->font.width;
681 		int	dofs = (ip->cols) * ip->font.width;
682 		int	i, j;
683 
684 		cursor32(ip, ERASE_CURSOR);
685 		for(j = ip->font.height-1; j >= 0; j--) {
686 		    int sofs2 = sofs, dofs2 = dofs;
687 		    for (i = (ip->cols - (sx + count))-1; i >= 0; i--) {
688 			int	t;
689 			sofs2 -= ip->font.width;
690 			dofs2 -= ip->font.width;
691 			asm("bfextu %1@{%2:%3},%0" : "=d" (t)
692 				: "a" (pl), "d" (sofs2), "d" (ip->font.width));
693 			asm("bfins %3,%0@{%1:%2}" :
694 				: "a" (pl), "d" (dofs2), "d" (ip->font.width),
695 				  "d" (t));
696 		    }
697 			pl += bm->bytes_per_row;
698 		}
699 	}
700 	else { /* SCROLL_LEFT */
701 		int sofs = (sx) * ip->font.width;
702 		int dofs = (sx - count) * ip->font.width;
703 		int i, j;
704 
705 		cursor32(ip, ERASE_CURSOR);
706 		for(j = ip->font.height-1; j >= 0; j--) {
707 		    int sofs2 = sofs, dofs2 = dofs;
708 		    for(i = (ip->cols - sx)-1; i >= 0; i--) {
709 			int	t;
710 
711 			asm("bfextu %1@{%2:%3},%0" : "=d" (t)
712 				: "a" (pl), "d" (sofs2), "d" (ip->font.width));
713 			asm("bfins %3,%0@{%1:%2}"
714 				: : "a" (pl), "d" (dofs2),"d" (ip->font.width),
715 				    "d" (t));
716 			sofs2 += ip->font.width;
717 			dofs2 += ip->font.width;
718 		    }
719 		    pl += bm->bytes_per_row;
720 		}
721     }
722 }
723 
724 static void
725 scrollbmap (bmap_t *bm, u_short x, u_short y, u_short width, u_short height, short dx, short dy)
726 {
727     u_short lwpr  = bm->bytes_per_row >> 2;
728 
729     if(dx) {
730     	/* FIX: */ panic ("delta x not supported in scroll bitmap yet.");
731     }
732 
733     if(dy == 0) {
734         return;
735     }
736     if(dy > 0) {
737 		u_long *pl       = (u_long *)bm->plane;
738 		u_long *src_y    = pl + (lwpr*y);
739 		u_long *dest_y   = pl + (lwpr*(y+dy));
740 		u_long count     = lwpr*(height-dy);
741 		u_long *clr_y    = src_y;
742 		u_long clr_count = dest_y - src_y;
743 		u_long bc, cbc;
744 
745 		src_y  += count - 1;
746 		dest_y += count - 1;
747 
748 		bc = count >> 4;
749 		count &= 0xf;
750 
751 		while (bc--) {
752 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
753 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
754 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
755 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
756 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
757 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
758 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
759 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
760 		}
761 		while (count--)
762 		    *dest_y-- = *src_y--;
763 
764 		cbc = clr_count >> 4;
765 		clr_count &= 0xf;
766 
767 		while (cbc--) {
768 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
769 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
770 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
771 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
772 		}
773 		while(clr_count--)
774 		    *clr_y++ = 0;
775     }
776 	else {
777 		u_long	*pl       = (u_long *)bm->plane;
778 		u_long	*src_y    = pl + (lwpr*(y-dy));
779 		u_long	*dest_y   = pl + (lwpr*y);
780 		long	count     = lwpr*(height + dy);
781 		u_long	*clr_y    = dest_y + count;
782 		u_long	clr_count = src_y - dest_y;
783 		u_long	bc, cbc;
784 
785 		bc = count >> 4;
786 		count &= 0xf;
787 
788 		while(bc--) {
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 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
793 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
794 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
795 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
796 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
797 		}
798 		while(count--)
799 		    *dest_y++ = *src_y++;
800 
801 		cbc = clr_count >> 4;
802 		clr_count &= 0xf;
803 
804 		while (cbc--) {
805 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
806 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
807 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
808 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
809 		}
810 		while (clr_count--)
811 		    *clr_y++ = 0;
812     }
813 }
814