syscons.c revision 3680
1/*-
2 * Copyright (c) 1992-1994 S�ren Schmidt
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * William Jolitz and Don Ahn.
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 *    in this position and unchanged.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	$Id: syscons.c,v 1.68 1994/10/17 22:12:06 sos Exp $
39 */
40
41#include "sc.h"
42
43#if NSC > 0
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/conf.h>
48#include <sys/ioctl.h>
49#include <sys/proc.h>
50#include <sys/user.h>
51#include <sys/tty.h>
52#include <sys/uio.h>
53#include <sys/callout.h>
54#include <sys/kernel.h>
55#include <sys/syslog.h>
56#include <sys/errno.h>
57#include <sys/malloc.h>
58#include <machine/console.h>
59#include <machine/psl.h>
60#include <machine/frame.h>
61#include <machine/pc/display.h>
62#include <i386/isa/isa.h>
63#include <i386/isa/isa_device.h>
64#include <i386/isa/timerreg.h>
65#include <i386/isa/kbdtables.h>
66#include <i386/i386/cons.h>
67
68#if !defined(NCONS)
69#define NCONS 12
70#endif
71
72#if defined(HARDFONTS)
73#include <i386/isa/iso8859.font>
74#endif
75
76/* status flags */
77#define LOCK_KEY_MASK	0x0000F
78#define LED_MASK	0x00007
79#define UNKNOWN_MODE	0x00010
80#define KBD_RAW_MODE	0x00020
81#define SWITCH_WAIT_REL	0x00040
82#define SWITCH_WAIT_ACQ	0x00080
83
84/* video hardware memory addresses */
85#define VIDEOMEM	0x000A0000
86
87/* misc defines */
88#define MAX_ESC_PAR 	5
89#define	LOAD		1
90#define SAVE		0
91#define	COL		80
92#define	ROW		25
93#define BELL_DURATION	5
94#define BELL_PITCH	800
95#define TIMER_FREQ	1193182			/* should be in isa.h */
96#define CONSOLE_BUFSIZE 1024
97#define PCBURST		128
98#define FONT_8_LOADED	0x001
99#define FONT_14_LOADED	0x002
100#define FONT_16_LOADED	0x004
101
102/* defines related to hardware addresses */
103#define	MONO_BASE	0x3B4			/* crt controller base mono */
104#define	COLOR_BASE	0x3D4			/* crt controller base color */
105#define MISC		0x3C2			/* misc output register */
106#define ATC		IO_VGA+0x00		/* attribute controller */
107#define TSIDX		IO_VGA+0x04		/* timing sequencer idx */
108#define TSREG		IO_VGA+0x05		/* timing sequencer data */
109#define PIXMASK		IO_VGA+0x06		/* pixel write mask */
110#define PALRADR		IO_VGA+0x07		/* palette read address */
111#define PALWADR		IO_VGA+0x08		/* palette write address */
112#define PALDATA		IO_VGA+0x09		/* palette data register */
113#define GDCIDX		IO_VGA+0x0E		/* graph data controller idx */
114#define GDCREG		IO_VGA+0x0F		/* graph data controller data */
115
116/* special characters */
117#define cntlc	0x03
118#define cntld	0x04
119#define bs	0x08
120#define lf	0x0a
121#define cr	0x0d
122#define del	0x7f
123
124typedef struct term_stat {
125	int 		esc;			/* processing escape sequence */
126	int 		num_param;		/* # of parameters to ESC */
127	int	 	last_param;		/* last parameter # */
128	int 		param[MAX_ESC_PAR];	/* contains ESC parameters */
129	int 		cur_attr;		/* current attributes */
130	int 		std_attr;		/* normal attributes */
131	int 		rev_attr;		/* reverse attributes */
132} term_stat;
133
134typedef struct scr_stat {
135	u_short 	*crt_base;		/* address of screen memory */
136	u_short 	*scr_buf;		/* buffer when off screen */
137	u_short 	*crtat;			/* cursor address */
138	int 		xpos;			/* current X position */
139	int 		ypos;			/* current Y position */
140	int 		xsize;			/* X size */
141	int 		ysize;			/* Y size */
142	term_stat 	term;			/* terminal emulation stuff */
143	char		cursor_start;		/* cursor start line # */
144	char		cursor_end;		/* cursor end line # */
145	u_char		border;			/* border color */
146	u_short		bell_duration;
147	u_short		bell_pitch;
148	u_short 	status;			/* status (bitfield) */
149	u_short 	mode;			/* mode */
150	pid_t 		pid;			/* pid of controlling proc */
151	struct proc 	*proc;			/* proc* of controlling proc */
152	struct vt_mode 	smode;			/* switch mode */
153} scr_stat;
154
155typedef struct default_attr {
156	int             std_attr;               /* normal attributes */
157	int 		rev_attr;		/* reverse attributes */
158} default_attr;
159
160static default_attr user_default = {
161	(FG_LIGHTGREY | BG_BLACK) << 8,
162	(FG_BLACK | BG_LIGHTGREY) << 8
163};
164
165static default_attr kernel_default = {
166	(FG_WHITE | BG_BLACK) << 8,
167	(FG_BLACK | BG_LIGHTGREY) << 8
168};
169
170static	scr_stat	console[NCONS];
171static	scr_stat	*cur_console = &console[0];
172static	scr_stat	*new_scp, *old_scp;
173static	term_stat	kernel_console;
174static	default_attr	*current_default;
175static	int 		console_buffer_count;
176static	char 		console_buffer[CONSOLE_BUFSIZE];
177static	int		switch_in_progress = 0;
178static 	u_short	 	*crtat = 0;
179static	u_int		crtc_addr = MONO_BASE;
180static	char		crtc_vga = 0;
181static 	u_char		shfts = 0, ctls = 0, alts = 0, agrs = 0, metas = 0;
182static 	u_char		nlkcnt = 0, clkcnt = 0, slkcnt = 0, alkcnt = 0;
183static	char		*font_8 = NULL, *font_14 = NULL, *font_16 = NULL;
184static  int		fonts_loaded = 0;
185static	char		palette[3*256];
186static 	const u_int 	n_fkey_tab = sizeof(fkey_tab) / sizeof(*fkey_tab);
187static	int 		cur_cursor_pos = -1;
188static	char 		in_putc = 0;
189static	char	 	polling = 0;
190#if ASYNCH
191static  u_char		kbd_reply = 0;
192#endif
193static	int	 	delayed_next_scr;
194static	char		saved_console = -1;	/* saved console number	*/
195static	long		scrn_blank_time = 0;	/* screen saver timeout value */
196static	int		scrn_blanked = 0;	/* screen saver active flag */
197static	int		scrn_saver = 0;		/* screen saver routine */
198static	long 		scrn_time_stamp;
199static  u_char		scr_map[256];
200
201/* function prototypes */
202int pcprobe(struct isa_device *dev);
203int pcattach(struct isa_device *dev);
204int pcopen(dev_t dev, int flag, int mode, struct proc *p);
205int pcclose(dev_t dev, int flag, int mode, struct proc *p);
206int pcread(dev_t dev, struct uio *uio, int flag);
207int pcwrite(dev_t dev, struct uio *uio, int flag);
208int pcparam(struct tty *tp, struct termios *t);
209int pcioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p);
210void pcxint(dev_t dev);
211void pcstart(struct tty *tp);
212void pccnprobe(struct consdev *cp);
213void pccninit(struct consdev *cp);
214void pccnputc(dev_t dev, char c);
215int pccngetc(dev_t dev);
216void scintr(int unit);
217int pcmmap(dev_t dev, int offset, int nprot);
218u_int sgetc(int noblock);
219int getchar(void);
220static void scinit(void);
221static void scput(u_char c);
222static u_int scgetc(int noblock);
223static struct tty *get_tty_ptr(dev_t dev);
224static scr_stat *get_scr_stat(dev_t dev);
225static int get_scr_num();
226static void cursor_shape(int start, int end);
227static void get_cursor_shape(int *start, int *end);
228static void cursor_pos(int force);
229static void clear_screen(scr_stat *scp);
230static int switch_scr(u_int next_scr);
231static void exchange_scr(void);
232static void move_crsr(scr_stat *scp, int x, int y);
233static void move_up(u_short *s, u_short *d, u_int len);
234static void move_down(u_short *s, u_short *d, u_int len);
235static void scan_esc(scr_stat *scp, u_char c);
236static void ansi_put(scr_stat *scp, u_char c);
237static u_char *get_fstr(u_int c, u_int *len);
238static void update_leds(int which);
239static void kbd_wait(void);
240static void kbd_cmd(u_char command);
241static void set_mode(scr_stat *scp);
242static void set_border(int color);
243static void set_vgaregs(char *modetable);
244static void copy_font(int direction, int segment, int size, char* font);
245static void save_palette(void);
246static void load_palette(void);
247
248/* available screen savers */
249static void none_saver(int test);
250static void blank_saver(int test);
251static void fade_saver(int test);
252static void star_saver(int test);
253static void snake_saver(int test);
254static void green_saver(int test);
255
256static const struct {
257	char	*name;
258	void	(*routine)();
259} screen_savers[] = {
260	{ "none",	none_saver },	/* 0 */
261	{ "blank",	blank_saver },	/* 1 */
262	{ "fade",	fade_saver },	/* 2 */
263	{ "star",	star_saver },	/* 3 */
264	{ "snake",	snake_saver },	/* 4 */
265	{ "green",	green_saver },	/* 5 */
266};
267#define SCRN_SAVER(arg)	(*screen_savers[scrn_saver].routine)(arg)
268#define NUM_SCRN_SAVERS	(sizeof(screen_savers) / sizeof(screen_savers[0]))
269
270/* OS specific stuff */
271#if 0
272#define VIRTUAL_TTY(x)	(pccons[x] = ttymalloc(pccons[x]))
273#define	CONSOLE_TTY	(pccons[NCONS] = ttymalloc(pccons[NCONS]))
274struct	tty 		*pccons[NCONS+1];
275#else
276#define VIRTUAL_TTY(x)	&pccons[x]
277#define	CONSOLE_TTY	&pccons[NCONS]
278struct	tty 		pccons[NCONS+1];
279#endif
280#define	timeout_t	timeout_func_t
281#define	MONO_BUF	(KERNBASE+0xB0000)
282#define	CGA_BUF		(KERNBASE+0xB8000)
283u_short			*Crtat = (u_short *)MONO_BUF;
284void 	consinit(void) 	{scinit();}
285extern  char 		*video_mode_ptr;
286
287struct	isa_driver scdriver = {
288	pcprobe, pcattach, "sc", 1
289};
290
291int
292pcprobe(struct isa_device *dev)
293{
294	int i, retries = 5;
295	unsigned char val;
296
297	/* Enable interrupts and keyboard controller */
298	kbd_wait();
299	outb(KB_STAT, KB_WRITE);
300	kbd_wait();
301	outb(KB_DATA, KB_MODE);
302
303	/* flush any noise in the buffer */
304	while (inb(KB_STAT) & KB_BUF_FULL) {
305		DELAY(10);
306		(void) inb(KB_DATA);
307	}
308
309	/* Reset keyboard hardware */
310	while (retries--) {
311		kbd_wait();
312		outb(KB_DATA, KB_RESET);
313		for (i=0; i<100000; i++) {
314			DELAY(10);
315			val = inb(KB_DATA);
316			if (val == KB_ACK || val == KB_ECHO)
317				goto gotres;
318			if (val == KB_RESEND)
319				break;
320		}
321	}
322gotres:
323	if (!retries)
324		printf("scprobe: keyboard won't accept RESET command\n");
325	else {
326gotack:
327		DELAY(10);
328		while ((inb(KB_STAT) & KB_BUF_FULL) == 0) DELAY(10);
329		DELAY(10);
330		val = inb(KB_DATA);
331		if (val == KB_ACK)
332			goto gotack;
333		if (val != KB_RESET_DONE)
334			printf("scprobe: keyboard RESET failed %02x\n", val);
335	}
336	return (IO_KBDSIZE);
337}
338
339int
340pcattach(struct isa_device *dev)
341{
342	int i;
343	struct scr_stat *scp;
344
345	printf("sc%d: ", dev->id_unit);
346	if (crtc_vga)
347		if (crtc_addr == MONO_BASE)
348			printf("VGA mono");
349		else
350			printf("VGA color");
351	else
352		if (crtc_addr == MONO_BASE)
353			printf("MDA/hercules");
354		else
355			printf("CGA/EGA");
356	if (NCONS > 1)
357		printf(" <%d virtual consoles>\n", NCONS);
358	else
359		printf("\n");
360	if (crtc_vga) {
361#if defined(HARDFONTS)
362		font_8 = font_8x8;
363		font_14 = font_8x14;
364		font_16 = font_8x16;
365		fonts_loaded = FONT_8_LOADED|FONT_14_LOADED|FONT_16_LOADED;
366		copy_font(LOAD, 1, 8, font_8);
367		copy_font(LOAD, 2, 14, font_14);
368		copy_font(LOAD, 0, 16, font_16);
369#else
370		font_8 = (char *)malloc(8*256, M_DEVBUF, M_NOWAIT);
371		font_14 = (char *)malloc(14*256, M_DEVBUF, M_NOWAIT);
372		font_16 = (char *)malloc(16*256, M_DEVBUF, M_NOWAIT);
373		copy_font(SAVE, 0, 16, font_16);
374		fonts_loaded = FONT_16_LOADED;
375#endif
376		save_palette();
377	}
378	for (i = 0; i < NCONS; i++) {
379		scp = &console[i];
380		scp->scr_buf = (u_short *)malloc(COL*ROW*2, M_DEVBUF, M_NOWAIT);
381		if (i > 0) {
382			scp->crt_base = scp->crtat = scp->scr_buf;
383			clear_screen(scp);
384		}
385	}
386	/* get cursor going */
387	cursor_pos(1);
388	update_leds(console[0].status);
389	return 0;
390}
391
392static struct tty
393*get_tty_ptr(dev_t dev)
394{
395	int unit = minor(dev);
396
397	if (unit > NCONS)
398		return(NULL);
399	if (unit == NCONS)
400		return(CONSOLE_TTY);
401	return(VIRTUAL_TTY(unit));
402}
403
404static scr_stat
405*get_scr_stat(dev_t dev)
406{
407	int unit = minor(dev);
408
409	if (unit > NCONS)
410		return(NULL);
411	if (unit == NCONS)
412		return(&console[0]);
413	return(&console[unit]);
414}
415
416static int
417get_scr_num()
418{
419	int i = 0;
420
421	while ((i < NCONS) && (cur_console != &console[i])) i++;
422	return i < NCONS ? i : 0;
423}
424
425int
426pcopen(dev_t dev, int flag, int mode, struct proc *p)
427{
428	struct tty *tp = get_tty_ptr(dev);
429
430	if (!tp)
431		return(ENXIO);
432
433	tp->t_oproc = pcstart;
434	tp->t_param = pcparam;
435	tp->t_dev = dev;
436	if (!(tp->t_state & TS_ISOPEN)) {
437		ttychars(tp);
438		tp->t_iflag = TTYDEF_IFLAG;
439		tp->t_oflag = TTYDEF_OFLAG;
440		tp->t_cflag = TTYDEF_CFLAG;
441		tp->t_lflag = TTYDEF_LFLAG;
442		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
443		pcparam(tp, &tp->t_termios);
444		ttsetwater(tp);
445	} else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
446		return(EBUSY);
447	tp->t_state |= TS_CARR_ON;
448	tp->t_cflag |= CLOCAL;
449	return((*linesw[tp->t_line].l_open)(dev, tp));
450}
451
452int
453pcclose(dev_t dev, int flag, int mode, struct proc *p)
454{
455	struct tty *tp = get_tty_ptr(dev);
456	struct scr_stat *scp;
457
458	if (!tp)
459		return(ENXIO);
460	if (minor(dev) < NCONS) {
461		scp = get_scr_stat(tp->t_dev);
462		if (scp->status & SWITCH_WAIT_ACQ)
463			wakeup((caddr_t)&scp->smode);
464		scp->pid = 0;
465		scp->proc = NULL;
466		scp->smode.mode = VT_AUTO;
467	}
468	(*linesw[tp->t_line].l_close)(tp, flag);
469	ttyclose(tp);
470	return(0);
471}
472
473int
474pcread(dev_t dev, struct uio *uio, int flag)
475{
476	struct tty *tp = get_tty_ptr(dev);
477
478	if (!tp)
479		return(ENXIO);
480	return((*linesw[tp->t_line].l_read)(tp, uio, flag));
481}
482
483int
484pcwrite(dev_t dev, struct uio *uio, int flag)
485{
486	struct tty *tp = get_tty_ptr(dev);
487
488	if (!tp)
489		return(ENXIO);
490	return((*linesw[tp->t_line].l_write)(tp, uio, flag));
491}
492
493void
494scintr(int unit)
495{
496	static struct tty *cur_tty;
497	int c, len;
498	u_char *cp;
499
500	/* make screensaver happy */
501	scrn_time_stamp = time.tv_sec;
502	if (scrn_blanked)
503		SCRN_SAVER(0);
504
505	c = scgetc(1);
506
507	cur_tty = VIRTUAL_TTY(get_scr_num());
508	if (!(cur_tty->t_state & TS_ISOPEN))
509		cur_tty = CONSOLE_TTY;
510
511	if (!(cur_tty->t_state & TS_ISOPEN) || polling)
512		return;
513
514	switch (c & 0xff00) {
515	case 0x0000: /* normal key */
516		(*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
517		break;
518	case NOKEY:	/* nothing there */
519		break;
520	case FKEY:	/* function key, return string */
521		if (cp = get_fstr((u_int)c, (u_int *)&len)) {
522			while (len-- >  0)
523				(*linesw[cur_tty->t_line].l_rint)
524					(*cp++ & 0xFF, cur_tty);
525		}
526		break;
527	case MKEY:	/* meta is active, prepend ESC */
528		(*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
529		(*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
530		break;
531	}
532}
533
534int
535pcparam(struct tty *tp, struct termios *t)
536{
537	int cflag = t->c_cflag;
538
539	/* and copy to tty */
540	tp->t_ispeed = t->c_ispeed;
541	tp->t_ospeed = t->c_ospeed;
542	tp->t_cflag = cflag;
543	return 0;
544}
545
546int
547pcioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
548{
549	int i, error;
550	struct tty *tp;
551	struct trapframe *fp;
552	scr_stat *scp;
553
554	tp = get_tty_ptr(dev);
555	if (!tp)
556		return ENXIO;
557	scp = get_scr_stat(tp->t_dev);
558
559	switch (cmd) {	/* process console hardware related ioctl's */
560
561	case GIO_ATTR:		/* get current attributes */
562		*(int*)data = scp->term.cur_attr;
563		return 0;
564
565	case GIO_COLOR:		/* is this a color console ? */
566		if (crtc_addr == COLOR_BASE)
567			*(int*)data = 1;
568		else
569			*(int*)data = 0;
570		return 0;
571
572	case CONS_CURRENT:	/* get current adapter type */
573		if (crtc_vga)
574			*(int*)data = KD_VGA;
575		else
576			if (crtc_addr == MONO_BASE)
577				*(int*)data = KD_MONO;
578			else
579				*(int*)data = KD_CGA;
580		return 0;
581
582	case CONS_GET:		/* get current video mode */
583		*(int*)data = scp->mode;
584		return 0;
585
586	case CONS_BLANKTIME:	/* set screen saver timeout (0 = no saver) */
587		scrn_blank_time = *(int*)data;
588		return 0;
589
590#define	SAVER(p) ((ssaver_t *)(p))
591	case CONS_SSAVER:	/* set screen saver */
592		if (SAVER(data)->num < 0
593		    || SAVER(data)->num >= NUM_SCRN_SAVERS)
594			return EIO;
595		SCRN_SAVER(0);
596		scrn_saver = SAVER(data)->num;
597		scrn_blank_time = SAVER(data)->time;
598		return 0;
599
600	case CONS_GSAVER:	/* get screen saver info */
601		if (SAVER(data)->num < 0)
602			SAVER(data)->num = scrn_saver;
603		else if (SAVER(data)->num >= NUM_SCRN_SAVERS)
604			return EIO;
605		SAVER(data)->time = scrn_blank_time;
606		strcpy(SAVER(data)->name, screen_savers[SAVER(data)->num].name);
607		return 0;
608
609	case CONS_GETINFO:	/* get current (virtual) console info */
610	{
611		vid_info_t *ptr = (vid_info_t*)data;
612		if (ptr->size == sizeof(struct vid_info)) {
613			ptr->m_num = get_scr_num();
614			ptr->mv_col = scp->xpos;
615			ptr->mv_row = scp->ypos;
616			ptr->mv_csz = scp->xsize;
617			ptr->mv_rsz = scp->ysize;
618			ptr->mv_norm.fore = (scp->term.std_attr & 0x0f00)>>8;
619			ptr->mv_norm.back = (scp->term.std_attr & 0xf000)>>12;
620			ptr->mv_rev.fore = (scp->term.rev_attr & 0x0f00)>>8;
621			ptr->mv_rev.back = (scp->term.rev_attr & 0xf000)>>12;
622			ptr->mv_grfc.fore = 0;		/* not supported */
623			ptr->mv_grfc.back = 0;		/* not supported */
624			ptr->mv_ovscan = scp->border;
625			ptr->mk_keylock = scp->status & LOCK_KEY_MASK;
626			return 0;
627		}
628		return EINVAL;
629	}
630
631	case CONS_GETVERS:	/* get version number */
632		*(int*)data = 0x103;	/* version 1.3 */
633		return 0;
634
635        case SW_VGA_C40x25:  case SW_VGA_C80x25:	/* VGA TEXT MODES */
636        case SW_VGA_M80x25:
637        case SW_VGA_C80x50:  case SW_VGA_M80x50:
638        case SW_B40x25:     case SW_C40x25:
639        case SW_B80x25:     case SW_C80x25:
640        case SW_ENH_B40x25: case SW_ENH_C40x25:
641        case SW_ENH_B80x25: case SW_ENH_C80x25:
642        case SW_ENH_B80x43: case SW_ENH_C80x43:
643
644        	if (!crtc_vga)
645        		return ENXIO;
646		cmd &= 0xFF;
647		i = cmd < M_VGA_C80x50 ?
648		    *(video_mode_ptr + (cmd*64) + 2) : 0x08;
649		switch (i) {
650		default:
651		case 0x08:
652			if (!(fonts_loaded & FONT_8_LOADED))
653				return EINVAL;
654			break;
655		case 0x0E:
656			if (!(fonts_loaded & FONT_14_LOADED))
657				return EINVAL;
658			break;
659		case 0x10:
660			if (!(fonts_loaded & FONT_16_LOADED))
661				return EINVAL;
662			break;
663		}
664		scp->mode = cmd;
665            	scp->status &= ~UNKNOWN_MODE;	/* text mode */
666		if (scp->mode < M_VGA_C80x50) {
667            		scp->xsize = *(video_mode_ptr + (scp->mode*64));
668            		scp->ysize = *(video_mode_ptr + (scp->mode*64) + 1) + 1;
669		}
670		else switch (scp->mode) {
671			case M_VGA_C80x50: case M_VGA_M80x50:
672				scp->xsize = 80;
673				scp->ysize = 50;
674				break;
675        		case M_ENH_B80x43: case M_ENH_C80x43:
676				scp->xsize = 80;
677				scp->ysize = 43;
678				break;
679		}
680		free(scp->scr_buf, M_DEVBUF);
681		scp->scr_buf = (u_short *)malloc(scp->xsize * scp->ysize * 2,
682					     M_DEVBUF, M_NOWAIT);
683		if (scp == cur_console)
684            		set_mode(scp);
685		else
686			scp->crt_base = scp->scr_buf;
687            	clear_screen(scp);
688		if (tp->t_winsize.ws_col != scp->xsize
689		    || tp->t_winsize.ws_row != scp->ysize) {
690			tp->t_winsize.ws_col = scp->xsize;
691			tp->t_winsize.ws_row = scp->ysize;
692			pgsignal(tp->t_pgrp, SIGWINCH, 1);
693		}
694            	return 0;
695
696        /* GRAPHICS MODES */
697        case SW_BG320:      case SW_CG320:      case SW_BG640:
698        case SW_CG320_D:    case SW_CG640_E:
699        case SW_CG640x350:  case SW_ENH_CG640:
700        case SW_BG640x480:  case SW_CG640x480:  case SW_VGA_CG320:
701
702	    	scp->mode = cmd & 0xFF;
703            	scp->status |= UNKNOWN_MODE;	/* graphics mode */
704		if (scp == cur_console)
705            		set_mode(scp);
706            	/* clear_graphics();*/
707            	return 0;
708
709	case VT_SETMODE:	/* set screen switcher mode */
710		bcopy(data, &scp->smode, sizeof(struct vt_mode));
711		if (scp->smode.mode == VT_PROCESS) {
712			scp->proc = p;
713			scp->pid = scp->proc->p_pid;
714		}
715		return 0;
716
717	case VT_GETMODE:	/* get screen switcher mode */
718		bcopy(&scp->smode, data, sizeof(struct vt_mode));
719		return 0;
720
721	case VT_RELDISP:	/* screen switcher ioctl */
722		switch(*data) {
723		case VT_FALSE:	/* user refuses to release screen, abort */
724			if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
725				old_scp->status &= ~SWITCH_WAIT_REL;
726				switch_in_progress = 0;
727				return 0;
728			}
729			return EINVAL;
730
731		case VT_TRUE:	/* user has released screen, go on */
732			if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
733				scp->status &= ~SWITCH_WAIT_REL;
734				exchange_scr();
735				if (new_scp->smode.mode == VT_PROCESS) {
736					new_scp->status |= SWITCH_WAIT_ACQ;
737					psignal(new_scp->proc,
738						new_scp->smode.acqsig);
739				}
740				else
741					switch_in_progress = 0;
742				return 0;
743			}
744			return EINVAL;
745
746		case VT_ACKACQ:	/* acquire acknowledged, switch completed */
747			if (scp == new_scp && (scp->status & SWITCH_WAIT_ACQ)) {
748				scp->status &= ~SWITCH_WAIT_ACQ;
749				switch_in_progress = 0;
750				return 0;
751			}
752			return EINVAL;
753
754		default:
755			return EINVAL;
756		}
757		/* NOT REACHED */
758
759	case VT_OPENQRY:	/* return free virtual console */
760 		for (i = 0; i < NCONS; i++) {
761			tp = VIRTUAL_TTY(i);
762 			if (!(tp->t_state & TS_ISOPEN)) {
763 				*data = i + 1;
764 				return 0;
765 			}
766		}
767 		return EINVAL;
768
769	case VT_ACTIVATE:	/* switch to screen *data */
770		return switch_scr((*data) - 1);
771
772	case VT_WAITACTIVE:	/* wait for switch to occur */
773		if (*data > NCONS)
774			return EINVAL;
775		if (minor(dev) == (*data) - 1)
776			return 0;
777		if (*data == 0) {
778			if (scp == cur_console)
779				return 0;
780			while ((error=tsleep((caddr_t)&scp->smode,
781			    	PZERO|PCATCH, "waitvt", 0)) == ERESTART) ;
782		}
783		else
784			while ((error=tsleep(
785      				(caddr_t)&console[*(data-1)].smode,
786			    	PZERO|PCATCH, "waitvt", 0)) == ERESTART) ;
787		return error;
788
789	case VT_GETACTIVE:
790		*data = get_scr_num()+1;
791		return 0;
792
793	case KDENABIO:		/* allow io operations */
794	 	fp = (struct trapframe *)p->p_md.md_regs;
795	 	fp->tf_eflags |= PSL_IOPL;
796		return 0;
797
798	case KDDISABIO:		/* disallow io operations (default) */
799	 	fp = (struct trapframe *)p->p_md.md_regs;
800	 	fp->tf_eflags &= ~PSL_IOPL;
801	 	return 0;
802
803        case KDSETMODE:		/* set current mode of this (virtual) console */
804		switch (*data) {
805		case KD_TEXT:	/* switch to TEXT (known) mode */
806				/* restore fonts & palette ! */
807			if (crtc_vga) {
808				if (fonts_loaded & FONT_16_LOADED)
809					copy_font(LOAD, 0, 16, font_16);
810				if (fonts_loaded & FONT_8_LOADED)
811					copy_font(LOAD, 1, 8, font_8);
812				if (fonts_loaded & FONT_14_LOADED)
813					copy_font(LOAD, 2, 14, font_14);
814				load_palette();
815			}
816			/* FALL THROUGH */
817
818		case KD_TEXT1:	/* switch to TEXT (known) mode */
819				/* no restore fonts & palette */
820			scp->status &= ~UNKNOWN_MODE;
821			set_mode(scp);
822			clear_screen(scp);
823			return 0;
824
825		case KD_GRAPHICS:/* switch to GRAPHICS (unknown) mode */
826			scp->status |= UNKNOWN_MODE;
827			return 0;
828		default:
829			return EINVAL;
830		}
831		/* NOT REACHED */
832
833	case KDGETMODE:		/* get current mode of this (virtual) console */
834		*data = (scp->status & UNKNOWN_MODE) ? KD_GRAPHICS : KD_TEXT;
835		return 0;
836
837	case KDSBORDER:		/* set border color of this (virtual) console */
838		if (!crtc_vga)
839			return ENXIO;
840		scp->border = *data;
841		if (scp == cur_console)
842			set_border(scp->border);
843		return 0;
844
845	case KDSKBSTATE:	/* set keyboard state (locks) */
846		if (*data >= 0 && *data <= LOCK_KEY_MASK) {
847			scp->status &= ~LOCK_KEY_MASK;
848			scp->status |= *data;
849			if (scp == cur_console)
850				update_leds(scp->status);
851			return 0;
852		}
853		return EINVAL;
854
855	case KDGKBSTATE:	/* get keyboard state (locks) */
856		*data = scp->status & LOCK_KEY_MASK;
857		return 0;
858
859	case KDSETRAD:		/* set keyboard repeat & delay rates */
860		if (*data & 0x80)
861			return EINVAL;
862		i = spltty();
863		kbd_cmd(KB_SETRAD);
864		kbd_cmd(*data);
865		splx(i);
866		return 0;
867
868	case KDSKBMODE:		/* set keyboard mode */
869		switch (*data) {
870		case K_RAW:	/* switch to RAW scancode mode */
871			scp->status |= KBD_RAW_MODE;
872			return 0;
873
874		case K_XLATE:	/* switch to XLT ascii mode */
875			if (scp == cur_console && scp->status == KBD_RAW_MODE)
876				shfts = ctls = alts = agrs = metas = 0;
877			scp->status &= ~KBD_RAW_MODE;
878			return 0;
879		default:
880			return EINVAL;
881		}
882		/* NOT REACHED */
883
884	case KDGKBMODE:		/* get keyboard mode */
885		*data = (scp->status & KBD_RAW_MODE) ? K_RAW : K_XLATE;
886		return 0;
887
888	case KDMKTONE:		/* sound the bell */
889		if (scp == cur_console)
890			sysbeep(scp->bell_pitch, scp->bell_duration);
891		return 0;
892
893	case KIOCSOUND:		/* make tone (*data) hz */
894		if (scp == cur_console) {
895			if (*(int*)data) {
896			int pitch = TIMER_FREQ/(*(int*)data);
897				/* set command for counter 2, 2 byte write */
898				if (acquire_timer2(TIMER_16BIT|TIMER_SQWAVE)) {
899					return EBUSY;
900				}
901				/* set pitch */
902				outb(TIMER_CNTR2, pitch);
903				outb(TIMER_CNTR2, (pitch>>8));
904				/* enable counter 2 output to speaker */
905				outb(IO_PPI, inb(IO_PPI) | 3);
906			}
907			else {
908				/* disable counter 2 output to speaker */
909				outb(IO_PPI, inb(IO_PPI) & 0xFC);
910				release_timer2();
911			}
912		}
913		return 0;
914
915	case KDGKBTYPE:		/* get keyboard type */
916		*data = 0;	/* type not known (yet) */
917		return 0;
918
919	case KDSETLED:		/* set keyboard LED status */
920		if (*data >= 0 && *data <= LED_MASK) {
921			scp->status &= ~LED_MASK;
922			scp->status |= *data;
923			if (scp == cur_console)
924				update_leds(scp->status);
925			return 0;
926		}
927		return EINVAL;
928
929	case KDGETLED:		/* get keyboard LED status */
930		*data = scp->status & LED_MASK;
931		return 0;
932
933	case GETFKEY:		/* get functionkey string */
934		if (*(u_short*)data < n_fkey_tab) {
935		 	fkeyarg_t *ptr = (fkeyarg_t*)data;
936			bcopy(&fkey_tab[ptr->keynum].str,
937			      ptr->keydef,
938			      fkey_tab[ptr->keynum].len);
939			ptr->flen = fkey_tab[ptr->keynum].len;
940			return 0;
941		}
942		else
943			return EINVAL;
944
945	case SETFKEY:		/* set functionkey string */
946		if (*(u_short*)data < n_fkey_tab) {
947		 	fkeyarg_t *ptr = (fkeyarg_t*)data;
948			bcopy(ptr->keydef,
949			      &fkey_tab[ptr->keynum].str,
950			      min(ptr->flen, MAXFK));
951			fkey_tab[ptr->keynum].len = min(ptr->flen, MAXFK);
952			return 0;
953		}
954		else
955			return EINVAL;
956
957	case GIO_SCRNMAP: 	/* get output translation table */
958		bcopy(&scr_map, data, sizeof(scr_map));
959		return 0;
960
961	case PIO_SCRNMAP:	/* set output translation table */
962		bcopy(data, &scr_map, sizeof(scr_map));
963		return 0;
964
965	case GIO_KEYMAP: 	/* get keyboard translation table */
966		bcopy(&key_map, data, sizeof(key_map));
967		return 0;
968
969	case PIO_KEYMAP:	/* set keyboard translation table */
970		bcopy(data, &key_map, sizeof(key_map));
971		return 0;
972
973	case PIO_FONT8x8:	/* set 8x8 dot font */
974		if (!crtc_vga)
975			return ENXIO;
976		bcopy(data, font_8, 8*256);
977		fonts_loaded |= FONT_8_LOADED;
978		copy_font(LOAD, 1, 8, font_8);
979		return 0;
980
981	case GIO_FONT8x8:	/* get 8x8 dot font */
982		if (!crtc_vga)
983			return ENXIO;
984		if (fonts_loaded & FONT_8_LOADED) {
985			bcopy(font_8, data, 8*256);
986			return 0;
987		}
988		else
989			return ENXIO;
990
991	case PIO_FONT8x14:	/* set 8x14 dot font */
992		if (!crtc_vga)
993			return ENXIO;
994		bcopy(data, font_14, 14*256);
995		fonts_loaded |= FONT_14_LOADED;
996		copy_font(LOAD, 2, 14, font_14);
997		return 0;
998
999	case GIO_FONT8x14:	/* get 8x14 dot font */
1000		if (!crtc_vga)
1001			return ENXIO;
1002		if (fonts_loaded & FONT_14_LOADED) {
1003			bcopy(font_14, data, 14*256);
1004			return 0;
1005		}
1006		else
1007			return ENXIO;
1008
1009	case PIO_FONT8x16:	/* set 8x16 dot font */
1010		if (!crtc_vga)
1011			return ENXIO;
1012		bcopy(data, font_16, 16*256);
1013		fonts_loaded |= FONT_16_LOADED;
1014		copy_font(LOAD, 0, 16, font_16);
1015		return 0;
1016
1017	case GIO_FONT8x16:	/* get 8x16 dot font */
1018		if (!crtc_vga)
1019			return ENXIO;
1020		if (fonts_loaded & FONT_16_LOADED) {
1021			bcopy(font_16, data, 16*256);
1022			return 0;
1023		}
1024		else
1025			return ENXIO;
1026
1027	case CONSOLE_X_MODE_ON:	/* just to be compatible */
1028		if (saved_console < 0) {
1029			saved_console = get_scr_num();
1030			switch_scr(minor(dev));
1031	 		fp = (struct trapframe *)p->p_md.md_regs;
1032	 		fp->tf_eflags |= PSL_IOPL;
1033			scp->status |= UNKNOWN_MODE;
1034			scp->status |= KBD_RAW_MODE;
1035			return 0;
1036		}
1037		return EAGAIN;
1038
1039	case CONSOLE_X_MODE_OFF:/* just to be compatible */
1040	 	fp = (struct trapframe *)p->p_md.md_regs;
1041	 	fp->tf_eflags &= ~PSL_IOPL;
1042		if (crtc_vga) {
1043			if (fonts_loaded & FONT_16_LOADED)
1044				copy_font(LOAD, 0, 16, font_16);
1045			if (fonts_loaded & FONT_8_LOADED)
1046				copy_font(LOAD, 1, 8, font_8);
1047			if (fonts_loaded & FONT_14_LOADED)
1048				copy_font(LOAD, 2, 14, font_14);
1049			load_palette();
1050		}
1051		scp->status &= ~UNKNOWN_MODE;
1052		set_mode(scp);
1053		clear_screen(scp);
1054		scp->status &= ~KBD_RAW_MODE;
1055		switch_scr(saved_console);
1056		saved_console = -1;
1057		return 0;
1058
1059	 case CONSOLE_X_BELL:	/* more compatibility */
1060                /*
1061                 * if set, data is a pointer to a length 2 array of
1062                 * integers. data[0] is the pitch in Hz and data[1]
1063                 * is the duration in msec.
1064                 */
1065                if (data)
1066	    		sysbeep(TIMER_FREQ/((int*)data)[0],
1067				((int*)data)[1]*hz/1000);
1068                else
1069			sysbeep(scp->bell_pitch, scp->bell_duration);
1070                return 0;
1071
1072	default:
1073		break;
1074	}
1075
1076	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1077	if (error >= 0)
1078		return(error);
1079	error = ttioctl(tp, cmd, data, flag);
1080	if (error >= 0)
1081		return(error);
1082	return(ENOTTY);
1083}
1084
1085void
1086pcxint(dev_t dev)
1087{
1088	struct tty *tp = get_tty_ptr(dev);
1089
1090	if (!tp)
1091		return;
1092	tp->t_state &= ~TS_BUSY;
1093	if (tp->t_line)
1094		(*linesw[tp->t_line].l_start)(tp);
1095	else
1096		pcstart(tp);
1097}
1098
1099void
1100pcstart(struct tty *tp)
1101{
1102	struct clist *rbp;
1103	int i, s, len;
1104	u_char buf[PCBURST];
1105	scr_stat *scp = get_scr_stat(tp->t_dev);
1106
1107	if (scp->status & SLKED)
1108		return;
1109	s = spltty();
1110	if (!(tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))) {
1111		tp->t_state |= TS_BUSY;
1112		splx(s);
1113		rbp = &tp->t_outq;
1114		while (rbp->c_cc) {
1115			len = q_to_b(rbp, buf, PCBURST);
1116			for (i=0; i<len; i++)
1117				if (buf[i]) ansi_put(scp, buf[i]);
1118		}
1119		s = spltty();
1120		tp->t_state &= ~TS_BUSY;
1121#if 0
1122		if (rbp->c_cc) {
1123			tp->t_state |= TS_TIMEOUT;
1124			timeout((timeout_t)ttrstrt, (caddr_t)tp, 1);
1125		}
1126#endif
1127		if (rbp->c_cc <= tp->t_lowat) {
1128			if (tp->t_state & TS_ASLEEP) {
1129				tp->t_state &= ~TS_ASLEEP;
1130				wakeup((caddr_t)rbp);
1131			}
1132			selwakeup(&tp->t_wsel);
1133		}
1134	}
1135	splx(s);
1136}
1137
1138void
1139pccnprobe(struct consdev *cp)
1140{
1141	int maj;
1142
1143	/* locate the major number */
1144	for (maj = 0; maj < nchrdev; maj++)
1145		if ((void*)cdevsw[maj].d_open == (void*)pcopen)
1146			break;
1147
1148	/* initialize required fields */
1149	cp->cn_dev = makedev(maj, NCONS);
1150	cp->cn_pri = CN_INTERNAL;
1151}
1152
1153void
1154pccninit(struct consdev *cp)
1155{
1156	scinit();
1157}
1158
1159void
1160pccnputc(dev_t dev, char c)
1161{
1162	if (c == '\n')
1163		scput('\r');
1164	scput(c);
1165	if (cur_console == &console[0]) {
1166	int 	pos = cur_console->crtat - cur_console->crt_base;
1167		if (pos != cur_cursor_pos) {
1168			cur_cursor_pos = pos;
1169			outb(crtc_addr,14);
1170			outb(crtc_addr+1,pos >> 8);
1171			outb(crtc_addr,15);
1172			outb(crtc_addr+1,pos&0xff);
1173		}
1174	}
1175}
1176
1177int
1178pccngetc(dev_t dev)
1179{
1180	int s = spltty();		/* block scintr while we poll */
1181	int c = scgetc(0);
1182	splx(s);
1183	if (c == '\r') c = '\n';
1184	return(c);
1185}
1186
1187static void
1188none_saver(int test)
1189{
1190}
1191
1192static void
1193fade_saver(int test)
1194{
1195	static int count = 0;
1196	int i;
1197
1198	if (test) {
1199		scrn_blanked = 1;
1200		if (count < 64) {
1201  			outb(PIXMASK, 0xFF);		/* no pixelmask */
1202  			outb(PALWADR, 0x00);
1203    			outb(PALDATA, 0);
1204    			outb(PALDATA, 0);
1205    			outb(PALDATA, 0);
1206  			for (i = 3; i < 768; i++) {
1207  				if (palette[i] - count > 15)
1208    					outb(PALDATA, palette[i]-count);
1209				else
1210    					outb(PALDATA, 15);
1211			}
1212			inb(crtc_addr+6);		/* reset flip/flop */
1213			outb(ATC, 0x20);		/* enable palette */
1214			count++;
1215		}
1216	}
1217	else {
1218		count = scrn_blanked = 0;
1219		load_palette();
1220	}
1221}
1222
1223static void
1224blank_saver(int test)
1225{
1226	u_char val;
1227	if (test) {
1228		scrn_blanked = 1;
1229  		outb(TSIDX, 0x01); val = inb(TSREG);
1230		outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
1231	}
1232	else {
1233		scrn_blanked = 0;
1234  		outb(TSIDX, 0x01); val = inb(TSREG);
1235		outb(TSIDX, 0x01); outb(TSREG, val & 0xDF);
1236	}
1237}
1238
1239static void
1240green_saver(int test)
1241{
1242	u_char val;
1243	if (test) {
1244		scrn_blanked = 1;
1245  		outb(TSIDX, 0x01); val = inb(TSREG);
1246		outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
1247		outb(crtc_addr, 0x17); val = inb(crtc_addr + 1);
1248		outb(crtc_addr + 1, val & ~0x80);
1249	}
1250	else {
1251		scrn_blanked = 0;
1252  		outb(TSIDX, 0x01); val = inb(TSREG);
1253		outb(TSIDX, 0x01); outb(TSREG, val & 0xDF);
1254                outb(crtc_addr, 0x17); val = inb(crtc_addr + 1);
1255                outb(crtc_addr + 1, val | 0x80);
1256	}
1257}
1258
1259#define NUM_STARS	50
1260
1261/*
1262 * Alternate saver that got its inspiration from a well known utility
1263 * package for an inferior^H^H^H^H^H^Hfamous OS.
1264 */
1265static void
1266star_saver(int test)
1267{
1268	scr_stat	*scp = cur_console;
1269	int		cell, i;
1270	char 		pattern[] = {"...........++++***   "};
1271	char		colors[] = {FG_DARKGREY, FG_LIGHTGREY,
1272				    FG_WHITE, FG_LIGHTCYAN};
1273	static u_short 	stars[NUM_STARS][2];
1274
1275	if (test) {
1276		if (!scrn_blanked) {
1277			bcopy(Crtat, scp->scr_buf,
1278			      scp->xsize * scp->ysize * 2);
1279			fillw((FG_LIGHTGREY|BG_BLACK)<<8 | scr_map[0x20], Crtat,
1280			      scp->xsize * scp->ysize);
1281			set_border(0);
1282			i = scp->ysize * scp->xsize + 5;
1283			outb(crtc_addr, 14);
1284			outb(crtc_addr+1, i >> 8);
1285			outb(crtc_addr, 15);
1286			outb(crtc_addr+1, i & 0xff);
1287			scrn_blanked = 1;
1288 			for(i=0; i<NUM_STARS; i++) {
1289  				stars[i][0] =
1290					random() % (scp->xsize*scp->ysize);
1291  				stars[i][1] = 0;
1292 			}
1293		}
1294   		cell = random() % NUM_STARS;
1295		*((u_short*)(Crtat + stars[cell][0])) =
1296			scr_map[pattern[stars[cell][1]]] |
1297			        colors[random()%sizeof(colors)] << 8;
1298		if ((stars[cell][1]+=(random()%4)) >= sizeof(pattern)-1) {
1299    			stars[cell][0] = random() % (scp->xsize*scp->ysize);
1300   			stars[cell][1] = 0;
1301		}
1302	}
1303	else {
1304		if (scrn_blanked) {
1305			bcopy(scp->scr_buf, Crtat, scp->xsize*scp->ysize*2);
1306			cur_cursor_pos = -1;
1307			set_border(scp->border);
1308			scrn_blanked = 0;
1309		}
1310	}
1311}
1312
1313static void
1314snake_saver(int test)
1315{
1316	const char	saves[] = {"FreeBSD-2.0"};
1317	static u_char	*savs[sizeof(saves)-1];
1318	static int	dirx, diry;
1319	int		f;
1320	scr_stat	*scp = cur_console;
1321
1322	if (test) {
1323		if (!scrn_blanked) {
1324			bcopy(Crtat, scp->scr_buf,
1325			      scp->xsize * scp->ysize * 2);
1326			fillw((FG_LIGHTGREY|BG_BLACK)<<8 | scr_map[0x20],
1327			      Crtat, scp->xsize * scp->ysize);
1328			set_border(0);
1329			dirx = (scp->xpos ? 1 : -1);
1330			diry = (scp->ypos ?
1331				scp->xsize : -scp->xsize);
1332			for (f=0; f< sizeof(saves)-1; f++)
1333				savs[f] = (u_char *)Crtat + 2 *
1334					  (scp->xpos+scp->ypos*scp->xsize);
1335			*(savs[0]) = scr_map[*saves];
1336			f = scp->ysize * scp->xsize + 5;
1337			outb(crtc_addr, 14);
1338			outb(crtc_addr+1, f >> 8);
1339			outb(crtc_addr, 15);
1340			outb(crtc_addr+1, f & 0xff);
1341			scrn_blanked = 1;
1342		}
1343		if (scrn_blanked++ < 4)
1344			return;
1345		scrn_blanked = 1;
1346		*(savs[sizeof(saves)-2]) = scr_map[0x20];
1347		for (f=sizeof(saves)-2; f > 0; f--)
1348			savs[f] = savs[f-1];
1349		f = (savs[0] - (u_char *)Crtat) / 2;
1350		if ((f % scp->xsize) == 0 ||
1351		    (f % scp->xsize) == scp->xsize - 1 ||
1352		    (random() % 50) == 0)
1353			dirx = -dirx;
1354		if ((f / scp->xsize) == 0 ||
1355		    (f / scp->xsize) == scp->ysize - 1 ||
1356		    (random() % 20) == 0)
1357			diry = -diry;
1358		savs[0] += 2*dirx + 2*diry;
1359		for (f=sizeof(saves)-2; f>=0; f--)
1360			*(savs[f]) = scr_map[saves[f]];
1361	}
1362	else {
1363		if (scrn_blanked) {
1364			bcopy(scp->scr_buf, Crtat,
1365			      scp->xsize * scp->ysize * 2);
1366			cur_cursor_pos = -1;
1367			set_border(scp->border);
1368			scrn_blanked = 0;
1369		}
1370	}
1371}
1372
1373static void
1374cursor_shape(int start, int end)
1375{
1376	outb(crtc_addr, 10);
1377	outb(crtc_addr+1, start & 0xFF);
1378	outb(crtc_addr, 11);
1379	outb(crtc_addr+1, end & 0xFF);
1380}
1381
1382#if !defined(FAT_CURSOR)
1383static void
1384get_cursor_shape(int *start, int *end)
1385{
1386	outb(crtc_addr, 10);
1387	*start = inb(crtc_addr+1) & 0x1F;
1388	outb(crtc_addr, 11);
1389	*end = inb(crtc_addr+1) & 0x1F;
1390}
1391#endif
1392
1393static void
1394cursor_pos(int force)
1395{
1396	int pos;
1397
1398	if (cur_console->status & UNKNOWN_MODE)
1399		return;
1400	if (scrn_blank_time && (time.tv_sec > scrn_time_stamp+scrn_blank_time))
1401		SCRN_SAVER(1);
1402	pos = cur_console->crtat - cur_console->crt_base;
1403	if (force || (!scrn_blanked && pos != cur_cursor_pos)) {
1404		cur_cursor_pos = pos;
1405		outb(crtc_addr, 14);
1406		outb(crtc_addr+1, pos>>8);
1407		outb(crtc_addr, 15);
1408		outb(crtc_addr+1, pos&0xff);
1409	}
1410	timeout((timeout_t)cursor_pos, 0, hz/20);
1411}
1412
1413static void
1414clear_screen(scr_stat *scp)
1415{
1416	move_crsr(scp, 0, 0);
1417	fillw(scp->term.cur_attr | scr_map[0x20], scp->crt_base,
1418	       scp->xsize * scp->ysize);
1419}
1420
1421static int
1422switch_scr(u_int next_scr)
1423{
1424	if (switch_in_progress &&
1425	    (cur_console->proc != pfind(cur_console->pid)))
1426		switch_in_progress = 0;
1427
1428    	if (next_scr >= NCONS || switch_in_progress
1429	    || (cur_console->smode.mode == VT_AUTO
1430	       	&& cur_console->status & UNKNOWN_MODE)) {
1431		sysbeep(BELL_PITCH, BELL_DURATION);
1432		return EINVAL;
1433	}
1434
1435	/* is the wanted virtual console open ? */
1436	if (next_scr) {
1437		struct tty *tp = VIRTUAL_TTY(next_scr);
1438		if (!(tp->t_state & TS_ISOPEN)) {
1439			sysbeep(BELL_PITCH, BELL_DURATION);
1440			return EINVAL;
1441		}
1442	}
1443	if (in_putc) {		/* delay switch if in putc */
1444		delayed_next_scr = next_scr+1;
1445		return 0;
1446	}
1447	switch_in_progress = 1;
1448	old_scp = cur_console;
1449	new_scp = &console[next_scr];
1450	wakeup((caddr_t)&new_scp->smode);
1451	if (new_scp == old_scp) {
1452		switch_in_progress = 0;
1453		return 0;
1454	}
1455
1456	/* has controlling process died? */
1457	if (old_scp->proc && (old_scp->proc != pfind(old_scp->pid)))
1458		old_scp->smode.mode = VT_AUTO;
1459	if (new_scp->proc && (new_scp->proc != pfind(new_scp->pid)))
1460		new_scp->smode.mode = VT_AUTO;
1461
1462	/* check the modes and switch approbiatly */
1463	if (old_scp->smode.mode == VT_PROCESS) {
1464		old_scp->status |= SWITCH_WAIT_REL;
1465		psignal(old_scp->proc, old_scp->smode.relsig);
1466	}
1467	else {
1468		exchange_scr();
1469		if (new_scp->smode.mode == VT_PROCESS) {
1470			new_scp->status |= SWITCH_WAIT_ACQ;
1471			psignal(new_scp->proc, new_scp->smode.acqsig);
1472		}
1473		else
1474			switch_in_progress = 0;
1475	}
1476	return 0;
1477}
1478
1479static void
1480exchange_scr(void)
1481{
1482	bcopy(Crtat, old_scp->scr_buf, old_scp->xsize * old_scp->ysize * 2);
1483	old_scp->crt_base = old_scp->scr_buf;
1484	move_crsr(old_scp, old_scp->xpos, old_scp->ypos);
1485	cur_console = new_scp;
1486	if (old_scp->mode != new_scp->mode || (old_scp->status & UNKNOWN_MODE))
1487		set_mode(new_scp);
1488	new_scp->crt_base = Crtat;
1489	move_crsr(new_scp, new_scp->xpos, new_scp->ypos);
1490	bcopy(new_scp->scr_buf, Crtat, new_scp->xsize * new_scp->ysize * 2);
1491	update_leds(new_scp->status);
1492	if ((old_scp->status & UNKNOWN_MODE) && crtc_vga) {
1493		if (fonts_loaded & FONT_16_LOADED)
1494			copy_font(LOAD, 0, 16, font_16);
1495		if (fonts_loaded & FONT_8_LOADED)
1496			copy_font(LOAD, 1, 8, font_8);
1497		if (fonts_loaded & FONT_14_LOADED)
1498			copy_font(LOAD, 2, 14, font_14);
1499		load_palette();
1500	}
1501	if (old_scp->status & KBD_RAW_MODE || new_scp->status & KBD_RAW_MODE)
1502		shfts = ctls = alts = agrs = metas = 0;
1503	delayed_next_scr = 0;
1504}
1505
1506static void
1507move_crsr(scr_stat *scp, int x, int y)
1508{
1509	if (x < 0 || y < 0 || x >= scp->xsize || y >= scp->ysize)
1510		return;
1511	scp->xpos = x;
1512	scp->ypos = y;
1513	scp->crtat = scp->crt_base + scp->ypos * scp->xsize + scp->xpos;
1514}
1515
1516static void
1517move_up(u_short *s, u_short *d, u_int len)
1518{
1519	s += len;
1520	d += len;
1521	while (len-- > 0)
1522		*--d = *--s;
1523}
1524
1525static void
1526move_down(u_short *s, u_short *d, u_int len)
1527{
1528	while (len-- > 0)
1529		*d++ = *s++;
1530}
1531
1532static void
1533scan_esc(scr_stat *scp, u_char c)
1534{
1535	static u_char ansi_col[16] =
1536		{0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15};
1537	int i, n;
1538	u_short *src, *dst, count;
1539
1540	if (scp->term.esc == 1) {
1541		switch (c) {
1542
1543		case '[': 	/* Start ESC [ sequence */
1544			scp->term.esc = 2;
1545			scp->term.last_param = -1;
1546			for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
1547				scp->term.param[i] = 1;
1548			scp->term.num_param = 0;
1549			return;
1550
1551		case 'M':	/* Move cursor up 1 line, scroll if at top */
1552			if (scp->ypos > 0)
1553				move_crsr(scp, scp->xpos, scp->ypos - 1);
1554			else {
1555				move_up(scp->crt_base,
1556					scp->crt_base + scp->xsize,
1557					(scp->ysize - 1) * scp->xsize);
1558				fillw(scp->term.cur_attr | scr_map[0x20],
1559				      scp->crt_base, scp->xsize);
1560			}
1561			break;
1562#if notyet
1563		case 'Q':
1564			scp->term.esc = 4;
1565			break;
1566#endif
1567		case 'c':	/* Clear screen & home */
1568			clear_screen(scp);
1569			break;
1570		}
1571	}
1572	else if (scp->term.esc == 2) {
1573		if (c >= '0' && c <= '9') {
1574			if (scp->term.num_param < MAX_ESC_PAR) {
1575				if (scp->term.last_param != scp->term.num_param) {
1576					scp->term.last_param = scp->term.num_param;
1577					scp->term.param[scp->term.num_param] = 0;
1578				}
1579				else
1580					scp->term.param[scp->term.num_param] *= 10;
1581				scp->term.param[scp->term.num_param] += c - '0';
1582				return;
1583			}
1584		}
1585		scp->term.num_param = scp->term.last_param + 1;
1586		switch (c) {
1587
1588		case ';':
1589			if (scp->term.num_param < MAX_ESC_PAR)
1590				return;
1591			break;
1592
1593		case '=':
1594			scp->term.esc = 3;
1595			scp->term.last_param = -1;
1596			for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
1597				scp->term.param[i] = 1;
1598			scp->term.num_param = 0;
1599			return;
1600
1601		case 'A': /* up n rows */
1602			n = scp->term.param[0]; if (n < 1) n = 1;
1603			move_crsr(scp, scp->xpos, scp->ypos - n);
1604			break;
1605
1606		case 'B': /* down n rows */
1607			n = scp->term.param[0]; if (n < 1) n = 1;
1608			move_crsr(scp, scp->xpos, scp->ypos + n);
1609			break;
1610
1611		case 'C': /* right n columns */
1612			n = scp->term.param[0]; if (n < 1) n = 1;
1613			move_crsr(scp, scp->xpos + n, scp->ypos);
1614			break;
1615
1616		case 'D': /* left n columns */
1617			n = scp->term.param[0]; if (n < 1) n = 1;
1618			move_crsr(scp, scp->xpos - n, scp->ypos);
1619			break;
1620
1621		case 'E': /* cursor to start of line n lines down */
1622			n = scp->term.param[0]; if (n < 1) n = 1;
1623			move_crsr(scp, 0, scp->ypos + n);
1624			break;
1625
1626		case 'F': /* cursor to start of line n lines up */
1627			n = scp->term.param[0]; if (n < 1) n = 1;
1628			move_crsr(scp, 0, scp->ypos - n);
1629			break;
1630
1631		case 'f': /* System V consoles .. */
1632		case 'H': /* Cursor move */
1633			if (scp->term.num_param == 0)
1634				move_crsr(scp, 0, 0);
1635			else if (scp->term.num_param == 2)
1636				move_crsr(scp, scp->term.param[1] - 1,
1637					  scp->term.param[0] - 1);
1638			break;
1639
1640		case 'J': /* Clear all or part of display */
1641			if (scp->term.num_param == 0)
1642				n = 0;
1643			else
1644				n = scp->term.param[0];
1645			switch (n) {
1646			case 0: /* clear form cursor to end of display */
1647				fillw(scp->term.cur_attr | scr_map[0x20],
1648				      scp->crtat, scp->crt_base +
1649				      scp->xsize * scp->ysize -
1650				      scp->crtat);
1651				break;
1652			case 1: /* clear from beginning of display to cursor */
1653				fillw(scp->term.cur_attr | scr_map[0x20],
1654				      scp->crt_base,
1655				      scp->crtat - scp->crt_base);
1656				break;
1657			case 2: /* clear entire display */
1658				clear_screen(scp);
1659				break;
1660			}
1661			break;
1662
1663		case 'K': /* Clear all or part of line */
1664			if (scp->term.num_param == 0)
1665				n = 0;
1666			else
1667				n = scp->term.param[0];
1668			switch (n) {
1669			case 0: /* clear form cursor to end of line */
1670				fillw(scp->term.cur_attr | scr_map[0x20],
1671				      scp->crtat, scp->xsize - scp->xpos);
1672				break;
1673			case 1: /* clear from beginning of line to cursor */
1674				fillw(scp->term.cur_attr|scr_map[0x20],
1675				      scp->crtat - (scp->xsize - scp->xpos),
1676				      (scp->xsize - scp->xpos) + 1);
1677				break;
1678			case 2: /* clear entire line */
1679				fillw(scp->term.cur_attr|scr_map[0x20],
1680				      scp->crtat - (scp->xsize - scp->xpos),
1681				      scp->xsize);
1682				break;
1683			}
1684			break;
1685
1686		case 'L':	/* Insert n lines */
1687			n = scp->term.param[0]; if (n < 1) n = 1;
1688			if (n > scp->ysize - scp->ypos)
1689				n = scp->ysize - scp->ypos;
1690			src = scp->crt_base + scp->ypos * scp->xsize;
1691			dst = src + n * scp->xsize;
1692			count = scp->ysize - (scp->ypos + n);
1693			move_up(src, dst, count * scp->xsize);
1694			fillw(scp->term.cur_attr | scr_map[0x20], src,
1695			      n * scp->xsize);
1696			break;
1697
1698		case 'M':	/* Delete n lines */
1699			n = scp->term.param[0]; if (n < 1) n = 1;
1700			if (n > scp->ysize - scp->ypos)
1701				n = scp->ysize - scp->ypos;
1702			dst = scp->crt_base + scp->ypos * scp->xsize;
1703			src = dst + n * scp->xsize;
1704			count = scp->ysize - (scp->ypos + n);
1705			move_down(src, dst, count * scp->xsize);
1706			src = dst + count * scp->xsize;
1707			fillw(scp->term.cur_attr | scr_map[0x20], src,
1708			      n * scp->xsize);
1709			break;
1710
1711		case 'P':	/* Delete n chars */
1712			n = scp->term.param[0]; if (n < 1) n = 1;
1713			if (n > scp->xsize - scp->xpos)
1714				n = scp->xsize - scp->xpos;
1715			dst = scp->crtat;
1716			src = dst + n;
1717			count = scp->xsize - (scp->xpos + n);
1718			move_down(src, dst, count);
1719			src = dst + count;
1720			fillw(scp->term.cur_attr | scr_map[0x20], src, n);
1721			break;
1722
1723		case '@':	/* Insert n chars */
1724			n = scp->term.param[0]; if (n < 1) n = 1;
1725			if (n > scp->xsize - scp->xpos)
1726				n = scp->xsize - scp->xpos;
1727			src = scp->crtat;
1728			dst = src + n;
1729			count = scp->xsize - (scp->xpos + n);
1730			move_up(src, dst, count);
1731			fillw(scp->term.cur_attr | scr_map[0x20], src, n);
1732			break;
1733
1734		case 'S':	/* scroll up n lines */
1735			n = scp->term.param[0]; if (n < 1)  n = 1;
1736			if (n > scp->ypos)
1737				n = scp->ypos;
1738			bcopy(scp->crt_base + (scp->xsize * n),
1739			      scp->crt_base,
1740			      scp->xsize * (scp->ysize - n) *
1741			      sizeof(u_short));
1742			fillw(scp->term.cur_attr | scr_map[0x20],
1743			      scp->crt_base + scp->xsize *
1744			      (scp->ysize - 1),
1745			      scp->xsize);
1746			break;
1747
1748		case 'T':	/* scroll down n lines */
1749			n = scp->term.param[0]; if (n < 1)  n = 1;
1750			if (n > scp->ysize - scp->ypos)
1751				n = scp->ysize - scp->ypos;
1752			bcopy(scp->crt_base,
1753			      scp->crt_base + (scp->xsize * n),
1754			      scp->xsize * (scp->ysize - n) *
1755			      sizeof(u_short));
1756			fillw(scp->term.cur_attr | scr_map[0x20],
1757			      scp->crt_base, scp->xsize);
1758			break;
1759
1760		case 'X':	/* delete n characters in line */
1761			n = scp->term.param[0]; if (n < 1)  n = 1;
1762			if (n > scp->xsize - scp->xpos)
1763				n = scp->xsize - scp->xpos;
1764			fillw(scp->term.cur_attr | scr_map[0x20],
1765                              scp->crt_base + scp->xpos +
1766			      ((scp->xsize*scp->ypos) * sizeof(u_short)), n);
1767			break;
1768
1769		case 'Z':	/* move n tabs backwards */
1770			n = scp->term.param[0]; if (n < 1)  n = 1;
1771			if ((i = scp->xpos & 0xf8) == scp->xpos)
1772				i -= 8*n;
1773			else
1774				i -= 8*(n-1);
1775			if (i < 0)
1776				i = 0;
1777			move_crsr(scp, i, scp->ypos);
1778			break;
1779
1780		case '`': 	/* move cursor to column n */
1781			n = scp->term.param[0]; if (n < 1)  n = 1;
1782			move_crsr(scp, n, scp->ypos);
1783			break;
1784
1785		case 'a': 	/* move cursor n columns to the right */
1786			n = scp->term.param[0]; if (n < 1)  n = 1;
1787			move_crsr(scp, scp->xpos + n, scp->ypos);
1788			break;
1789
1790		case 'd': 	/* move cursor to row n */
1791			n = scp->term.param[0]; if (n < 1)  n = 1;
1792			move_crsr(scp, scp->xpos, n);
1793			break;
1794
1795		case 'e': 	/* move cursor n rows down */
1796			n = scp->term.param[0]; if (n < 1)  n = 1;
1797			move_crsr(scp, scp->xpos, scp->ypos + n);
1798			break;
1799
1800		case 'm': 	/* change attribute */
1801			if (scp->term.num_param == 0) {
1802				scp->term.cur_attr = scp->term.std_attr;
1803				break;
1804			}
1805			for (i = 0; i < scp->term.num_param; i++) {
1806				switch (n = scp->term.param[i]) {
1807				case 0:	/* back to normal */
1808					scp->term.cur_attr = scp->term.std_attr;
1809					break;
1810				case 1:	/* highlight (bold) */
1811					scp->term.cur_attr &= 0xFF00;
1812					scp->term.cur_attr |= 0x0800;
1813					break;
1814				case 4: /* highlight (underline) */
1815					scp->term.cur_attr &= 0xFF00;
1816					scp->term.cur_attr |= 0x0800;
1817					break;
1818				case 5: /* blink */
1819					scp->term.cur_attr &= 0xFF00;
1820					scp->term.cur_attr |= 0x8000;
1821					break;
1822				case 7: /* reverse video */
1823					scp->term.cur_attr = scp->term.rev_attr;
1824					break;
1825				case 30: case 31: /* set fg color */
1826				case 32: case 33: case 34:
1827				case 35: case 36: case 37:
1828					scp->term.cur_attr =
1829						(scp->term.cur_attr & 0xF8FF)
1830						| (ansi_col[(n-30) & 7] << 8);
1831					break;
1832				case 40: case 41: /* set bg color */
1833				case 42: case 43: case 44:
1834				case 45: case 46: case 47:
1835					scp->term.cur_attr =
1836						(scp->term.cur_attr & 0x8FFF)
1837						| (ansi_col[(n-40) & 7] << 12);
1838					break;
1839				}
1840			}
1841			break;
1842
1843		case 'x':
1844			if (scp->term.num_param == 0)
1845				n = 0;
1846			else
1847				n = scp->term.param[0];
1848			switch (n) {
1849			case 0: 	/* reset attributes */
1850				scp->term.cur_attr = scp->term.std_attr =
1851					current_default->std_attr;
1852				scp->term.rev_attr = current_default->rev_attr;
1853				break;
1854			case 1: 	/* set ansi background */
1855				scp->term.cur_attr = scp->term.std_attr =
1856					(scp->term.std_attr & 0x0F00) |
1857					(ansi_col[(scp->term.param[1])&0x0F]<<12);
1858				break;
1859			case 2: 	/* set ansi foreground */
1860				scp->term.cur_attr = scp->term.std_attr =
1861					(scp->term.std_attr & 0xF000) |
1862					(ansi_col[(scp->term.param[1])&0x0F]<<8);
1863				break;
1864			case 3: 	/* set ansi attribute directly */
1865				scp->term.cur_attr = scp->term.std_attr =
1866					(scp->term.param[1]&0xFF)<<8;
1867				break;
1868			case 5: 	/* set ansi reverse video background */
1869				scp->term.rev_attr =
1870					(scp->term.rev_attr & 0x0F00) |
1871					(ansi_col[(scp->term.param[1])&0x0F]<<12);
1872				break;
1873			case 6: 	/* set ansi reverse video foreground */
1874				scp->term.rev_attr =
1875					(scp->term.rev_attr & 0xF000) |
1876					(ansi_col[(scp->term.param[1])&0x0F]<<8);
1877				break;
1878			case 7: 	/* set ansi reverse video directly */
1879				scp->term.rev_attr = (scp->term.param[1]&0xFF)<<8;
1880				break;
1881			}
1882			break;
1883
1884		case 'z':	/* switch to (virtual) console n */
1885			if (scp->term.num_param == 1)
1886				switch_scr(scp->term.param[0]);
1887			break;
1888		}
1889	}
1890	else if (scp->term.esc == 3) {
1891		if (c >= '0' && c <= '9') {
1892			if (scp->term.num_param < MAX_ESC_PAR) {
1893				if (scp->term.last_param != scp->term.num_param) {
1894					scp->term.last_param = scp->term.num_param;
1895					scp->term.param[scp->term.num_param] = 0;
1896				}
1897				else
1898					scp->term.param[scp->term.num_param] *= 10;
1899				scp->term.param[scp->term.num_param] += c - '0';
1900				return;
1901			}
1902		}
1903		scp->term.num_param = scp->term.last_param + 1;
1904		switch (c) {
1905
1906		case ';':
1907			if (scp->term.num_param < MAX_ESC_PAR)
1908				return;
1909			break;
1910
1911		case 'A':	/* set display border color */
1912			if (scp->term.num_param == 1)
1913				scp->border=scp->term.param[0] & 0xff;
1914				if (scp == cur_console)
1915					set_border(scp->border);
1916			break;
1917
1918		case 'B':	/* set bell pitch and duration */
1919			if (scp->term.num_param == 2) {
1920				scp->bell_pitch = scp->term.param[0];
1921				scp->bell_duration = scp->term.param[1]*10;
1922			}
1923			break;
1924
1925		case 'C': 	/* set cursor shape (start & end line) */
1926			if (scp->term.num_param == 2) {
1927				scp->cursor_start = scp->term.param[0] & 0x1F;
1928				scp->cursor_end = scp->term.param[1] & 0x1F;
1929				if (scp == cur_console)
1930					cursor_shape(scp->cursor_start,
1931						     scp->cursor_end);
1932			}
1933			break;
1934
1935		case 'F':	/* set ansi foreground */
1936			if (scp->term.num_param == 1)
1937				scp->term.cur_attr = scp->term.std_attr =
1938					(scp->term.std_attr & 0xF000)
1939					| ((scp->term.param[0] & 0x0F) << 8);
1940			break;
1941
1942		case 'G': 	/* set ansi background */
1943			if (scp->term.num_param == 1)
1944				scp->term.cur_attr = scp->term.std_attr =
1945					(scp->term.std_attr & 0x0F00)
1946					| ((scp->term.param[0] & 0x0F) << 12);
1947			break;
1948
1949		case 'H':	/* set ansi reverse video foreground */
1950			if (scp->term.num_param == 1)
1951				scp->term.rev_attr =
1952					(scp->term.rev_attr & 0xF000)
1953					| ((scp->term.param[0] & 0x0F) << 8);
1954			break;
1955
1956		case 'I': 	/* set ansi reverse video background */
1957			if (scp->term.num_param == 1)
1958				scp->term.rev_attr =
1959					(scp->term.rev_attr & 0x0F00)
1960					| ((scp->term.param[0] & 0x0F) << 12);
1961			break;
1962		}
1963	}
1964	scp->term.esc = 0;
1965}
1966
1967static void
1968ansi_put(scr_stat *scp, u_char c)
1969{
1970	if (scp->status & UNKNOWN_MODE)
1971		return;
1972
1973	/* make screensaver happy */
1974	if (scp == cur_console) {
1975		scrn_time_stamp = time.tv_sec;
1976		if (scrn_blanked)
1977			SCRN_SAVER(0);
1978	}
1979	in_putc++;
1980	if (scp->term.esc)
1981		scan_esc(scp, c);
1982	else switch(c) {
1983	case 0x1B:	/* start escape sequence */
1984		scp->term.esc = 1;
1985		scp->term.num_param = 0;
1986		break;
1987	case 0x07:
1988		if (scp == cur_console)
1989		 	sysbeep(scp->bell_pitch, scp->bell_duration);
1990		break;
1991	case '\t':	/* non-destructive tab */
1992		scp->crtat += (8 - scp->xpos % 8);
1993		scp->xpos += (8 - scp->xpos % 8);
1994		break;
1995	case '\b':      /* non-destructive backspace */
1996		if (scp->crtat > scp->crt_base) {
1997			scp->crtat--;
1998			if (scp->xpos > 0)
1999				scp->xpos--;
2000			else {
2001				scp->xpos += scp->xsize - 1;
2002				scp->ypos--;
2003			}
2004		}
2005		break;
2006	case '\r':	/* return to pos 0 */
2007		move_crsr(scp, 0, scp->ypos);
2008		break;
2009	case '\n':	/* newline, same pos */
2010		scp->crtat += scp->xsize;
2011		scp->ypos++;
2012		break;
2013	case '\f':	/* form feed, clears screen */
2014		clear_screen(scp);
2015		break;
2016	default:
2017		/* Print only printables */
2018		*scp->crtat = (scp->term.cur_attr | scr_map[c]);
2019		scp->crtat++;
2020		if (++scp->xpos >= scp->xsize) {
2021			scp->xpos = 0;
2022			scp->ypos++;
2023		}
2024		break;
2025	}
2026	if (scp->crtat >= scp->crt_base + scp->ysize * scp->xsize) {
2027		bcopy(scp->crt_base + scp->xsize, scp->crt_base,
2028			scp->xsize * (scp->ysize - 1) * sizeof(u_short));
2029		fillw(scp->term.cur_attr | scr_map[0x20],
2030			scp->crt_base + scp->xsize * (scp->ysize - 1),
2031			scp->xsize);
2032		scp->crtat -= scp->xsize;
2033		scp->ypos--;
2034	}
2035	in_putc--;
2036	if (delayed_next_scr)
2037		switch_scr(delayed_next_scr - 1);
2038}
2039
2040static void
2041scinit(void)
2042{
2043	u_short volatile *cp = Crtat + (CGA_BUF-MONO_BUF)/sizeof(u_short), was;
2044	unsigned cursorat;
2045	int start = -1, end = -1, i;
2046	scr_stat *scp;
2047
2048	/*
2049	 * catch that once in a blue moon occurence when scinit is called
2050	 * TWICE, adding the CGA_BUF offset again -> poooff
2051	 */
2052	if (crtat != 0)
2053		return;
2054	/*
2055	 * Crtat initialized to point to MONO buffer, if not present change
2056	 * to CGA_BUF offset. ONLY ADD the difference since locore.s adds
2057	 * in the remapped offset at the "right" time
2058	 */
2059	was = *cp;
2060	*cp = (u_short) 0xA55A;
2061	if (*cp != 0xA55A)
2062		crtc_addr = MONO_BASE;
2063	else {
2064		*cp = was;
2065		crtc_addr = COLOR_BASE;
2066		Crtat = Crtat + (CGA_BUF-MONO_BUF)/sizeof(u_short);
2067	}
2068
2069	/* Extract cursor location */
2070	outb(crtc_addr,14);
2071	cursorat = inb(crtc_addr+1)<<8 ;
2072	outb(crtc_addr,15);
2073	cursorat |= inb(crtc_addr+1);
2074	crtat = Crtat + cursorat;
2075
2076	/* is this a VGA or higher ? */
2077	outb(crtc_addr, 7);
2078	if (inb(crtc_addr) == 7) {
2079		crtc_vga = 1;
2080#if defined(FAT_CURSOR)
2081                start = 0;
2082                end = 18;
2083		cursor_shape(start, end);
2084#else
2085		get_cursor_shape(&start, &end);
2086#endif
2087	}
2088	current_default = &user_default;
2089	for (i = 0; i < NCONS; i++) {
2090		scp = &console[i];
2091		scp->mode = M_VGA_C80x25;
2092		scp->term.esc = 0;
2093		scp->term.std_attr = current_default->std_attr;
2094		scp->term.rev_attr = current_default->rev_attr;
2095		scp->term.cur_attr = scp->term.std_attr;
2096		scp->border = BG_BLACK;
2097		scp->cursor_start = start;
2098		scp->cursor_end = end;
2099		scp->xsize = COL;
2100		scp->ysize = ROW;
2101		scp->bell_pitch = BELL_PITCH;
2102		scp->bell_duration = BELL_DURATION;
2103#ifndef LAPTOP
2104		scp->status = NLKED;
2105#else
2106		scp->status = 0;
2107#endif
2108		scp->pid = 0;
2109		scp->proc = NULL;
2110		scp->smode.mode = VT_AUTO;
2111		if (i == 0) {
2112			scp->xpos = cursorat % COL;
2113			scp->ypos = cursorat / COL;
2114			scp->crt_base = Crtat;
2115			scp->crtat = crtat;
2116		}
2117	}
2118	kernel_console.esc = 0;
2119	kernel_console.std_attr = kernel_default.std_attr;
2120	kernel_console.rev_attr = kernel_default.rev_attr;
2121	kernel_console.cur_attr = kernel_default.std_attr;
2122	/* initialize mapscrn array to a one to one map */
2123	for (i=0; i<sizeof(scr_map); i++)
2124		scr_map[i] = i;
2125}
2126
2127static void
2128scput(u_char c)
2129{
2130	scr_stat *scp = &console[0];
2131	term_stat save;
2132
2133	if (crtat == 0)
2134		scinit();
2135	if( in_putc == 0) {
2136		++in_putc;
2137		save = scp->term;
2138		scp->term = kernel_console;
2139		current_default = &kernel_default;
2140		ansi_put(scp, c);
2141		kernel_console = scp->term;
2142		current_default = &user_default;
2143		scp->term = save;
2144		--in_putc;
2145	} else {
2146		if( console_buffer_count < CONSOLE_BUFSIZE)
2147			console_buffer[console_buffer_count++] = c;
2148	}
2149}
2150
2151static u_char
2152*get_fstr(u_int c, u_int *len)
2153{
2154	u_int i;
2155
2156	if (!(c & FKEY))
2157		return(NULL);
2158	i = (c & 0xFF) - F_FN;
2159	if (i > n_fkey_tab)
2160		return(NULL);
2161	*len = fkey_tab[i].len;
2162	return(fkey_tab[i].str);
2163}
2164
2165static void
2166update_leds(int which)
2167{
2168	int s;
2169  	static u_char xlate_leds[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
2170
2171	/* replace CAPS led with ALTGR led for ALTGR keyboards */
2172	if (key_map.n_keys > ALTGR_OFFSET) {
2173		if (which & ALKED)
2174			which |= CLKED;
2175		else
2176			which &= ~CLKED;
2177	}
2178	s = spltty();
2179	kbd_cmd(KB_SETLEDS);
2180	kbd_cmd(xlate_leds[which & LED_MASK]);
2181	splx(s);
2182}
2183
2184/*
2185 * scgetc(noblock) - get character from keyboard.
2186 * If noblock = 0 wait until a key is pressed.
2187 * Else return NOKEY.
2188 */
2189u_int
2190scgetc(int noblock)
2191{
2192	u_char scancode, keycode;
2193	u_int state, action;
2194	struct key_t *key;
2195	static u_char esc_flag = 0, compose = 0;
2196	static u_int chr = 0;
2197
2198next_code:
2199	kbd_wait();
2200	/* First see if there is something in the keyboard port */
2201	if (inb(KB_STAT) & KB_BUF_FULL)
2202		scancode = inb(KB_DATA);
2203	else if (noblock)
2204		return(NOKEY);
2205	else
2206		goto next_code;
2207
2208	if (cur_console->status & KBD_RAW_MODE)
2209		return scancode;
2210#if ASYNCH
2211	if (scancode == KB_ACK || scancode == KB_RESEND) {
2212		kbd_reply = scancode;
2213		if (noblock)
2214			return(NOKEY);
2215		goto next_code;
2216	}
2217#endif
2218	keycode = scancode & 0x7F;
2219	switch (esc_flag) {
2220	case 0x00:		/* normal scancode */
2221		switch(scancode) {
2222		case 0xB8:	/* left alt  (compose key) */
2223			if (compose) {
2224				compose = 0;
2225				if (chr > 255) {
2226					sysbeep(BELL_PITCH, BELL_DURATION);
2227					chr = 0;
2228				}
2229			}
2230			break;
2231		case 0x38:
2232			if (!compose) {
2233				compose = 1;
2234				chr = 0;
2235			}
2236			break;
2237		case 0xE0:
2238		case 0xE1:
2239			esc_flag = scancode;
2240			goto next_code;
2241		}
2242		break;
2243	case 0xE0:		/* 0xE0 prefix */
2244		esc_flag = 0;
2245		switch (keycode) {
2246		case 0x1C:	/* right enter key */
2247			keycode = 0x59;
2248			break;
2249		case 0x1D:	/* right ctrl key */
2250			keycode = 0x5A;
2251			break;
2252		case 0x35:	/* keypad divide key */
2253			keycode = 0x5B;
2254			break;
2255		case 0x37:	/* print scrn key */
2256			keycode = 0x5C;
2257			break;
2258		case 0x38:	/* right alt key (alt gr) */
2259			keycode = 0x5D;
2260			break;
2261		case 0x47:	/* grey home key */
2262			keycode = 0x5E;
2263			break;
2264		case 0x48:	/* grey up arrow key */
2265			keycode = 0x5F;
2266			break;
2267		case 0x49:	/* grey page up key */
2268			keycode = 0x60;
2269			break;
2270		case 0x4B:	/* grey left arrow key */
2271			keycode = 0x61;
2272			break;
2273		case 0x4D:	/* grey right arrow key */
2274			keycode = 0x62;
2275			break;
2276		case 0x4F:	/* grey end key */
2277			keycode = 0x63;
2278			break;
2279		case 0x50:	/* grey down arrow key */
2280			keycode = 0x64;
2281			break;
2282		case 0x51:	/* grey page down key */
2283			keycode = 0x65;
2284			break;
2285		case 0x52:	/* grey insert key */
2286			keycode = 0x66;
2287			break;
2288		case 0x53:	/* grey delete key */
2289			keycode = 0x67;
2290			break;
2291		default:	/* ignore everything else */
2292			goto next_code;
2293		}
2294		break;
2295	case 0xE1:		/* 0xE1 prefix */
2296		esc_flag = 0;
2297		if (keycode == 0x1D)
2298			esc_flag = 0x1D;
2299		goto next_code;
2300		/* NOT REACHED */
2301	case 0x1D:		/* pause / break */
2302		esc_flag = 0;
2303		if (keycode != 0x45)
2304			goto next_code;
2305		keycode = 0x68;
2306		break;
2307	}
2308
2309	if (compose) {
2310		switch (scancode) {
2311		/* key pressed process it */
2312		case 0x47: case 0x48: case 0x49:	/* keypad 7,8,9 */
2313			chr = (scancode - 0x40) + chr*10;
2314			goto next_code;
2315		case 0x4B: case 0x4C: case 0x4D:	/* keypad 4,5,6 */
2316			chr = (scancode - 0x47) + chr*10;
2317			goto next_code;
2318		case 0x4F: case 0x50: case 0x51:	/* keypad 1,2,3 */
2319			chr = (scancode - 0x4E) + chr*10;
2320			goto next_code;
2321		case 0x52:				/* keypad 0 */
2322			chr *= 10;
2323			goto next_code;
2324
2325		/* key release, no interest here */
2326		case 0xC7: case 0xC8: case 0xC9:	/* keypad 7,8,9 */
2327		case 0xCB: case 0xCC: case 0xCD:	/* keypad 4,5,6 */
2328		case 0xCF: case 0xD0: case 0xD1:	/* keypad 1,2,3 */
2329		case 0xD2:				/* keypad 0 */
2330			goto next_code;
2331
2332		case 0x38:				/* left alt key */
2333			break;
2334		default:
2335			if (chr) {
2336				compose = chr = 0;
2337				sysbeep(BELL_PITCH, BELL_DURATION);
2338				goto next_code;
2339			}
2340			break;
2341		}
2342	}
2343
2344	state = (shfts ? 1 : 0 ) | (2 * (ctls ? 1 : 0)) | (4 * (alts ? 1 : 0));
2345	if ((!agrs && (cur_console->status & ALKED))
2346	    || (agrs && !(cur_console->status & ALKED)))
2347		keycode += ALTGR_OFFSET;
2348	key = &key_map.key[keycode];
2349	if ( ((key->flgs & FLAG_LOCK_C) && (cur_console->status & CLKED))
2350	     || ((key->flgs & FLAG_LOCK_N) && (cur_console->status & NLKED)) )
2351		state ^= 1;
2352
2353	/* Check for make/break */
2354	action = key->map[state];
2355	if (scancode & 0x80) { 		/* key released */
2356		if (key->spcl & 0x80) {
2357			switch (action) {
2358			case LSH:
2359				shfts &= ~1;
2360				break;
2361			case RSH:
2362				shfts &= ~2;
2363				break;
2364			case LCTR:
2365				ctls &= ~1;
2366				break;
2367			case RCTR:
2368				ctls &= ~2;
2369				break;
2370			case LALT:
2371				alts &= ~1;
2372				break;
2373			case RALT:
2374				alts &= ~2;
2375				break;
2376			case NLK:
2377				nlkcnt = 0;
2378				break;
2379			case CLK:
2380				clkcnt = 0;
2381				break;
2382			case SLK:
2383				slkcnt = 0;
2384				break;
2385			case ASH:
2386				agrs = 0;
2387				break;
2388			case ALK:
2389				alkcnt = 0;
2390				break;
2391			case META:
2392				metas = 0;
2393				break;
2394			}
2395		}
2396		if (chr && !compose) {
2397			action = chr;
2398			chr = 0;
2399			return(action);
2400		}
2401	} else {
2402		/* key pressed */
2403		if (key->spcl & (0x80>>state)) {
2404			switch (action) {
2405			/* LOCKING KEYS */
2406			case NLK:
2407				if (!nlkcnt) {
2408					nlkcnt++;
2409					if (cur_console->status & NLKED)
2410						cur_console->status &= ~NLKED;
2411					else
2412						cur_console->status |= NLKED;
2413					update_leds(cur_console->status);
2414				}
2415				break;
2416			case CLK:
2417				if (!clkcnt) {
2418					clkcnt++;
2419					if (cur_console->status & CLKED)
2420						cur_console->status &= ~CLKED;
2421					else
2422						cur_console->status |= CLKED;
2423					update_leds(cur_console->status);
2424				}
2425				break;
2426			case SLK:
2427				if (!slkcnt) {
2428					slkcnt++;
2429					if (cur_console->status & SLKED) {
2430						cur_console->status &= ~SLKED;
2431						pcstart(VIRTUAL_TTY(get_scr_num()));
2432					}
2433					else
2434						cur_console->status |= SLKED;
2435					update_leds(cur_console->status);
2436				}
2437				break;
2438 			case ALK:
2439				if (!alkcnt) {
2440					alkcnt++;
2441 					if (cur_console->status & ALKED)
2442 						cur_console->status &= ~ALKED;
2443 					else
2444 						cur_console->status |= ALKED;
2445					update_leds(cur_console->status);
2446				}
2447  				break;
2448
2449			/* NON-LOCKING KEYS */
2450			case NOP:
2451				break;
2452			case RBT:
2453				shutdown_nice();
2454				break;
2455			case SUSP:
2456				break;
2457
2458			case DBG:
2459#ifdef DDB			/* try to switch to console 0 */
2460				if (cur_console->smode.mode == VT_AUTO &&
2461		    		    console[0].smode.mode == VT_AUTO)
2462					switch_scr(0);
2463				Debugger("manual escape to debugger");
2464				return(NOKEY);
2465#else
2466				printf("No debugger in kernel\n");
2467#endif
2468				break;
2469			case LSH:
2470				shfts |= 1;
2471				break;
2472			case RSH:
2473				shfts |= 2;
2474				break;
2475			case LCTR:
2476				ctls |= 1;
2477				break;
2478			case RCTR:
2479				ctls |= 2;
2480				break;
2481			case LALT:
2482				alts |= 1;
2483				break;
2484			case RALT:
2485				alts |= 2;
2486				break;
2487			case ASH:
2488				agrs = 1;
2489				break;
2490			case META:
2491				metas = 1;
2492				break;
2493			case NEXT:
2494				switch_scr((get_scr_num()+1)%NCONS);
2495				break;
2496			default:
2497				if (action >= F_SCR && action <= L_SCR) {
2498					switch_scr(action - F_SCR);
2499					break;
2500				}
2501				if (action >= F_FN && action <= L_FN)
2502					action |= FKEY;
2503				return(action);
2504			}
2505		}
2506		else {
2507			if (metas)
2508				action |= MKEY;
2509 			return(action);
2510		}
2511	}
2512	goto next_code;
2513}
2514
2515int
2516getchar(void)
2517{
2518	u_char thechar;
2519	int s;
2520
2521	polling = 1;
2522	s = splhigh();
2523	scput('>');
2524	thechar = (u_char) scgetc(0);
2525	polling = 0;
2526	splx(s);
2527	switch (thechar) {
2528	default:
2529		if (thechar >= scr_map[0x20])
2530			scput(thechar);
2531		return(thechar);
2532	case cr:
2533	case lf:
2534		scput(cr); scput(lf);
2535		return(lf);
2536	case bs:
2537	case del:
2538		scput(bs); scput(scr_map[0x20]); scput(bs);
2539		return(thechar);
2540	case cntld:
2541		scput('^'); scput('D'); scput('\r'); scput('\n');
2542		return(0);
2543	}
2544}
2545
2546u_int
2547sgetc(int noblock)
2548{
2549	return (scgetc(noblock) & 0xff);
2550}
2551
2552int
2553pcmmap(dev_t dev, int offset, int nprot)
2554{
2555	if (offset > 0x20000)
2556		return EINVAL;
2557	return i386_btop((VIDEOMEM + offset));
2558}
2559
2560static void
2561kbd_wait(void)
2562{
2563	int i = 1000;
2564
2565	while (i--) {
2566		if ((inb(KB_STAT) & KB_READY) == 0)
2567			break;
2568		DELAY (10);
2569	}
2570}
2571
2572static void
2573kbd_cmd(u_char command)
2574{
2575	int retry = 5;
2576	do {
2577		int i = 100000;
2578
2579		kbd_wait();
2580#if ASYNCH
2581		kbd_reply = 0;
2582		outb(KB_DATA, command);
2583		while (i--) {
2584			if (kbd_reply == KB_ACK)
2585				return;
2586			if (kbd_reply == KB_RESEND)
2587				break;
2588		}
2589#else
2590		outb(KB_DATA, command);
2591		while (i--) {
2592			if (inb(KB_STAT) & KB_BUF_FULL) {
2593				int val;
2594				DELAY(10);
2595				val = inb(KB_DATA);
2596				if (val == KB_ACK)
2597					return;
2598				if (val == KB_RESEND)
2599					break;
2600			}
2601		}
2602#endif
2603	} while (retry--);
2604}
2605
2606static void
2607set_mode(scr_stat *scp)
2608{
2609	char *modetable;
2610	char special_modetable[64];
2611	int mode, font_size;
2612
2613	if (scp != cur_console)
2614		return;
2615
2616	/* mode change only on VGA's */
2617	if (!crtc_vga) {
2618		/* (re)activate cursor */
2619		untimeout((timeout_t)cursor_pos, 0);
2620		cursor_pos(1);
2621		return;
2622	}
2623
2624	/* setup video hardware for the given mode */
2625	switch (scp->mode) {
2626       	case M_VGA_C80x50:
2627		bcopy(video_mode_ptr+(64*M_VGA_C80x25), &special_modetable, 64);
2628		special_modetable[2] = 8;
2629		special_modetable[19] = 7;
2630		modetable = special_modetable;
2631		goto setup_mode;
2632
2633	case M_VGA_M80x50:
2634		bcopy(video_mode_ptr+(64*M_VGA_M80x25), &special_modetable, 64);
2635		special_modetable[2] = 8;
2636		special_modetable[19] = 7;
2637		modetable = special_modetable;
2638		goto setup_mode;
2639
2640       	case M_ENH_B80x43:
2641		bcopy(video_mode_ptr+(64*M_ENH_B80x25), &special_modetable, 64);
2642		special_modetable[2] = 8;
2643		special_modetable[19] = 7;
2644		special_modetable[28] = 87;
2645		modetable = special_modetable;
2646		goto setup_mode;
2647
2648	case M_ENH_C80x43:
2649		bcopy(video_mode_ptr+(64*M_ENH_C80x25), &special_modetable, 64);
2650		special_modetable[2] = 8;
2651		special_modetable[19] = 7;
2652		special_modetable[28] = 87;
2653		modetable = special_modetable;
2654		goto setup_mode;
2655
2656       	case M_VGA_C40x25: case M_VGA_C80x25:	/* VGA TEXT MODES */
2657	case M_VGA_M80x25:
2658       	case M_B40x25:     case M_C40x25:
2659       	case M_B80x25:     case M_C80x25:
2660       	case M_ENH_B40x25: case M_ENH_C40x25:
2661       	case M_ENH_B80x25: case M_ENH_C80x25:
2662
2663		modetable = (char*)(video_mode_ptr + (scp->mode * 64));
2664setup_mode:
2665		set_vgaregs(modetable);
2666		font_size = *(modetable + 2);
2667		/* change cursor type if set */
2668		if (scp->cursor_start != -1 && scp->cursor_end != -1)
2669			cursor_shape(
2670			(scp->cursor_start >= font_size)
2671			? font_size - 1
2672			: scp->cursor_start,
2673			(scp->cursor_end >= font_size)
2674			? font_size - 1
2675			: scp->cursor_end);
2676
2677		/* set font type (size) */
2678 		switch (font_size) {
2679 		case 0x08:
2680 	    		outb(TSIDX, 0x03); outb(TSREG, 0x05);	/* font 1 */
2681 			break;
2682 		case 0x0E:
2683 	    		outb(TSIDX, 0x03); outb(TSREG, 0x0A);	/* font 2 */
2684 			break;
2685 		case 0x10:
2686 	    		outb(TSIDX, 0x03); outb(TSREG, 0x00);	/* font 0 */
2687 			break;
2688 		default:
2689 	    		outb(TSIDX, 0x03); outb(TSREG, 0x05);	/* font 1 */
2690 		}
2691
2692 		/* (re)activate cursor */
2693 		untimeout((timeout_t)cursor_pos, 0);
2694 		cursor_pos(1);
2695		break;
2696
2697	case M_BG320:      case M_CG320:      case M_BG640:
2698	case M_CG320_D:    case M_CG640_E:
2699	case M_CG640x350:  case M_ENH_CG640:
2700	case M_BG640x480:  case M_CG640x480:  case M_VGA_CG320:
2701
2702 		set_vgaregs(video_mode_ptr + (scp->mode * 64));
2703		break;
2704
2705	default:
2706		/* call user defined function XXX */
2707		break;
2708	}
2709
2710	/* set border color for this (virtual) console */
2711	set_border(scp->border);
2712	return;
2713}
2714
2715static void
2716set_border(int color)
2717{
2718	inb(crtc_addr+6); 				/* reset flip-flop */
2719	outb(ATC, 0x11); outb(ATC, color);
2720 	inb(crtc_addr+6); 				/* reset flip-flop */
2721 	outb(ATC, 0x20);				/* enable Palette */
2722}
2723
2724static void
2725set_vgaregs(char *modetable)
2726{
2727	int i, s = splhigh();
2728
2729	outb(TSIDX, 0x00); outb(TSREG, 0x01);	/* stop sequencer */
2730	outb(TSIDX, 0x07); outb(TSREG, 0x00);	/* unlock registers */
2731	for (i=0; i<4; i++) {			/* program sequencer */
2732		outb(TSIDX, i+1);
2733		outb(TSREG, modetable[i+5]);
2734	}
2735	outb(MISC, modetable[9]);		/* set dot-clock */
2736	outb(TSIDX, 0x00); outb(TSREG, 0x03);	/* start sequencer */
2737	outb(crtc_addr, 0x11);
2738	outb(crtc_addr+1, inb(crtc_addr+1) & 0x7F);
2739	for (i=0; i<25; i++) {			/* program crtc */
2740		outb(crtc_addr, i);
2741		outb(crtc_addr+1, modetable[i+10]);
2742	}
2743	inb(crtc_addr+6); 			/* reset flip-flop */
2744	for (i=0; i<20; i++) {			/* program attribute ctrl */
2745		outb(ATC, i);
2746		outb(ATC, modetable[i+35]);
2747	}
2748	for (i=0; i<9; i++) {			/* program graph data ctrl */
2749		outb(GDCIDX, i);
2750		outb(GDCREG, modetable[i+55]);
2751	}
2752	inb(crtc_addr+6); 			/* reset flip-flop */
2753	outb(ATC ,0x20);			/* enable palette */
2754	splx(s);
2755}
2756
2757static void
2758copy_font(int direction, int segment, int size, char* font)
2759{
2760  	int ch, line, s;
2761	u_char val;
2762
2763 	outb(TSIDX, 0x01); val = inb(TSREG); 		/* blank screen */
2764	outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
2765
2766	/* setup vga for loading fonts (graphics plane mode) */
2767	inb(crtc_addr+6);				/* reset flip/flop */
2768	outb(ATC, 0x30); outb(ATC, 0x01);
2769	outb(TSIDX, 0x02); outb(TSREG, 0x04);
2770	outb(TSIDX, 0x04); outb(TSREG, 0x06);
2771	outb(GDCIDX, 0x04); outb(GDCREG, 0x02);
2772	outb(GDCIDX, 0x05); outb(GDCREG, 0x00);
2773	outb(GDCIDX, 0x06); outb(GDCREG, 0x05);		/* addr = a0000, 64kb */
2774    	for (ch=0; ch < 256; ch++)
2775	    for (line=0; line < size; line++)
2776		if (direction)
2777		    *((char *)atdevbase+(segment*0x4000)+(ch*32)+line) =
2778					font[(ch*size)+line];
2779		else
2780		    font[(ch*size)+line] =
2781		    *((char *)atdevbase+(segment*0x4000)+(ch*32)+line);
2782	/* setup vga for text mode again */
2783	s = splhigh();
2784	inb(crtc_addr+6);				/* reset flip/flop */
2785	outb(ATC, 0x30); outb(ATC, 0x0C);
2786	outb(TSIDX, 0x02); outb(TSREG, 0x03);
2787	outb(TSIDX, 0x04); outb(TSREG, 0x02);
2788	outb(GDCIDX, 0x04); outb(GDCREG, 0x00);
2789	outb(GDCIDX, 0x05); outb(GDCREG, 0x10);
2790	if (crtc_addr == MONO_BASE) {
2791		outb(GDCIDX, 0x06); outb(GDCREG, 0x0A);	/* addr = b0000, 32kb */
2792	}
2793	else {
2794		outb(GDCIDX, 0x06); outb(GDCREG, 0x0E);	/* addr = b8000, 32kb */
2795	}
2796	splx(s);
2797 	outb(TSIDX, 0x01); val = inb(TSREG); 		/* unblank screen */
2798	outb(TSIDX, 0x01); outb(TSREG, val & 0xDF);
2799}
2800
2801static void
2802load_palette(void)
2803{
2804	int i;
2805
2806  	outb(PIXMASK, 0xFF);			/* no pixelmask */
2807  	outb(PALWADR, 0x00);
2808  	for (i=0x00; i<0x300; i++)
2809    		 outb(PALDATA, palette[i]);
2810	inb(crtc_addr+6);			/* reset flip/flop */
2811	outb(ATC, 0x20);			/* enable palette */
2812}
2813
2814static void
2815save_palette(void)
2816{
2817	int i;
2818
2819  	outb(PALRADR, 0x00);
2820  	for (i=0x00; i<0x300; i++)
2821    		palette[i] = inb(PALDATA);
2822	inb(crtc_addr+6);			/* reset flip/flop */
2823}
2824
2825#endif /* NSC */
2826