Home | History | Annotate | Line # | Download | only in boot
vga.c revision 1.1
      1 /*	$Id: vga.c,v 1.1 1998/10/26 00:45:47 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 typedef unsigned short u_short;
     37 typedef unsigned char  u_char;
     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 unsigned char background = 0;  /* Black */
     48 unsigned char foreground = 7;  /* White */
     49 
     50 unsigned int addr_6845;
     51 unsigned 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 unsigned short	pccolor;		/* color/attributes for tty output */
     87 unsigned 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 fillw(unsigned short val, unsigned short *buf, int num)
    127 {
    128 	/* Need to byte swap value */
    129 	unsigned short tmp;
    130 	tmp = val;
    131 	while (num-- > 0)
    132 	{
    133 		*buf++ = tmp;
    134 	}
    135 }
    136 
    137 /*
    138  * vga_putc (nee sput) has support for emulation of the 'ibmpc' termcap entry.
    139  * This is a bare-bones implementation of a bare-bones entry
    140  * One modification: Change li#24 to li#25 to reflect 25 lines
    141  * "ca" is the color/attributes value (left-shifted by 8)
    142  * or 0 if the current regular color for that screen is to be used.
    143  */
    144 void
    145 vga_putc(u_char c)
    146 {
    147 	struct screen *d = &screen;
    148 	u_short *base;
    149 	int i, j;
    150 	u_short *pp;
    151 
    152 	base = Crtat;
    153 
    154 	switch (d->state) {
    155 	case NORMAL:
    156 		switch (c) {
    157 		case 0x0:		/* Ignore pad characters */
    158 			return;
    159 
    160 		case 0x1B:
    161 			d->state = ESC;
    162 			break;
    163 
    164 		case '\t':
    165 			do {
    166 				wrtchar(d->color | ' ', d);
    167 			} while (d->row % 8);
    168 			break;
    169 
    170 		case '\b':  /* non-destructive backspace */
    171 			if (d->cp > base) {
    172 				d->cp--;
    173 				d->row--;
    174 				if (d->row < 0)
    175 					d->row += COL;	/* prev column */
    176 			}
    177 			break;
    178 
    179 		case '\n':
    180 			d->cp += COL;
    181 		case '\r':
    182 			d->cp -= d->row;
    183 			d->row = 0;
    184 			break;
    185 
    186 		case '\007':
    187 			break;
    188 
    189 		default:
    190 			if (d->so) {
    191 				wrtchar(d->color_so|(c<<8), d);
    192 			} else {
    193 				wrtchar(d->color | (c<<8), d);
    194 			}
    195 			if (d->row >= COL)
    196 				d->row = 0;
    197 			break;
    198 		}
    199 		break;
    200 
    201 	case EBRAC:
    202 		/*
    203 		 * In this state, the action at the end of the switch
    204 		 * on the character type is to go to NORMAL state,
    205 		 * and intermediate states do a return rather than break.
    206 		 */
    207 		switch (c) {
    208 		case 'm':
    209 			d->so = d->cx;
    210 			break;
    211 
    212 		case 'A': /* back one row */
    213 			if (d->cp >= base + COL)
    214 				d->cp -= COL;
    215 			break;
    216 
    217 		case 'B': /* down one row */
    218 			d->cp += COL;
    219 			break;
    220 
    221 		case 'C': /* right cursor */
    222 			d->cp++;
    223 			d->row++;
    224 			break;
    225 
    226 		case 'D': /* left cursor */
    227 			if (d->cp > base) {
    228 				d->cp--;
    229 				d->row--;
    230 				if (d->row < 0)
    231 					d->row += COL;	/* prev column ??? */
    232 			}
    233 			break;
    234 
    235 		case 'J': /* Clear to end of display */
    236 			fillw(d->color|(' '<<8), d->cp, base + COL * ROW - d->cp);
    237 			break;
    238 
    239 		case 'K': /* Clear to EOL */
    240 			fillw(d->color|(' '<<8), d->cp, COL - (d->cp - base) % COL);
    241 			break;
    242 
    243 		case 'H': /* Cursor move */
    244 			if (d->cx > ROW)
    245 				d->cx = ROW;
    246 			if (d->cy > COL)
    247 				d->cy = COL;
    248 			if (d->cx == 0 || d->cy == 0) {
    249 				d->cp = base;
    250 				d->row = 0;
    251 			} else {
    252 				d->cp = base + (d->cx - 1) * COL + d->cy - 1;
    253 				d->row = d->cy - 1;
    254 			}
    255 			break;
    256 
    257 		case '_': /* set cursor */
    258 			if (d->cx)
    259 				d->cx = 1;		/* block */
    260 			else
    261 				d->cx = 12;	/* underline */
    262 			outb(addr_6845, 10);
    263 			outb(addr_6845+1, d->cx);
    264 			outb(addr_6845, 11);
    265 			outb(addr_6845+1, 13);
    266 			break;
    267 
    268 		case ';': /* Switch params in cursor def */
    269 			d->accp = &d->cy;
    270 			return;
    271 
    272 		case '=': /* ESC[= color change */
    273 			d->state = EBRACEQ;
    274 			return;
    275 
    276 		case 'L':	/* Insert line */
    277 			i = (d->cp - base) / COL;
    278 			/* avoid deficiency of bcopy implementation */
    279 			pp = base + COL * (ROW-2);
    280 			for (j = ROW - 1 - i; j--; pp -= COL)
    281 				bcopy(pp, pp + COL, COL * CHR);
    282 			fillw(d->color|(' '<<8), base + i * COL, COL);
    283 			break;
    284 
    285 		case 'M':	/* Delete line */
    286 			i = (d->cp - base) / COL;
    287 			pp = base + i * COL;
    288 			bcopy(pp + COL, pp, (ROW-1 - i)*COL*CHR);
    289 			fillw(d->color|(' '<<8), base + COL * (ROW - 1), COL);
    290 			break;
    291 
    292 		default: /* Only numbers valid here */
    293 			if ((c >= '0') && (c <= '9')) {
    294 				*(d->accp) *= 10;
    295 				*(d->accp) += c - '0';
    296 				return;
    297 			} else
    298 				break;
    299 		}
    300 		d->state = NORMAL;
    301 		break;
    302 
    303 	case EBRACEQ: {
    304 		/*
    305 		 * In this state, the action at the end of the switch
    306 		 * on the character type is to go to NORMAL state,
    307 		 * and intermediate states do a return rather than break.
    308 		 */
    309 		u_char *colp;
    310 
    311 		/*
    312 		 * Set foreground/background color
    313 		 * for normal mode, standout mode
    314 		 * or kernel output.
    315 		 * Based on code from kentp@svmp03.
    316 		 */
    317 		switch (c) {
    318 		case 'F':
    319 			colp = ATTR_ADDR(d->color);
    320 	do_fg:
    321 			*colp = (*colp & 0xf0) | (d->cx);
    322 			break;
    323 
    324 		case 'G':
    325 			colp = ATTR_ADDR(d->color);
    326 	do_bg:
    327 			*colp = (*colp & 0xf) | (d->cx << 4);
    328 			break;
    329 
    330 		case 'H':
    331 			colp = ATTR_ADDR(d->color_so);
    332 			goto do_fg;
    333 
    334 		case 'I':
    335 			colp = ATTR_ADDR(d->color_so);
    336 			goto do_bg;
    337 
    338 		case 'S':
    339 			d->save_color = d->color;
    340 			d->save_color_so = d->color_so;
    341 			break;
    342 
    343 		case 'R':
    344 			d->color = d->save_color;
    345 			d->color_so = d->save_color_so;
    346 			break;
    347 
    348 		default: /* Only numbers valid here */
    349 			if ((c >= '0') && (c <= '9')) {
    350 				d->cx *= 10;
    351 				d->cx += c - '0';
    352 				return;
    353 			} else
    354 				break;
    355 		}
    356 		d->state = NORMAL;
    357 	    }
    358 	    break;
    359 
    360 	case ESC:
    361 		switch (c) {
    362 		case 'c':	/* Clear screen & home */
    363 			fillw(d->color|(' '<<8), base, COL * ROW);
    364 			d->cp = base;
    365 			d->row = 0;
    366 			d->state = NORMAL;
    367 			break;
    368 		case '[':	/* Start ESC [ sequence */
    369 			d->state = EBRAC;
    370 			d->cx = 0;
    371 			d->cy = 0;
    372 			d->accp = &d->cx;
    373 			break;
    374 		default: /* Invalid, clear state */
    375 			d->state = NORMAL;
    376 			break;
    377 		}
    378 		break;
    379 	}
    380 	if (d->cp >= base + (COL * ROW)) { /* scroll check */
    381 		bcopy(base + COL, base, COL * (ROW - 1) * CHR);
    382 		fillw(d->color|(' '<<8), base + COL * (ROW - 1), COL);
    383 		d->cp -= COL;
    384 	}
    385 	cursor();
    386 }
    387 
    388 void
    389 vga_puts(char *s)
    390 {
    391 	char c;
    392 	while (c = *s++)
    393 	{
    394 		vga_putc(c);
    395 	}
    396 }
    397 
    398 video_on()
    399 { /* Enable video */
    400 	outb(0x3C4, 0x01);
    401 	outb(0x3C5, inb(0x3C5)&~0x20);
    402 }
    403 
    404 video_off()
    405 { /* Disable video */
    406 	outb(0x3C4, 0x01);
    407 	outb(0x3C5, inb(0x3C5)|0x20);
    408 }
    409 
    410 vga_init(unsigned char *ISA_mem)
    411 {
    412 	struct screen *d = &screen;
    413 	bzero(d, sizeof(screen));
    414 	video_on();
    415 	d->cp = Crtat = (u_short *)&ISA_mem[0x0B8000];
    416 	addr_6845 = CGA_BASE;
    417 	initscreen();
    418 	fillw(pccolor|(' '<<8), d->cp, COL * ROW);
    419 	return (1);
    420 }
    421 #endif /* CONS_VGA */
    422