Home | History | Annotate | Line # | Download | only in boot
vga.c revision 1.3
      1 /*	$NetBSD: vga.c,v 1.3 1999/06/28 01:20:45 sakamoto Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1995-1997 Gary Thomas (gdt (at) linuxppc.org)
      5  * All rights reserved.
      6  *
      7  * VGA 'glass TTY' emulator
      8  *
      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.
     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 #ifdef CONS_VGA
     36 #include <stand.h>
     37 #include "boot.h"
     38 
     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
     46 
     47 u_char background = 0;  /* Black */
     48 u_char foreground = 7;  /* White */
     49 
     50 u_int addr_6845;
     51 u_short *Crtat;
     52 int lastpos;
     53 int scroll;
     54 
     55 /*
     56  * The current state of virtual displays
     57  */
     58 struct screen {
     59 	u_short *cp;		/* the current character address */
     60 	enum state {
     61 		NORMAL,			/* no pending escape */
     62 		ESC,			/* saw ESC */
     63 		EBRAC,			/* saw ESC[ */
     64 		EBRACEQ			/* saw ESC[= */
     65 	} state;		/* command parser state */
     66 	int	cx;		/* the first escape seq argument */
     67 	int	cy;		/* the second escap seq argument */
     68 	int	*accp;		/* pointer to the current processed argument */
     69 	int	row;		/* current column */
     70 	int	so;		/* standout mode */
     71 	u_short color;		/* normal character color */
     72 	u_short color_so;	/* standout color */
     73 	u_short save_color;	/* saved normal color */
     74 	u_short save_color_so;	/* saved standout color */
     75 } screen;
     76 
     77 /*
     78  * Color and attributes for normal, standout and kernel output
     79  * are stored in the least-significant byte of a u_short
     80  * so they don't have to be shifted for use.
     81  * This is all byte-order dependent.
     82  */
     83 #define	CATTR(x) (x)		/* store color/attributes un-shifted */
     84 #define	ATTR_ADDR(which) (((u_char *)&(which))+1) /* address of attributes */
     85 
     86 u_short	pccolor;		/* color/attributes for tty output */
     87 u_short	pccolor_so;		/* color/attributes, standout mode */
     88 
     89 /*
     90  * cursor() sets an offset (0-1999) into the 80x25 text area
     91  */
     92 static void
     93 cursor()
     94 {
     95  	int pos = screen.cp - Crtat;
     96 
     97 	if (lastpos != pos) {
     98 		outb(addr_6845, 14);
     99 		outb(addr_6845+1, pos >> 8);
    100 		outb(addr_6845, 15);
    101 		outb(addr_6845+1, pos);
    102 		lastpos = pos;
    103 	}
    104 }
    105 
    106 static void
    107 initscreen()
    108 {
    109 	struct screen *d = &screen;
    110 
    111 	pccolor = CATTR((background<<4)|foreground);
    112 	pccolor_so = CATTR((foreground<<4)|background);
    113 	d->color = pccolor;
    114 	d->save_color = pccolor;
    115 	d->color_so = pccolor_so;
    116 	d->save_color_so = pccolor_so;
    117 }
    118 
    119 
    120 #define	wrtchar(c, d) { \
    121 	*(d->cp) = c; \
    122 	d->cp++; \
    123 	d->row++; \
    124 }
    125 
    126 void
    127 fillw(val, buf, num)
    128 	u_short val;
    129 	u_short *buf;
    130 	int num;
    131 {
    132 	/* Need to byte swap value */
    133 	u_short tmp;
    134 
    135 	tmp = val;
    136 	while (num-- > 0)
    137 		*buf++ = tmp;
    138 }
    139 
    140 /*
    141  * vga_putc (nee sput) has support for emulation of the 'ibmpc' termcap entry.
    142  * This is a bare-bones implementation of a bare-bones entry
    143  * One modification: Change li#24 to li#25 to reflect 25 lines
    144  * "ca" is the color/attributes value (left-shifted by 8)
    145  * or 0 if the current regular color for that screen is to be used.
    146  */
    147 void
    148 vga_putc(int c)
    149 {
    150 	struct screen *d = &screen;
    151 	u_short *base;
    152 	int i, j;
    153 	u_short *pp;
    154 
    155 	base = Crtat;
    156 
    157 	switch (d->state) {
    158 	case NORMAL:
    159 		switch (c) {
    160 		case 0x0:		/* Ignore pad characters */
    161 			return;
    162 
    163 		case 0x1B:
    164 			d->state = ESC;
    165 			break;
    166 
    167 		case '\t':
    168 			do {
    169 				wrtchar(d->color | ' ', d);
    170 			} while (d->row % 8);
    171 			break;
    172 
    173 		case '\b':  /* non-destructive backspace */
    174 			if (d->cp > base) {
    175 				d->cp--;
    176 				d->row--;
    177 				if (d->row < 0)
    178 					d->row += COL;	/* prev column */
    179 			}
    180 			break;
    181 
    182 		case '\n':
    183 			d->cp += COL;
    184 		case '\r':
    185 			d->cp -= d->row;
    186 			d->row = 0;
    187 			break;
    188 
    189 		case '\007':
    190 			break;
    191 
    192 		default:
    193 			if (d->so) {
    194 				wrtchar(d->color_so|(c<<8), d);
    195 			} else {
    196 				wrtchar(d->color | (c<<8), d);
    197 			}
    198 			if (d->row >= COL)
    199 				d->row = 0;
    200 			break;
    201 		}
    202 		break;
    203 
    204 	case EBRAC:
    205 		/*
    206 		 * In this state, the action at the end of the switch
    207 		 * on the character type is to go to NORMAL state,
    208 		 * and intermediate states do a return rather than break.
    209 		 */
    210 		switch (c) {
    211 		case 'm':
    212 			d->so = d->cx;
    213 			break;
    214 
    215 		case 'A': /* back one row */
    216 			if (d->cp >= base + COL)
    217 				d->cp -= COL;
    218 			break;
    219 
    220 		case 'B': /* down one row */
    221 			d->cp += COL;
    222 			break;
    223 
    224 		case 'C': /* right cursor */
    225 			d->cp++;
    226 			d->row++;
    227 			break;
    228 
    229 		case 'D': /* left cursor */
    230 			if (d->cp > base) {
    231 				d->cp--;
    232 				d->row--;
    233 				if (d->row < 0)
    234 					d->row += COL;	/* prev column ??? */
    235 			}
    236 			break;
    237 
    238 		case 'J': /* Clear to end of display */
    239 			fillw(d->color|(' '<<8), d->cp, base + COL * ROW - d->cp);
    240 			break;
    241 
    242 		case 'K': /* Clear to EOL */
    243 			fillw(d->color|(' '<<8), d->cp, COL - (d->cp - base) % COL);
    244 			break;
    245 
    246 		case 'H': /* Cursor move */
    247 			if (d->cx > ROW)
    248 				d->cx = ROW;
    249 			if (d->cy > COL)
    250 				d->cy = COL;
    251 			if (d->cx == 0 || d->cy == 0) {
    252 				d->cp = base;
    253 				d->row = 0;
    254 			} else {
    255 				d->cp = base + (d->cx - 1) * COL + d->cy - 1;
    256 				d->row = d->cy - 1;
    257 			}
    258 			break;
    259 
    260 		case '_': /* set cursor */
    261 			if (d->cx)
    262 				d->cx = 1;		/* block */
    263 			else
    264 				d->cx = 12;	/* underline */
    265 			outb(addr_6845, 10);
    266 			outb(addr_6845+1, d->cx);
    267 			outb(addr_6845, 11);
    268 			outb(addr_6845+1, 13);
    269 			break;
    270 
    271 		case ';': /* Switch params in cursor def */
    272 			d->accp = &d->cy;
    273 			return;
    274 
    275 		case '=': /* ESC[= color change */
    276 			d->state = EBRACEQ;
    277 			return;
    278 
    279 		case 'L':	/* Insert line */
    280 			i = (d->cp - base) / COL;
    281 			/* avoid deficiency of bcopy implementation */
    282 			pp = base + COL * (ROW-2);
    283 			for (j = ROW - 1 - i; j--; pp -= COL)
    284 				bcopy(pp, pp + COL, COL * CHR);
    285 			fillw(d->color|(' '<<8), base + i * COL, COL);
    286 			break;
    287 
    288 		case 'M':	/* Delete line */
    289 			i = (d->cp - base) / COL;
    290 			pp = base + i * COL;
    291 			bcopy(pp + COL, pp, (ROW-1 - i)*COL*CHR);
    292 			fillw(d->color|(' '<<8), base + COL * (ROW - 1), COL);
    293 			break;
    294 
    295 		default: /* Only numbers valid here */
    296 			if ((c >= '0') && (c <= '9')) {
    297 				*(d->accp) *= 10;
    298 				*(d->accp) += c - '0';
    299 				return;
    300 			} else
    301 				break;
    302 		}
    303 		d->state = NORMAL;
    304 		break;
    305 
    306 	case EBRACEQ: {
    307 		/*
    308 		 * In this state, the action at the end of the switch
    309 		 * on the character type is to go to NORMAL state,
    310 		 * and intermediate states do a return rather than break.
    311 		 */
    312 		u_char *colp;
    313 
    314 		/*
    315 		 * Set foreground/background color
    316 		 * for normal mode, standout mode
    317 		 * or kernel output.
    318 		 * Based on code from kentp@svmp03.
    319 		 */
    320 		switch (c) {
    321 		case 'F':
    322 			colp = ATTR_ADDR(d->color);
    323 	do_fg:
    324 			*colp = (*colp & 0xf0) | (d->cx);
    325 			break;
    326 
    327 		case 'G':
    328 			colp = ATTR_ADDR(d->color);
    329 	do_bg:
    330 			*colp = (*colp & 0xf) | (d->cx << 4);
    331 			break;
    332 
    333 		case 'H':
    334 			colp = ATTR_ADDR(d->color_so);
    335 			goto do_fg;
    336 
    337 		case 'I':
    338 			colp = ATTR_ADDR(d->color_so);
    339 			goto do_bg;
    340 
    341 		case 'S':
    342 			d->save_color = d->color;
    343 			d->save_color_so = d->color_so;
    344 			break;
    345 
    346 		case 'R':
    347 			d->color = d->save_color;
    348 			d->color_so = d->save_color_so;
    349 			break;
    350 
    351 		default: /* Only numbers valid here */
    352 			if ((c >= '0') && (c <= '9')) {
    353 				d->cx *= 10;
    354 				d->cx += c - '0';
    355 				return;
    356 			} else
    357 				break;
    358 		}
    359 		d->state = NORMAL;
    360 	    }
    361 	    break;
    362 
    363 	case ESC:
    364 		switch (c) {
    365 		case 'c':	/* Clear screen & home */
    366 			fillw(d->color|(' '<<8), base, COL * ROW);
    367 			d->cp = base;
    368 			d->row = 0;
    369 			d->state = NORMAL;
    370 			break;
    371 		case '[':	/* Start ESC [ sequence */
    372 			d->state = EBRAC;
    373 			d->cx = 0;
    374 			d->cy = 0;
    375 			d->accp = &d->cx;
    376 			break;
    377 		default: /* Invalid, clear state */
    378 			d->state = NORMAL;
    379 			break;
    380 		}
    381 		break;
    382 	}
    383 	if (d->cp >= base + (COL * ROW)) { /* scroll check */
    384 		bcopy(base + COL, base, COL * (ROW - 1) * CHR);
    385 		fillw(d->color|(' '<<8), base + COL * (ROW - 1), COL);
    386 		d->cp -= COL;
    387 	}
    388 	cursor();
    389 }
    390 
    391 void
    392 vga_puts(char *s)
    393 {
    394 	char c;
    395 	while ((c = *s++)) {
    396 		vga_putc(c);
    397 	}
    398 }
    399 
    400 void
    401 video_on()
    402 {
    403 
    404 	/* Enable video */
    405 	outb(0x3C4, 0x01);
    406 	outb(0x3C5, inb(0x3C5) & ~0x20);
    407 }
    408 
    409 void
    410 video_off()
    411 {
    412 
    413 	/* Disable video */
    414 	outb(0x3C4, 0x01);
    415 	outb(0x3C5, inb(0x3C5) | 0x20);
    416 }
    417 
    418 void
    419 vga_init(ISA_mem)
    420 	u_char *ISA_mem;
    421 {
    422 	struct screen *d = &screen;
    423 
    424 	memset(d, 0, sizeof (screen));
    425 	video_on();
    426 
    427 	d->cp = Crtat = (u_short *)&ISA_mem[0x0B8000];
    428 	addr_6845 = CGA_BASE;
    429 	initscreen();
    430 	fillw(pccolor|(' '<<8), d->cp, COL * ROW);
    431 }
    432 #endif /* CONS_VGA */
    433