No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / bebox / stand / boot / vga.c
blob7a4b70f4da85ee63a5d5662fbd7841c1a9007b5b
1 /* $NetBSD: vga.c,v 1.9 2009/03/14 15:36:04 dsl Exp $ */
3 /*-
4 * Copyright (C) 1995-1997 Gary Thomas (gdt@linuxppc.org)
5 * All rights reserved.
7 * VGA 'glass TTY' emulator
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Gary Thomas.
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.
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.
35 #ifdef CONS_VGA
36 #include <lib/libsa/stand.h>
37 #include "boot.h"
39 #define COL 80
40 #define ROW 25
41 #define CHR 2
42 #define MONO_BASE 0x3B4
43 #define MONO_BUF 0xB0000
44 #define CGA_BASE 0x3D4
45 #define CGA_BUF 0xB8000
47 static u_char background = 0; /* Black */
48 static u_char foreground = 7; /* White */
50 u_int addr_6845;
51 u_short *Crtat;
52 int lastpos;
55 * The current state of virtual displays
57 struct screen {
58 u_short *cp; /* the current character address */
59 enum state {
60 NORMAL, /* no pending escape */
61 ESC, /* saw ESC */
62 EBRAC, /* saw ESC[ */
63 EBRACEQ /* saw ESC[= */
64 } state; /* command parser state */
65 int cx; /* the first escape seq argument */
66 int cy; /* the second escap seq argument */
67 int *accp; /* pointer to the current processed argument */
68 int row; /* current column */
69 int so; /* standout mode */
70 u_short color; /* normal character color */
71 u_short color_so; /* standout color */
72 u_short save_color; /* saved normal color */
73 u_short save_color_so; /* saved standout color */
74 } screen;
77 * Color and attributes for normal, standout and kernel output
78 * are stored in the least-significant byte of a u_short
79 * so they don't have to be shifted for use.
80 * This is all byte-order dependent.
82 #define CATTR(x) (x) /* store color/attributes un-shifted */
83 #define ATTR_ADDR(which) (((u_char *)&(which))+1) /* address of attributes */
85 u_short pccolor; /* color/attributes for tty output */
86 u_short pccolor_so; /* color/attributes, standout mode */
88 static void cursor(void);
89 static void initscreen(void);
90 void fillw(u_short, u_short *, int);
91 void video_on(void);
92 void video_off(void);
95 * cursor() sets an offset (0-1999) into the 80x25 text area
97 static void
98 cursor(void)
100 int pos = screen.cp - Crtat;
102 if (lastpos != pos) {
103 outb(addr_6845, 14);
104 outb(addr_6845+1, pos >> 8);
105 outb(addr_6845, 15);
106 outb(addr_6845+1, pos);
107 lastpos = pos;
111 static void
112 initscreen(void)
114 struct screen *d = &screen;
116 pccolor = CATTR((background<<4)|foreground);
117 pccolor_so = CATTR((foreground<<4)|background);
118 d->color = pccolor;
119 d->save_color = pccolor;
120 d->color_so = pccolor_so;
121 d->save_color_so = pccolor_so;
125 #define wrtchar(c, d) { \
126 *(d->cp) = c; \
127 d->cp++; \
128 d->row++; \
131 void
132 fillw(u_short val, u_short *buf, int num)
134 /* Need to byte swap value */
135 u_short tmp;
137 tmp = val;
138 while (num-- > 0)
139 *buf++ = tmp;
143 * vga_putc (nee sput) has support for emulation of the 'ibmpc' termcap entry.
144 * This is a bare-bones implementation of a bare-bones entry
145 * One modification: Change li#24 to li#25 to reflect 25 lines
146 * "ca" is the color/attributes value (left-shifted by 8)
147 * or 0 if the current regular color for that screen is to be used.
149 void
150 vga_putc(int c)
152 struct screen *d = &screen;
153 u_short *base;
154 int i, j;
155 u_short *pp;
157 base = Crtat;
159 switch (d->state) {
160 case NORMAL:
161 switch (c) {
162 case 0x0: /* Ignore pad characters */
163 return;
165 case 0x1B:
166 d->state = ESC;
167 break;
169 case '\t':
170 do {
171 wrtchar(d->color | ' ', d);
172 } while (d->row % 8);
173 break;
175 case '\b': /* non-destructive backspace */
176 if (d->cp > base) {
177 d->cp--;
178 d->row--;
179 if (d->row < 0)
180 d->row += COL; /* prev column */
182 break;
184 case '\n':
185 d->cp += COL;
186 case '\r':
187 d->cp -= d->row;
188 d->row = 0;
189 break;
191 case '\007':
192 break;
194 default:
195 if (d->so) {
196 wrtchar(d->color_so|(c<<8), d);
197 } else {
198 wrtchar(d->color | (c<<8), d);
200 if (d->row >= COL)
201 d->row = 0;
202 break;
204 break;
206 case EBRAC:
208 * In this state, the action at the end of the switch
209 * on the character type is to go to NORMAL state,
210 * and intermediate states do a return rather than break.
212 switch (c) {
213 case 'm':
214 d->so = d->cx;
215 break;
217 case 'A': /* back one row */
218 if (d->cp >= base + COL)
219 d->cp -= COL;
220 break;
222 case 'B': /* down one row */
223 d->cp += COL;
224 break;
226 case 'C': /* right cursor */
227 d->cp++;
228 d->row++;
229 break;
231 case 'D': /* left cursor */
232 if (d->cp > base) {
233 d->cp--;
234 d->row--;
235 if (d->row < 0)
236 d->row += COL; /* prev column ??? */
238 break;
240 case 'J': /* Clear to end of display */
241 fillw(d->color|(' '<<8), d->cp, base + COL * ROW - d->cp);
242 break;
244 case 'K': /* Clear to EOL */
245 fillw(d->color|(' '<<8), d->cp, COL - (d->cp - base) % COL);
246 break;
248 case 'H': /* Cursor move */
249 if (d->cx > ROW)
250 d->cx = ROW;
251 if (d->cy > COL)
252 d->cy = COL;
253 if (d->cx == 0 || d->cy == 0) {
254 d->cp = base;
255 d->row = 0;
256 } else {
257 d->cp = base + (d->cx - 1) * COL + d->cy - 1;
258 d->row = d->cy - 1;
260 break;
262 case '_': /* set cursor */
263 if (d->cx)
264 d->cx = 1; /* block */
265 else
266 d->cx = 12; /* underline */
267 outb(addr_6845, 10);
268 outb(addr_6845+1, d->cx);
269 outb(addr_6845, 11);
270 outb(addr_6845+1, 13);
271 break;
273 case ';': /* Switch params in cursor def */
274 d->accp = &d->cy;
275 return;
277 case '=': /* ESC[= color change */
278 d->state = EBRACEQ;
279 return;
281 case 'L': /* Insert line */
282 i = (d->cp - base) / COL;
283 /* avoid deficiency of bcopy implementation */
284 /* XXX: comment and hack relevant? */
285 pp = base + COL * (ROW-2);
286 for (j = ROW - 1 - i; j--; pp -= COL)
287 memmove(pp + COL, pp, COL * CHR);
288 fillw(d->color|(' '<<8), base + i * COL, COL);
289 break;
291 case 'M': /* Delete line */
292 i = (d->cp - base) / COL;
293 pp = base + i * COL;
294 memmove(pp, pp + COL, (ROW-1 - i)*COL*CHR);
295 fillw(d->color|(' '<<8), base + COL * (ROW - 1), COL);
296 break;
298 default: /* Only numbers valid here */
299 if ((c >= '0') && (c <= '9')) {
300 *(d->accp) *= 10;
301 *(d->accp) += c - '0';
302 return;
303 } else
304 break;
306 d->state = NORMAL;
307 break;
309 case EBRACEQ: {
311 * In this state, the action at the end of the switch
312 * on the character type is to go to NORMAL state,
313 * and intermediate states do a return rather than break.
315 u_char *colp;
318 * Set foreground/background color
319 * for normal mode, standout mode
320 * or kernel output.
321 * Based on code from kentp@svmp03.
323 switch (c) {
324 case 'F':
325 colp = ATTR_ADDR(d->color);
326 do_fg:
327 *colp = (*colp & 0xf0) | (d->cx);
328 break;
330 case 'G':
331 colp = ATTR_ADDR(d->color);
332 do_bg:
333 *colp = (*colp & 0xf) | (d->cx << 4);
334 break;
336 case 'H':
337 colp = ATTR_ADDR(d->color_so);
338 goto do_fg;
340 case 'I':
341 colp = ATTR_ADDR(d->color_so);
342 goto do_bg;
344 case 'S':
345 d->save_color = d->color;
346 d->save_color_so = d->color_so;
347 break;
349 case 'R':
350 d->color = d->save_color;
351 d->color_so = d->save_color_so;
352 break;
354 default: /* Only numbers valid here */
355 if ((c >= '0') && (c <= '9')) {
356 d->cx *= 10;
357 d->cx += c - '0';
358 return;
359 } else
360 break;
362 d->state = NORMAL;
364 break;
366 case ESC:
367 switch (c) {
368 case 'c': /* Clear screen & home */
369 fillw(d->color|(' '<<8), base, COL * ROW);
370 d->cp = base;
371 d->row = 0;
372 d->state = NORMAL;
373 break;
374 case '[': /* Start ESC [ sequence */
375 d->state = EBRAC;
376 d->cx = 0;
377 d->cy = 0;
378 d->accp = &d->cx;
379 break;
380 default: /* Invalid, clear state */
381 d->state = NORMAL;
382 break;
384 break;
386 if (d->cp >= base + (COL * ROW)) { /* scroll check */
387 memmove(base, base + COL, COL * (ROW - 1) * CHR);
388 fillw(d->color|(' '<<8), base + COL * (ROW - 1), COL);
389 d->cp -= COL;
391 cursor();
394 void
395 vga_puts(char *s)
397 char c;
398 while ((c = *s++)) {
399 vga_putc(c);
403 void
404 video_on(void)
407 /* Enable video */
408 outb(0x3C4, 0x01);
409 outb(0x3C5, inb(0x3C5) & ~0x20);
412 void
413 video_off(void)
416 /* Disable video */
417 outb(0x3C4, 0x01);
418 outb(0x3C5, inb(0x3C5) | 0x20);
421 void
422 vga_init(u_char *ISA_mem)
424 struct screen *d = &screen;
426 memset(d, 0, sizeof (screen));
427 video_on();
429 d->cp = Crtat = (u_short *)&ISA_mem[0x0B8000];
430 addr_6845 = CGA_BASE;
431 initscreen();
432 fillw(pccolor|(' '<<8), d->cp, COL * ROW);
434 #endif /* CONS_VGA */