Home | History | Annotate | Line # | Download | only in dev
ite_cc.c revision 1.40
      1 /*	$NetBSD: ite_cc.c,v 1.40 2019/06/29 16:41:19 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.40 2019/06/29 16:41:19 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 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 		return;
    235 	}
    236 
    237 	sc = device_private(self);
    238 	sc->g_device = self;
    239 	sc->g_unit = device_unit(self);
    240 	grfsp[sc->g_unit] = sc;
    241 
    242 	if ((cfdata_grf != NULL) && (sc->g_unit == congrf.g_unit)) {
    243 		/*
    244 		 * We inited earlier just copy the info, take care
    245 		 * not to copy the device struct though.
    246 		 */
    247 		memcpy(&sc->g_display, &congrf.g_display,
    248 			(char *)&sc[1] - (char *)&sc->g_display);
    249 	} else {
    250 		sc->g_grfdev  = makedev(maj, sc->g_unit);
    251 		sc->g_itedev  = (dev_t)-1;
    252 		sc->g_flags   = GF_ALIVE;
    253 		sc->g_mode    = grf_mode;
    254 		sc->g_conpri  = 0;
    255 		sc->g_viewdev = sc->g_unit;
    256 		grfcc_iteinit(sc);
    257 		grf_viewsync(sc);
    258 	}
    259 
    260 	printf(": width %d height %d", sc->g_display.gd_dwidth,
    261 		    sc->g_display.gd_dheight);
    262 	if (sc->g_display.gd_colors == 2)
    263 		printf(" monochrome\n");
    264 	else
    265 		printf(" colors %d\n", sc->g_display.gd_colors);
    266 
    267 	/*
    268 	 * try and attach an ite
    269 	 */
    270 	config_found(self, sc /* XXX */, grfccprint);
    271 
    272 	/*
    273 	 * If attaching the first unit, go ahead and 'find' the rest of us
    274 	 */
    275 	if (first_attach) {
    276 		first_attach = 0;
    277 		grf_auxp.from_bus_match = 0;
    278 		for (grf_auxp.unit=1; grf_auxp.unit < NGRFCC; grf_auxp.unit++) {
    279 			config_found(parent, &grf_auxp, grf_bus_auxp->busprint);
    280 		}
    281 	}
    282 }
    283 
    284 static int
    285 grfccprint(void *aux, const char *pnp)
    286 {
    287 
    288 	if (pnp)
    289 		aprint_normal("ite at %s", pnp);
    290 	return UNCONF;
    291 }
    292 
    293 /*
    294  * called from grf_cc to return console priority
    295  */
    296 int
    297 grfcc_cnprobe(void)
    298 {
    299 	return CN_INTERNAL;
    300 }
    301 /*
    302  * called from grf_cc to init ite portion of
    303  * grf_softc struct
    304  */
    305 void
    306 grfcc_iteinit(struct grf_softc *sc)
    307 {
    308 
    309 	sc->g_itecursor = cursor32;
    310 	sc->g_iteputc   = putc8;
    311 	sc->g_iteclear  = clear8;
    312 	sc->g_itescroll = scroll8;
    313 	sc->g_iteinit   = view_init;
    314 	sc->g_itedeinit = view_deinit;
    315 }
    316 
    317 static void
    318 view_deinit(struct ite_softc *ip)
    319 {
    320 	ip->flags &= ~ITE_INITED;
    321 }
    322 
    323 static void
    324 view_init(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
    347 		ip->priv = cci = malloc(sizeof(*cci), M_DEVBUF, M_WAITOK);
    348 	if (cci == NULL)
    349 		panic("No memory for ite-view");
    350 	memset(cci, 0, 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(struct ite_softc *ip, struct itewinsize *winsz)
    373 {
    374 	struct view_size	vs;
    375 	ipriv_t			*cci = ip->priv;
    376 	u_long			i, j;
    377 	int			error = 0;
    378 	view_t			*view;
    379 	extern const struct cdevsw view_cdevsw;
    380 
    381 	vs.x      = winsz->x;
    382 	vs.y      = winsz->y;
    383 	vs.width  = winsz->width;
    384 	vs.height = winsz->height;
    385 	vs.depth  = winsz->depth;
    386 
    387 	error = (*view_cdevsw.d_ioctl)(ip->grf->g_viewdev, VIOCSSIZE,
    388 				       (void *)&vs, 0, NOLWP);
    389 	view  = viewview(ip->grf->g_viewdev);
    390 
    391 	/*
    392 	 * Reinitialize our structs
    393 	 */
    394 	ip->cols = view->display.width  / ip->font.width;
    395 	ip->rows = view->display.height / ip->font.height;
    396 
    397 	/*
    398 	 * save new values so that future opens use them
    399 	 * this may not be correct when we implement Virtual Consoles
    400 	 */
    401 	ite_default_height = view->display.height;
    402 	ite_default_width  = view->display.width;
    403 	ite_default_x      = view->display.x;
    404 	ite_default_y      = view->display.y;
    405 	ite_default_depth  = view->bitmap->depth;
    406 
    407 	if (cci->row_ptr && (cci->row_ptr != con_rows)) {
    408 		free(cci->row_ptr, M_DEVBUF);
    409 		cci->row_ptr = NULL;
    410 	}
    411 	if (cci->column_offset && (cci->column_offset != con_columns)) {
    412 		free(cci->column_offset, M_DEVBUF);
    413 		cci->column_offset = NULL;
    414 	}
    415 
    416 	if (!atari_realconfig) {
    417 		cci->row_ptr       = con_rows;
    418 		cci->column_offset = con_columns;
    419 	} else {
    420 		cci->row_ptr = malloc(sizeof(u_char *) * ip->rows,
    421 		    M_DEVBUF, M_NOWAIT);
    422 		cci->column_offset = malloc(sizeof(u_int) * ip->cols,
    423 		    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(struct ite_softc *ip, u_long cmd, void * addr, int flag,
    458     struct lwp *l)
    459 {
    460 	struct winsize		ws;
    461 	struct itewinsize	*is;
    462 	int			error = 0;
    463 	view_t			*view = viewview(ip->grf->g_viewdev);
    464 	extern const struct cdevsw ite_cdevsw;
    465 	extern const struct cdevsw view_cdevsw;
    466 
    467 	switch (cmd) {
    468 	case ITEIOCSWINSZ:
    469 		is = (struct itewinsize *)addr;
    470 
    471 		if (ite_newsize(ip, is))
    472 			error = ENOMEM;
    473 		else {
    474 			view         = viewview(ip->grf->g_viewdev);
    475 			ws.ws_row    = ip->rows;
    476 			ws.ws_col    = ip->cols;
    477 			ws.ws_xpixel = view->display.width;
    478 			ws.ws_ypixel = view->display.height;
    479 			ite_reset(ip);
    480 			/*
    481 			 * XXX tell tty about the change
    482 			 * XXX this is messy, but works
    483 			 */
    484 			(*ite_cdevsw.d_ioctl)(ip->grf->g_itedev, TIOCSWINSZ,
    485 					      (void *)&ws, 0, l);
    486 		}
    487 		break;
    488 	case VIOCSCMAP:
    489 	case VIOCGCMAP:
    490 		/*
    491 		 * XXX watchout for that NOLWP. its not really the kernel
    492 		 * XXX talking these two commands don't use the proc pointer
    493 		 * XXX though.
    494 		 */
    495 		error = (*view_cdevsw.d_ioctl)(ip->grf->g_viewdev, cmd, addr,
    496 					       flag, NOLWP);
    497 		break;
    498 	default:
    499 		error = EPASSTHROUGH;
    500 		break;
    501 	}
    502 	return error;
    503 }
    504 
    505 static void
    506 cursor32(struct ite_softc *ip, int flag)
    507 {
    508 	int	cend;
    509 	u_char	*pl;
    510 	ipriv_t	*cci;
    511 
    512 	cci = ip->priv;
    513 
    514 	if (flag == END_CURSOROPT)
    515 		cci->cursor_opt--;
    516 	else if (flag == START_CURSOROPT) {
    517 		if (!cci->cursor_opt)
    518 			cursor32(ip, ERASE_CURSOR);
    519 		cci->cursor_opt++;
    520 		return;		  /* if we are already opted. */
    521 	}
    522 
    523 	if (cci->cursor_opt)
    524 		return;		  /* if we are still nested. */
    525 				  /* else we draw the cursor. */
    526 
    527 	if (flag != DRAW_CURSOR && flag != END_CURSOROPT) {
    528 		/*
    529 		 * erase the cursor
    530 		 */
    531 		cend = ip->font.height - 1;
    532 		pl   = cci->column_offset[ip->cursorx]
    533 				+ cci->row_ptr[ip->cursory];
    534 		__asm volatile
    535 			("1: notb  %0@ ; addaw %4,%0\n\t"
    536 			 "dbra  %1,1b"
    537 			 : "=a" (pl), "=d" (cend)
    538 			 : "0" (pl), "1" (cend),
    539 			 "d" (cci->row_offset)
    540 			 );
    541 	}
    542 
    543 	if (flag != DRAW_CURSOR && flag != MOVE_CURSOR && flag != END_CURSOROPT)
    544 		return;
    545 
    546 	/*
    547 	 * draw the cursor
    548 	 */
    549 	cend = uimin(ip->curx, ip->cols - 1);
    550 	if (flag == DRAW_CURSOR
    551 		&& ip->cursorx == cend && ip->cursory == ip->cury)
    552 		return;
    553 	ip->cursorx = cend;
    554 	ip->cursory = ip->cury;
    555 	cend        = ip->font.height - 1;
    556 	pl          = cci->column_offset[ip->cursorx]
    557 			+ cci->row_ptr[ip->cursory];
    558 
    559 	__asm volatile
    560 		("1: notb  %0@ ; addaw %4,%0\n\t"
    561 		 "dbra  %1,1b"
    562 		 : "=a" (pl), "=d" (cend)
    563 		 : "0" (pl), "1" (cend),
    564 		 "d" (cci->row_offset)
    565 		 );
    566 }
    567 
    568 static void
    569 putc8(struct ite_softc *ip, int c, int dy, int dx, int mode)
    570 {
    571 	ipriv_t	*cci = (ipriv_t *)ip->priv;
    572 	u_char	*pl  = cci->column_offset[dx] + cci->row_ptr[dy];
    573 	u_int	fh   = cci->ft_y;
    574 	u_int	ro   = cci->row_offset;
    575 	u_char	eor_mask;
    576 	u_char	bl, tmp, ul;
    577 	u_char	*ft;
    578 
    579 	if (c < ip->font.font_lo || c > ip->font.font_hi)
    580 		return;
    581 
    582 	ft = cci->font_cell[c];
    583 
    584 	if (!mode) {
    585 		while (fh--) {
    586 			*pl = *ft++; pl += ro;
    587 		}
    588 		return;
    589 	}
    590 
    591 	eor_mask = (mode & ATTR_INV) ? 0xff : 0x00;
    592 	bl       = (mode & ATTR_BOLD) ? 1 : 0;
    593 	ul       = (mode & ATTR_UL) ? fh - cci->underline : fh;
    594 	for (; fh--; pl += ro) {
    595 		if (fh != ul) {
    596 			tmp = *ft++;
    597 			if (bl)
    598 				tmp |= (tmp >> 1);
    599 			*pl = tmp ^ eor_mask;
    600 		} else {
    601 			*pl = 0xff ^ eor_mask;
    602 			ft++;
    603 		}
    604 	}
    605 }
    606 
    607 static void
    608 clear8(struct ite_softc *ip, int sy, int sx, int h, int w)
    609 {
    610 	ipriv_t	*cci = (ipriv_t *) ip->priv;
    611 	view_t	*v   = viewview(ip->grf->g_viewdev);
    612 	bmap_t	*bm  = v->bitmap;
    613 
    614 	if ((sx == 0) && (w == ip->cols)) {
    615 		/* common case: clearing whole lines */
    616 		while (h--) {
    617 			int i;
    618 			u_char *ptr = cci->row_ptr[sy];
    619 			for (i = 0; i < ip->font.height; i++) {
    620 				memset(ptr, 0, bm->bytes_per_row);
    621 				ptr += bm->bytes_per_row;
    622 			}
    623 			sy++;
    624 		}
    625 	} else {
    626 		/*
    627 		 * clearing only part of a line
    628 		 * XXX could be optimized MUCH better, but is it worth the
    629 		 * trouble?
    630 		 */
    631 
    632 		int i;
    633 		u_char *pls, *ple;
    634 
    635 		pls = cci->row_ptr[sy];
    636 		ple = pls + cci->column_offset[sx + w - 1];
    637 		pls = pls + cci->column_offset[sx];
    638 
    639 		for (i = ((ip->font.height) * h) - 1; i >= 0; i--) {
    640 			u_char *p = pls;
    641 			while (p <= ple)
    642 				*p++ = 0;
    643 			pls += bm->bytes_per_row;
    644 			ple += bm->bytes_per_row;
    645 		}
    646 	}
    647 }
    648 
    649 /* Note: sx is only relevant for SCROLL_LEFT or SCROLL_RIGHT.  */
    650 static void
    651 scroll8(register struct ite_softc *ip, register int sy, int sx, int count,
    652     int dir)
    653 {
    654 	bmap_t *bm = viewview(ip->grf->g_viewdev)->bitmap;
    655 	u_char *pl = ((ipriv_t *)ip->priv)->row_ptr[sy];
    656 
    657 	if (dir == SCROLL_UP) {
    658 		int dy = sy - count;
    659 
    660 		cursor32(ip, ERASE_CURSOR);
    661 		scrollbmap(bm, 0, dy * ip->font.height, bm->bytes_per_row >> 3,
    662 		    (ip->bottom_margin - dy + 1) * ip->font.height,
    663 		    0, -(count * ip->font.height));
    664 	} else if (dir == SCROLL_DOWN) {
    665         	cursor32(ip, ERASE_CURSOR);
    666 		scrollbmap(bm, 0, sy * ip->font.height, bm->bytes_per_row >> 3,
    667 		    (ip->bottom_margin - sy + 1) * ip->font.height,
    668 		    0, count * ip->font.height);
    669 	} else if (dir == SCROLL_RIGHT) {
    670 		int sofs = (ip->cols - count) * ip->font.width;
    671 		int dofs = (ip->cols) * ip->font.width;
    672 		int i, j;
    673 
    674 		cursor32(ip, ERASE_CURSOR);
    675 		for (j = ip->font.height - 1; j >= 0; j--) {
    676 			int sofs2 = sofs, dofs2 = dofs;
    677 			for (i = (ip->cols - (sx + count)) - 1; i >= 0; i--) {
    678 				int t;
    679 				sofs2 -= ip->font.width;
    680 				dofs2 -= ip->font.width;
    681 				__asm("bfextu %1@{%2:%3}, %0" : "=d" (t)
    682 				    : "a" (pl), "d" (sofs2),
    683 				    "d" (ip->font.width));
    684 				__asm("bfins %3, %0@{%1:%2}" :
    685 				    : "a" (pl), "d" (dofs2),
    686 				    "d" (ip->font.width), "d" (t));
    687 			}
    688 			pl += bm->bytes_per_row;
    689 		}
    690 	} else { /* SCROLL_LEFT */
    691 		int sofs = (sx) * ip->font.width;
    692 		int dofs = (sx - count) * ip->font.width;
    693 		int i, j;
    694 
    695 		cursor32(ip, ERASE_CURSOR);
    696 		for (j = ip->font.height - 1; j >= 0; j--) {
    697 			int sofs2 = sofs, dofs2 = dofs;
    698 			for (i = (ip->cols - sx) - 1; i >= 0; i--) {
    699 				int t;
    700 
    701 				__asm("bfextu %1@{%2:%3}, %0" : "=d" (t)
    702 				    : "a" (pl), "d" (sofs2),
    703 				    "d" (ip->font.width));
    704 				__asm("bfins %3, %0@{%1:%2}" :
    705 				    : "a" (pl), "d" (dofs2),
    706 				    "d" (ip->font.width), "d" (t));
    707 				sofs2 += ip->font.width;
    708 				dofs2 += ip->font.width;
    709 			}
    710 			pl += bm->bytes_per_row;
    711 		}
    712 	}
    713 }
    714 
    715 static void
    716 scrollbmap (bmap_t *bm, u_short x, u_short y, u_short width, u_short height,
    717     short dx, short dy)
    718 {
    719 	u_short lwpr  = bm->bytes_per_row >> 2;
    720 
    721 	if (dx) {
    722 		/* FIX: */
    723 		panic ("delta x not supported in scroll bitmap yet.");
    724 	}
    725 
    726 	if (dy == 0) {
    727 	        return;
    728     	}
    729 	if (dy > 0) {
    730 		u_long *pl       = (u_long *)bm->plane;
    731 		u_long *src_y    = pl + (lwpr * y);
    732 		u_long *dest_y   = pl + (lwpr * (y + dy));
    733 		u_long count     = lwpr * (height - dy);
    734 		u_long *clr_y    = src_y;
    735 		u_long clr_count = dest_y - src_y;
    736 		u_long bc, cbc;
    737 
    738 		src_y  += count - 1;
    739 		dest_y += count - 1;
    740 
    741 		bc = count >> 4;
    742 		count &= 0xf;
    743 
    744 		while (bc--) {
    745 			*dest_y-- = *src_y--; *dest_y-- = *src_y--;
    746 			*dest_y-- = *src_y--; *dest_y-- = *src_y--;
    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 		}
    754 		while (count--)
    755 			*dest_y-- = *src_y--;
    756 
    757 		cbc = clr_count >> 4;
    758 		clr_count &= 0xf;
    759 
    760 		while (cbc--) {
    761 			*clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
    762 			*clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
    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 		}
    766 		while (clr_count--)
    767 			*clr_y++ = 0;
    768 	} else {
    769 		u_long	*pl       = (u_long *)bm->plane;
    770 		u_long	*src_y    = pl + (lwpr * (y - dy));
    771 		u_long	*dest_y   = pl + (lwpr * y);
    772 		long	count     = lwpr * (height + dy);
    773 		u_long	*clr_y    = dest_y + count;
    774 		u_long	clr_count = src_y - dest_y;
    775 		u_long	bc, cbc;
    776 
    777 		bc = count >> 4;
    778 		count &= 0xf;
    779 
    780 		while (bc--) {
    781 			*dest_y++ = *src_y++; *dest_y++ = *src_y++;
    782 			*dest_y++ = *src_y++; *dest_y++ = *src_y++;
    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 		}
    790 		while (count--)
    791 			*dest_y++ = *src_y++;
    792 
    793 		cbc = clr_count >> 4;
    794 		clr_count &= 0xf;
    795 
    796 		while (cbc--) {
    797 			*clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
    798 			*clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
    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 		}
    802 		while (clr_count--)
    803 			*clr_y++ = 0;
    804 	}
    805 }
    806