xref: /netbsd-src/sys/arch/amiga/dev/ite_cv3d.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: ite_cv3d.c,v 1.2 1997/11/10 12:17:20 is Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Michael Teske
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Christian E. Hopps,
18  *      Ezra Story, Kari Mettinen, Markus Wild, Lutz Vieweg
19  *      and Michael Teske.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * This code is based on ite_cl.c and ite_rh.c by
37  * Ezra Story, Kari Mettinen, Markus Wild, Lutz Vieweg.
38  */
39 
40 #include "grfcv3d.h"
41 #if NGRFCV3D > 0
42 
43 #include <sys/param.h>
44 #include <sys/conf.h>
45 #include <sys/proc.h>
46 #include <sys/device.h>
47 #include <sys/ioctl.h>
48 #include <sys/tty.h>
49 #include <sys/systm.h>
50 #include <sys/queue.h>
51 #include <sys/termios.h>
52 #include <sys/malloc.h>
53 #include <dev/cons.h>
54 #include <machine/cpu.h>
55 #include <amiga/dev/itevar.h>
56 #include <amiga/dev/iteioctl.h>
57 #include <amiga/amiga/device.h>
58 #include <amiga/dev/grfioctl.h>
59 #include <amiga/dev/grfvar.h>
60 #include <amiga/dev/grf_cv3dreg.h>
61 
62 void cv3d_ite_init __P((struct ite_softc *));
63 void cv3d_ite_deinit __P((struct ite_softc *));
64 static void cv3d_cursor __P((struct ite_softc *, int));
65 static void cv3d_putc __P((struct ite_softc *, int, int, int, int));
66 static void cv3d_clear __P((struct ite_softc *, int, int, int, int));
67 static void cv3d_scroll __P((struct ite_softc *, int, int, int, int));
68 
69 /*
70  * called from grf_cv3d to return console priority
71  */
72 int
73 grfcv3d_cnprobe()
74 {
75 	static int done;
76 	int rv;
77 
78 	if (done == 0)
79 #ifdef CV3DCONSOLE
80 		rv = CN_INTERNAL;
81 #else
82 		rv = CN_DEAD;
83 #endif
84 	else
85 #ifdef CV3DCONSOLE
86 		rv = CN_NORMAL;
87 #else
88 		rv = CN_DEAD;
89 #endif
90 	done = 1;
91 	return(rv);
92 }
93 
94 
95 /*
96  * called from grf_cv3d to init ite portion of
97  * grf_softc struct
98  */
99 void
100 grfcv3d_iteinit(gp)
101 	struct grf_softc *gp;
102 {
103 	gp->g_itecursor = cv3d_cursor;
104 	gp->g_iteputc = cv3d_putc;
105 	gp->g_iteclear = cv3d_clear;
106 	gp->g_itescroll = cv3d_scroll;
107 	gp->g_iteinit = cv3d_ite_init;
108 	gp->g_itedeinit = cv3d_ite_deinit;
109 }
110 
111 
112 void
113 cv3d_ite_deinit(ip)
114 	struct ite_softc *ip;
115 {
116 	ip->flags &= ~ITE_INITED;
117 }
118 
119 
120 static unsigned short cv3d_rowc[MAXCOLS*(MAXROWS+1)];
121 
122 /*
123  * Console buffer to avoid the slow reading from gfx mem.
124  */
125 
126 static unsigned short *console_buffer;
127 
128 void
129 cv3d_ite_init(ip)
130 	register struct ite_softc *ip;
131 {
132 	struct grfcv3dtext_mode *md;
133 	int i;
134 	static char first = 1;
135 	volatile unsigned short *fb = (volatile unsigned short *)ip->grf->g_fbkva;
136 	unsigned short *buffer;
137 
138 
139 	ip->priv = ip->grf->g_data;
140 	md = (struct grfcv3dtext_mode *) ip->grf->g_data;
141 
142 	ip->cols = md->cols;
143 	ip->rows = md->rows;
144 
145 	/* alloc buffers */
146 
147 #if 0  /* XXX malloc seems not to work in early init :( */
148 	if (cv3d_rowc)
149 		free(cv3d_rowc, M_DEVBUF);
150 
151 	/* alloc all in one */
152 	cv3d_rowc = malloc(sizeof(short) * (ip->rows + 1) * (ip->cols + 2),
153 		M_DEVBUF, M_WAITOK);
154 	if (!cv3d_rowc)
155 		panic("No buffers for ite_cv3d!");
156 #endif
157 
158 	console_buffer = cv3d_rowc + ip->rows + 1;
159 
160 
161 	for (i = 0; i < ip->rows; i++)
162 		cv3d_rowc[i] = i * ip->cols;
163 
164 	if (first) {
165 		for (i = 0; i < ip->rows * ip->cols; i++)
166 			console_buffer[i] = 0x2007;
167 		first = 0;
168 	} else { /* restore console */
169 		buffer = console_buffer;
170 		for (i = 0; i < ip->rows * ip->cols; i++) {
171 			*fb++ = *buffer++;
172 			*fb++;
173 		}
174 	}
175 }
176 
177 
178 void
179 cv3d_cursor(ip, flag)
180 	struct ite_softc *ip;
181 	int flag;
182 {
183 	volatile caddr_t ba = ip->grf->g_regkva;
184 
185 	switch (flag) {
186 	    case DRAW_CURSOR:
187 		/*WCrt(ba, CRT_ID_CURSOR_START, & ~0x20); */
188 	    case MOVE_CURSOR:
189 		flag = ip->curx + ip->cury * ip->cols;
190 		WCrt(ba, CRT_ID_CURSOR_LOC_LOW, flag & 0xff);
191 		WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, flag >> 8);
192 		ip->cursorx = ip->curx;
193 		ip->cursory = ip->cury;
194 		break;
195 	    case ERASE_CURSOR:
196 		/*WCrt(ba, CRT_ID_CURSOR_START, | 0x20); */
197 	    case START_CURSOROPT:
198 	    case END_CURSOROPT:
199 	    default:
200 		break;
201 	}
202 }
203 
204 
205 void
206 cv3d_putc(ip, c, dy, dx, mode)
207 	struct ite_softc *ip;
208 	int c;
209 	int dy;
210 	int dx;
211 	int mode;
212 {
213 	caddr_t fb = ip->grf->g_fbkva;
214 	unsigned char attr;
215 	unsigned char *cp;
216 
217 	attr = (unsigned char) ((mode & ATTR_INV) ? (0x70) : (0x07));
218 	if (mode & ATTR_UL)     attr  = 0x01;
219 	if (mode & ATTR_BOLD)   attr |= 0x08;
220 	if (mode & ATTR_BLINK)  attr |= 0x80;
221 
222 	cp = fb + ((cv3d_rowc[dy] + dx) << 2); /* *4 */
223 	*cp++ = (unsigned char) c;
224 	*cp = (unsigned char) attr;
225 
226 	cp = (unsigned char *) &console_buffer[cv3d_rowc[dy]+dx];
227 	*cp++ = (unsigned char) c;
228 	*cp = (unsigned char) attr;
229 }
230 
231 
232 void
233 cv3d_clear(ip, sy, sx, h, w)
234 	struct ite_softc *ip;
235 	int sy;
236 	int sx;
237 	int h;
238 	int w;
239 {
240 	/* cv3d_clear and cv3d_scroll both rely on ite passing arguments
241 	 * which describe continuous regions.  For a VT200 terminal,
242 	 * this is safe behavior.
243 	 */
244 	unsigned short  *dst;
245 	int len;
246 
247 	dst = (unsigned short *) (ip->grf->g_fbkva + (((sy * ip->cols) + sx) << 2));
248 
249 	for (len = w * h; len > 0 ; len--) {
250 		*dst = 0x2007;
251 		dst +=2;
252 	}
253 
254 	dst = &console_buffer[(sy * ip->cols) + sx];
255 	for (len = w * h; len > 0 ; len--) {
256 		*dst++ = 0x2007;
257 	}
258 }
259 
260 void
261 cv3d_scroll(ip, sy, sx, count, dir)
262 	struct ite_softc *ip;
263 	int sy;
264 	int sx;
265 	int count;
266 	int dir;
267 {
268 	unsigned short *src, *dst, *dst2;
269 	int i;
270 	int len;
271 
272 	src = (unsigned short *)(ip->grf->g_fbkva + (cv3d_rowc[sy] << 2));
273 
274 	switch (dir) {
275 	    case SCROLL_UP:
276 		dst = src - ((cv3d_rowc[count])<<1);
277 
278 		len = cv3d_rowc[(ip->bottom_margin + 1 - sy)];
279 		src = &console_buffer[cv3d_rowc[sy]];
280 
281 		if (count > sy) { /* boundary checks */
282 			dst2 = console_buffer;
283 			dst = (unsigned short *)(ip->grf->g_fbkva);
284 			len -= cv3d_rowc[(count - sy)];
285 			src += cv3d_rowc[(count - sy)];
286 		} else
287 			dst2 = &console_buffer[cv3d_rowc[(sy-count)]];
288 
289 		bcopy (src, dst2, len << 1);
290 
291 		for (i = 0; i < len; i++) {
292 			*dst++ = *dst2++;
293 			dst++;
294 		}
295 		break;
296 	    case SCROLL_DOWN:
297 		dst = src + ((cv3d_rowc[count]) << 1);
298 
299 		len = cv3d_rowc[(ip->bottom_margin + 1 - (sy + count))];
300 		src = &console_buffer[cv3d_rowc[sy]];
301 		dst2 = &console_buffer[cv3d_rowc[(sy + count)]];
302 
303 		if (len < 0)
304 			return;  /* do some boundary check */
305 
306 		bcopy (src, dst2, len << 1);
307 
308 		for (i = 0; i < len; i++) {
309 			*dst++ = *dst2++;
310 			dst++;
311 		}
312 		break;
313 	    case SCROLL_RIGHT:
314 		dst = src + ((sx+count)<<1);
315 		src = &console_buffer[cv3d_rowc[sy] + sx];
316 		len = ip->cols - (sx + count);
317 		dst2 = &console_buffer[cv3d_rowc[sy] + sx + count];
318 		bcopy (src, dst2, len << 1);
319 
320 		for (i = 0; i < len; i++) {
321 			*dst++ = *dst2++;
322 			dst++;
323 		}
324 		break;
325 	    case SCROLL_LEFT:
326 		dst = src + ((sx - count)<<1);
327 		src = &console_buffer[cv3d_rowc[sy] + sx];
328 		len = ip->cols - sx;
329 		dst2 = &console_buffer[cv3d_rowc[sy] + sx - count];
330 		bcopy (src, dst2, len << 1);
331 
332 		for (i = 0; i < len; i++) {
333 			*dst++ = *dst2++;
334 			dst++;
335 		}
336 	}
337 }
338 
339 #endif /* NGRFCV3D */
340