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