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