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