xref: /netbsd-src/sys/arch/amiga/dev/ite_cc.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*	$NetBSD: ite_cc.c,v 1.21 1994/12/01 17:25:21 chopps Exp $	*/
2 
3 #include "grfcc.h"
4 #if NGRFCC > 0
5 
6 #include <sys/param.h>
7 #include <sys/conf.h>
8 #include <sys/proc.h>
9 #include <sys/device.h>
10 #include <sys/ioctl.h>
11 #include <sys/tty.h>
12 #include <sys/systm.h>
13 #include <sys/queue.h>
14 #include <sys/termios.h>
15 #include <dev/cons.h>
16 #include <machine/cpu.h>
17 #include <amiga/dev/itevar.h>
18 #include <amiga/dev/iteioctl.h>
19 #include <amiga/amiga/cc.h>
20 #include <amiga/amiga/device.h>
21 #include <amiga/dev/grfabs_reg.h>
22 #include <amiga/dev/grfioctl.h>
23 #include <amiga/dev/grfvar.h>
24 #include <amiga/dev/grf_ccreg.h>
25 #include <amiga/dev/viewioctl.h>
26 #include <amiga/dev/viewvar.h>
27 
28 #ifndef KFONT_CUSTOM
29 #ifdef KFONT_8X11
30 #define kernel_font_width       kernel_font_width_8x11
31 #define kernel_font_height      kernel_font_height_8x11
32 #define kernel_font_baseline    kernel_font_baseline_8x11
33 #define kernel_font_boldsmear   kernel_font_boldsmear_8x11
34 #define kernel_font_lo  kernel_font_lo_8x11
35 #define kernel_font_hi  kernel_font_hi_8x11
36 #define kernel_font     kernel_font_8x11
37 #define kernel_cursor   kernel_cursor_8x11
38 #else
39 #define kernel_font_width       kernel_font_width_8x8
40 #define kernel_font_height      kernel_font_height_8x8
41 #define kernel_font_baseline    kernel_font_baseline_8x8
42 #define kernel_font_boldsmear   kernel_font_boldsmear_8x8
43 #define kernel_font_lo  kernel_font_lo_8x8
44 #define kernel_font_hi  kernel_font_hi_8x8
45 #define kernel_font     kernel_font_8x8
46 #define kernel_cursor   kernel_cursor_8x8
47 #endif
48 #endif
49 
50 extern u_char kernel_font_width, kernel_font_height, kernel_font_baseline;
51 extern short  kernel_font_boldsmear;
52 extern u_char kernel_font_lo, kernel_font_hi;
53 extern u_char kernel_font[], kernel_cursor[];
54 
55 /*
56  * This is what ip->priv points to;
57  * it contains local variables for custom-chip ites.
58  */
59 struct ite_priv {
60 	view_t *view;		/* the view for this ite. */
61 	u_char **row_ptr;	/* array of pointers into the bitmap  */
62 	u_long row_bytes;
63 	u_long cursor_opt;
64 	u_int  *column_offset;	/* array of offsets for columns */
65 	u_int  row_offset;	/* the row offset */
66 	u_short width;		/* the bitmap width */
67 	u_short underline;	/* where the underline goes */
68 	u_short ft_x;		/* the font width */
69 	u_short ft_y;		/* the font height */
70 	u_char *font_cell[256];	/* the font pointer */
71 };
72 typedef struct ite_priv ipriv_t;
73 
74 void view_deinit __P((struct ite_softc *));
75 void view_init __P((struct ite_softc *));
76 
77 static void putc8 __P((struct ite_softc *, int, int, int, int));
78 static void clear8 __P((struct ite_softc *, int, int, int, int));
79 static void scroll8 __P((struct ite_softc *, int, int, int, int));
80 static void cursor32 __P((struct ite_softc *, int));
81 static void scrollbmap __P((bmap_t *, u_short, u_short, u_short, u_short,
82     short, short, u_char));
83 
84 /* patchable */
85 int ite_default_x = 0;		/* def leftedge offset */
86 int ite_default_y = 0;		/* def topedge offset */
87 int ite_default_width = 640;	/* def width */
88 int ite_default_depth = 2;	/* def depth */
89 #if defined (GRF_NTSC)
90 int ite_default_height = 400;	/* def NTSC height */
91 #elif defined (GRF_PAL)
92 int ite_default_height = 512;	/* def PAL height */
93 #else
94 int ite_default_height = 400;	/* def NON-PAL/NTSC height (?) */
95 #endif
96 
97 /* audio bell stuff */
98 u_int bvolume = 10;
99 u_int bpitch = 660;
100 u_int bmsec = 75;
101 
102 static char *bsamplep;
103 static char sample[20] = {
104 	0,39,75,103,121,127,121,103,75,39,0,
105 	-39,-75,-103,-121,-127,-121,-103,-75,-39
106 };
107 
108 /*
109  * called from grf_cc to return console priority
110  */
111 int
112 grfcc_cnprobe()
113 {
114 	return(CN_INTERNAL);
115 }
116 
117 /*
118  * called from grf_cc to init ite portion of
119  * grf_softc struct
120  */
121 void
122 grfcc_iteinit(gp)
123 	struct grf_softc *gp;
124 {
125 	gp->g_itecursor = cursor32;
126 	gp->g_iteputc = putc8;
127 	gp->g_iteclear = clear8;
128 	gp->g_itescroll = scroll8;
129 	gp->g_iteinit = view_init;
130 	gp->g_itedeinit = view_deinit;
131 }
132 
133 void
134 init_bell()
135 {
136 	short i;
137 
138 	if (bsamplep != NULL)
139 		return;
140 	bsamplep = alloc_chipmem(20);
141 	if (bsamplep == NULL)
142 		panic("no chipmem for ccbell");
143 
144 	bcopy(sample, bsamplep, 20);
145 }
146 
147 void
148 cc_bell()
149 {
150 	u_int clock;
151 	u_int period;
152 	u_int count;
153 
154 	clock = 3579545; 	/* PAL 3546895 */
155 
156 	/*
157 	 * the number of clock ticks per sample byte must be > 124
158 	 * ergo bpitch must be < clock / 124*20
159 	 * i.e. ~1443, 1300 to be safe (PAL etc.). also not zero obviously
160 	 */
161 	period = clock / (bpitch * 20);
162 	count = bmsec * bpitch / 1000;
163 
164 	play_sample(10, PREP_DMA_MEM(bsamplep), period, bvolume, 0x3, count);
165 }
166 
167 
168 int
169 ite_newsize(ip, winsz)
170 	struct ite_softc *ip;
171 	struct itewinsize *winsz;
172 {
173 	extern struct view_softc views[];
174 	struct view_size vs;
175 	ipriv_t *cci = ip->priv;
176 	u_long fbp, i;
177 	int error;
178 
179 	vs.x = winsz->x;
180 	vs.y = winsz->y;
181 	vs.width = winsz->width;
182 	vs.height = winsz->height;
183 	vs.depth = winsz->depth;
184 	error = viewioctl(0, VIOCSSIZE, &vs, 0, -1);
185 
186 	/*
187 	 * Reinitialize our structs
188 	 */
189 	cci->view = views[0].view;
190 
191 	/* -1 for bold. */
192 	ip->cols = (cci->view->display.width - 1) / ip->ftwidth;
193 	ip->rows = cci->view->display.height / ip->ftheight;
194 
195 	/*
196 	 * save new values so that future opens use them
197 	 * this may not be correct when we implement Virtual Consoles
198 	 */
199 	ite_default_height = cci->view->display.height;
200 	ite_default_width = cci->view->display.width;
201 	ite_default_x = cci->view->display.x;
202 	ite_default_y = cci->view->display.y;
203 	ite_default_depth = cci->view->bitmap->depth;
204 
205 	if (cci->row_ptr)
206 		free_chipmem(cci->row_ptr);
207 	if (cci->column_offset)
208 		free_chipmem(cci->column_offset);
209 
210 	cci->row_ptr = alloc_chipmem(sizeof(u_char *) * ip->rows);
211 	cci->column_offset = alloc_chipmem(sizeof(u_int) * ip->cols);
212 
213 	if (cci->row_ptr == NULL || cci->column_offset == NULL)
214 		panic("no chipmem for itecc data");
215 
216 
217 	cci->width = cci->view->bitmap->bytes_per_row << 3;
218 	cci->underline = ip->ftbaseline + 1;
219 	cci->row_offset = cci->view->bitmap->bytes_per_row
220 	    + cci->view->bitmap->row_mod;
221 	cci->ft_x = ip->ftwidth;
222 	cci->ft_y = ip->ftheight;
223 
224 	cci->row_bytes = cci->row_offset * ip->ftheight;
225 
226 	cci->row_ptr[0] = VDISPLAY_LINE (cci->view, 0, 0);
227 	for (i = 1; i < ip->rows; i++)
228 		cci->row_ptr[i] = cci->row_ptr[i-1] + cci->row_bytes;
229 
230 	/* initialize the column offsets */
231 	cci->column_offset[0] = 0;
232 	for (i = 1; i < ip->cols; i++)
233 		cci->column_offset[i] = cci->column_offset[i - 1] + cci->ft_x;
234 
235 	/* initialize the font cell pointers */
236 	cci->font_cell[ip->font_lo] = ip->font;
237 	for (i=ip->font_lo+1; i<=ip->font_hi; i++)
238 		cci->font_cell[i] = cci->font_cell[i-1] + ip->ftheight;
239 
240 	return (error);
241 }
242 
243 void
244 view_init(ip)
245 	register struct ite_softc *ip;
246 {
247 	struct itewinsize wsz;
248 	ipriv_t *cci;
249 
250 	cci = ip->priv;
251 
252 	if (cci)
253 		return;
254 
255 	init_bell();
256 
257 	ip->font     = kernel_font;
258 	ip->font_lo  = kernel_font_lo;
259 	ip->font_hi  = kernel_font_hi;
260 	ip->ftwidth  = kernel_font_width;
261 	ip->ftheight = kernel_font_height;
262 	ip->ftbaseline = kernel_font_baseline;
263 	ip->ftboldsmear = kernel_font_boldsmear;
264 
265 	/* Find the correct set of rendering routines for this font.  */
266 	if (ip->ftwidth > 8)
267 		panic("kernel font size not supported");
268 	cci = alloc_chipmem(sizeof (*cci));
269 	if (cci == NULL)
270 		panic("no memory for console device.");
271 
272 	ip->priv = cci;
273 	cci->cursor_opt = 0;
274 	cci->view = NULL;
275 	cci->row_ptr = NULL;
276 	cci->column_offset = NULL;
277 
278 	wsz.x = ite_default_x;
279 	wsz.y = ite_default_y;
280 	wsz.width = ite_default_width;
281 	wsz.height = ite_default_height;
282 	wsz.depth = ite_default_depth;
283 
284 	ite_newsize (ip, &wsz);
285 	cc_mode(ip->grf, GM_GRFON, NULL, 0, 0);
286 }
287 
288 int
289 ite_grf_ioctl (ip, cmd, addr, flag, p)
290 	struct ite_softc *ip;
291 	u_long cmd;
292 	caddr_t addr;
293 	int flag;
294 	struct proc *p;
295 {
296 	struct winsize ws;
297 	struct itewinsize *is;
298 	struct itebell *ib;
299 	ipriv_t *cci;
300 	int error;
301 
302 	cci = ip->priv;
303 	error = 0;
304 
305 	switch (cmd) {
306 	case ITEIOCGWINSZ:
307 		is = (struct itewinsize *)addr;
308 		is->x = cci->view->display.x;
309 		is->y = cci->view->display.y;
310 		is->width = cci->view->display.width;
311 		is->height = cci->view->display.height;
312 		is->depth = cci->view->bitmap->depth;
313 		break;
314 	case ITEIOCSWINSZ:
315 		is = (struct itewinsize *)addr;
316 
317 		if (ite_newsize(ip, is))
318 			error = ENOMEM;
319 		else {
320 			ws.ws_row = ip->rows;
321 			ws.ws_col = ip->cols;
322 			ws.ws_xpixel = cci->view->display.width;
323 			ws.ws_ypixel = cci->view->display.height;
324 			ite_reset (ip);
325 			/*
326 			 * XXX tell tty about the change
327 			 * XXX this is messy, but works
328 			 */
329 			iteioctl(0, TIOCSWINSZ, (caddr_t)&ws, 0, p);
330 		}
331 		break;
332 	case ITEIOCDSPWIN:
333 		cc_mode(ip->grf, GM_GRFON, NULL, 0, 0);
334 		break;
335 	case ITEIOCREMWIN:
336 		cc_mode(ip->grf, GM_GRFOFF, NULL, 0, 0);
337 		break;
338 	case ITEIOCGBELL:
339 		ib = (struct itebell *)addr;
340 		ib->volume = bvolume;
341 		ib->pitch = bpitch;
342 		ib->msec = bmsec;
343 		break;
344 	case ITEIOCSBELL:
345 		ib = (struct itebell *)addr;
346 		/* bounds check */
347 		if (ib->pitch > MAXBPITCH || ib->pitch < MINBPITCH ||
348 		    ib->volume > MAXBVOLUME || ib->msec > MAXBTIME)
349 			error = EINVAL;
350 		else {
351 			bvolume = ib->volume;
352 			bpitch = ib->pitch;
353 			bmsec = ib->msec;
354 		}
355 		break;
356 	case VIOCSCMAP:
357 	case VIOCGCMAP:
358 		/*
359 		 * XXX needs to be fixed when multiple console implemented
360 		 * XXX watchout for that -1 its not really the kernel talking
361 		 * XXX these two commands don't use the proc pointer though
362 		 */
363 		error = viewioctl(0, cmd, addr, flag, -1);
364 		break;
365 	default:
366 		error = -1;
367 		break;
368 	}
369 	return (error);
370 }
371 
372 void
373 view_deinit(ip)
374 	struct ite_softc *ip;
375 {
376 	ip->flags &= ~ITE_INITED;
377 }
378 
379 /*** (M<8)-by-N routines ***/
380 
381 static void
382 cursor32(struct ite_softc *ip, int flag)
383 {
384 	int cend, ofs, h, cstart, dr_plane;
385 	u_char *pl, opclr, opset;
386 	ipriv_t *cci;
387 	bmap_t *bm;
388 	view_t *v;
389 
390 	cci = ip->priv;
391 	v = cci->view;
392    	bm = v->bitmap;
393 	dr_plane = (bm->depth > 1 ? bm->depth-1 : 0);
394 
395 	if (flag == END_CURSOROPT)
396 		cci->cursor_opt--;
397 	else if (flag == START_CURSOROPT) {
398 		if (!cci->cursor_opt)
399 			cursor32 (ip, ERASE_CURSOR);
400 		cci->cursor_opt++;
401 		return;		  /* if we are already opted. */
402 	}
403 
404 	if (cci->cursor_opt)
405 		return;		  /* if we are still nested. */
406 				  /* else we draw the cursor. */
407 	cstart = 0;
408 	cend = ip->ftheight-1;
409 	pl = VDISPLAY_LINE(v, dr_plane, (ip->cursory * ip->ftheight + cstart));
410 	ofs = (ip->cursorx * ip->ftwidth);
411 
412 	if (flag != DRAW_CURSOR && flag != END_CURSOROPT) {
413 		/*
414 		 * erase the cursor
415 		 */
416 		int h;
417 
418 		if (dr_plane) {
419 			for (h = cend; h >= 0; h--) {
420 				asm("bfclr %0@{%1:%2}" : : "a" (pl),
421 				    "d" (ofs), "d" (ip->ftwidth));
422 				pl += cci->row_offset;
423 			}
424 		} else {
425 			for (h = cend; h >= 0; h--) {
426 				asm("bfchg %0@{%1:%2}" : : "a" (pl),
427 				    "d" (ofs), "d" (ip->ftwidth));
428 				pl += cci->row_offset;
429 			}
430 		}
431 	}
432 
433 	if (flag != DRAW_CURSOR && flag != MOVE_CURSOR &&
434 	    flag != END_CURSOROPT)
435 		return;
436 
437 	/*
438 	 * draw the cursor
439 	 */
440 
441 	ip->cursorx = min(ip->curx, ip->cols-1);
442 	ip->cursory = ip->cury;
443 	cstart = 0;
444 	cend = ip->ftheight-1;
445 	pl = VDISPLAY_LINE(v, dr_plane, ip->cursory * ip->ftheight + cstart);
446 	ofs = ip->cursorx * ip->ftwidth;
447 
448 	if (dr_plane) {
449 		for (h = cend; h >= 0; h--) {
450 			asm("bfset %0@{%1:%2}" : : "a" (pl),
451 			    "d" (ofs), "d" (ip->ftwidth));
452 			pl += cci->row_offset;
453 		}
454 	} else {
455 		for (h = cend; h >= 0; h--) {
456 			asm("bfchg %0@{%1:%2}" : : "a" (pl),
457 			    "d" (ofs), "d" (ip->ftwidth));
458 			pl += cci->row_offset;
459 		}
460 	}
461 }
462 
463 
464 static inline
465 int expbits (int data)
466 {
467 	int i, nd = 0;
468 
469 	if (data & 1)
470 		nd |= 0x02;
471 	for (i=1; i < 32; i++) {
472 		if (data & (1 << i))
473 			nd |= 0x5 << (i-1);
474 	}
475 	nd &= ~data;
476 	return(~nd);
477 }
478 
479 
480 /* Notes: optimizations given the kernel_font_(width|height) #define'd.
481  *        the dbra loops could be elminated and unrolled using height,
482  *        the :width in the bfxxx instruction could be made immediate instead
483  *        of a data register as it now is.
484  *        the underline could be added when the loop is unrolled
485  *
486  *        It would look like hell but be very fast.*/
487 
488 static void
489 putc_nm (cci,p,f,co,ro,fw,fh)
490     register ipriv_t *cci;
491     register u_char  *p;
492     register u_char  *f;
493     register u_int    co;
494     register u_int    ro;
495     register u_int    fw;
496     register u_int    fh;
497 {
498     while (fh--) {
499 	asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
500 	     "d" (*f++), "a" (p), "d" (co), "d" (fw));
501 	p += ro;
502     }
503 }
504 
505 static void
506 putc_in (cci,p,f,co,ro,fw,fh)
507     register ipriv_t *cci;
508     register u_char  *p;
509     register u_char  *f;
510     register u_int    co;
511     register u_int    ro;
512     register u_int    fw;
513     register u_int    fh;
514 {
515     while (fh--) {
516 	asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
517 	     "d" (~(*f++)), "a" (p), "d" (co), "d" (fw));
518 	p += ro;
519     }
520 }
521 
522 
523 static void
524 putc_ul (cci,p,f,co,ro,fw,fh)
525     register ipriv_t *cci;
526     register u_char  *p;
527     register u_char  *f;
528     register u_int    co;
529     register u_int    ro;
530     register u_int    fw;
531     register u_int    fh;
532 {
533     int underline = cci->underline;
534     while (underline--) {
535 	asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
536 	     "d" (*f++), "a" (p), "d" (co), "d" (fw));
537 	p += ro;
538     }
539 
540     asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
541 	 "d" (expbits(*f++)), "a" (p), "d" (co), "d" (fw));
542     p += ro;
543 
544     underline = fh - cci->underline - 1;
545     while (underline--) {
546 	asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
547 	     "d" (*f++), "a" (p), "d" (co), "d" (fw));
548 	p += ro;
549     }
550 }
551 
552 
553 static void
554 putc_ul_in (cci,p,f,co,ro,fw,fh)
555     register ipriv_t *cci;
556     register u_char  *p;
557     register u_char  *f;
558     register u_int    co;
559     register u_int    ro;
560     register u_int    fw;
561     register u_int    fh;
562 {
563     int underline = cci->underline;
564     while (underline--) {
565 	asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
566 	     "d" (~(*f++)), "a" (p), "d" (co), "d" (fw));
567 	p += ro;
568     }
569 
570     asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
571 	 "d" (~expbits(*f++)), "a" (p), "d" (co), "d" (fw));
572     p += ro;
573 
574     underline = fh - cci->underline - 1;
575     while (underline--) {
576 	asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
577 	     "d" (~(*f++)), "a" (p), "d" (co), "d" (fw));
578 	p += ro;
579     }
580 }
581 
582 /* bold */
583 static void
584 putc_bd (cci,p,f,co,ro,fw,fh)
585     register ipriv_t *cci;
586     register u_char  *p;
587     register u_char  *f;
588     register u_int    co;
589     register u_int    ro;
590     register u_int    fw;
591     register u_int    fh;
592 {
593     u_short ch;
594 
595     while (fh--) {
596 	ch = *f++;
597 	ch |= ch << 1;
598 	asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
599 	     "d" (ch), "a" (p), "d" (co), "d" (fw+1));
600 	p += ro;
601     }
602 }
603 
604 static void
605 putc_bd_in (cci,p,f,co,ro,fw,fh)
606     register ipriv_t *cci;
607     register u_char  *p;
608     register u_char  *f;
609     register u_int    co;
610     register u_int    ro;
611     register u_int    fw;
612     register u_int    fh;
613 {
614     u_short ch;
615 
616     while (fh--) {
617 	ch = *f++;
618 	ch |= ch << 1;
619 	asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
620 	     "d" (~(ch)), "a" (p), "d" (co), "d" (fw+1));
621 	p += ro;
622     }
623 }
624 
625 
626 static void
627 putc_bd_ul (cci,p,f,co,ro,fw,fh)
628     register ipriv_t *cci;
629     register u_char  *p;
630     register u_char  *f;
631     register u_int    co;
632     register u_int    ro;
633     register u_int    fw;
634     register u_int    fh;
635 {
636     int underline = cci->underline;
637     u_short ch;
638 
639     while (underline--) {
640 	ch = *f++;
641 	ch |= ch << 1;
642 	asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
643 	     "d" (ch), "a" (p), "d" (co), "d" (fw+1));
644 	p += ro;
645     }
646 
647     ch = *f++;
648     ch |= ch << 1;
649     asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
650 	 "d" (expbits(ch)), "a" (p), "d" (co), "d" (fw+1));
651     p += ro;
652 
653     underline = fh - cci->underline - 1;
654     while (underline--) {
655 	ch = *f++;
656 	ch |= ch << 1;
657 	asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
658 	     "d" (ch), "a" (p), "d" (co), "d" (fw+1));
659 	p += ro;
660     }
661 }
662 
663 
664 static void
665 putc_bd_ul_in (cci,p,f,co,ro,fw,fh)
666     register ipriv_t *cci;
667     register u_char  *p;
668     register u_char  *f;
669     register u_int    co;
670     register u_int    ro;
671     register u_int    fw;
672     register u_int    fh;
673 {
674     int underline = cci->underline;
675     u_short ch;
676 
677     while (underline--) {
678 	ch = *f++;
679 	ch |= ch << 1;
680 	asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
681 	     "d" (~(ch)), "a" (p), "d" (co), "d" (fw+1));
682 	p += ro;
683     }
684 
685     ch = *f++;
686     ch |= ch << 1;
687     asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
688 	 "d" (~expbits(ch)), "a" (p), "d" (co), "d" (fw+1));
689     p += ro;
690 
691     underline = fh - cci->underline - 1;
692     while (underline--) {
693 	ch = *f++;
694 	ch |= ch << 1;
695 	asm ("bfins %0,%1@{%2:%3}" : /* no output */ :
696 	     "d" (~(ch)), "a" (p), "d" (co), "d" (fw+1));
697 	p += ro;
698     }
699 }
700 
701 
702 typedef void cc_putc_func ();
703 
704 cc_putc_func *put_func[ATTR_ALL+1] = {
705     putc_nm,
706     putc_in,
707     putc_ul,
708     putc_ul_in,
709     putc_bd,
710     putc_bd_in,
711     putc_bd_ul,
712     putc_bd_ul_in,
713 /* no support for blink */
714     putc_nm,
715     putc_in,
716     putc_ul,
717     putc_ul_in,
718     putc_bd,
719     putc_bd_in,
720     putc_bd_ul,
721     putc_bd_ul_in
722 };
723 
724 
725 /* FIX: shouldn't this advance the cursor even if the character to
726         be output is not available in the font? -ch */
727 
728 static void
729 putc8(struct ite_softc *ip, int c, int dy, int dx, int mode)
730 {
731     register ipriv_t *cci = (ipriv_t *) ip->priv;
732     if (c < ip->font_lo || c > ip->font_hi)
733 	return;
734 
735     put_func[mode](cci,
736 		   cci->row_ptr[dy],
737 		   cci->font_cell[c],
738 		   cci->column_offset[dx],
739 		   cci->row_offset,
740 		   cci->ft_x,
741 		   cci->ft_y);
742 }
743 
744 static void
745 clear8(struct ite_softc *ip, int sy, int sx, int h, int w)
746 {
747   ipriv_t *cci = (ipriv_t *) ip->priv;
748   view_t *v = cci->view;
749   bmap_t *bm = cci->view->bitmap;
750 
751   if ((sx == 0) && (w == ip->cols))
752     {
753       /* common case: clearing whole lines */
754       while (h--)
755 	{
756 	  int i;
757 	  u_char *ptr = cci->row_ptr[sy];
758 	  for (i=0; i < ip->ftheight; i++) {
759             bzero(ptr, bm->bytes_per_row);
760             ptr += bm->bytes_per_row + bm->row_mod;			/* don't get any smart
761                                                    ideas, becuase this is for
762                                                    interleaved bitmaps */
763           }
764 	  sy++;
765 	}
766     }
767   else
768     {
769       /* clearing only part of a line */
770       /* XXX could be optimized MUCH better, but is it worth the trouble? */
771       while (h--)
772 	{
773 	  u_char *pl = cci->row_ptr[sy];
774           int ofs = sx * ip->ftwidth;
775 	  int i, j;
776 	  for (i = w-1; i >= 0; i--)
777 	    {
778 	      u_char *ppl = pl;
779               for (j = ip->ftheight-1; j >= 0; j--)
780 	        {
781 	          asm("bfclr %0@{%1:%2}"
782 	              : : "a" (ppl), "d" (ofs), "d" (ip->ftwidth));
783 	          ppl += bm->row_mod + bm->bytes_per_row;
784 	        }
785 	      ofs += ip->ftwidth;
786 	    }
787 	  sy++;
788 	}
789     }
790 }
791 
792 /* Note: sx is only relevant for SCROLL_LEFT or SCROLL_RIGHT.  */
793 static void
794 scroll8(ip, sy, sx, count, dir)
795         register struct ite_softc *ip;
796         register int sy;
797         int dir, sx, count;
798 {
799   bmap_t *bm = ((ipriv_t *)ip->priv)->view->bitmap;
800   u_char *pl = ((ipriv_t *)ip->priv)->row_ptr[sy];
801 
802   if (dir == SCROLL_UP)
803     {
804       int dy = sy - count;
805       int height = ip->bottom_margin - sy + 1;
806       int i;
807 
808       /*FIX: add scroll bitmap call */
809         cursor32(ip, ERASE_CURSOR);
810 	scrollbmap (bm, 0, dy*ip->ftheight,
811 		       bm->bytes_per_row >> 3, (ip->bottom_margin-dy+1)*ip->ftheight,
812 		       0, -(count*ip->ftheight), 0x1);
813 /*	if (ip->cursory <= bot || ip->cursory >= dy) {
814 	    ip->cursory -= count;
815 	} */
816     }
817   else if (dir == SCROLL_DOWN)
818     {
819       int dy = sy + count;
820       int height = ip->bottom_margin - dy + 1;
821       int i;
822 
823       /* FIX: add scroll bitmap call */
824         cursor32(ip, ERASE_CURSOR);
825 	scrollbmap (bm, 0, sy*ip->ftheight,
826 		       bm->bytes_per_row >> 3, (ip->bottom_margin-sy+1)*ip->ftheight,
827 		       0, count*ip->ftheight, 0x1);
828 /*	if (ip->cursory <= bot || ip->cursory >= sy) {
829 	    ip->cursory += count;
830 	} */
831     }
832   else if (dir == SCROLL_RIGHT)
833     {
834       int sofs = (ip->cols - count) * ip->ftwidth;
835       int dofs = (ip->cols) * ip->ftwidth;
836       int i, j;
837 
838       cursor32(ip, ERASE_CURSOR);
839       for (j = ip->ftheight-1; j >= 0; j--)
840 	{
841 	  int sofs2 = sofs, dofs2 = dofs;
842 	  for (i = (ip->cols - (sx + count))-1; i >= 0; i--)
843 	    {
844 	      int t;
845 	      sofs2 -= ip->ftwidth;
846 	      dofs2 -= ip->ftwidth;
847 	      asm("bfextu %1@{%2:%3},%0"
848 	          : "=d" (t)
849 		  : "a" (pl), "d" (sofs2), "d" (ip->ftwidth));
850 	      asm("bfins %3,%0@{%1:%2}"
851 	          : : "a" (pl), "d" (dofs2), "d" (ip->ftwidth), "d" (t));
852 	    }
853 	  pl += bm->row_mod + bm->bytes_per_row;
854 	}
855     }
856   else /* SCROLL_LEFT */
857     {
858       int sofs = (sx) * ip->ftwidth;
859       int dofs = (sx - count) * ip->ftwidth;
860       int i, j;
861 
862       cursor32(ip, ERASE_CURSOR);
863       for (j = ip->ftheight-1; j >= 0; j--)
864 	{
865 	  int sofs2 = sofs, dofs2 = dofs;
866 	  for (i = (ip->cols - sx)-1; i >= 0; i--)
867 	    {
868 	      int t;
869 	      asm("bfextu %1@{%2:%3},%0"
870 	          : "=d" (t)
871 		  : "a" (pl), "d" (sofs2), "d" (ip->ftwidth));
872 	      asm("bfins %3,%0@{%1:%2}"
873 	          : : "a" (pl), "d" (dofs2), "d" (ip->ftwidth), "d" (t));
874 	      sofs2 += ip->ftwidth;
875 	      dofs2 += ip->ftwidth;
876 	    }
877 	  pl += bm->row_mod + bm->bytes_per_row;
878 	}
879     }
880 }
881 
882 void
883 scrollbmap (bmap_t *bm, u_short x, u_short y, u_short width, u_short height, short dx, short dy, u_char mask)
884 {
885     u_short depth = bm->depth;
886     u_short lwpr = bm->bytes_per_row >> 2;
887     if (dx) {
888     	/* FIX: */ panic ("delta x not supported in scroll bitmap yet.");
889     }
890     if (bm->flags & BMF_INTERLEAVED) {
891 	height *= depth;
892 	depth = 1;
893     }
894     if (dy == 0) {
895         return;
896     }
897     if (dy > 0) {
898     	int i;
899     	for (i=0; i < depth && mask; i++, mask >>= 1) {
900     	    if (0x1 & mask) {
901 	    	u_long *pl = (u_long *)bm->plane[i];
902 		u_long *src_y = pl + (lwpr*y);
903 		u_long *dest_y = pl + (lwpr*(y+dy));
904 		u_long count = lwpr*(height-dy);
905 		u_long *clr_y = src_y;
906 		u_long clr_count = dest_y - src_y;
907 		u_long bc, cbc;
908 
909 		src_y += count - 1;
910 		dest_y += count - 1;
911 
912 		bc = count >> 4;
913 		count &= 0xf;
914 
915 		while (bc--) {
916 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
917 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
918 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
919 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
920 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
921 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
922 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
923 		    *dest_y-- = *src_y--; *dest_y-- = *src_y--;
924 		}
925 		while (count--) {
926 		    *dest_y-- = *src_y--;
927 		}
928 
929 		cbc = clr_count >> 4;
930 		clr_count &= 0xf;
931 
932 		while (cbc--) {
933 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
934 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
935 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
936 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
937 		}
938 		while (clr_count--) {
939 		    *clr_y++ = 0;
940 		}
941     	    }
942 	}
943     } else if (dy < 0) {
944     	int i;
945     	for (i=0; i < depth && mask; i++, mask >>= 1) {
946     	    if (0x1 & mask) {
947     		u_long *pl = (u_long *)bm->plane[i];
948     		u_long *src_y = pl + (lwpr*(y-dy));
949     		u_long *dest_y = pl + (lwpr*y);
950 		long count = lwpr*(height + dy);
951 		u_long *clr_y = dest_y + count;
952 		u_long clr_count = src_y - dest_y;
953 		u_long bc, cbc;
954 
955 		bc = count >> 4;
956 		count &= 0xf;
957 
958 		while (bc--) {
959 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
960 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
961 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
962 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
963 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
964 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
965 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
966 		    *dest_y++ = *src_y++; *dest_y++ = *src_y++;
967 		}
968 		while (count--) {
969 		    *dest_y++ = *src_y++;
970 		}
971 
972 		cbc = clr_count >> 4;
973 		clr_count &= 0xf;
974 
975 		while (cbc--) {
976 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
977 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
978 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
979 		    *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0; *clr_y++ = 0;
980 		}
981 		while (clr_count--) {
982 		    *clr_y++ = 0;
983 		}
984 	    }
985 	}
986     }
987 }
988 
989 #endif /* NGRFCC */
990