xref: /netbsd-src/sys/arch/atari/dev/ite_cc.c (revision 53d1339bf7f9c7367b35a9e1ebe693f9b047a47b)
1 /*	$NetBSD: ite_cc.c,v 1.42 2021/04/27 14:48:29 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/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ite_cc.c,v 1.42 2021/04/27 14:48:29 thorpej 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 static void grfccattach(device_t, device_t, void *);
109 static int  grfccmatch(device_t, cfdata_t, void *);
110 static int  grfccprint(void *, const char *);
111 
112 CFATTACH_DECL_NEW(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 static int
131 grfccmatch(device_t parent, cfdata_t cf, void *aux)
132 {
133 	static int did_consinit = 0;
134 	static int must_probe = 1;
135 	grf_auxp_t *grf_auxp = aux;
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)(cf->cf_unit, 0, 0, NULL))
165 			return 0;
166 		cfdata_grf = cf;
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 != cf->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)(cf->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  : self is NULL during early console init.
196  */
197 static void
198 grfccattach(device_t parent, device_t self, void *aux)
199 {
200 	static struct grf_softc congrf;
201 	static int first_attach = 1;
202 	grf_auxp_t *grf_bus_auxp = aux;
203 	grf_auxp_t grf_auxp;
204 	struct grf_softc *sc;
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 (self == NULL) {
217 		struct device itedev;
218 
219 		memset(&itedev, 0, sizeof(itedev));
220 		itedev.dv_private = &congrf;
221 
222 		congrf.g_unit    = cfdata_grf->cf_unit;
223 		congrf.g_grfdev  = makedev(maj, congrf.g_unit);
224 		congrf.g_itedev  = (dev_t)-1;
225 		congrf.g_flags   = GF_ALIVE;
226 		congrf.g_mode    = grf_mode;
227 		congrf.g_conpri  = grfcc_cnprobe();
228 		congrf.g_viewdev = congrf.g_unit;
229 		grfcc_iteinit(&congrf);
230 		grf_viewsync(&congrf);
231 
232 		/* Attach console ite */
233 		atari_config_found(cfdata_grf, &itedev, &congrf, grfccprint,
234 		    CFARG_EOL);
235 		return;
236 	}
237 
238 	sc = device_private(self);
239 	sc->g_device = self;
240 	sc->g_unit = device_unit(self);
241 	grfsp[sc->g_unit] = sc;
242 
243 	if ((cfdata_grf != NULL) && (sc->g_unit == congrf.g_unit)) {
244 		/*
245 		 * We inited earlier just copy the info, take care
246 		 * not to copy the device struct though.
247 		 */
248 		memcpy(&sc->g_display, &congrf.g_display,
249 			(char *)&sc[1] - (char *)&sc->g_display);
250 	} else {
251 		sc->g_grfdev  = makedev(maj, sc->g_unit);
252 		sc->g_itedev  = (dev_t)-1;
253 		sc->g_flags   = GF_ALIVE;
254 		sc->g_mode    = grf_mode;
255 		sc->g_conpri  = 0;
256 		sc->g_viewdev = sc->g_unit;
257 		grfcc_iteinit(sc);
258 		grf_viewsync(sc);
259 	}
260 
261 	printf(": width %d height %d", sc->g_display.gd_dwidth,
262 		    sc->g_display.gd_dheight);
263 	if (sc->g_display.gd_colors == 2)
264 		printf(" monochrome\n");
265 	else
266 		printf(" colors %d\n", sc->g_display.gd_colors);
267 
268 	/*
269 	 * try and attach an ite
270 	 */
271 	config_found(self, sc /* XXX */, grfccprint, CFARG_EOL);
272 
273 	/*
274 	 * If attaching the first unit, go ahead and 'find' the rest of us
275 	 */
276 	if (first_attach) {
277 		first_attach = 0;
278 		grf_auxp.from_bus_match = 0;
279 		for (grf_auxp.unit=1; grf_auxp.unit < NGRFCC; grf_auxp.unit++) {
280 			config_found(parent, &grf_auxp, grf_bus_auxp->busprint,
281 			    CFARG_EOL);
282 		}
283 	}
284 }
285 
286 static int
287 grfccprint(void *aux, const char *pnp)
288 {
289 
290 	if (pnp)
291 		aprint_normal("ite at %s", pnp);
292 	return UNCONF;
293 }
294 
295 /*
296  * called from grf_cc to return console priority
297  */
298 int
299 grfcc_cnprobe(void)
300 {
301 	return CN_INTERNAL;
302 }
303 /*
304  * called from grf_cc to init ite portion of
305  * grf_softc struct
306  */
307 void
308 grfcc_iteinit(struct grf_softc *sc)
309 {
310 
311 	sc->g_itecursor = cursor32;
312 	sc->g_iteputc   = putc8;
313 	sc->g_iteclear  = clear8;
314 	sc->g_itescroll = scroll8;
315 	sc->g_iteinit   = view_init;
316 	sc->g_itedeinit = view_deinit;
317 }
318 
319 static void
320 view_deinit(struct ite_softc *ip)
321 {
322 	ip->flags &= ~ITE_INITED;
323 }
324 
325 static void
326 view_init(register struct ite_softc *ip)
327 {
328 	struct itewinsize	wsz;
329 	ipriv_t			*cci;
330 
331 	if ((cci = ip->priv) != NULL)
332 		return;
333 
334 	ip->itexx_ioctl = itecc_ioctl;
335 
336 #if defined(KFONT_8X8)
337 	ip->font = font_info_8x8;
338 #else
339 	ip->font = font_info_8x16;
340 #endif
341 
342 	/* Find the correct set of rendering routines for this font.  */
343 	if (ip->font.width != 8)
344 		panic("kernel font size not supported");
345 
346 	if (!atari_realconfig)
347 		ip->priv = cci = &con_ipriv;
348 	else
349 		ip->priv = cci = malloc(sizeof(*cci), M_DEVBUF, M_WAITOK);
350 	if (cci == NULL)
351 		panic("No memory for ite-view");
352 	memset(cci, 0, sizeof(*cci));
353 
354 	cci->cursor_opt    = 0;
355 	cci->row_ptr       = NULL;
356 	cci->column_offset = NULL;
357 
358 	wsz.x      = ite_default_x;
359 	wsz.y      = ite_default_y;
360 	wsz.width  = ite_default_width;
361 	wsz.height = ite_default_height;
362 	wsz.depth  = ite_default_depth;
363 
364 	ite_newsize (ip, &wsz);
365 
366 	/*
367 	 * Only console will be turned on by default..
368 	 */
369 	if (ip->flags & ITE_ISCONS)
370 		ip->grf->g_mode(ip->grf, GM_GRFON, NULL, 0, 0);
371 }
372 
373 static int
374 ite_newsize(struct ite_softc *ip, 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 				       (void *)&vs, 0, NOLWP);
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 	} else {
422 		cci->row_ptr = malloc(sizeof(u_char *) * ip->rows,
423 		    M_DEVBUF, M_NOWAIT);
424 		cci->column_offset = malloc(sizeof(u_int) * ip->cols,
425 		    M_DEVBUF, M_NOWAIT);
426 	}
427 
428 	if (!cci->row_ptr || !cci->column_offset)
429 		panic("No memory for ite-view");
430 
431 	cci->width      = view->bitmap->bytes_per_row << 3;
432 	cci->underline  = ip->font.baseline + 1;
433 	cci->row_offset = view->bitmap->bytes_per_row;
434 	cci->ft_x       = ip->font.width;
435 	cci->ft_y       = ip->font.height;
436 	cci->row_bytes  = cci->row_offset * cci->ft_y;
437 	cci->row_ptr[0] = view->bitmap->plane;
438 	for (i = 1; i < ip->rows; i++)
439 		cci->row_ptr[i] = cci->row_ptr[i - 1] + cci->row_bytes;
440 
441 	/*
442 	 * Initialize the column offsets to point at the correct location
443 	 * in the first plane. This definitely assumes a font width of 8!
444 	 */
445 	j = view->bitmap->depth * 2;
446 	cci->column_offset[0] = 0;
447 	for (i = 1; i < ip->cols; i++)
448 		cci->column_offset[i] = ((i >> 1) * j) + (i & 1);
449 
450 	/* initialize the font cell pointers */
451 	cci->font_cell[ip->font.font_lo] = ip->font.font_p;
452 	for (i = ip->font.font_lo + 1; i <= ip->font.font_hi; i++)
453 		cci->font_cell[i] = cci->font_cell[i - 1] + ip->font.height;
454 
455 	return error;
456 }
457 
458 int
459 itecc_ioctl(struct ite_softc *ip, u_long cmd, void * addr, int flag,
460     struct lwp *l)
461 {
462 	struct winsize		ws;
463 	struct itewinsize	*is;
464 	int			error = 0;
465 	view_t			*view = viewview(ip->grf->g_viewdev);
466 	extern const struct cdevsw ite_cdevsw;
467 	extern const struct cdevsw view_cdevsw;
468 
469 	switch (cmd) {
470 	case ITEIOCSWINSZ:
471 		is = (struct itewinsize *)addr;
472 
473 		if (ite_newsize(ip, is))
474 			error = ENOMEM;
475 		else {
476 			view         = viewview(ip->grf->g_viewdev);
477 			ws.ws_row    = ip->rows;
478 			ws.ws_col    = ip->cols;
479 			ws.ws_xpixel = view->display.width;
480 			ws.ws_ypixel = view->display.height;
481 			ite_reset(ip);
482 			/*
483 			 * XXX tell tty about the change
484 			 * XXX this is messy, but works
485 			 */
486 			(*ite_cdevsw.d_ioctl)(ip->grf->g_itedev, TIOCSWINSZ,
487 					      (void *)&ws, 0, l);
488 		}
489 		break;
490 	case VIOCSCMAP:
491 	case VIOCGCMAP:
492 		/*
493 		 * XXX watchout for that NOLWP. its not really the kernel
494 		 * XXX talking these two commands don't use the proc pointer
495 		 * XXX though.
496 		 */
497 		error = (*view_cdevsw.d_ioctl)(ip->grf->g_viewdev, cmd, addr,
498 					       flag, NOLWP);
499 		break;
500 	default:
501 		error = EPASSTHROUGH;
502 		break;
503 	}
504 	return error;
505 }
506 
507 static void
508 cursor32(struct ite_softc *ip, int flag)
509 {
510 	int	cend;
511 	u_char	*pl;
512 	ipriv_t	*cci;
513 
514 	cci = ip->priv;
515 
516 	if (flag == END_CURSOROPT)
517 		cci->cursor_opt--;
518 	else if (flag == START_CURSOROPT) {
519 		if (!cci->cursor_opt)
520 			cursor32(ip, ERASE_CURSOR);
521 		cci->cursor_opt++;
522 		return;		  /* if we are already opted. */
523 	}
524 
525 	if (cci->cursor_opt)
526 		return;		  /* if we are still nested. */
527 				  /* else we draw the cursor. */
528 
529 	if (flag != DRAW_CURSOR && flag != END_CURSOROPT) {
530 		/*
531 		 * erase the cursor
532 		 */
533 		cend = ip->font.height - 1;
534 		pl   = cci->column_offset[ip->cursorx]
535 				+ cci->row_ptr[ip->cursory];
536 		__asm volatile
537 			("1: notb  %0@ ; addaw %4,%0\n\t"
538 			 "dbra  %1,1b"
539 			 : "=a" (pl), "=d" (cend)
540 			 : "0" (pl), "1" (cend),
541 			 "d" (cci->row_offset)
542 			 );
543 	}
544 
545 	if (flag != DRAW_CURSOR && flag != MOVE_CURSOR && flag != END_CURSOROPT)
546 		return;
547 
548 	/*
549 	 * draw the cursor
550 	 */
551 	cend = uimin(ip->curx, ip->cols - 1);
552 	if (flag == DRAW_CURSOR
553 		&& ip->cursorx == cend && ip->cursory == ip->cury)
554 		return;
555 	ip->cursorx = cend;
556 	ip->cursory = ip->cury;
557 	cend        = ip->font.height - 1;
558 	pl          = cci->column_offset[ip->cursorx]
559 			+ cci->row_ptr[ip->cursory];
560 
561 	__asm volatile
562 		("1: notb  %0@ ; addaw %4,%0\n\t"
563 		 "dbra  %1,1b"
564 		 : "=a" (pl), "=d" (cend)
565 		 : "0" (pl), "1" (cend),
566 		 "d" (cci->row_offset)
567 		 );
568 }
569 
570 static void
571 putc8(struct ite_softc *ip, int c, int dy, int dx, int mode)
572 {
573 	ipriv_t	*cci = (ipriv_t *)ip->priv;
574 	u_char	*pl  = cci->column_offset[dx] + cci->row_ptr[dy];
575 	u_int	fh   = cci->ft_y;
576 	u_int	ro   = cci->row_offset;
577 	u_char	eor_mask;
578 	u_char	bl, tmp, ul;
579 	u_char	*ft;
580 
581 	if (c < ip->font.font_lo || c > ip->font.font_hi)
582 		return;
583 
584 	ft = cci->font_cell[c];
585 
586 	if (!mode) {
587 		while (fh--) {
588 			*pl = *ft++; pl += ro;
589 		}
590 		return;
591 	}
592 
593 	eor_mask = (mode & ATTR_INV) ? 0xff : 0x00;
594 	bl       = (mode & ATTR_BOLD) ? 1 : 0;
595 	ul       = (mode & ATTR_UL) ? fh - cci->underline : fh;
596 	for (; fh--; pl += ro) {
597 		if (fh != ul) {
598 			tmp = *ft++;
599 			if (bl)
600 				tmp |= (tmp >> 1);
601 			*pl = tmp ^ eor_mask;
602 		} else {
603 			*pl = 0xff ^ eor_mask;
604 			ft++;
605 		}
606 	}
607 }
608 
609 static void
610 clear8(struct ite_softc *ip, int sy, int sx, int h, int w)
611 {
612 	ipriv_t	*cci = (ipriv_t *) ip->priv;
613 	view_t	*v   = viewview(ip->grf->g_viewdev);
614 	bmap_t	*bm  = v->bitmap;
615 
616 	if ((sx == 0) && (w == ip->cols)) {
617 		/* common case: clearing whole lines */
618 		while (h--) {
619 			int i;
620 			u_char *ptr = cci->row_ptr[sy];
621 			for (i = 0; i < ip->font.height; i++) {
622 				memset(ptr, 0, bm->bytes_per_row);
623 				ptr += bm->bytes_per_row;
624 			}
625 			sy++;
626 		}
627 	} else {
628 		/*
629 		 * clearing only part of a line
630 		 * XXX could be optimized MUCH better, but is it worth the
631 		 * trouble?
632 		 */
633 
634 		int i;
635 		u_char *pls, *ple;
636 
637 		pls = cci->row_ptr[sy];
638 		ple = pls + cci->column_offset[sx + w - 1];
639 		pls = pls + cci->column_offset[sx];
640 
641 		for (i = ((ip->font.height) * h) - 1; i >= 0; i--) {
642 			u_char *p = pls;
643 			while (p <= ple)
644 				*p++ = 0;
645 			pls += bm->bytes_per_row;
646 			ple += bm->bytes_per_row;
647 		}
648 	}
649 }
650 
651 /* Note: sx is only relevant for SCROLL_LEFT or SCROLL_RIGHT.  */
652 static void
653 scroll8(register struct ite_softc *ip, register int sy, int sx, int count,
654     int dir)
655 {
656 	bmap_t *bm = viewview(ip->grf->g_viewdev)->bitmap;
657 	u_char *pl = ((ipriv_t *)ip->priv)->row_ptr[sy];
658 
659 	if (dir == SCROLL_UP) {
660 		int dy = sy - count;
661 
662 		cursor32(ip, ERASE_CURSOR);
663 		scrollbmap(bm, 0, dy * ip->font.height, bm->bytes_per_row >> 3,
664 		    (ip->bottom_margin - dy + 1) * ip->font.height,
665 		    0, -(count * ip->font.height));
666 	} else if (dir == SCROLL_DOWN) {
667         	cursor32(ip, ERASE_CURSOR);
668 		scrollbmap(bm, 0, sy * ip->font.height, bm->bytes_per_row >> 3,
669 		    (ip->bottom_margin - sy + 1) * ip->font.height,
670 		    0, count * ip->font.height);
671 	} else if (dir == SCROLL_RIGHT) {
672 		int sofs = (ip->cols - count) * ip->font.width;
673 		int dofs = (ip->cols) * ip->font.width;
674 		int i, j;
675 
676 		cursor32(ip, ERASE_CURSOR);
677 		for (j = ip->font.height - 1; j >= 0; j--) {
678 			int sofs2 = sofs, dofs2 = dofs;
679 			for (i = (ip->cols - (sx + count)) - 1; i >= 0; i--) {
680 				int t;
681 				sofs2 -= ip->font.width;
682 				dofs2 -= ip->font.width;
683 				__asm("bfextu %1@{%2:%3}, %0" : "=d" (t)
684 				    : "a" (pl), "d" (sofs2),
685 				    "d" (ip->font.width));
686 				__asm("bfins %3, %0@{%1:%2}" :
687 				    : "a" (pl), "d" (dofs2),
688 				    "d" (ip->font.width), "d" (t));
689 			}
690 			pl += bm->bytes_per_row;
691 		}
692 	} else { /* SCROLL_LEFT */
693 		int sofs = (sx) * ip->font.width;
694 		int dofs = (sx - count) * ip->font.width;
695 		int i, j;
696 
697 		cursor32(ip, ERASE_CURSOR);
698 		for (j = ip->font.height - 1; j >= 0; j--) {
699 			int sofs2 = sofs, dofs2 = dofs;
700 			for (i = (ip->cols - sx) - 1; i >= 0; i--) {
701 				int t;
702 
703 				__asm("bfextu %1@{%2:%3}, %0" : "=d" (t)
704 				    : "a" (pl), "d" (sofs2),
705 				    "d" (ip->font.width));
706 				__asm("bfins %3, %0@{%1:%2}" :
707 				    : "a" (pl), "d" (dofs2),
708 				    "d" (ip->font.width), "d" (t));
709 				sofs2 += ip->font.width;
710 				dofs2 += ip->font.width;
711 			}
712 			pl += bm->bytes_per_row;
713 		}
714 	}
715 }
716 
717 static void
718 scrollbmap (bmap_t *bm, u_short x, u_short y, u_short width, u_short height,
719     short dx, short dy)
720 {
721 	u_short lwpr  = bm->bytes_per_row >> 2;
722 
723 	if (dx) {
724 		/* FIX: */
725 		panic ("delta x not supported in scroll bitmap yet.");
726 	}
727 
728 	if (dy == 0) {
729 	        return;
730     	}
731 	if (dy > 0) {
732 		u_long *pl       = (u_long *)bm->plane;
733 		u_long *src_y    = pl + (lwpr * y);
734 		u_long *dest_y   = pl + (lwpr * (y + dy));
735 		u_long count     = lwpr * (height - dy);
736 		u_long *clr_y    = src_y;
737 		u_long clr_count = dest_y - src_y;
738 		u_long bc, cbc;
739 
740 		src_y  += count - 1;
741 		dest_y += count - 1;
742 
743 		bc = count >> 4;
744 		count &= 0xf;
745 
746 		while (bc--) {
747 			*dest_y-- = *src_y--; *dest_y-- = *src_y--;
748 			*dest_y-- = *src_y--; *dest_y-- = *src_y--;
749 			*dest_y-- = *src_y--; *dest_y-- = *src_y--;
750 			*dest_y-- = *src_y--; *dest_y-- = *src_y--;
751 			*dest_y-- = *src_y--; *dest_y-- = *src_y--;
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 		}
756 		while (count--)
757 			*dest_y-- = *src_y--;
758 
759 		cbc = clr_count >> 4;
760 		clr_count &= 0xf;
761 
762 		while (cbc--) {
763 			*clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
764 			*clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
765 			*clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
766 			*clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
767 		}
768 		while (clr_count--)
769 			*clr_y++ = 0;
770 	} else {
771 		u_long	*pl       = (u_long *)bm->plane;
772 		u_long	*src_y    = pl + (lwpr * (y - dy));
773 		u_long	*dest_y   = pl + (lwpr * y);
774 		long	count     = lwpr * (height + dy);
775 		u_long	*clr_y    = dest_y + count;
776 		u_long	clr_count = src_y - dest_y;
777 		u_long	bc, cbc;
778 
779 		bc = count >> 4;
780 		count &= 0xf;
781 
782 		while (bc--) {
783 			*dest_y++ = *src_y++; *dest_y++ = *src_y++;
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 		}
792 		while (count--)
793 			*dest_y++ = *src_y++;
794 
795 		cbc = clr_count >> 4;
796 		clr_count &= 0xf;
797 
798 		while (cbc--) {
799 			*clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
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 		}
804 		while (clr_count--)
805 			*clr_y++ = 0;
806 	}
807 }
808