syscons.c revision 35029
1/*-
2 * Copyright (c) 1992-1997 S�ren Schmidt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software withough specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 *  $Id: syscons.c,v 1.256 1998/02/13 17:54:53 phk Exp $
29 */
30
31#include "sc.h"
32#include "apm.h"
33#include "opt_ddb.h"
34#include "opt_devfs.h"
35#include "opt_syscons.h"
36
37#if NSC > 0
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/conf.h>
41#include <sys/proc.h>
42#include <sys/signalvar.h>
43#include <sys/tty.h>
44#include <sys/kernel.h>
45#include <sys/malloc.h>
46#ifdef	DEVFS
47#include <sys/devfsext.h>
48#endif
49
50#include <machine/clock.h>
51#include <machine/cons.h>
52#include <machine/console.h>
53#include <machine/mouse.h>
54#include <machine/md_var.h>
55#include <machine/psl.h>
56#include <machine/frame.h>
57#include <machine/pc/display.h>
58#include <machine/apm_bios.h>
59#include <machine/random.h>
60#include <machine/bootinfo.h>
61
62#include <vm/vm.h>
63#include <vm/vm_param.h>
64#include <vm/pmap.h>
65
66#include <i386/isa/isa.h>
67#include <i386/isa/isa_device.h>
68#include <i386/isa/timerreg.h>
69#include <i386/isa/kbdtables.h>
70#include <i386/isa/kbdio.h>
71#include <i386/isa/syscons.h>
72
73#if !defined(MAXCONS)
74#define MAXCONS 16
75#endif
76
77#if !defined(SC_MAX_HISTORY_SIZE)
78#define SC_MAX_HISTORY_SIZE	(1000 * MAXCONS)
79#endif
80
81#if !defined(SC_HISTORY_SIZE)
82#define SC_HISTORY_SIZE		(ROW * 4)
83#endif
84
85#if (SC_HISTORY_SIZE * MAXCONS) > SC_MAX_HISTORY_SIZE
86#undef SC_MAX_HISTORY_SIZE
87#define SC_MAX_HISTORY_SIZE	(SC_HISTORY_SIZE * MAXCONS)
88#endif
89
90#if !defined(SC_MOUSE_CHAR)
91#define SC_MOUSE_CHAR		(0xd0)
92#endif
93
94#define COLD 0
95#define WARM 1
96
97#define MODE_MAP_SIZE		(M_VGA_CG320 + 1)
98#define MODE_PARAM_SIZE		64
99
100/* for backward compatibility */
101#define OLD_CONS_MOUSECTL	_IOWR('c', 10, old_mouse_info_t)
102
103typedef struct old_mouse_data {
104    int x;
105    int y;
106    int buttons;
107} old_mouse_data_t;
108
109typedef struct old_mouse_info {
110    int operation;
111    union {
112	struct old_mouse_data data;
113	struct mouse_mode mode;
114    } u;
115} old_mouse_info_t;
116
117/* XXX use sc_bcopy where video memory is concerned */
118extern void generic_bcopy(const void *, void *, size_t);
119
120static default_attr user_default = {
121    (FG_LIGHTGREY | BG_BLACK) << 8,
122    (FG_BLACK | BG_LIGHTGREY) << 8
123};
124
125static default_attr kernel_default = {
126    (FG_WHITE | BG_BLACK) << 8,
127    (FG_BLACK | BG_LIGHTGREY) << 8
128};
129
130static  scr_stat    	main_console;
131static  scr_stat    	*console[MAXCONS];
132#ifdef DEVFS
133static	void		*sc_devfs_token[MAXCONS];
134static	void		*sc_mouse_devfs_token;
135static	void		*sc_console_devfs_token;
136#endif
137	scr_stat    	*cur_console;
138static  scr_stat    	*new_scp, *old_scp;
139static  term_stat   	kernel_console;
140static  default_attr    *current_default;
141static  int     	flags = 0;
142static  int		sc_port = IO_KBD;
143static  KBDC		sc_kbdc = NULL;
144static  char        	init_done = COLD;
145static  u_short		sc_buffer[ROW*COL];
146static  char        	font_loading_in_progress = FALSE;
147static  char        	switch_in_progress = FALSE;
148static  char        	write_in_progress = FALSE;
149static  char        	blink_in_progress = FALSE;
150static  int        	blinkrate = 0;
151	u_int       	crtc_addr = MONO_BASE;
152	char		crtc_type = KD_MONO;
153	char        	crtc_vga = FALSE;
154static  u_char      	shfts = 0, ctls = 0, alts = 0, agrs = 0, metas = 0;
155static  u_char		accents = 0;
156static  u_char      	nlkcnt = 0, clkcnt = 0, slkcnt = 0, alkcnt = 0;
157static  const u_int     n_fkey_tab = sizeof(fkey_tab) / sizeof(*fkey_tab);
158static  int     	delayed_next_scr = FALSE;
159static  long        	scrn_blank_time = 0;    /* screen saver timeout value */
160	int     	scrn_blanked = 0;       /* screen saver active flag */
161static  struct timeval 	scrn_time_stamp;
162	u_char      	scr_map[256];
163	u_char      	scr_rmap[256];
164	char        	*video_mode_ptr = NULL;
165static	int		vesa_mode;
166	int     	fonts_loaded = 0
167#ifdef STD8X16FONT
168	| FONT_16
169#endif
170	;
171
172	char        	font_8[256*8];
173	char		font_14[256*14];
174#ifdef STD8X16FONT
175extern
176#endif
177	unsigned char	font_16[256*16];
178	char        	palette[256*3];
179static  char		*mode_map[MODE_MAP_SIZE];
180static  char		vgaregs[MODE_PARAM_SIZE];
181static  char		vgaregs2[MODE_PARAM_SIZE];
182static  int		rows_offset = 1;
183static	char 		*cut_buffer;
184static	int		mouse_level = 0;	/* sysmouse protocol level */
185static	mousestatus_t	mouse_status = { 0, 0, 0, 0, 0, 0 };
186static  u_short 	mouse_and_mask[16] = {
187				0xc000, 0xe000, 0xf000, 0xf800,
188				0xfc00, 0xfe00, 0xff00, 0xff80,
189				0xfe00, 0x1e00, 0x1f00, 0x0f00,
190				0x0f00, 0x0000, 0x0000, 0x0000
191			};
192static  u_short 	mouse_or_mask[16] = {
193				0x0000, 0x4000, 0x6000, 0x7000,
194				0x7800, 0x7c00, 0x7e00, 0x6800,
195				0x0c00, 0x0c00, 0x0600, 0x0600,
196				0x0000, 0x0000, 0x0000, 0x0000
197			};
198
199static int		extra_history_size =
200			    SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE * MAXCONS;
201
202static void    		none_saver(int blank) { }
203static void    		(*current_saver)(int blank) = none_saver;
204int  			(*sc_user_ioctl)(dev_t dev, int cmd, caddr_t data,
205					 int flag, struct proc *p) = NULL;
206
207/* OS specific stuff */
208#ifdef not_yet_done
209#define VIRTUAL_TTY(x)  (sccons[x] = ttymalloc(sccons[x]))
210struct  CONSOLE_TTY 	(sccons[MAXCONS] = ttymalloc(sccons[MAXCONS]))
211struct  MOUSE_TTY 	(sccons[MAXCONS+1] = ttymalloc(sccons[MAXCONS+1]))
212struct  tty         	*sccons[MAXCONS+2];
213#else
214#define VIRTUAL_TTY(x)  &sccons[x]
215#define CONSOLE_TTY 	&sccons[MAXCONS]
216#define MOUSE_TTY 	&sccons[MAXCONS+1]
217static struct tty     	sccons[MAXCONS+2];
218#endif
219#define SC_MOUSE 	128
220#define SC_CONSOLE	255
221#define MONO_BUF    	pa_to_va(0xB0000)
222#define CGA_BUF     	pa_to_va(0xB8000)
223u_short         	*Crtat;
224static const int	nsccons = MAXCONS+2;
225
226#define WRAPHIST(scp, pointer, offset)\
227    ((scp->history) + ((((pointer) - (scp->history)) + (scp->history_size)\
228    + (offset)) % (scp->history_size)))
229#define ISSIGVALID(sig)	((sig) > 0 && (sig) < NSIG)
230
231/* this should really be in `rtc.h' */
232#define RTC_EQUIPMENT		0x14
233
234/* prototypes */
235static int scattach(struct isa_device *dev);
236static int scparam(struct tty *tp, struct termios *t);
237static int scprobe(struct isa_device *dev);
238static int scvidprobe(int unit, int flags);
239static int sckbdprobe(int unit, int flags);
240static void scstart(struct tty *tp);
241static void scmousestart(struct tty *tp);
242static void scinit(void);
243static void map_mode_table(char *map[], char *table, int max);
244static u_char map_mode_num(u_char mode);
245static char *get_mode_param(scr_stat *scp, u_char mode);
246static u_int scgetc(u_int flags);
247#define SCGETC_CN	1
248#define SCGETC_NONBLOCK	2
249static void sccnupdate(scr_stat *scp);
250static scr_stat *get_scr_stat(dev_t dev);
251static scr_stat *alloc_scp(void);
252static void init_scp(scr_stat *scp);
253static void sc_bcopy(u_short *p, int from, int to, int mark);
254static int get_scr_num(void);
255static timeout_t scrn_timer;
256static void scrn_update(scr_stat *scp, int show_cursor);
257static void stop_scrn_saver(void (*saver)(int));
258static void clear_screen(scr_stat *scp);
259static int switch_scr(scr_stat *scp, u_int next_scr);
260static void exchange_scr(void);
261static void move_crsr(scr_stat *scp, int x, int y);
262static void scan_esc(scr_stat *scp, u_char c);
263static void draw_cursor_image(scr_stat *scp);
264static void remove_cursor_image(scr_stat *scp);
265static void ansi_put(scr_stat *scp, u_char *buf, int len);
266static u_char *get_fstr(u_int c, u_int *len);
267static void history_to_screen(scr_stat *scp);
268static int history_up_line(scr_stat *scp);
269static int history_down_line(scr_stat *scp);
270static int mask2attr(struct term_stat *term);
271static void set_keyboard(int command, int data);
272static void update_leds(int which);
273static void set_vgaregs(char *modetable);
274static void read_vgaregs(char *buf);
275#define COMP_IDENTICAL	0
276#define COMP_SIMILAR	1
277#define COMP_DIFFERENT	2
278static int comp_vgaregs(u_char *buf1, u_char *buf2);
279static void dump_vgaregs(u_char *buf);
280#define PARAM_BUFSIZE	6
281static void set_font_mode(u_char *buf);
282static void set_normal_mode(u_char *buf);
283static void set_destructive_cursor(scr_stat *scp);
284static void set_mouse_pos(scr_stat *scp);
285static int skip_spc_right(scr_stat *scp, u_short *p);
286static int skip_spc_left(scr_stat *scp, u_short *p);
287static void mouse_cut(scr_stat *scp);
288static void mouse_cut_start(scr_stat *scp);
289static void mouse_cut_end(scr_stat *scp);
290static void mouse_cut_word(scr_stat *scp);
291static void mouse_cut_line(scr_stat *scp);
292static void mouse_cut_extend(scr_stat *scp);
293static void mouse_paste(scr_stat *scp);
294static void draw_mouse_image(scr_stat *scp);
295static void remove_mouse_image(scr_stat *scp);
296static void draw_cutmarking(scr_stat *scp);
297static void remove_cutmarking(scr_stat *scp);
298static void save_palette(void);
299static void do_bell(scr_stat *scp, int pitch, int duration);
300static timeout_t blink_screen;
301#ifdef SC_SPLASH_SCREEN
302static void toggle_splash_screen(scr_stat *scp);
303#endif
304
305struct  isa_driver scdriver = {
306    scprobe, scattach, "sc", 1
307};
308
309static	d_open_t	scopen;
310static	d_close_t	scclose;
311static	d_read_t	scread;
312static	d_write_t	scwrite;
313static	d_ioctl_t	scioctl;
314static	d_devtotty_t	scdevtotty;
315static	d_mmap_t	scmmap;
316
317#define CDEV_MAJOR 12
318static	struct cdevsw	scdevsw = {
319	scopen,		scclose,	scread,		scwrite,
320	scioctl,	nullstop,	noreset,	scdevtotty,
321	ttpoll,		scmmap,		nostrategy,	"sc",	NULL,	-1 };
322
323/*
324 * These functions need to be before calls to them so they can be inlined.
325 */
326static void
327draw_cursor_image(scr_stat *scp)
328{
329    u_short cursor_image, *ptr = Crtat + (scp->cursor_pos - scp->scr_buf);
330    u_short prev_image;
331
332    if (vesa_mode) {
333	sc_bcopy(scp->scr_buf, scp->cursor_pos - scp->scr_buf,
334	  scp->cursor_pos - scp->scr_buf, 1);
335	return;
336    }
337
338    /* do we have a destructive cursor ? */
339    if (flags & CHAR_CURSOR) {
340	prev_image = scp->cursor_saveunder;
341	cursor_image = *ptr & 0x00ff;
342	if (cursor_image == DEAD_CHAR)
343	    cursor_image = prev_image & 0x00ff;
344	cursor_image |= *(scp->cursor_pos) & 0xff00;
345	scp->cursor_saveunder = cursor_image;
346	/* update the cursor bitmap if the char under the cursor has changed */
347	if (prev_image != cursor_image)
348	    set_destructive_cursor(scp);
349	/* modify cursor_image */
350	if (!(flags & BLINK_CURSOR)||((flags & BLINK_CURSOR)&&(blinkrate & 4))){
351	    /*
352	     * When the mouse pointer is at the same position as the cursor,
353	     * the cursor bitmap needs to be updated even if the char under
354	     * the cursor hasn't changed, because the mouse pionter may
355	     * have moved by a few dots within the cursor cel.
356	     */
357	    if ((prev_image == cursor_image)
358		    && (cursor_image != *(scp->cursor_pos)))
359	        set_destructive_cursor(scp);
360	    cursor_image &= 0xff00;
361	    cursor_image |= DEAD_CHAR;
362	}
363    } else {
364	cursor_image = (*(ptr) & 0x00ff) | *(scp->cursor_pos) & 0xff00;
365	scp->cursor_saveunder = cursor_image;
366	if (!(flags & BLINK_CURSOR)||((flags & BLINK_CURSOR)&&(blinkrate & 4))){
367	    if ((cursor_image & 0x7000) == 0x7000) {
368		cursor_image &= 0x8fff;
369		if(!(cursor_image & 0x0700))
370		    cursor_image |= 0x0700;
371	    } else {
372		cursor_image |= 0x7000;
373		if ((cursor_image & 0x0700) == 0x0700)
374		    cursor_image &= 0xf0ff;
375	    }
376	}
377    }
378    *ptr = cursor_image;
379}
380
381static void
382remove_cursor_image(scr_stat *scp)
383{
384    if (vesa_mode)
385	sc_bcopy(scp->scr_buf, scp->cursor_oldpos - scp->scr_buf,
386	  scp->cursor_oldpos - scp->scr_buf, 0);
387    else
388	*(Crtat + (scp->cursor_oldpos - scp->scr_buf)) = scp->cursor_saveunder;
389}
390
391static void
392move_crsr(scr_stat *scp, int x, int y)
393{
394    if (x < 0)
395	x = 0;
396    if (y < 0)
397	y = 0;
398    if (x >= scp->xsize)
399	x = scp->xsize-1;
400    if (y >= scp->ysize)
401	y = scp->ysize-1;
402    scp->xpos = x;
403    scp->ypos = y;
404    scp->cursor_pos = scp->scr_buf + scp->ypos * scp->xsize + scp->xpos;
405}
406
407static int
408scprobe(struct isa_device *dev)
409{
410    if (!scvidprobe(dev->id_unit, dev->id_flags)) {
411	if (bootverbose)
412	    printf("sc%d: no video adapter is found.\n", dev->id_unit);
413	return (0);
414    }
415
416    sc_port = dev->id_iobase;
417    if (sckbdprobe(dev->id_unit, dev->id_flags))
418	return (IO_KBDSIZE);
419    else
420        return ((dev->id_flags & DETECT_KBD) ? 0 : IO_KBDSIZE);
421}
422
423/* probe video adapters, return TRUE if found */
424static int
425scvidprobe(int unit, int flags)
426{
427    /*
428     * XXX don't try to `printf' anything here, the console may not have
429     * been configured yet.
430     */
431    u_short volatile *cp;
432    u_short was;
433    u_long  pa;
434    u_long  segoff;
435
436    /* do this test only once */
437    if (init_done != COLD)
438	return (Crtat != 0);
439
440    /*
441     * Finish defaulting crtc variables for a mono screen.  Crtat is a
442     * bogus common variable so that it can be shared with pcvt, so it
443     * can't be statically initialized.  XXX.
444     */
445    Crtat = (u_short *)MONO_BUF;
446    crtc_type = KD_MONO;
447    /* If CGA memory seems to work, switch to color.  */
448    cp = (u_short *)CGA_BUF;
449    was = *cp;
450    *cp = (u_short) 0xA55A;
451    if (bootinfo.bi_vesa == 0x102) {
452	vesa_mode = bootinfo.bi_vesa;
453	Crtat  = (u_short *)pa_to_va(0xA0000);
454	crtc_type = KD_PIXEL;
455        bzero(Crtat, 800*600/8);
456    } else if (*cp == 0xA55A) {
457	Crtat = (u_short *)CGA_BUF;
458	crtc_addr = COLOR_BASE;
459	crtc_type = KD_CGA;
460    } else {
461        cp = Crtat;
462	was = *cp;
463	*cp = (u_short) 0xA55A;
464	if (*cp != 0xA55A) {
465	    /* no screen at all, bail out */
466	    Crtat = 0;
467	    return FALSE;
468	}
469    }
470    *cp = was;
471
472    if (crtc_type != KD_PIXEL) {
473	/*
474	 * Check rtc and BIOS date area.
475	 * XXX: don't use BIOSDATA_EQUIPMENT, it is not a dead copy
476	 * of RTC_EQUIPMENT. The bit 4 and 5 of the ETC_EQUIPMENT are
477	 * zeros for EGA and VGA. However, the EGA/VGA BIOS will set
478	 * these bits in BIOSDATA_EQUIPMENT according to the monitor
479	 * type detected.
480	 */
481	switch ((rtcin(RTC_EQUIPMENT) >> 4) & 3) {	/* bit 4 and 5 */
482	case 0: /* EGA/VGA, or nothing */
483	    crtc_type = KD_EGA;
484	    /* the color adapter may be in the 40x25 mode... XXX */
485	    break;
486	case 1: /* CGA 40x25 */
487	    /* switch to the 80x25 mode? XXX */
488	    /* FALL THROUGH */
489	case 2: /* CGA 80x25 */
490	    /* `crtc_type' has already been set... */
491	    /* crtc_type = KD_CGA; */
492	    break;
493	case 3: /* MDA */
494	    /* `crtc_type' has already been set... */
495	    /* crtc_type = KD_MONO; */
496	    break;
497	}
498
499	/* is this a VGA or higher ? */
500	outb(crtc_addr, 7);
501	if (inb(crtc_addr) == 7) {
502
503	    crtc_type = KD_VGA;
504	    crtc_vga = TRUE;
505	    read_vgaregs(vgaregs);
506
507	    /* Get the BIOS video mode pointer */
508	    segoff = *(u_long *)pa_to_va(0x4a8);
509	    pa = (((segoff & 0xffff0000) >> 12) + (segoff & 0xffff));
510	    if (ISMAPPED(pa, sizeof(u_long))) {
511		segoff = *(u_long *)pa_to_va(pa);
512		pa = (((segoff & 0xffff0000) >> 12) + (segoff & 0xffff));
513		if (ISMAPPED(pa, MODE_PARAM_SIZE))
514		    video_mode_ptr = (char *)pa_to_va(pa);
515	    }
516	}
517    }
518
519    return TRUE;
520}
521
522/* probe the keyboard, return TRUE if found */
523static int
524sckbdprobe(int unit, int flags)
525{
526    int codeset;
527    int c = -1;
528    int m;
529
530    sc_kbdc = kbdc_open(sc_port);
531
532    if (!kbdc_lock(sc_kbdc, TRUE)) {
533	/* driver error? */
534	printf("sc%d: unable to lock the controller.\n", unit);
535        return ((flags & DETECT_KBD) ? FALSE : TRUE);
536    }
537
538    /* discard anything left after UserConfig */
539    empty_both_buffers(sc_kbdc, 10);
540
541    /* save the current keyboard controller command byte */
542    m = kbdc_get_device_mask(sc_kbdc) & ~KBD_KBD_CONTROL_BITS;
543    c = get_controller_command_byte(sc_kbdc);
544    if (c == -1) {
545	/* CONTROLLER ERROR */
546	printf("sc%d: unable to get the current command byte value.\n", unit);
547	goto fail;
548    }
549    if (bootverbose)
550	printf("sc%d: the current keyboard controller command byte %04x\n",
551	    unit, c);
552#if 0
553    /* override the keyboard lock switch */
554    c |= KBD_OVERRIDE_KBD_LOCK;
555#endif
556
557    /*
558     * The keyboard may have been screwed up by the boot block.
559     * We may just be able to recover from error by testing the controller
560     * and the keyboard port. The controller command byte needs to be saved
561     * before this recovery operation, as some controllers seem to set
562     * the command byte to particular values.
563     */
564    test_controller(sc_kbdc);
565    test_kbd_port(sc_kbdc);
566
567    /* enable the keyboard port, but disable the keyboard intr. */
568    if (!set_controller_command_byte(sc_kbdc,
569            KBD_KBD_CONTROL_BITS,
570            KBD_ENABLE_KBD_PORT | KBD_DISABLE_KBD_INT)) {
571	/* CONTROLLER ERROR
572	 * there is very little we can do...
573	 */
574	printf("sc%d: unable to set the command byte.\n", unit);
575	goto fail;
576     }
577
578     /*
579      * Check if we have an XT keyboard before we attempt to reset it.
580      * The procedure assumes that the keyboard and the controller have
581      * been set up properly by BIOS and have not been messed up
582      * during the boot process.
583      */
584     codeset = -1;
585     if (flags & XT_KEYBD)
586	 /* the user says there is a XT keyboard */
587	 codeset = 1;
588#ifdef DETECT_XT_KEYBOARD
589     else if ((c & KBD_TRANSLATION) == 0) {
590	 /* SET_SCANCODE_SET is not always supported; ignore error */
591	 if (send_kbd_command_and_data(sc_kbdc, KBDC_SET_SCANCODE_SET, 0)
592		 == KBD_ACK)
593	     codeset = read_kbd_data(sc_kbdc);
594     }
595     if (bootverbose)
596         printf("sc%d: keyboard scancode set %d\n", unit, codeset);
597#endif /* DETECT_XT_KEYBOARD */
598
599    if (flags & KBD_NORESET) {
600        write_kbd_command(sc_kbdc, KBDC_ECHO);
601        if (read_kbd_data(sc_kbdc) != KBD_ECHO) {
602            empty_both_buffers(sc_kbdc, 10);
603            test_controller(sc_kbdc);
604            test_kbd_port(sc_kbdc);
605            if (bootverbose)
606                printf("sc%d: failed to get response from the keyboard.\n",
607		    unit);
608	    goto fail;
609	}
610    } else {
611        /* reset keyboard hardware */
612        if (!reset_kbd(sc_kbdc)) {
613            /* KEYBOARD ERROR
614             * Keyboard reset may fail either because the keyboard doen't
615             * exist, or because the keyboard doesn't pass the self-test,
616             * or the keyboard controller on the motherboard and the keyboard
617             * somehow fail to shake hands. It is just possible, particularly
618             * in the last case, that the keyoard controller may be left
619             * in a hung state. test_controller() and test_kbd_port() appear
620             * to bring the keyboard controller back (I don't know why and
621             * how, though.)
622             */
623            empty_both_buffers(sc_kbdc, 10);
624            test_controller(sc_kbdc);
625            test_kbd_port(sc_kbdc);
626            /* We could disable the keyboard port and interrupt... but,
627             * the keyboard may still exist (see above).
628             */
629            if (bootverbose)
630                printf("sc%d: failed to reset the keyboard.\n", unit);
631            goto fail;
632        }
633    }
634
635    /*
636     * Allow us to set the XT_KEYBD flag in UserConfig so that keyboards
637     * such as those on the IBM ThinkPad laptop computers can be used
638     * with the standard console driver.
639     */
640    if (codeset == 1) {
641	if (send_kbd_command_and_data(
642	        sc_kbdc, KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
643	    /* XT kbd doesn't need scan code translation */
644	    c &= ~KBD_TRANSLATION;
645	} else {
646	    /* KEYBOARD ERROR
647	     * The XT kbd isn't usable unless the proper scan code set
648	     * is selected.
649	     */
650	    printf("sc%d: unable to set the XT keyboard mode.\n", unit);
651	    goto fail;
652	}
653    }
654    /* enable the keyboard port and intr. */
655    if (!set_controller_command_byte(sc_kbdc,
656            KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
657	    (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
658	        | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
659	/* CONTROLLER ERROR
660	 * This is serious; we are left with the disabled keyboard intr.
661	 */
662	printf("sc%d: unable to enable the keyboard port and intr.\n", unit);
663	goto fail;
664    }
665
666    kbdc_set_device_mask(sc_kbdc, m | KBD_KBD_CONTROL_BITS),
667    kbdc_lock(sc_kbdc, FALSE);
668    return TRUE;
669
670fail:
671    if (c != -1)
672        /* try to restore the command byte as before, if possible */
673        set_controller_command_byte(sc_kbdc, 0xff, c);
674    kbdc_set_device_mask(sc_kbdc,
675        (flags & DETECT_KBD) ? m : m | KBD_KBD_CONTROL_BITS);
676    kbdc_lock(sc_kbdc, FALSE);
677    return FALSE;
678}
679
680#if NAPM > 0
681static int
682scresume(void *dummy)
683{
684	shfts = ctls = alts = agrs = metas = accents = 0;
685	return 0;
686}
687#endif
688
689static int
690scattach(struct isa_device *dev)
691{
692    scr_stat *scp;
693    dev_t cdev = makedev(CDEV_MAJOR, 0);
694    char *p;
695#ifdef DEVFS
696    int vc;
697#endif
698
699    scinit();
700    flags = dev->id_flags;
701    if (!crtc_vga)
702	flags &= ~CHAR_CURSOR;
703
704    scp = console[0];
705
706    if (crtc_vga) {
707    	cut_buffer = (char *)malloc(scp->xsize*scp->ysize, M_DEVBUF, M_NOWAIT);
708    }
709
710    scp->scr_buf = (u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
711				     M_DEVBUF, M_NOWAIT);
712
713    /* copy temporary buffer to final buffer */
714    bcopy(sc_buffer, scp->scr_buf, scp->xsize * scp->ysize * sizeof(u_short));
715
716    scp->cursor_pos = scp->cursor_oldpos =
717	scp->scr_buf + scp->xpos + scp->ypos * scp->xsize;
718    scp->mouse_pos = scp->mouse_oldpos =
719	scp->scr_buf + ((scp->mouse_ypos/scp->font_size)*scp->xsize +
720	    		scp->mouse_xpos/8);
721
722    /* initialize history buffer & pointers */
723    scp->history_head = scp->history_pos =
724	(u_short *)malloc(scp->history_size*sizeof(u_short),
725			  M_DEVBUF, M_NOWAIT);
726    if (scp->history_head != NULL)
727        bzero(scp->history_head, scp->history_size*sizeof(u_short));
728    scp->history = scp->history_head;
729
730    /* initialize cursor stuff */
731    if (!(scp->status & UNKNOWN_MODE))
732    	draw_cursor_image(scp);
733
734    /* get screen update going */
735    scrn_timer(NULL);
736
737    update_leds(scp->status);
738
739    if ((crtc_type == KD_VGA) && bootverbose) {
740        printf("sc%d: BIOS video mode:%d\n",
741	    dev->id_unit, *(u_char *)pa_to_va(0x449));
742        printf("sc%d: VGA registers upon power-up\n", dev->id_unit);
743        dump_vgaregs(vgaregs);
744        printf("sc%d: video mode:%d\n", dev->id_unit, scp->mode);
745        printf("sc%d: VGA registers in BIOS for mode:%d\n",
746		dev->id_unit, scp->mode);
747        dump_vgaregs(vgaregs2);
748	p = get_mode_param(scp, scp->mode);
749        if (p != NULL) {
750            printf("sc%d: VGA registers to be used for mode:%d\n",
751		dev->id_unit, scp->mode);
752            dump_vgaregs(p);
753        }
754        printf("sc%d: rows_offset:%d\n", dev->id_unit, rows_offset);
755    }
756    if ((crtc_type == KD_VGA) && (video_mode_ptr == NULL))
757        printf("sc%d: WARNING: video mode switching is only partially supported\n",
758	        dev->id_unit);
759
760    printf("sc%d: ", dev->id_unit);
761    switch(crtc_type) {
762    case KD_VGA:
763	if (crtc_addr == MONO_BASE)
764	    printf("VGA mono");
765	else
766	    printf("VGA color");
767	break;
768    case KD_EGA:
769	if (crtc_addr == MONO_BASE)
770	    printf("EGA mono");
771	else
772	    printf("EGA color");
773	break;
774    case KD_CGA:
775	printf("CGA");
776	break;
777    case KD_PIXEL:
778	printf("Graphics display (VESA mode = 0x%x)", vesa_mode);
779	break;
780    case KD_MONO:
781    case KD_HERCULES:
782    default:
783	printf("MDA/hercules");
784	break;
785    }
786    printf(" <%d virtual consoles, flags=0x%x>\n", MAXCONS, flags);
787
788#if NAPM > 0
789    scp->r_hook.ah_fun = scresume;
790    scp->r_hook.ah_arg = NULL;
791    scp->r_hook.ah_name = "system keyboard";
792    scp->r_hook.ah_order = APM_MID_ORDER;
793    apm_hook_establish(APM_HOOK_RESUME , &scp->r_hook);
794#endif
795
796    cdevsw_add(&cdev, &scdevsw, NULL);
797
798#ifdef DEVFS
799    for (vc = 0; vc < MAXCONS; vc++)
800        sc_devfs_token[vc] = devfs_add_devswf(&scdevsw, vc, DV_CHR,
801				UID_ROOT, GID_WHEEL, 0600, "ttyv%n", vc);
802    sc_mouse_devfs_token = devfs_add_devswf(&scdevsw, SC_MOUSE, DV_CHR,
803				UID_ROOT, GID_WHEEL, 0600, "sysmouse");
804    sc_console_devfs_token = devfs_add_devswf(&scdevsw, SC_CONSOLE, DV_CHR,
805				UID_ROOT, GID_WHEEL, 0600, "consolectl");
806#endif
807    return 0;
808}
809
810struct tty
811*scdevtotty(dev_t dev)
812{
813    int unit = minor(dev);
814
815    if (init_done == COLD)
816	return(NULL);
817    if (unit == SC_CONSOLE)
818	return CONSOLE_TTY;
819    if (unit == SC_MOUSE)
820	return MOUSE_TTY;
821    if (unit >= MAXCONS || unit < 0)
822	return(NULL);
823    return VIRTUAL_TTY(unit);
824}
825
826int
827scopen(dev_t dev, int flag, int mode, struct proc *p)
828{
829    struct tty *tp = scdevtotty(dev);
830
831    if (!tp)
832	return(ENXIO);
833
834    tp->t_oproc = (minor(dev) == SC_MOUSE) ? scmousestart : scstart;
835    tp->t_param = scparam;
836    tp->t_dev = dev;
837    if (!(tp->t_state & TS_ISOPEN)) {
838	ttychars(tp);
839        /* Use the current setting of the <-- key as default VERASE. */
840        /* If the Delete key is preferable, an stty is necessary     */
841        tp->t_cc[VERASE] = key_map.key[0x0e].map[0];
842	tp->t_iflag = TTYDEF_IFLAG;
843	tp->t_oflag = TTYDEF_OFLAG;
844	tp->t_cflag = TTYDEF_CFLAG;
845	tp->t_lflag = TTYDEF_LFLAG;
846	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
847	scparam(tp, &tp->t_termios);
848	ttsetwater(tp);
849	(*linesw[tp->t_line].l_modem)(tp, 1);
850    	if (minor(dev) == SC_MOUSE)
851	    mouse_level = 0;		/* XXX */
852    }
853    else
854	if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
855	    return(EBUSY);
856    if (minor(dev) < MAXCONS && !console[minor(dev)]) {
857	console[minor(dev)] = alloc_scp();
858    }
859    if (minor(dev)<MAXCONS && !tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
860	tp->t_winsize.ws_col = console[minor(dev)]->xsize;
861	tp->t_winsize.ws_row = console[minor(dev)]->ysize;
862    }
863    return ((*linesw[tp->t_line].l_open)(dev, tp));
864}
865
866int
867scclose(dev_t dev, int flag, int mode, struct proc *p)
868{
869    struct tty *tp = scdevtotty(dev);
870    struct scr_stat *scp;
871
872    if (!tp)
873	return(ENXIO);
874    if (minor(dev) < MAXCONS) {
875	scp = get_scr_stat(tp->t_dev);
876	if (scp->status & SWITCH_WAIT_ACQ)
877	    wakeup((caddr_t)&scp->smode);
878#if not_yet_done
879	if (scp == &main_console) {
880	    scp->pid = 0;
881	    scp->proc = NULL;
882	    scp->smode.mode = VT_AUTO;
883	}
884	else {
885	    free(scp->scr_buf, M_DEVBUF);
886	    if (scp->history != NULL) {
887		free(scp->history, M_DEVBUF);
888		if (scp->history_size / scp->xsize
889			> imax(SC_HISTORY_SIZE, scp->ysize))
890		    extra_history_size += scp->history_size / scp->xsize
891			- imax(SC_HISTORY_SIZE, scp->ysize);
892	    }
893	    free(scp, M_DEVBUF);
894	    console[minor(dev)] = NULL;
895	}
896#else
897	scp->pid = 0;
898	scp->proc = NULL;
899	scp->smode.mode = VT_AUTO;
900#endif
901    }
902    spltty();
903    (*linesw[tp->t_line].l_close)(tp, flag);
904    ttyclose(tp);
905    spl0();
906    return(0);
907}
908
909int
910scread(dev_t dev, struct uio *uio, int flag)
911{
912    struct tty *tp = scdevtotty(dev);
913
914    if (!tp)
915	return(ENXIO);
916    return((*linesw[tp->t_line].l_read)(tp, uio, flag));
917}
918
919int
920scwrite(dev_t dev, struct uio *uio, int flag)
921{
922    struct tty *tp = scdevtotty(dev);
923
924    if (!tp)
925	return(ENXIO);
926    return((*linesw[tp->t_line].l_write)(tp, uio, flag));
927}
928
929void
930scintr(int unit)
931{
932    static struct tty *cur_tty;
933    int c, len;
934    u_char *cp;
935
936    /*
937     * Loop while there is still input to get from the keyboard.
938     * I don't think this is nessesary, and it doesn't fix
939     * the Xaccel-2.1 keyboard hang, but it can't hurt.		XXX
940     */
941    while ((c = scgetc(SCGETC_NONBLOCK)) != NOKEY) {
942
943	cur_tty = VIRTUAL_TTY(get_scr_num());
944	if (!(cur_tty->t_state & TS_ISOPEN))
945	    if (!((cur_tty = CONSOLE_TTY)->t_state & TS_ISOPEN))
946		continue;
947
948	switch (c & 0xff00) {
949	case 0x0000: /* normal key */
950	    (*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
951	    break;
952	case FKEY:  /* function key, return string */
953	    if (cp = get_fstr((u_int)c, (u_int *)&len)) {
954	    	while (len-- >  0)
955		    (*linesw[cur_tty->t_line].l_rint)(*cp++ & 0xFF, cur_tty);
956	    }
957	    break;
958	case MKEY:  /* meta is active, prepend ESC */
959	    (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
960	    (*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
961	    break;
962	case BKEY:  /* backtab fixed sequence (esc [ Z) */
963	    (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
964	    (*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
965	    (*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
966	    break;
967	}
968    }
969
970    if (cur_console->status & MOUSE_ENABLED) {
971	cur_console->status &= ~MOUSE_VISIBLE;
972	remove_mouse_image(cur_console);
973    }
974}
975
976static int
977scparam(struct tty *tp, struct termios *t)
978{
979    tp->t_ispeed = t->c_ispeed;
980    tp->t_ospeed = t->c_ospeed;
981    tp->t_cflag = t->c_cflag;
982    return 0;
983}
984
985int
986scioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
987{
988    int error;
989    u_int i;
990    struct tty *tp;
991    scr_stat *scp;
992    u_short *usp;
993    char *mp;
994    int s;
995
996    tp = scdevtotty(dev);
997    if (!tp)
998	return ENXIO;
999    scp = get_scr_stat(tp->t_dev);
1000
1001    /* If there is a user_ioctl function call that first */
1002    if (sc_user_ioctl) {
1003	if (error = (*sc_user_ioctl)(dev, cmd, data, flag, p))
1004	    return error;
1005    }
1006
1007    switch (cmd) {  		/* process console hardware related ioctl's */
1008
1009    case GIO_ATTR:      	/* get current attributes */
1010	*(int*)data = (scp->term.cur_attr >> 8) & 0xFF;
1011	return 0;
1012
1013    case GIO_COLOR:     	/* is this a color console ? */
1014	if (crtc_addr == COLOR_BASE)
1015	    *(int*)data = 1;
1016	else
1017	    *(int*)data = 0;
1018	return 0;
1019
1020    case CONS_CURRENT:  	/* get current adapter type */
1021	*(int *)data = crtc_type;
1022	return 0;
1023
1024    case CONS_GET:      	/* get current video mode */
1025	*(int*)data = scp->mode;
1026	return 0;
1027
1028    case CONS_BLANKTIME:    	/* set screen saver timeout (0 = no saver) */
1029	if (*(int *)data < 0)
1030            return EINVAL;
1031	scrn_blank_time = *(int *)data;
1032	if (scrn_blank_time == 0)
1033	    getmicroruntime(&scrn_time_stamp);
1034	return 0;
1035
1036    case CONS_CURSORTYPE:   	/* set cursor type blink/noblink */
1037	if ((*(int*)data) & 0x01)
1038	    flags |= BLINK_CURSOR;
1039	else
1040	    flags &= ~BLINK_CURSOR;
1041	if ((*(int*)data) & 0x02) {
1042	    if (!crtc_vga)
1043		return ENXIO;
1044	    flags |= CHAR_CURSOR;
1045	} else
1046	    flags &= ~CHAR_CURSOR;
1047	/*
1048	 * The cursor shape is global property; all virtual consoles
1049	 * are affected. Update the cursor in the current console...
1050	 */
1051	if (!(cur_console->status & UNKNOWN_MODE)) {
1052            remove_cursor_image(cur_console);
1053	    if (flags & CHAR_CURSOR)
1054	        set_destructive_cursor(cur_console);
1055	    draw_cursor_image(cur_console);
1056	}
1057	return 0;
1058
1059    case CONS_BELLTYPE: 	/* set bell type sound/visual */
1060	if (*data)
1061	    flags |= VISUAL_BELL;
1062	else
1063	    flags &= ~VISUAL_BELL;
1064	return 0;
1065
1066    case CONS_HISTORY:  	/* set history size */
1067	if (*(int *)data > 0) {
1068	    int lines;	/* buffer size to allocate */
1069	    int lines0;	/* current buffer size */
1070
1071	    lines = imax(*(int *)data, scp->ysize);
1072	    lines0 = (scp->history != NULL) ?
1073		      scp->history_size / scp->xsize : scp->ysize;
1074	    /*
1075	     * syscons unconditionally allocates buffers upto SC_HISTORY_SIZE
1076	     * lines or scp->ysize lines, whichever is larger. A value
1077	     * greater than that is allowed, subject to extra_history_size.
1078	     */
1079	    if (lines > imax(lines0, SC_HISTORY_SIZE) + extra_history_size)
1080                return EINVAL;
1081            if (cur_console->status & BUFFER_SAVED)
1082                return EBUSY;
1083	    usp = scp->history;
1084	    scp->history = NULL;
1085	    if (usp != NULL)
1086		free(usp, M_DEVBUF);
1087	    scp->history_size = lines * scp->xsize;
1088	    /*
1089	     * extra_history_size +=
1090	     *    (lines0 > imax(SC_HISTORY_SIZE, scp->ysize)) ?
1091	     *     lines0 - imax(SC_HISTORY_SIZE, scp->ysize)) : 0;
1092	     * extra_history_size -=
1093	     *    (lines > imax(SC_HISTORY_SIZE, scp->ysize)) ?
1094	     *	   lines - imax(SC_HISTORY_SIZE, scp->ysize)) : 0;
1095	     * lines0 >= ysize && lines >= ysize... Hey, the above can be
1096	     * reduced to the following...
1097	     */
1098	    extra_history_size +=
1099		imax(lines0, SC_HISTORY_SIZE) - imax(lines, SC_HISTORY_SIZE);
1100	    usp = (u_short *)malloc(scp->history_size * sizeof(u_short),
1101				    M_DEVBUF, M_WAITOK);
1102	    bzero(usp, scp->history_size * sizeof(u_short));
1103	    scp->history_head = scp->history_pos = usp;
1104	    scp->history = usp;
1105	    return 0;
1106	}
1107	else
1108	    return EINVAL;
1109
1110    case CONS_MOUSECTL:		/* control mouse arrow */
1111    case OLD_CONS_MOUSECTL:
1112    {
1113	/* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
1114	static butmap[8] = {
1115            MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP
1116		| MOUSE_MSC_BUTTON3UP,
1117            MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1118            MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1119            MOUSE_MSC_BUTTON3UP,
1120            MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1121            MOUSE_MSC_BUTTON2UP,
1122            MOUSE_MSC_BUTTON1UP,
1123            0,
1124	};
1125	mouse_info_t *mouse = (mouse_info_t*)data;
1126	mouse_info_t buf;
1127
1128	if (!crtc_vga)
1129	    return ENODEV;
1130
1131	if (cmd == OLD_CONS_MOUSECTL) {
1132	    static unsigned char swapb[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
1133	    old_mouse_info_t *old_mouse = (old_mouse_info_t *)data;
1134
1135	    mouse = &buf;
1136	    mouse->operation = old_mouse->operation;
1137	    switch (mouse->operation) {
1138	    case MOUSE_MODE:
1139		mouse->u.mode = old_mouse->u.mode;
1140		break;
1141	    case MOUSE_SHOW:
1142	    case MOUSE_HIDE:
1143		break;
1144	    case MOUSE_MOVEABS:
1145	    case MOUSE_MOVEREL:
1146	    case MOUSE_ACTION:
1147		mouse->u.data.x = old_mouse->u.data.x;
1148		mouse->u.data.y = old_mouse->u.data.y;
1149		mouse->u.data.z = 0;
1150		mouse->u.data.buttons = swapb[old_mouse->u.data.buttons & 0x7];
1151		break;
1152	    case MOUSE_GETINFO:
1153		old_mouse->u.data.x = scp->mouse_xpos;
1154		old_mouse->u.data.y = scp->mouse_ypos;
1155		old_mouse->u.data.buttons = swapb[scp->mouse_buttons & 0x7];
1156		break;
1157	    default:
1158		return EINVAL;
1159	    }
1160	}
1161
1162	switch (mouse->operation) {
1163	case MOUSE_MODE:
1164	    if (ISSIGVALID(mouse->u.mode.signal)) {
1165		scp->mouse_signal = mouse->u.mode.signal;
1166		scp->mouse_proc = p;
1167		scp->mouse_pid = p->p_pid;
1168	    }
1169	    else {
1170		scp->mouse_signal = 0;
1171		scp->mouse_proc = NULL;
1172		scp->mouse_pid = 0;
1173	    }
1174	    break;
1175
1176	case MOUSE_SHOW:
1177	    if (!(scp->status & MOUSE_ENABLED)) {
1178		scp->status |= (MOUSE_ENABLED | MOUSE_VISIBLE);
1179		scp->mouse_oldpos = scp->mouse_pos;
1180		mark_all(scp);
1181	    }
1182	    else
1183		return EINVAL;
1184	    break;
1185
1186	case MOUSE_HIDE:
1187	    if (scp->status & MOUSE_ENABLED) {
1188		scp->status &= ~(MOUSE_ENABLED | MOUSE_VISIBLE);
1189		mark_all(scp);
1190	    }
1191	    else
1192		return EINVAL;
1193	    break;
1194
1195	case MOUSE_MOVEABS:
1196	    scp->mouse_xpos = mouse->u.data.x;
1197	    scp->mouse_ypos = mouse->u.data.y;
1198	    set_mouse_pos(scp);
1199	    break;
1200
1201	case MOUSE_MOVEREL:
1202	    scp->mouse_xpos += mouse->u.data.x;
1203	    scp->mouse_ypos += mouse->u.data.y;
1204	    set_mouse_pos(scp);
1205	    break;
1206
1207	case MOUSE_GETINFO:
1208	    mouse->u.data.x = scp->mouse_xpos;
1209	    mouse->u.data.y = scp->mouse_ypos;
1210	    mouse->u.data.z = 0;
1211	    mouse->u.data.buttons = scp->mouse_buttons;
1212	    break;
1213
1214	case MOUSE_ACTION:
1215	case MOUSE_MOTION_EVENT:
1216	    /* this should maybe only be settable from /dev/consolectl SOS */
1217	    /* send out mouse event on /dev/sysmouse */
1218
1219	    mouse_status.dx += mouse->u.data.x;
1220	    mouse_status.dy += mouse->u.data.y;
1221	    mouse_status.dz += mouse->u.data.z;
1222	    if (mouse->operation == MOUSE_ACTION)
1223	        mouse_status.button = mouse->u.data.buttons;
1224	    mouse_status.flags |=
1225		((mouse->u.data.x || mouse->u.data.y || mouse->u.data.z) ?
1226		    MOUSE_POSCHANGED : 0)
1227		| (mouse_status.obutton ^ mouse_status.button);
1228
1229	    if (cur_console->status & MOUSE_ENABLED)
1230	    	cur_console->status |= MOUSE_VISIBLE;
1231
1232	    if ((MOUSE_TTY)->t_state & TS_ISOPEN) {
1233		u_char buf[MOUSE_SYS_PACKETSIZE];
1234		int j;
1235
1236		/* the first five bytes are compatible with MouseSystems' */
1237		buf[0] = MOUSE_MSC_SYNC
1238		    | butmap[mouse_status.button & MOUSE_STDBUTTONS];
1239		j = imax(imin(mouse->u.data.x, 255), -256);
1240		buf[1] = j >> 1;
1241		buf[3] = j - buf[1];
1242		j = -imax(imin(mouse->u.data.y, 255), -256);
1243		buf[2] = j >> 1;
1244		buf[4] = j - buf[2];
1245		for (j = 0; j < MOUSE_MSC_PACKETSIZE; j++)
1246	    		(*linesw[(MOUSE_TTY)->t_line].l_rint)(buf[j],MOUSE_TTY);
1247		if (mouse_level >= 1) { 	/* extended part */
1248		    j = imax(imin(mouse->u.data.z, 127), -128);
1249		    buf[5] = (j >> 1) & 0x7f;
1250		    buf[6] = (j - (j >> 1)) & 0x7f;
1251		    /* buttons 4-10 */
1252		    buf[7] = (~mouse_status.button >> 3) & 0x7f;
1253		    for (j = MOUSE_MSC_PACKETSIZE;
1254			 j < MOUSE_SYS_PACKETSIZE; j++)
1255	    		(*linesw[(MOUSE_TTY)->t_line].l_rint)(buf[j],MOUSE_TTY);
1256		}
1257	    }
1258
1259	    if (cur_console->mouse_signal) {
1260		cur_console->mouse_buttons = mouse->u.data.buttons;
1261    		/* has controlling process died? */
1262		if (cur_console->mouse_proc &&
1263		    (cur_console->mouse_proc != pfind(cur_console->mouse_pid))){
1264		    	cur_console->mouse_signal = 0;
1265			cur_console->mouse_proc = NULL;
1266			cur_console->mouse_pid = 0;
1267		}
1268		else
1269		    psignal(cur_console->mouse_proc, cur_console->mouse_signal);
1270	    }
1271	    else if (mouse->operation == MOUSE_ACTION) {
1272		/* process button presses */
1273		if ((cur_console->mouse_buttons ^ mouse->u.data.buttons) &&
1274		    !(cur_console->status & UNKNOWN_MODE)) {
1275		    cur_console->mouse_buttons = mouse->u.data.buttons;
1276		    if (cur_console->mouse_buttons & MOUSE_BUTTON1DOWN)
1277			mouse_cut_start(cur_console);
1278		    else
1279			mouse_cut_end(cur_console);
1280		    if (cur_console->mouse_buttons & MOUSE_BUTTON2DOWN ||
1281			cur_console->mouse_buttons & MOUSE_BUTTON3DOWN)
1282			mouse_paste(cur_console);
1283		}
1284	    }
1285
1286	    if (mouse->u.data.x != 0 || mouse->u.data.y != 0) {
1287		cur_console->mouse_xpos += mouse->u.data.x;
1288		cur_console->mouse_ypos += mouse->u.data.y;
1289		set_mouse_pos(cur_console);
1290	    }
1291
1292	    break;
1293
1294	case MOUSE_BUTTON_EVENT:
1295	    if ((mouse->u.event.id & MOUSE_BUTTONS) == 0)
1296		return EINVAL;
1297	    if (mouse->u.event.value < 0)
1298		return EINVAL;
1299
1300	    if (mouse->u.event.value > 0) {
1301	        cur_console->mouse_buttons |= mouse->u.event.id;
1302	        mouse_status.button |= mouse->u.event.id;
1303	    } else {
1304	        cur_console->mouse_buttons &= ~mouse->u.event.id;
1305	        mouse_status.button &= ~mouse->u.event.id;
1306	    }
1307	    mouse_status.flags |=
1308		((mouse->u.data.x || mouse->u.data.y || mouse->u.data.z) ?
1309		    MOUSE_POSCHANGED : 0)
1310		| (mouse_status.obutton ^ mouse_status.button);
1311
1312	    if (cur_console->status & MOUSE_ENABLED)
1313	    	cur_console->status |= MOUSE_VISIBLE;
1314
1315	    if ((MOUSE_TTY)->t_state & TS_ISOPEN) {
1316		u_char buf[8];
1317		int i;
1318
1319		buf[0] = MOUSE_MSC_SYNC
1320			 | butmap[mouse_status.button & MOUSE_STDBUTTONS];
1321		buf[7] = (~mouse_status.button >> 3) & 0x7f;
1322		buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
1323		for (i = 0;
1324		     i < ((mouse_level >= 1) ? MOUSE_SYS_PACKETSIZE
1325					     : MOUSE_MSC_PACKETSIZE); i++)
1326	    	    (*linesw[(MOUSE_TTY)->t_line].l_rint)(buf[i],MOUSE_TTY);
1327	    }
1328
1329	    if (cur_console->mouse_signal) {
1330		if (cur_console->mouse_proc &&
1331		    (cur_console->mouse_proc != pfind(cur_console->mouse_pid))){
1332		    	cur_console->mouse_signal = 0;
1333			cur_console->mouse_proc = NULL;
1334			cur_console->mouse_pid = 0;
1335		}
1336		else
1337		    psignal(cur_console->mouse_proc, cur_console->mouse_signal);
1338		break;
1339	    }
1340
1341	    if (cur_console->status & UNKNOWN_MODE)
1342		break;
1343
1344	    switch (mouse->u.event.id) {
1345	    case MOUSE_BUTTON1DOWN:
1346	        switch (mouse->u.event.value % 4) {
1347		case 0:	/* up */
1348		    mouse_cut_end(cur_console);
1349		    break;
1350		case 1:
1351		    mouse_cut_start(cur_console);
1352		    break;
1353		case 2:
1354		    mouse_cut_word(cur_console);
1355		    break;
1356		case 3:
1357		    mouse_cut_line(cur_console);
1358		    break;
1359		}
1360		break;
1361	    case MOUSE_BUTTON2DOWN:
1362	        switch (mouse->u.event.value) {
1363		case 0:	/* up */
1364		    break;
1365		default:
1366		    mouse_paste(cur_console);
1367		    break;
1368		}
1369		break;
1370	    case MOUSE_BUTTON3DOWN:
1371	        switch (mouse->u.event.value) {
1372		case 0:	/* up */
1373		    if (!(cur_console->mouse_buttons & MOUSE_BUTTON1DOWN))
1374		        mouse_cut_end(cur_console);
1375		    break;
1376		default:
1377		    mouse_cut_extend(cur_console);
1378		    break;
1379		}
1380		break;
1381	    }
1382	    break;
1383
1384	default:
1385	    return EINVAL;
1386	}
1387	/* make screensaver happy */
1388	getmicroruntime(&scrn_time_stamp);
1389	return 0;
1390    }
1391
1392    /* MOUSE_XXX: /dev/sysmouse ioctls */
1393    case MOUSE_GETHWINFO:	/* get device information */
1394    {
1395	mousehw_t *hw = (mousehw_t *)data;
1396
1397	if (tp != MOUSE_TTY)
1398	    return ENOTTY;
1399	hw->buttons = 10;		/* XXX unknown */
1400	hw->iftype = MOUSE_IF_SYSMOUSE;
1401	hw->type = MOUSE_MOUSE;
1402	hw->model = MOUSE_MODEL_GENERIC;
1403	hw->hwid = 0;
1404	return 0;
1405    }
1406
1407    case MOUSE_GETMODE:		/* get protocol/mode */
1408    {
1409	mousemode_t *mode = (mousemode_t *)data;
1410
1411	if (tp != MOUSE_TTY)
1412	    return ENOTTY;
1413	mode->level = mouse_level;
1414	switch (mode->level) {
1415	case 0:
1416	    /* at this level, sysmouse emulates MouseSystems protocol */
1417	    mode->protocol = MOUSE_PROTO_MSC;
1418	    mode->rate = -1;		/* unknown */
1419	    mode->resolution = -1;	/* unknown */
1420	    mode->accelfactor = 0;	/* disabled */
1421	    mode->packetsize = MOUSE_MSC_PACKETSIZE;
1422	    mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
1423	    mode->syncmask[1] = MOUSE_MSC_SYNC;
1424	    break;
1425
1426	case 1:
1427	    /* at this level, sysmouse uses its own protocol */
1428	    mode->protocol = MOUSE_PROTO_SYSMOUSE;
1429	    mode->rate = -1;
1430	    mode->resolution = -1;
1431	    mode->accelfactor = 0;
1432	    mode->packetsize = MOUSE_SYS_PACKETSIZE;
1433	    mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
1434	    mode->syncmask[1] = MOUSE_SYS_SYNC;
1435	    break;
1436	}
1437	return 0;
1438    }
1439
1440    case MOUSE_SETMODE:		/* set protocol/mode */
1441    {
1442	mousemode_t *mode = (mousemode_t *)data;
1443
1444	if (tp != MOUSE_TTY)
1445	    return ENOTTY;
1446	if ((mode->level < 0) || (mode->level > 1))
1447	    return EINVAL;
1448	mouse_level = mode->level;
1449	return 0;
1450    }
1451
1452    case MOUSE_GETLEVEL:	/* get operation level */
1453	if (tp != MOUSE_TTY)
1454	    return ENOTTY;
1455	*(int *)data = mouse_level;
1456	return 0;
1457
1458    case MOUSE_SETLEVEL:	/* set operation level */
1459	if (tp != MOUSE_TTY)
1460	    return ENOTTY;
1461	if ((*(int *)data  < 0) || (*(int *)data > 1))
1462	    return EINVAL;
1463	mouse_level = *(int *)data;
1464	return 0;
1465
1466    case MOUSE_GETSTATUS:	/* get accumulated mouse events */
1467	if (tp != MOUSE_TTY)
1468	    return ENOTTY;
1469	s = spltty();
1470	*(mousestatus_t *)data = mouse_status;
1471	mouse_status.flags = 0;
1472	mouse_status.obutton = mouse_status.button;
1473	mouse_status.dx = 0;
1474	mouse_status.dy = 0;
1475	mouse_status.dz = 0;
1476	splx(s);
1477	return 0;
1478
1479#if notyet
1480    case MOUSE_GETVARS:		/* get internal mouse variables */
1481    case MOUSE_SETVARS:		/* set internal mouse variables */
1482	if (tp != MOUSE_TTY)
1483	    return ENOTTY;
1484	return ENODEV;
1485#endif
1486
1487    case MOUSE_READSTATE:	/* read status from the device */
1488    case MOUSE_READDATA:	/* read data from the device */
1489	if (tp != MOUSE_TTY)
1490	    return ENOTTY;
1491	return ENODEV;
1492
1493    case CONS_GETINFO:  	/* get current (virtual) console info */
1494    {
1495	vid_info_t *ptr = (vid_info_t*)data;
1496	if (ptr->size == sizeof(struct vid_info)) {
1497	    ptr->m_num = get_scr_num();
1498	    ptr->mv_col = scp->xpos;
1499	    ptr->mv_row = scp->ypos;
1500	    ptr->mv_csz = scp->xsize;
1501	    ptr->mv_rsz = scp->ysize;
1502	    ptr->mv_norm.fore = (scp->term.std_color & 0x0f00)>>8;
1503	    ptr->mv_norm.back = (scp->term.std_color & 0xf000)>>12;
1504	    ptr->mv_rev.fore = (scp->term.rev_color & 0x0f00)>>8;
1505	    ptr->mv_rev.back = (scp->term.rev_color & 0xf000)>>12;
1506	    ptr->mv_grfc.fore = 0;      /* not supported */
1507	    ptr->mv_grfc.back = 0;      /* not supported */
1508	    ptr->mv_ovscan = scp->border;
1509	    ptr->mk_keylock = scp->status & LOCK_KEY_MASK;
1510	    return 0;
1511	}
1512	return EINVAL;
1513    }
1514
1515    case CONS_GETVERS:  	/* get version number */
1516	*(int*)data = 0x200;    /* version 2.0 */
1517	return 0;
1518
1519    /* VGA TEXT MODES */
1520    case SW_VGA_C40x25:
1521    case SW_VGA_C80x25: case SW_VGA_M80x25:
1522    case SW_VGA_C80x30: case SW_VGA_M80x30:
1523    case SW_VGA_C80x50: case SW_VGA_M80x50:
1524    case SW_VGA_C80x60: case SW_VGA_M80x60:
1525    case SW_B40x25:     case SW_C40x25:
1526    case SW_B80x25:     case SW_C80x25:
1527    case SW_ENH_B40x25: case SW_ENH_C40x25:
1528    case SW_ENH_B80x25: case SW_ENH_C80x25:
1529    case SW_ENH_B80x43: case SW_ENH_C80x43:
1530    case SW_EGAMONO80x25:
1531
1532	if (!crtc_vga)
1533 	    return ENODEV;
1534 	mp = get_mode_param(scp, cmd & 0xff);
1535 	if (mp == NULL)
1536 	    return ENODEV;
1537
1538	if (scp->history != NULL)
1539	    i = imax(scp->history_size / scp->xsize
1540		     - imax(SC_HISTORY_SIZE, scp->ysize), 0);
1541	else
1542	    i = 0;
1543	switch (cmd & 0xff) {
1544	case M_VGA_C80x60: case M_VGA_M80x60:
1545	    if (!(fonts_loaded & FONT_8))
1546		return EINVAL;
1547	    scp->xsize = 80;
1548	    scp->ysize = 60;
1549	    scp->font_size = 8;
1550	    break;
1551	case M_VGA_C80x50: case M_VGA_M80x50:
1552	    if (!(fonts_loaded & FONT_8))
1553		return EINVAL;
1554	    scp->xsize = 80;
1555	    scp->ysize = 50;
1556	    scp->font_size = 8;
1557	    break;
1558	case M_ENH_B80x43: case M_ENH_C80x43:
1559	    if (!(fonts_loaded & FONT_8))
1560		return EINVAL;
1561	    scp->xsize = 80;
1562	    scp->ysize = 43;
1563	    scp->font_size = 8;
1564	    break;
1565	case M_VGA_C80x30: case M_VGA_M80x30:
1566	    scp->xsize = 80;
1567	    scp->ysize = 30;
1568	    scp->font_size = mp[2];
1569	    break;
1570	case M_ENH_C40x25: case M_ENH_B40x25:
1571	case M_ENH_C80x25: case M_ENH_B80x25:
1572	case M_EGAMONO80x25:
1573	    if (!(fonts_loaded & FONT_14))
1574		return EINVAL;
1575	    /* FALL THROUGH */
1576	default:
1577	    if ((cmd & 0xff) > M_VGA_CG320)
1578		return EINVAL;
1579            scp->xsize = mp[0];
1580            scp->ysize = mp[1] + rows_offset;
1581	    scp->font_size = mp[2];
1582	    break;
1583	}
1584
1585	scp->status &= ~MOUSE_VISIBLE;
1586	scp->mode = cmd & 0xff;
1587	scp->xpixel = scp->xsize * 8;
1588	scp->ypixel = scp->ysize * scp->font_size;
1589	free(scp->scr_buf, M_DEVBUF);
1590	scp->scr_buf = (u_short *)
1591	    malloc(scp->xsize*scp->ysize*sizeof(u_short), M_DEVBUF, M_WAITOK);
1592	/* move the text cursor to the home position */
1593	move_crsr(scp, 0, 0);
1594	/* move the mouse cursor at the center of the screen */
1595	scp->mouse_xpos = scp->xpixel / 2;
1596	scp->mouse_ypos = scp->ypixel / 2;
1597    	scp->mouse_pos = scp->mouse_oldpos =
1598	    scp->scr_buf + (scp->mouse_ypos / scp->font_size) * scp->xsize
1599	    + scp->mouse_xpos / 8;
1600	free(cut_buffer, M_DEVBUF);
1601    	cut_buffer = (char *)malloc(scp->xsize*scp->ysize, M_DEVBUF, M_NOWAIT);
1602	cut_buffer[0] = 0x00;
1603	usp = scp->history;
1604	scp->history = NULL;
1605	if (usp != NULL) {
1606	    free(usp, M_DEVBUF);
1607	    extra_history_size += i;
1608	}
1609	scp->history_size = imax(SC_HISTORY_SIZE, scp->ysize) * scp->xsize;
1610	usp = (u_short *)malloc(scp->history_size * sizeof(u_short),
1611				M_DEVBUF, M_NOWAIT);
1612	if (usp != NULL)
1613	    bzero(usp, scp->history_size * sizeof(u_short));
1614	scp->history_head = scp->history_pos = usp;
1615	scp->history = usp;
1616	if (scp == cur_console)
1617	    set_mode(scp);
1618	scp->status &= ~UNKNOWN_MODE;
1619	clear_screen(scp);
1620
1621	if (tp->t_winsize.ws_col != scp->xsize
1622	    || tp->t_winsize.ws_row != scp->ysize) {
1623	    tp->t_winsize.ws_col = scp->xsize;
1624	    tp->t_winsize.ws_row = scp->ysize;
1625	    pgsignal(tp->t_pgrp, SIGWINCH, 1);
1626	}
1627	return 0;
1628
1629    /* GRAPHICS MODES */
1630    case SW_BG320:     case SW_BG640:
1631    case SW_CG320:     case SW_CG320_D:   case SW_CG640_E:
1632    case SW_CG640x350: case SW_ENH_CG640:
1633    case SW_BG640x480: case SW_CG640x480: case SW_VGA_CG320:
1634
1635	if (!crtc_vga)
1636	    return ENODEV;
1637	mp = get_mode_param(scp, cmd & 0xff);
1638	if (mp == NULL)
1639	    return ENODEV;
1640
1641	scp->status &= ~MOUSE_VISIBLE;
1642	scp->mode = cmd & 0xFF;
1643	scp->xpixel = mp[0] * 8;
1644	scp->ypixel = (mp[1] + rows_offset) * mp[2];
1645	scp->font_size = FONT_NONE;
1646	/* move the mouse cursor at the center of the screen */
1647	scp->mouse_xpos = scp->xpixel / 2;
1648	scp->mouse_ypos = scp->ypixel / 2;
1649	if (scp == cur_console)
1650	    set_mode(scp);
1651	scp->status |= UNKNOWN_MODE;    /* graphics mode */
1652	/* clear_graphics();*/
1653
1654	if (tp->t_winsize.ws_xpixel != scp->xpixel
1655	    || tp->t_winsize.ws_ypixel != scp->ypixel) {
1656	    tp->t_winsize.ws_xpixel = scp->xpixel;
1657	    tp->t_winsize.ws_ypixel = scp->ypixel;
1658	    pgsignal(tp->t_pgrp, SIGWINCH, 1);
1659	}
1660	return 0;
1661
1662    case SW_VGA_MODEX:
1663	if (!crtc_vga)
1664	    return ENODEV;
1665	mp = get_mode_param(scp, cmd & 0xff);
1666	if (mp == NULL)
1667	    return ENODEV;
1668
1669	scp->mode = cmd & 0xFF;
1670	if (scp == cur_console)
1671	    set_mode(scp);
1672	scp->status |= UNKNOWN_MODE;    /* graphics mode */
1673	/* clear_graphics();*/
1674	scp->xpixel = 320;
1675	scp->ypixel = 240;
1676	if (tp->t_winsize.ws_xpixel != scp->xpixel
1677	    || tp->t_winsize.ws_ypixel != scp->ypixel) {
1678	    tp->t_winsize.ws_xpixel = scp->xpixel;
1679	    tp->t_winsize.ws_ypixel = scp->ypixel;
1680	    pgsignal(tp->t_pgrp, SIGWINCH, 1);
1681	}
1682	return 0;
1683
1684    case VT_SETMODE:    	/* set screen switcher mode */
1685    {
1686	struct vt_mode *mode;
1687
1688	mode = (struct vt_mode *)data;
1689	if (ISSIGVALID(mode->relsig) && ISSIGVALID(mode->acqsig) &&
1690	    ISSIGVALID(mode->frsig)) {
1691	    bcopy(data, &scp->smode, sizeof(struct vt_mode));
1692	    if (scp->smode.mode == VT_PROCESS) {
1693		scp->proc = p;
1694		scp->pid = scp->proc->p_pid;
1695	    }
1696	    return 0;
1697	} else
1698	    return EINVAL;
1699    }
1700
1701    case VT_GETMODE:    	/* get screen switcher mode */
1702	bcopy(&scp->smode, data, sizeof(struct vt_mode));
1703	return 0;
1704
1705    case VT_RELDISP:    	/* screen switcher ioctl */
1706	switch(*data) {
1707	case VT_FALSE:  	/* user refuses to release screen, abort */
1708	    if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
1709		old_scp->status &= ~SWITCH_WAIT_REL;
1710		switch_in_progress = FALSE;
1711		return 0;
1712	    }
1713	    return EINVAL;
1714
1715	case VT_TRUE:   	/* user has released screen, go on */
1716	    if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
1717		scp->status &= ~SWITCH_WAIT_REL;
1718		exchange_scr();
1719		if (new_scp->smode.mode == VT_PROCESS) {
1720		    new_scp->status |= SWITCH_WAIT_ACQ;
1721		    psignal(new_scp->proc, new_scp->smode.acqsig);
1722		}
1723		else
1724		    switch_in_progress = FALSE;
1725		return 0;
1726	    }
1727	    return EINVAL;
1728
1729	case VT_ACKACQ: 	/* acquire acknowledged, switch completed */
1730	    if (scp == new_scp && (scp->status & SWITCH_WAIT_ACQ)) {
1731		scp->status &= ~SWITCH_WAIT_ACQ;
1732		switch_in_progress = FALSE;
1733		return 0;
1734	    }
1735	    return EINVAL;
1736
1737	default:
1738	    return EINVAL;
1739	}
1740	/* NOT REACHED */
1741
1742    case VT_OPENQRY:    	/* return free virtual console */
1743	for (i = 0; i < MAXCONS; i++) {
1744	    tp = VIRTUAL_TTY(i);
1745	    if (!(tp->t_state & TS_ISOPEN)) {
1746		*data = i + 1;
1747		return 0;
1748	    }
1749	}
1750	return EINVAL;
1751
1752    case VT_ACTIVATE:   	/* switch to screen *data */
1753	return switch_scr(scp, (*data) - 1);
1754
1755    case VT_WAITACTIVE: 	/* wait for switch to occur */
1756	if (*data > MAXCONS || *data < 0)
1757	    return EINVAL;
1758	if (minor(dev) == (*data) - 1)
1759	    return 0;
1760	if (*data == 0) {
1761	    if (scp == cur_console)
1762		return 0;
1763	}
1764	else
1765	    scp = console[(*data) - 1];
1766	while ((error=tsleep((caddr_t)&scp->smode, PZERO|PCATCH,
1767			     "waitvt", 0)) == ERESTART) ;
1768	return error;
1769
1770    case VT_GETACTIVE:
1771	*data = get_scr_num()+1;
1772	return 0;
1773
1774    case KDENABIO:      	/* allow io operations */
1775	error = suser(p->p_ucred, &p->p_acflag);
1776	if (error != 0)
1777	    return error;
1778	if (securelevel > 0)
1779	    return EPERM;
1780	p->p_md.md_regs->tf_eflags |= PSL_IOPL;
1781	return 0;
1782
1783    case KDDISABIO:     	/* disallow io operations (default) */
1784	p->p_md.md_regs->tf_eflags &= ~PSL_IOPL;
1785	return 0;
1786
1787    case KDSETMODE:     	/* set current mode of this (virtual) console */
1788	switch (*data) {
1789	case KD_TEXT:   	/* switch to TEXT (known) mode */
1790	    /* restore fonts & palette ! */
1791	    if (crtc_vga) {
1792		if (fonts_loaded & FONT_8)
1793		    copy_font(LOAD, FONT_8, font_8);
1794		if (fonts_loaded & FONT_14)
1795		    copy_font(LOAD, FONT_14, font_14);
1796		if (fonts_loaded & FONT_16)
1797		    copy_font(LOAD, FONT_16, font_16);
1798		load_palette(palette);
1799	    }
1800
1801	    /* move hardware cursor out of the way */
1802	    outb(crtc_addr, 14);
1803	    outb(crtc_addr + 1, 0xff);
1804	    outb(crtc_addr, 15);
1805	    outb(crtc_addr + 1, 0xff);
1806
1807	    /* FALL THROUGH */
1808
1809	case KD_TEXT1:  	/* switch to TEXT (known) mode */
1810	    scp->status &= ~MOUSE_VISIBLE;
1811	    /* no restore fonts & palette */
1812	    if (crtc_vga)
1813		set_mode(scp);
1814	    scp->status &= ~UNKNOWN_MODE;
1815	    clear_screen(scp);
1816	    return 0;
1817
1818	case KD_GRAPHICS:	/* switch to GRAPHICS (unknown) mode */
1819	    scp->status &= ~MOUSE_VISIBLE;
1820	    scp->status |= UNKNOWN_MODE;
1821	    return 0;
1822	default:
1823	    return EINVAL;
1824	}
1825	/* NOT REACHED */
1826
1827    case KDGETMODE:     	/* get current mode of this (virtual) console */
1828	*data = (scp->status & UNKNOWN_MODE) ? KD_GRAPHICS : KD_TEXT;
1829	return 0;
1830
1831    case KDSBORDER:     	/* set border color of this (virtual) console */
1832	scp->border = *data;
1833	if (scp == cur_console)
1834	    set_border(scp->border);
1835	return 0;
1836
1837    case KDSKBSTATE:    	/* set keyboard state (locks) */
1838	if (*data >= 0 && *data <= LOCK_KEY_MASK) {
1839	    scp->status &= ~LOCK_KEY_MASK;
1840	    scp->status |= *data;
1841	    if (scp == cur_console)
1842		update_leds(scp->status);
1843	    return 0;
1844	}
1845	return EINVAL;
1846
1847    case KDGKBSTATE:    	/* get keyboard state (locks) */
1848	*data = scp->status & LOCK_KEY_MASK;
1849	return 0;
1850
1851    case KDSETRAD:      	/* set keyboard repeat & delay rates */
1852	if (*data & 0x80)
1853	    return EINVAL;
1854	if (sc_kbdc != NULL)
1855	    set_keyboard(KBDC_SET_TYPEMATIC, *data);
1856	return 0;
1857
1858    case KDSKBMODE:     	/* set keyboard mode */
1859	switch (*data) {
1860	case K_RAW: 		/* switch to RAW scancode mode */
1861	    scp->status &= ~KBD_CODE_MODE;
1862	    scp->status |= KBD_RAW_MODE;
1863	    return 0;
1864
1865	case K_CODE: 		/* switch to CODE mode */
1866	    scp->status &= ~KBD_RAW_MODE;
1867	    scp->status |= KBD_CODE_MODE;
1868	    return 0;
1869
1870	case K_XLATE:   	/* switch to XLT ascii mode */
1871	    if (scp == cur_console && scp->status & KBD_RAW_MODE)
1872		shfts = ctls = alts = agrs = metas = accents = 0;
1873	    scp->status &= ~(KBD_RAW_MODE | KBD_CODE_MODE);
1874	    return 0;
1875	default:
1876	    return EINVAL;
1877	}
1878	/* NOT REACHED */
1879
1880    case KDGKBMODE:     	/* get keyboard mode */
1881	*data = (scp->status & KBD_RAW_MODE) ? K_RAW :
1882		((scp->status & KBD_CODE_MODE) ? K_CODE : K_XLATE);
1883	return 0;
1884
1885    case KDMKTONE:      	/* sound the bell */
1886	if (*(int*)data)
1887	    do_bell(scp, (*(int*)data)&0xffff,
1888		    (((*(int*)data)>>16)&0xffff)*hz/1000);
1889	else
1890	    do_bell(scp, scp->bell_pitch, scp->bell_duration);
1891	return 0;
1892
1893    case KIOCSOUND:     	/* make tone (*data) hz */
1894	if (scp == cur_console) {
1895	    if (*(int*)data) {
1896		int pitch = timer_freq / *(int*)data;
1897
1898		/* set command for counter 2, 2 byte write */
1899		if (acquire_timer2(TIMER_16BIT|TIMER_SQWAVE))
1900		    return EBUSY;
1901
1902		/* set pitch */
1903		outb(TIMER_CNTR2, pitch);
1904		outb(TIMER_CNTR2, (pitch>>8));
1905
1906		/* enable counter 2 output to speaker */
1907		outb(IO_PPI, inb(IO_PPI) | 3);
1908	    }
1909	    else {
1910		/* disable counter 2 output to speaker */
1911		outb(IO_PPI, inb(IO_PPI) & 0xFC);
1912		release_timer2();
1913	    }
1914	}
1915	return 0;
1916
1917    case KDGKBTYPE:     	/* get keyboard type */
1918	*data = 0;  		/* type not known (yet) */
1919	return 0;
1920
1921    case KDSETLED:      	/* set keyboard LED status */
1922	if (*data >= 0 && *data <= LED_MASK) {
1923	    scp->status &= ~LED_MASK;
1924	    scp->status |= *data;
1925	    if (scp == cur_console)
1926		update_leds(scp->status);
1927	    return 0;
1928	}
1929	return EINVAL;
1930
1931    case KDGETLED:      	/* get keyboard LED status */
1932	*data = scp->status & LED_MASK;
1933	return 0;
1934
1935    case GETFKEY:       	/* get functionkey string */
1936	if (*(u_short*)data < n_fkey_tab) {
1937	    fkeyarg_t *ptr = (fkeyarg_t*)data;
1938	    bcopy(&fkey_tab[ptr->keynum].str, ptr->keydef,
1939		  fkey_tab[ptr->keynum].len);
1940	    ptr->flen = fkey_tab[ptr->keynum].len;
1941	    return 0;
1942	}
1943	else
1944	    return EINVAL;
1945
1946    case SETFKEY:       	/* set functionkey string */
1947	if (*(u_short*)data < n_fkey_tab) {
1948	    fkeyarg_t *ptr = (fkeyarg_t*)data;
1949	    bcopy(ptr->keydef, &fkey_tab[ptr->keynum].str,
1950		  min(ptr->flen, MAXFK));
1951	    fkey_tab[ptr->keynum].len = min(ptr->flen, MAXFK);
1952	    return 0;
1953	}
1954	else
1955	    return EINVAL;
1956
1957    case GIO_SCRNMAP:   	/* get output translation table */
1958	bcopy(&scr_map, data, sizeof(scr_map));
1959	return 0;
1960
1961    case PIO_SCRNMAP:   	/* set output translation table */
1962	bcopy(data, &scr_map, sizeof(scr_map));
1963	for (i=0; i<sizeof(scr_map); i++)
1964	    scr_rmap[scr_map[i]] = i;
1965	return 0;
1966
1967    case GIO_KEYMAP:    	/* get keyboard translation table */
1968	bcopy(&key_map, data, sizeof(key_map));
1969	return 0;
1970
1971    case PIO_KEYMAP:    	/* set keyboard translation table */
1972	accents = 0;
1973	bzero(&accent_map, sizeof(accent_map));
1974	bcopy(data, &key_map, sizeof(key_map));
1975	return 0;
1976
1977    case GIO_DEADKEYMAP:    	/* get accent key translation table */
1978	bcopy(&accent_map, data, sizeof(accent_map));
1979	return 0;
1980
1981    case PIO_DEADKEYMAP:    	/* set accent key translation table */
1982	accents = 0;
1983	bcopy(data, &accent_map, sizeof(accent_map));
1984	return 0;
1985
1986    case PIO_FONT8x8:   	/* set 8x8 dot font */
1987	if (!crtc_vga)
1988	    return ENXIO;
1989	bcopy(data, font_8, 8*256);
1990	fonts_loaded |= FONT_8;
1991	if (!(cur_console->status & UNKNOWN_MODE)) {
1992	    copy_font(LOAD, FONT_8, font_8);
1993	    if (flags & CHAR_CURSOR)
1994	        set_destructive_cursor(cur_console);
1995	}
1996	return 0;
1997
1998    case GIO_FONT8x8:   	/* get 8x8 dot font */
1999	if (!crtc_vga)
2000	    return ENXIO;
2001	if (fonts_loaded & FONT_8) {
2002	    bcopy(font_8, data, 8*256);
2003	    return 0;
2004	}
2005	else
2006	    return ENXIO;
2007
2008    case PIO_FONT8x14:  	/* set 8x14 dot font */
2009	if (!crtc_vga)
2010	    return ENXIO;
2011	bcopy(data, font_14, 14*256);
2012	fonts_loaded |= FONT_14;
2013	if (!(cur_console->status & UNKNOWN_MODE)) {
2014	    copy_font(LOAD, FONT_14, font_14);
2015	    if (flags & CHAR_CURSOR)
2016	        set_destructive_cursor(cur_console);
2017	}
2018	return 0;
2019
2020    case GIO_FONT8x14:  	/* get 8x14 dot font */
2021	if (!crtc_vga)
2022	    return ENXIO;
2023	if (fonts_loaded & FONT_14) {
2024	    bcopy(font_14, data, 14*256);
2025	    return 0;
2026	}
2027	else
2028	    return ENXIO;
2029
2030    case PIO_FONT8x16:  	/* set 8x16 dot font */
2031	if (!crtc_vga && crtc_type != KD_PIXEL)
2032	    return ENXIO;
2033	bcopy(data, font_16, 16*256);
2034	fonts_loaded |= FONT_16;
2035	if (crtc_vga && !(cur_console->status & UNKNOWN_MODE)) {
2036	    copy_font(LOAD, FONT_16, font_16);
2037	    if (flags & CHAR_CURSOR)
2038	        set_destructive_cursor(cur_console);
2039	}
2040	return 0;
2041
2042    case GIO_FONT8x16:  	/* get 8x16 dot font */
2043	if (!crtc_vga && crtc_type != KD_PIXEL)
2044	    return ENXIO;
2045	if (fonts_loaded & FONT_16) {
2046	    bcopy(font_16, data, 16*256);
2047	    return 0;
2048	}
2049	else
2050	    return ENXIO;
2051    default:
2052	break;
2053    }
2054
2055    error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
2056    if (error != ENOIOCTL)
2057	return(error);
2058    error = ttioctl(tp, cmd, data, flag);
2059    if (error != ENOIOCTL)
2060	return(error);
2061    return(ENOTTY);
2062}
2063
2064static void
2065scstart(struct tty *tp)
2066{
2067    struct clist *rbp;
2068    int s, len;
2069    u_char buf[PCBURST];
2070    scr_stat *scp = get_scr_stat(tp->t_dev);
2071
2072    if (scp->status & SLKED || blink_in_progress)
2073	return; /* XXX who repeats the call when the above flags are cleared? */
2074    s = spltty();
2075    if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
2076	tp->t_state |= TS_BUSY;
2077	rbp = &tp->t_outq;
2078	while (rbp->c_cc) {
2079	    len = q_to_b(rbp, buf, PCBURST);
2080	    splx(s);
2081	    ansi_put(scp, buf, len);
2082	    s = spltty();
2083	}
2084	tp->t_state &= ~TS_BUSY;
2085	ttwwakeup(tp);
2086    }
2087    splx(s);
2088}
2089
2090static void
2091scmousestart(struct tty *tp)
2092{
2093    struct clist *rbp;
2094    int s;
2095    u_char buf[PCBURST];
2096
2097    s = spltty();
2098    if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
2099	tp->t_state |= TS_BUSY;
2100	rbp = &tp->t_outq;
2101	while (rbp->c_cc) {
2102	    q_to_b(rbp, buf, PCBURST);
2103	}
2104	tp->t_state &= ~TS_BUSY;
2105	ttwwakeup(tp);
2106    }
2107    splx(s);
2108}
2109
2110void
2111sccnprobe(struct consdev *cp)
2112{
2113    struct isa_device *dvp;
2114
2115    /*
2116     * Take control if we are the highest priority enabled display device.
2117     */
2118    dvp = find_display();
2119    if (dvp == NULL || dvp->id_driver != &scdriver) {
2120	cp->cn_pri = CN_DEAD;
2121	return;
2122    }
2123
2124    if (!scvidprobe(dvp->id_unit, dvp->id_flags)) {
2125	cp->cn_pri = CN_DEAD;
2126	return;
2127    }
2128
2129    /* initialize required fields */
2130    cp->cn_dev = makedev(CDEV_MAJOR, SC_CONSOLE);
2131    cp->cn_pri = CN_INTERNAL;
2132
2133    sc_kbdc = kbdc_open(sc_port);
2134}
2135
2136void
2137sccninit(struct consdev *cp)
2138{
2139    scinit();
2140}
2141
2142void
2143sccnputc(dev_t dev, int c)
2144{
2145    u_char buf[1];
2146    int s;
2147    scr_stat *scp = console[0];
2148    term_stat save = scp->term;
2149
2150    scp->term = kernel_console;
2151    current_default = &kernel_default;
2152    if (scp == cur_console && !(scp->status & UNKNOWN_MODE))
2153	remove_cursor_image(scp);
2154    buf[0] = c;
2155    ansi_put(scp, buf, 1);
2156    kernel_console = scp->term;
2157    current_default = &user_default;
2158    scp->term = save;
2159
2160    s = spltty();	/* block scintr and scrn_timer */
2161    sccnupdate(scp);
2162    splx(s);
2163}
2164
2165int
2166sccngetc(dev_t dev)
2167{
2168    int s = spltty();	/* block scintr and scrn_timer while we poll */
2169    int c;
2170
2171    /*
2172     * Stop the screen saver and update the screen if necessary.
2173     * What if we have been running in the screen saver code... XXX
2174     */
2175    sccnupdate(cur_console);
2176
2177    c = scgetc(SCGETC_CN);
2178    splx(s);
2179    return(c);
2180}
2181
2182int
2183sccncheckc(dev_t dev)
2184{
2185    int s = spltty();	/* block scintr and scrn_timer while we poll */
2186    int c;
2187
2188    sccnupdate(cur_console);
2189    c = scgetc(SCGETC_CN | SCGETC_NONBLOCK);
2190    splx(s);
2191    return(c == NOKEY ? -1 : c);	/* c == -1 can't happen */
2192}
2193
2194static void
2195sccnupdate(scr_stat *scp)
2196{
2197    if (scp == cur_console && !font_loading_in_progress) {
2198	if (scrn_blanked > 0)
2199            stop_scrn_saver(current_saver);
2200	if (!(scp->status & UNKNOWN_MODE) && scrn_blanked <= 0
2201	    && !blink_in_progress && !switch_in_progress)
2202	    scrn_update(scp, TRUE);
2203    }
2204}
2205
2206static scr_stat
2207*get_scr_stat(dev_t dev)
2208{
2209    int unit = minor(dev);
2210
2211    if (unit == SC_CONSOLE)
2212	return console[0];
2213    if (unit >= MAXCONS || unit < 0)
2214	return(NULL);
2215    return console[unit];
2216}
2217
2218static int
2219get_scr_num()
2220{
2221    int i = 0;
2222
2223    while ((i < MAXCONS) && (cur_console != console[i]))
2224	i++;
2225    return i < MAXCONS ? i : 0;
2226}
2227
2228static void
2229scrn_timer(void *arg)
2230{
2231    struct timeval tv;
2232    scr_stat *scp = cur_console;
2233    int s;
2234
2235    /* don't do anything when we are touching font */
2236    if (font_loading_in_progress) {
2237	timeout(scrn_timer, NULL, hz / 10);
2238	return;
2239    }
2240    s = spltty();
2241
2242    /*
2243     * With release 2.1 of the Xaccel server, the keyboard is left
2244     * hanging pretty often. Apparently an interrupt from the
2245     * keyboard is lost, and I don't know why (yet).
2246     * This ugly hack calls scintr if input is ready for the keyboard
2247     * and conveniently hides the problem.			XXX
2248     */
2249    /* Try removing anything stuck in the keyboard controller; whether
2250     * it's a keyboard scan code or mouse data. `scintr()' doesn't
2251     * read the mouse data directly, but `kbdio' routines will, as a
2252     * side effect.
2253     */
2254    if (kbdc_lock(sc_kbdc, TRUE)) {
2255	/*
2256	 * We have seen the lock flag is not set. Let's reset the flag early;
2257	 * otherwise `update_led()' failes which may want the lock
2258	 * during `scintr()'.
2259	 */
2260	kbdc_lock(sc_kbdc, FALSE);
2261	if (kbdc_data_ready(sc_kbdc))
2262	    scintr(0);
2263    }
2264
2265    /* should we just return ? */
2266    if ((scp->status&UNKNOWN_MODE) || blink_in_progress || switch_in_progress) {
2267	timeout(scrn_timer, NULL, hz / 10);
2268	splx(s);
2269	return;
2270    }
2271
2272    /* should we stop the screen saver? */
2273    getmicroruntime(&tv);
2274    if (panicstr)
2275	scrn_time_stamp = tv;
2276    if (tv.tv_sec <= scrn_time_stamp.tv_sec + scrn_blank_time)
2277	if (scrn_blanked > 0)
2278            stop_scrn_saver(current_saver);
2279    scp = cur_console;
2280    if (scrn_blanked <= 0)
2281	scrn_update(scp, TRUE);
2282    /* should we activate the screen saver? */
2283    if ((scrn_blank_time != 0)
2284	    && (tv.tv_sec > scrn_time_stamp.tv_sec + scrn_blank_time))
2285	(*current_saver)(TRUE);
2286
2287    timeout(scrn_timer, NULL, hz / 25);
2288    splx(s);
2289}
2290
2291static void
2292scrn_update(scr_stat *scp, int show_cursor)
2293{
2294    /* update screen image */
2295    if (scp->start <= scp->end)
2296        sc_bcopy(scp->scr_buf, scp->start, scp->end, 0);
2297
2298    /* we are not to show the cursor and the mouse pointer... */
2299    if (!show_cursor) {
2300        scp->end = 0;
2301        scp->start = scp->xsize*scp->ysize - 1;
2302	return;
2303    }
2304
2305    /* update "pseudo" mouse pointer image */
2306    if (scp->status & MOUSE_VISIBLE) {
2307        /* did mouse move since last time ? */
2308        if (scp->status & MOUSE_MOVED) {
2309            /* do we need to remove old mouse pointer image ? */
2310            if (scp->mouse_cut_start != NULL ||
2311                (scp->mouse_pos-scp->scr_buf) <= scp->start ||
2312                (scp->mouse_pos+scp->xsize + 1 - scp->scr_buf) >= scp->end) {
2313                remove_mouse_image(scp);
2314            }
2315            scp->status &= ~MOUSE_MOVED;
2316            draw_mouse_image(scp);
2317        }
2318        else {
2319            /* mouse didn't move, has it been overwritten ? */
2320            if ((scp->mouse_pos+scp->xsize + 1 - scp->scr_buf) >= scp->start &&
2321                (scp->mouse_pos - scp->scr_buf) <= scp->end) {
2322                draw_mouse_image(scp);
2323            }
2324        }
2325    }
2326
2327    /* update cursor image */
2328    if (scp->status & CURSOR_ENABLED) {
2329        /* did cursor move since last time ? */
2330        if (scp->cursor_pos != scp->cursor_oldpos) {
2331            /* do we need to remove old cursor image ? */
2332            if ((scp->cursor_oldpos - scp->scr_buf) < scp->start ||
2333                ((scp->cursor_oldpos - scp->scr_buf) > scp->end)) {
2334                remove_cursor_image(scp);
2335            }
2336            scp->cursor_oldpos = scp->cursor_pos;
2337            draw_cursor_image(scp);
2338        }
2339        else {
2340            /* cursor didn't move, has it been overwritten ? */
2341            if (scp->cursor_pos - scp->scr_buf >= scp->start &&
2342                scp->cursor_pos - scp->scr_buf <= scp->end) {
2343                draw_cursor_image(scp);
2344            } else {
2345                /* if its a blinking cursor, we may have to update it */
2346                if (flags & BLINK_CURSOR)
2347                    draw_cursor_image(scp);
2348            }
2349        }
2350        blinkrate++;
2351    }
2352
2353    if (scp->mouse_cut_start != NULL)
2354        draw_cutmarking(scp);
2355
2356    scp->end = 0;
2357    scp->start = scp->xsize*scp->ysize - 1;
2358}
2359
2360int
2361add_scrn_saver(void (*this_saver)(int))
2362{
2363    if (current_saver != none_saver)
2364	return EBUSY;
2365    current_saver = this_saver;
2366    return 0;
2367}
2368
2369int
2370remove_scrn_saver(void (*this_saver)(int))
2371{
2372    if (current_saver != this_saver)
2373	return EINVAL;
2374
2375    /*
2376     * In order to prevent `current_saver' from being called by
2377     * the timeout routine `scrn_timer()' while we manipulate
2378     * the saver list, we shall set `current_saver' to `none_saver'
2379     * before stopping the current saver, rather than blocking by `splXX()'.
2380     */
2381    current_saver = none_saver;
2382    if (scrn_blanked > 0)
2383        stop_scrn_saver(this_saver);
2384
2385    return 0;
2386}
2387
2388static void
2389stop_scrn_saver(void (*saver)(int))
2390{
2391    (*saver)(FALSE);
2392    getmicroruntime(&scrn_time_stamp);
2393    mark_all(cur_console);
2394}
2395
2396static void
2397clear_screen(scr_stat *scp)
2398{
2399    move_crsr(scp, 0, 0);
2400    scp->cursor_oldpos = scp->cursor_pos;
2401    fillw(scp->term.cur_color | scr_map[0x20], scp->scr_buf,
2402	  scp->xsize * scp->ysize);
2403    mark_all(scp);
2404    remove_cutmarking(scp);
2405}
2406
2407static int
2408switch_scr(scr_stat *scp, u_int next_scr)
2409{
2410    if (switch_in_progress && (cur_console->proc != pfind(cur_console->pid)))
2411	switch_in_progress = FALSE;
2412
2413    if (next_scr >= MAXCONS || switch_in_progress ||
2414	(cur_console->smode.mode == VT_AUTO
2415	 && cur_console->status & UNKNOWN_MODE)) {
2416	do_bell(scp, BELL_PITCH, BELL_DURATION);
2417	return EINVAL;
2418    }
2419
2420    /* is the wanted virtual console open ? */
2421    if (next_scr) {
2422	struct tty *tp = VIRTUAL_TTY(next_scr);
2423	if (!(tp->t_state & TS_ISOPEN)) {
2424	    do_bell(scp, BELL_PITCH, BELL_DURATION);
2425	    return EINVAL;
2426	}
2427    }
2428    /* delay switch if actively updating screen */
2429    if (write_in_progress || blink_in_progress) {
2430	delayed_next_scr = next_scr+1;
2431	return 0;
2432    }
2433    switch_in_progress = TRUE;
2434    old_scp = cur_console;
2435    new_scp = console[next_scr];
2436    wakeup((caddr_t)&new_scp->smode);
2437    if (new_scp == old_scp) {
2438	switch_in_progress = FALSE;
2439	delayed_next_scr = FALSE;
2440	return 0;
2441    }
2442
2443    /* has controlling process died? */
2444    if (old_scp->proc && (old_scp->proc != pfind(old_scp->pid)))
2445	old_scp->smode.mode = VT_AUTO;
2446    if (new_scp->proc && (new_scp->proc != pfind(new_scp->pid)))
2447	new_scp->smode.mode = VT_AUTO;
2448
2449    /* check the modes and switch appropriately */
2450    if (old_scp->smode.mode == VT_PROCESS) {
2451	old_scp->status |= SWITCH_WAIT_REL;
2452	psignal(old_scp->proc, old_scp->smode.relsig);
2453    }
2454    else {
2455	exchange_scr();
2456	if (new_scp->smode.mode == VT_PROCESS) {
2457	    new_scp->status |= SWITCH_WAIT_ACQ;
2458	    psignal(new_scp->proc, new_scp->smode.acqsig);
2459	}
2460	else
2461	    switch_in_progress = FALSE;
2462    }
2463    return 0;
2464}
2465
2466static void
2467exchange_scr(void)
2468{
2469    move_crsr(old_scp, old_scp->xpos, old_scp->ypos);
2470    cur_console = new_scp;
2471    if (old_scp->mode != new_scp->mode || (old_scp->status & UNKNOWN_MODE)){
2472	if (crtc_vga)
2473	    set_mode(new_scp);
2474    }
2475    move_crsr(new_scp, new_scp->xpos, new_scp->ypos);
2476    if (!(new_scp->status & UNKNOWN_MODE) && (flags & CHAR_CURSOR))
2477	set_destructive_cursor(new_scp);
2478    if ((old_scp->status & UNKNOWN_MODE) && crtc_vga)
2479	load_palette(palette);
2480    if (old_scp->status & KBD_RAW_MODE || new_scp->status & KBD_RAW_MODE ||
2481        old_scp->status & KBD_CODE_MODE || new_scp->status & KBD_CODE_MODE)
2482	shfts = ctls = alts = agrs = metas = accents = 0;
2483    set_border(new_scp->border);
2484    update_leds(new_scp->status);
2485    delayed_next_scr = FALSE;
2486    mark_all(new_scp);
2487    if (vesa_mode == 0x102) {
2488	bzero(Crtat, 800*600/8);
2489    }
2490}
2491
2492static void
2493scan_esc(scr_stat *scp, u_char c)
2494{
2495    static u_char ansi_col[16] =
2496	{0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15};
2497    int i, n;
2498    u_short *src, *dst, count;
2499
2500    if (scp->term.esc == 1) {	/* seen ESC */
2501	switch (c) {
2502
2503	case '7':   /* Save cursor position */
2504	    scp->saved_xpos = scp->xpos;
2505	    scp->saved_ypos = scp->ypos;
2506	    break;
2507
2508	case '8':   /* Restore saved cursor position */
2509	    if (scp->saved_xpos >= 0 && scp->saved_ypos >= 0)
2510		move_crsr(scp, scp->saved_xpos, scp->saved_ypos);
2511	    break;
2512
2513	case '[':   /* Start ESC [ sequence */
2514	    scp->term.esc = 2;
2515	    scp->term.last_param = -1;
2516	    for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
2517		scp->term.param[i] = 1;
2518	    scp->term.num_param = 0;
2519	    return;
2520
2521	case 'M':   /* Move cursor up 1 line, scroll if at top */
2522	    if (scp->ypos > 0)
2523		move_crsr(scp, scp->xpos, scp->ypos - 1);
2524	    else {
2525		bcopy(scp->scr_buf, scp->scr_buf + scp->xsize,
2526		       (scp->ysize - 1) * scp->xsize * sizeof(u_short));
2527		fillw(scp->term.cur_color | scr_map[0x20],
2528		      scp->scr_buf, scp->xsize);
2529    		mark_all(scp);
2530	    }
2531	    break;
2532#if notyet
2533	case 'Q':
2534	    scp->term.esc = 4;
2535	    return;
2536#endif
2537	case 'c':   /* Clear screen & home */
2538	    clear_screen(scp);
2539	    break;
2540
2541	case '(':   /* iso-2022: designate 94 character set to G0 */
2542	    scp->term.esc = 5;
2543	    return;
2544	}
2545    }
2546    else if (scp->term.esc == 2) {	/* seen ESC [ */
2547	if (c >= '0' && c <= '9') {
2548	    if (scp->term.num_param < MAX_ESC_PAR) {
2549	    if (scp->term.last_param != scp->term.num_param) {
2550		scp->term.last_param = scp->term.num_param;
2551		scp->term.param[scp->term.num_param] = 0;
2552	    }
2553	    else
2554		scp->term.param[scp->term.num_param] *= 10;
2555	    scp->term.param[scp->term.num_param] += c - '0';
2556	    return;
2557	    }
2558	}
2559	scp->term.num_param = scp->term.last_param + 1;
2560	switch (c) {
2561
2562	case ';':
2563	    if (scp->term.num_param < MAX_ESC_PAR)
2564		return;
2565	    break;
2566
2567	case '=':
2568	    scp->term.esc = 3;
2569	    scp->term.last_param = -1;
2570	    for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
2571		scp->term.param[i] = 1;
2572	    scp->term.num_param = 0;
2573	    return;
2574
2575	case 'A':   /* up n rows */
2576	    n = scp->term.param[0]; if (n < 1) n = 1;
2577	    move_crsr(scp, scp->xpos, scp->ypos - n);
2578	    break;
2579
2580	case 'B':   /* down n rows */
2581	    n = scp->term.param[0]; if (n < 1) n = 1;
2582	    move_crsr(scp, scp->xpos, scp->ypos + n);
2583	    break;
2584
2585	case 'C':   /* right n columns */
2586	    n = scp->term.param[0]; if (n < 1) n = 1;
2587	    move_crsr(scp, scp->xpos + n, scp->ypos);
2588	    break;
2589
2590	case 'D':   /* left n columns */
2591	    n = scp->term.param[0]; if (n < 1) n = 1;
2592	    move_crsr(scp, scp->xpos - n, scp->ypos);
2593	    break;
2594
2595	case 'E':   /* cursor to start of line n lines down */
2596	    n = scp->term.param[0]; if (n < 1) n = 1;
2597	    move_crsr(scp, 0, scp->ypos + n);
2598	    break;
2599
2600	case 'F':   /* cursor to start of line n lines up */
2601	    n = scp->term.param[0]; if (n < 1) n = 1;
2602	    move_crsr(scp, 0, scp->ypos - n);
2603	    break;
2604
2605	case 'f':   /* Cursor move */
2606	case 'H':
2607	    if (scp->term.num_param == 0)
2608		move_crsr(scp, 0, 0);
2609	    else if (scp->term.num_param == 2)
2610		move_crsr(scp, scp->term.param[1] - 1, scp->term.param[0] - 1);
2611	    break;
2612
2613	case 'J':   /* Clear all or part of display */
2614	    if (scp->term.num_param == 0)
2615		n = 0;
2616	    else
2617		n = scp->term.param[0];
2618	    switch (n) {
2619	    case 0: /* clear form cursor to end of display */
2620		fillw(scp->term.cur_color | scr_map[0x20],
2621		      scp->cursor_pos,
2622		      scp->scr_buf + scp->xsize * scp->ysize - scp->cursor_pos);
2623    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2624    		mark_for_update(scp, scp->xsize * scp->ysize);
2625		remove_cutmarking(scp);
2626		break;
2627	    case 1: /* clear from beginning of display to cursor */
2628		fillw(scp->term.cur_color | scr_map[0x20],
2629		      scp->scr_buf,
2630		      scp->cursor_pos - scp->scr_buf);
2631    		mark_for_update(scp, 0);
2632    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2633		remove_cutmarking(scp);
2634		break;
2635	    case 2: /* clear entire display */
2636		fillw(scp->term.cur_color | scr_map[0x20], scp->scr_buf,
2637		      scp->xsize * scp->ysize);
2638		mark_all(scp);
2639		remove_cutmarking(scp);
2640		break;
2641	    }
2642	    break;
2643
2644	case 'K':   /* Clear all or part of line */
2645	    if (scp->term.num_param == 0)
2646		n = 0;
2647	    else
2648		n = scp->term.param[0];
2649	    switch (n) {
2650	    case 0: /* clear form cursor to end of line */
2651		fillw(scp->term.cur_color | scr_map[0x20],
2652		      scp->cursor_pos,
2653		      scp->xsize - scp->xpos);
2654    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2655    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf +
2656				scp->xsize - scp->xpos);
2657		break;
2658	    case 1: /* clear from beginning of line to cursor */
2659		fillw(scp->term.cur_color | scr_map[0x20],
2660		      scp->cursor_pos - scp->xpos,
2661		      scp->xpos + 1);
2662    		mark_for_update(scp, scp->ypos * scp->xsize);
2663    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2664		break;
2665	    case 2: /* clear entire line */
2666		fillw(scp->term.cur_color | scr_map[0x20],
2667		      scp->cursor_pos - scp->xpos,
2668		      scp->xsize);
2669    		mark_for_update(scp, scp->ypos * scp->xsize);
2670    		mark_for_update(scp, (scp->ypos + 1) * scp->xsize);
2671		break;
2672	    }
2673	    break;
2674
2675	case 'L':   /* Insert n lines */
2676	    n = scp->term.param[0]; if (n < 1) n = 1;
2677	    if (n > scp->ysize - scp->ypos)
2678		n = scp->ysize - scp->ypos;
2679	    src = scp->scr_buf + scp->ypos * scp->xsize;
2680	    dst = src + n * scp->xsize;
2681	    count = scp->ysize - (scp->ypos + n);
2682	    bcopy(src, dst, count * scp->xsize * sizeof(u_short));
2683	    fillw(scp->term.cur_color | scr_map[0x20], src,
2684		  n * scp->xsize);
2685	    mark_for_update(scp, scp->ypos * scp->xsize);
2686	    mark_for_update(scp, scp->xsize * scp->ysize);
2687	    break;
2688
2689	case 'M':   /* Delete n lines */
2690	    n = scp->term.param[0]; if (n < 1) n = 1;
2691	    if (n > scp->ysize - scp->ypos)
2692		n = scp->ysize - scp->ypos;
2693	    dst = scp->scr_buf + scp->ypos * scp->xsize;
2694	    src = dst + n * scp->xsize;
2695	    count = scp->ysize - (scp->ypos + n);
2696	    bcopy(src, dst, count * scp->xsize * sizeof(u_short));
2697	    src = dst + count * scp->xsize;
2698	    fillw(scp->term.cur_color | scr_map[0x20], src,
2699		  n * scp->xsize);
2700	    mark_for_update(scp, scp->ypos * scp->xsize);
2701	    mark_for_update(scp, scp->xsize * scp->ysize);
2702	    break;
2703
2704	case 'P':   /* Delete n chars */
2705	    n = scp->term.param[0]; if (n < 1) n = 1;
2706	    if (n > scp->xsize - scp->xpos)
2707		n = scp->xsize - scp->xpos;
2708	    dst = scp->cursor_pos;
2709	    src = dst + n;
2710	    count = scp->xsize - (scp->xpos + n);
2711	    bcopy(src, dst, count * sizeof(u_short));
2712	    src = dst + count;
2713	    fillw(scp->term.cur_color | scr_map[0x20], src, n);
2714	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2715	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n + count);
2716	    break;
2717
2718	case '@':   /* Insert n chars */
2719	    n = scp->term.param[0]; if (n < 1) n = 1;
2720	    if (n > scp->xsize - scp->xpos)
2721		n = scp->xsize - scp->xpos;
2722	    src = scp->cursor_pos;
2723	    dst = src + n;
2724	    count = scp->xsize - (scp->xpos + n);
2725	    bcopy(src, dst, count * sizeof(u_short));
2726	    fillw(scp->term.cur_color | scr_map[0x20], src, n);
2727	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2728	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n + count);
2729	    break;
2730
2731	case 'S':   /* scroll up n lines */
2732	    n = scp->term.param[0]; if (n < 1)  n = 1;
2733	    if (n > scp->ysize)
2734		n = scp->ysize;
2735	    bcopy(scp->scr_buf + (scp->xsize * n),
2736		   scp->scr_buf,
2737		   scp->xsize * (scp->ysize - n) * sizeof(u_short));
2738	    fillw(scp->term.cur_color | scr_map[0x20],
2739		  scp->scr_buf + scp->xsize * (scp->ysize - n),
2740		  scp->xsize * n);
2741    	    mark_all(scp);
2742	    break;
2743
2744	case 'T':   /* scroll down n lines */
2745	    n = scp->term.param[0]; if (n < 1)  n = 1;
2746	    if (n > scp->ysize)
2747		n = scp->ysize;
2748	    bcopy(scp->scr_buf,
2749		  scp->scr_buf + (scp->xsize * n),
2750		  scp->xsize * (scp->ysize - n) *
2751		  sizeof(u_short));
2752	    fillw(scp->term.cur_color | scr_map[0x20],
2753		  scp->scr_buf, scp->xsize * n);
2754    	    mark_all(scp);
2755	    break;
2756
2757	case 'X':   /* erase n characters in line */
2758	    n = scp->term.param[0]; if (n < 1)  n = 1;
2759	    if (n > scp->xsize - scp->xpos)
2760		n = scp->xsize - scp->xpos;
2761	    fillw(scp->term.cur_color | scr_map[0x20],
2762		  scp->cursor_pos, n);
2763	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2764	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n);
2765	    break;
2766
2767	case 'Z':   /* move n tabs backwards */
2768	    n = scp->term.param[0]; if (n < 1)  n = 1;
2769	    if ((i = scp->xpos & 0xf8) == scp->xpos)
2770		i -= 8*n;
2771	    else
2772		i -= 8*(n-1);
2773	    if (i < 0)
2774		i = 0;
2775	    move_crsr(scp, i, scp->ypos);
2776	    break;
2777
2778	case '`':   /* move cursor to column n */
2779	    n = scp->term.param[0]; if (n < 1)  n = 1;
2780	    move_crsr(scp, n - 1, scp->ypos);
2781	    break;
2782
2783	case 'a':   /* move cursor n columns to the right */
2784	    n = scp->term.param[0]; if (n < 1)  n = 1;
2785	    move_crsr(scp, scp->xpos + n, scp->ypos);
2786	    break;
2787
2788	case 'd':   /* move cursor to row n */
2789	    n = scp->term.param[0]; if (n < 1)  n = 1;
2790	    move_crsr(scp, scp->xpos, n - 1);
2791	    break;
2792
2793	case 'e':   /* move cursor n rows down */
2794	    n = scp->term.param[0]; if (n < 1)  n = 1;
2795	    move_crsr(scp, scp->xpos, scp->ypos + n);
2796	    break;
2797
2798	case 'm':   /* change attribute */
2799	    if (scp->term.num_param == 0) {
2800		scp->term.attr_mask = NORMAL_ATTR;
2801		scp->term.cur_attr =
2802		    scp->term.cur_color = scp->term.std_color;
2803		break;
2804	    }
2805	    for (i = 0; i < scp->term.num_param; i++) {
2806		switch (n = scp->term.param[i]) {
2807		case 0: /* back to normal */
2808		    scp->term.attr_mask = NORMAL_ATTR;
2809		    scp->term.cur_attr =
2810			scp->term.cur_color = scp->term.std_color;
2811		    break;
2812		case 1: /* bold */
2813		    scp->term.attr_mask |= BOLD_ATTR;
2814		    scp->term.cur_attr = mask2attr(&scp->term);
2815		    break;
2816		case 4: /* underline */
2817		    scp->term.attr_mask |= UNDERLINE_ATTR;
2818		    scp->term.cur_attr = mask2attr(&scp->term);
2819		    break;
2820		case 5: /* blink */
2821		    scp->term.attr_mask |= BLINK_ATTR;
2822		    scp->term.cur_attr = mask2attr(&scp->term);
2823		    break;
2824		case 7: /* reverse video */
2825		    scp->term.attr_mask |= REVERSE_ATTR;
2826		    scp->term.cur_attr = mask2attr(&scp->term);
2827		    break;
2828		case 30: case 31: /* set fg color */
2829		case 32: case 33: case 34:
2830		case 35: case 36: case 37:
2831		    scp->term.attr_mask |= FOREGROUND_CHANGED;
2832		    scp->term.cur_color =
2833			(scp->term.cur_color&0xF000) | (ansi_col[(n-30)&7]<<8);
2834		    scp->term.cur_attr = mask2attr(&scp->term);
2835		    break;
2836		case 40: case 41: /* set bg color */
2837		case 42: case 43: case 44:
2838		case 45: case 46: case 47:
2839		    scp->term.attr_mask |= BACKGROUND_CHANGED;
2840		    scp->term.cur_color =
2841			(scp->term.cur_color&0x0F00) | (ansi_col[(n-40)&7]<<12);
2842		    scp->term.cur_attr = mask2attr(&scp->term);
2843		    break;
2844		}
2845	    }
2846	    break;
2847
2848	case 's':   /* Save cursor position */
2849	    scp->saved_xpos = scp->xpos;
2850	    scp->saved_ypos = scp->ypos;
2851	    break;
2852
2853	case 'u':   /* Restore saved cursor position */
2854	    if (scp->saved_xpos >= 0 && scp->saved_ypos >= 0)
2855		move_crsr(scp, scp->saved_xpos, scp->saved_ypos);
2856	    break;
2857
2858	case 'x':
2859	    if (scp->term.num_param == 0)
2860		n = 0;
2861	    else
2862		n = scp->term.param[0];
2863	    switch (n) {
2864	    case 0:     /* reset attributes */
2865		scp->term.attr_mask = NORMAL_ATTR;
2866		scp->term.cur_attr =
2867		    scp->term.cur_color = scp->term.std_color =
2868		    current_default->std_color;
2869		scp->term.rev_color = current_default->rev_color;
2870		break;
2871	    case 1:     /* set ansi background */
2872		scp->term.attr_mask &= ~BACKGROUND_CHANGED;
2873		scp->term.cur_color = scp->term.std_color =
2874		    (scp->term.std_color & 0x0F00) |
2875		    (ansi_col[(scp->term.param[1])&0x0F]<<12);
2876		scp->term.cur_attr = mask2attr(&scp->term);
2877		break;
2878	    case 2:     /* set ansi foreground */
2879		scp->term.attr_mask &= ~FOREGROUND_CHANGED;
2880		scp->term.cur_color = scp->term.std_color =
2881		    (scp->term.std_color & 0xF000) |
2882		    (ansi_col[(scp->term.param[1])&0x0F]<<8);
2883		scp->term.cur_attr = mask2attr(&scp->term);
2884		break;
2885	    case 3:     /* set ansi attribute directly */
2886		scp->term.attr_mask &= ~(FOREGROUND_CHANGED|BACKGROUND_CHANGED);
2887		scp->term.cur_color = scp->term.std_color =
2888		    (scp->term.param[1]&0xFF)<<8;
2889		scp->term.cur_attr = mask2attr(&scp->term);
2890		break;
2891	    case 5:     /* set ansi reverse video background */
2892		scp->term.rev_color =
2893		    (scp->term.rev_color & 0x0F00) |
2894		    (ansi_col[(scp->term.param[1])&0x0F]<<12);
2895		scp->term.cur_attr = mask2attr(&scp->term);
2896		break;
2897	    case 6:     /* set ansi reverse video foreground */
2898		scp->term.rev_color =
2899		    (scp->term.rev_color & 0xF000) |
2900		    (ansi_col[(scp->term.param[1])&0x0F]<<8);
2901		scp->term.cur_attr = mask2attr(&scp->term);
2902		break;
2903	    case 7:     /* set ansi reverse video directly */
2904		scp->term.rev_color =
2905		    (scp->term.param[1]&0xFF)<<8;
2906		scp->term.cur_attr = mask2attr(&scp->term);
2907		break;
2908	    }
2909	    break;
2910
2911	case 'z':   /* switch to (virtual) console n */
2912	    if (scp->term.num_param == 1)
2913		switch_scr(scp, scp->term.param[0]);
2914	    break;
2915	}
2916    }
2917    else if (scp->term.esc == 3) {	/* seen ESC [0-9]+ = */
2918	if (c >= '0' && c <= '9') {
2919	    if (scp->term.num_param < MAX_ESC_PAR) {
2920	    if (scp->term.last_param != scp->term.num_param) {
2921		scp->term.last_param = scp->term.num_param;
2922		scp->term.param[scp->term.num_param] = 0;
2923	    }
2924	    else
2925		scp->term.param[scp->term.num_param] *= 10;
2926	    scp->term.param[scp->term.num_param] += c - '0';
2927	    return;
2928	    }
2929	}
2930	scp->term.num_param = scp->term.last_param + 1;
2931	switch (c) {
2932
2933	case ';':
2934	    if (scp->term.num_param < MAX_ESC_PAR)
2935		return;
2936	    break;
2937
2938	case 'A':   /* set display border color */
2939	    if (scp->term.num_param == 1) {
2940		scp->border=scp->term.param[0] & 0xff;
2941		if (scp == cur_console)
2942		    set_border(scp->border);
2943            }
2944	    break;
2945
2946	case 'B':   /* set bell pitch and duration */
2947	    if (scp->term.num_param == 2) {
2948		scp->bell_pitch = scp->term.param[0];
2949		scp->bell_duration = scp->term.param[1]*10;
2950	    }
2951	    break;
2952
2953	case 'C':   /* set cursor type & shape */
2954	    if (scp->term.num_param == 1) {
2955		if (scp->term.param[0] & 0x01)
2956		    flags |= BLINK_CURSOR;
2957		else
2958		    flags &= ~BLINK_CURSOR;
2959		if ((scp->term.param[0] & 0x02) && crtc_vga)
2960		    flags |= CHAR_CURSOR;
2961		else
2962		    flags &= ~CHAR_CURSOR;
2963	    }
2964	    else if (scp->term.num_param == 2) {
2965		scp->cursor_start = scp->term.param[0] & 0x1F;
2966		scp->cursor_end = scp->term.param[1] & 0x1F;
2967	    }
2968	    /*
2969	     * The cursor shape is global property; all virtual consoles
2970	     * are affected. Update the cursor in the current console...
2971	     */
2972	    if (!(cur_console->status & UNKNOWN_MODE)) {
2973		remove_cursor_image(cur_console);
2974		if (crtc_vga && (flags & CHAR_CURSOR))
2975	            set_destructive_cursor(cur_console);
2976		draw_cursor_image(cur_console);
2977	    }
2978	    break;
2979
2980	case 'F':   /* set ansi foreground */
2981	    if (scp->term.num_param == 1) {
2982		scp->term.attr_mask &= ~FOREGROUND_CHANGED;
2983		scp->term.cur_color = scp->term.std_color =
2984		    (scp->term.std_color & 0xF000)
2985		    | ((scp->term.param[0] & 0x0F) << 8);
2986		scp->term.cur_attr = mask2attr(&scp->term);
2987	    }
2988	    break;
2989
2990	case 'G':   /* set ansi background */
2991	    if (scp->term.num_param == 1) {
2992		scp->term.attr_mask &= ~BACKGROUND_CHANGED;
2993		scp->term.cur_color = scp->term.std_color =
2994		    (scp->term.std_color & 0x0F00)
2995		    | ((scp->term.param[0] & 0x0F) << 12);
2996		scp->term.cur_attr = mask2attr(&scp->term);
2997	    }
2998	    break;
2999
3000	case 'H':   /* set ansi reverse video foreground */
3001	    if (scp->term.num_param == 1) {
3002		scp->term.rev_color =
3003		    (scp->term.rev_color & 0xF000)
3004		    | ((scp->term.param[0] & 0x0F) << 8);
3005		scp->term.cur_attr = mask2attr(&scp->term);
3006	    }
3007	    break;
3008
3009	case 'I':   /* set ansi reverse video background */
3010	    if (scp->term.num_param == 1) {
3011		scp->term.rev_color =
3012		    (scp->term.rev_color & 0x0F00)
3013		    | ((scp->term.param[0] & 0x0F) << 12);
3014		scp->term.cur_attr = mask2attr(&scp->term);
3015	    }
3016	    break;
3017	}
3018    }
3019#if notyet
3020    else if (scp->term.esc == 4) {	/* seen ESC Q */
3021	/* to be filled */
3022    }
3023#endif
3024    else if (scp->term.esc == 5) {	/* seen ESC ( */
3025	switch (c) {
3026	case 'B':   /* iso-2022: desginate ASCII into G0 */
3027	    break;
3028	/* other items to be filled */
3029	default:
3030	    break;
3031	}
3032    }
3033    scp->term.esc = 0;
3034}
3035
3036static void
3037ansi_put(scr_stat *scp, u_char *buf, int len)
3038{
3039    u_char *ptr = buf;
3040
3041    /* make screensaver happy */
3042    if (scp == cur_console)
3043	getmicroruntime(&scrn_time_stamp);
3044
3045    write_in_progress++;
3046outloop:
3047    if (scp->term.esc) {
3048	scan_esc(scp, *ptr++);
3049	len--;
3050    }
3051    else if (PRINTABLE(*ptr)) {     /* Print only printables */
3052 	int cnt = len <= (scp->xsize-scp->xpos) ? len : (scp->xsize-scp->xpos);
3053 	u_short cur_attr = scp->term.cur_attr;
3054 	u_short *cursor_pos = scp->cursor_pos;
3055	do {
3056	    /*
3057	     * gcc-2.6.3 generates poor (un)sign extension code.  Casting the
3058	     * pointers in the following to volatile should have no effect,
3059	     * but in fact speeds up this inner loop from 26 to 18 cycles
3060	     * (+ cache misses) on i486's.
3061	     */
3062#define	UCVP(ucp)	((u_char volatile *)(ucp))
3063	    *cursor_pos++ = UCVP(scr_map)[*UCVP(ptr)] | cur_attr;
3064	    ptr++;
3065	    cnt--;
3066	} while (cnt && PRINTABLE(*ptr));
3067	len -= (cursor_pos - scp->cursor_pos);
3068	scp->xpos += (cursor_pos - scp->cursor_pos);
3069	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
3070	mark_for_update(scp, cursor_pos - scp->scr_buf);
3071	scp->cursor_pos = cursor_pos;
3072	if (scp->xpos >= scp->xsize) {
3073	    scp->xpos = 0;
3074	    scp->ypos++;
3075	}
3076    }
3077    else  {
3078	switch(*ptr) {
3079	case 0x07:
3080	    do_bell(scp, scp->bell_pitch, scp->bell_duration);
3081	    break;
3082
3083	case 0x08:      /* non-destructive backspace */
3084	    if (scp->cursor_pos > scp->scr_buf) {
3085	    	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
3086		scp->cursor_pos--;
3087	    	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
3088		if (scp->xpos > 0)
3089		    scp->xpos--;
3090		else {
3091		    scp->xpos += scp->xsize - 1;
3092		    scp->ypos--;
3093		}
3094	    }
3095	    break;
3096
3097	case 0x09:  /* non-destructive tab */
3098	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
3099	    scp->cursor_pos += (8 - scp->xpos % 8u);
3100	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
3101	    if ((scp->xpos += (8 - scp->xpos % 8u)) >= scp->xsize) {
3102	        scp->xpos = 0;
3103	        scp->ypos++;
3104	    }
3105	    break;
3106
3107	case 0x0a:  /* newline, same pos */
3108	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
3109	    scp->cursor_pos += scp->xsize;
3110	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
3111	    scp->ypos++;
3112	    break;
3113
3114	case 0x0c:  /* form feed, clears screen */
3115	    clear_screen(scp);
3116	    break;
3117
3118	case 0x0d:  /* return, return to pos 0 */
3119	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
3120	    scp->cursor_pos -= scp->xpos;
3121	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
3122	    scp->xpos = 0;
3123	    break;
3124
3125	case 0x1b:  /* start escape sequence */
3126	    scp->term.esc = 1;
3127	    scp->term.num_param = 0;
3128	    break;
3129	}
3130	ptr++; len--;
3131    }
3132    /* do we have to scroll ?? */
3133    if (scp->cursor_pos >= scp->scr_buf + scp->ysize * scp->xsize) {
3134	remove_cutmarking(scp);
3135	if (scp->history) {
3136	    bcopy(scp->scr_buf, scp->history_head,
3137		   scp->xsize * sizeof(u_short));
3138	    scp->history_head += scp->xsize;
3139	    if (scp->history_head + scp->xsize >
3140		scp->history + scp->history_size)
3141		scp->history_head = scp->history;
3142	}
3143	bcopy(scp->scr_buf + scp->xsize, scp->scr_buf,
3144	       scp->xsize * (scp->ysize - 1) * sizeof(u_short));
3145	fillw(scp->term.cur_color | scr_map[0x20],
3146	      scp->scr_buf + scp->xsize * (scp->ysize - 1),
3147	      scp->xsize);
3148	scp->cursor_pos -= scp->xsize;
3149	scp->ypos--;
3150    	mark_all(scp);
3151    }
3152    if (len)
3153	goto outloop;
3154    write_in_progress--;
3155    if (delayed_next_scr)
3156	switch_scr(scp, delayed_next_scr - 1);
3157}
3158
3159static void
3160scinit(void)
3161{
3162    u_int hw_cursor;
3163    u_int i;
3164
3165    if (init_done != COLD)
3166	return;
3167    init_done = WARM;
3168
3169    /*
3170     * Ensure a zero start address.  This is mainly to recover after
3171     * switching from pcvt using userconfig().  The registers are w/o
3172     * for old hardware so it's too hard to relocate the active screen
3173     * memory.
3174     */
3175    outb(crtc_addr, 12);
3176    outb(crtc_addr + 1, 0);
3177    outb(crtc_addr, 13);
3178    outb(crtc_addr + 1, 0);
3179
3180    /* extract cursor location */
3181    outb(crtc_addr, 14);
3182    hw_cursor = inb(crtc_addr + 1) << 8;
3183    outb(crtc_addr, 15);
3184    hw_cursor |= inb(crtc_addr + 1);
3185
3186    /*
3187     * Validate cursor location.  It may be off the screen.  Then we must
3188     * not use it for the initial buffer offset.
3189     */
3190    if (hw_cursor >= ROW * COL)
3191	hw_cursor = (ROW - 1) * COL;
3192
3193    /* move hardware cursor out of the way */
3194    outb(crtc_addr, 14);
3195    outb(crtc_addr + 1, 0xff);
3196    outb(crtc_addr, 15);
3197    outb(crtc_addr + 1, 0xff);
3198
3199    /* set up the first console */
3200    current_default = &user_default;
3201    console[0] = &main_console;
3202    init_scp(console[0]);
3203    cur_console = console[0];
3204
3205    /* discard the video mode table if we are not familiar with it... */
3206    if (video_mode_ptr) {
3207        bzero(mode_map, sizeof(mode_map));
3208	bcopy(video_mode_ptr + MODE_PARAM_SIZE*console[0]->mode,
3209	      vgaregs2, sizeof(vgaregs2));
3210        switch (comp_vgaregs(vgaregs, video_mode_ptr
3211                    + MODE_PARAM_SIZE*console[0]->mode)) {
3212        case COMP_IDENTICAL:
3213            map_mode_table(mode_map, video_mode_ptr, M_VGA_CG320 + 1);
3214            /*
3215             * This is a kludge for Toshiba DynaBook SS433 whose BIOS video
3216             * mode table entry has the actual # of rows at the offset 1;
3217	     * BIOSes from other manufacturers store the # of rows - 1 there.
3218	     * XXX
3219             */
3220	    rows_offset = vgaregs[1] + 1
3221		- video_mode_ptr[MODE_PARAM_SIZE*console[0]->mode + 1];
3222            break;
3223        case COMP_SIMILAR:
3224            map_mode_table(mode_map, video_mode_ptr, M_VGA_CG320 + 1);
3225            mode_map[console[0]->mode] = vgaregs;
3226	    rows_offset = vgaregs[1] + 1
3227		- video_mode_ptr[MODE_PARAM_SIZE*console[0]->mode + 1];
3228            vgaregs[1] -= rows_offset - 1;
3229            break;
3230        case COMP_DIFFERENT:
3231        default:
3232            video_mode_ptr = NULL;
3233            mode_map[console[0]->mode] = vgaregs;
3234	    rows_offset = 1;
3235            break;
3236        }
3237    }
3238
3239    /* copy screen to temporary buffer */
3240    if (crtc_type != KD_PIXEL)
3241	    generic_bcopy(Crtat, sc_buffer,
3242		   console[0]->xsize * console[0]->ysize * sizeof(u_short));
3243
3244    console[0]->scr_buf = console[0]->mouse_pos = console[0]->mouse_oldpos
3245	= sc_buffer;
3246    console[0]->cursor_pos = console[0]->cursor_oldpos = sc_buffer + hw_cursor;
3247    console[0]->cursor_saveunder = *console[0]->cursor_pos;
3248    console[0]->xpos = hw_cursor % COL;
3249    console[0]->ypos = hw_cursor / COL;
3250    for (i=1; i<MAXCONS; i++)
3251	console[i] = NULL;
3252    kernel_console.esc = 0;
3253    kernel_console.attr_mask = NORMAL_ATTR;
3254    kernel_console.cur_attr =
3255	kernel_console.cur_color = kernel_console.std_color =
3256	kernel_default.std_color;
3257    kernel_console.rev_color = kernel_default.rev_color;
3258
3259    /* initialize mapscrn arrays to a one to one map */
3260    for (i=0; i<sizeof(scr_map); i++) {
3261	scr_map[i] = scr_rmap[i] = i;
3262    }
3263
3264    /* Save font and palette if VGA */
3265    if (crtc_vga) {
3266	if (fonts_loaded & FONT_16) {
3267		copy_font(LOAD, FONT_16, font_16);
3268	} else {
3269		copy_font(SAVE, FONT_16, font_16);
3270		fonts_loaded = FONT_16;
3271	}
3272	save_palette();
3273	set_destructive_cursor(console[0]);
3274    }
3275
3276#ifdef SC_SPLASH_SCREEN
3277    /*
3278     * Now put up a graphics image, and maybe cycle a
3279     * couble of palette entries for simple animation.
3280     */
3281    toggle_splash_screen(cur_console);
3282#endif
3283}
3284
3285static void
3286map_mode_table(char *map[], char *table, int max)
3287{
3288    int i;
3289
3290    for(i = 0; i < max; ++i)
3291	map[i] = table + i*MODE_PARAM_SIZE;
3292    for(; i < MODE_MAP_SIZE; ++i)
3293	map[i] = NULL;
3294}
3295
3296static u_char
3297map_mode_num(u_char mode)
3298{
3299    static struct {
3300        u_char from;
3301        u_char to;
3302    } mode_map[] = {
3303        { M_ENH_B80x43, M_ENH_B80x25 },
3304        { M_ENH_C80x43, M_ENH_C80x25 },
3305        { M_VGA_M80x30, M_VGA_M80x25 },
3306        { M_VGA_C80x30, M_VGA_C80x25 },
3307        { M_VGA_M80x50, M_VGA_M80x25 },
3308        { M_VGA_C80x50, M_VGA_C80x25 },
3309        { M_VGA_M80x60, M_VGA_M80x25 },
3310        { M_VGA_C80x60, M_VGA_C80x25 },
3311        { M_VGA_MODEX,  M_VGA_CG320 },
3312    };
3313    int i;
3314
3315    for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
3316        if (mode_map[i].from == mode)
3317            return mode_map[i].to;
3318    }
3319    return mode;
3320}
3321
3322static char
3323*get_mode_param(scr_stat *scp, u_char mode)
3324{
3325    if (mode >= MODE_MAP_SIZE)
3326	mode = map_mode_num(mode);
3327    if (mode < MODE_MAP_SIZE)
3328	return mode_map[mode];
3329    else
3330	return NULL;
3331}
3332
3333static scr_stat
3334*alloc_scp()
3335{
3336    scr_stat *scp;
3337
3338    scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
3339    init_scp(scp);
3340    scp->scr_buf = scp->cursor_pos = scp->cursor_oldpos =
3341	(u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
3342			  M_DEVBUF, M_WAITOK);
3343    scp->mouse_pos = scp->mouse_oldpos =
3344	scp->scr_buf + ((scp->mouse_ypos/scp->font_size)*scp->xsize +
3345			scp->mouse_xpos/8);
3346    scp->history_head = scp->history_pos =
3347	(u_short *)malloc(scp->history_size*sizeof(u_short),
3348			  M_DEVBUF, M_WAITOK);
3349    bzero(scp->history_head, scp->history_size*sizeof(u_short));
3350    scp->history = scp->history_head;
3351/* SOS
3352    if (crtc_vga && video_mode_ptr)
3353	set_mode(scp);
3354*/
3355    clear_screen(scp);
3356    scp->cursor_saveunder = *scp->cursor_pos;
3357    return scp;
3358}
3359
3360static void
3361init_scp(scr_stat *scp)
3362{
3363    if (crtc_vga)
3364	if (crtc_addr == MONO_BASE)
3365	    scp->mode = M_VGA_M80x25;
3366	else
3367	    scp->mode = M_VGA_C80x25;
3368    else
3369	if (crtc_addr == MONO_BASE)
3370	    scp->mode = M_B80x25;
3371	else
3372	    scp->mode = M_C80x25;
3373    scp->initial_mode = scp->mode;
3374
3375    scp->font_size = 16;
3376    scp->xsize = COL;
3377    scp->ysize = ROW;
3378    scp->xpixel = scp->xsize * 8;
3379    scp->ypixel = scp->ysize * scp->font_size;
3380    scp->xpos = scp->ypos = 0;
3381    scp->saved_xpos = scp->saved_ypos = -1;
3382    scp->start = scp->xsize * scp->ysize;
3383    scp->end = 0;
3384    scp->term.esc = 0;
3385    scp->term.attr_mask = NORMAL_ATTR;
3386    scp->term.cur_attr =
3387	scp->term.cur_color = scp->term.std_color =
3388	current_default->std_color;
3389    scp->term.rev_color = current_default->rev_color;
3390    scp->border = BG_BLACK;
3391    scp->cursor_start = *(char *)pa_to_va(0x461);
3392    scp->cursor_end = *(char *)pa_to_va(0x460);
3393    scp->mouse_xpos = scp->xsize*8/2;
3394    scp->mouse_ypos = scp->ysize*scp->font_size/2;
3395    scp->mouse_cut_start = scp->mouse_cut_end = NULL;
3396    scp->mouse_signal = 0;
3397    scp->mouse_pid = 0;
3398    scp->mouse_proc = NULL;
3399    scp->bell_pitch = BELL_PITCH;
3400    scp->bell_duration = BELL_DURATION;
3401    scp->status = (*(char *)pa_to_va(0x417) & 0x20) ? NLKED : 0;
3402    scp->status |= CURSOR_ENABLED;
3403    scp->pid = 0;
3404    scp->proc = NULL;
3405    scp->smode.mode = VT_AUTO;
3406    scp->history_head = scp->history_pos = scp->history = NULL;
3407    scp->history_size = imax(SC_HISTORY_SIZE, scp->ysize) * scp->xsize;
3408}
3409
3410static u_char
3411*get_fstr(u_int c, u_int *len)
3412{
3413    u_int i;
3414
3415    if (!(c & FKEY))
3416	return(NULL);
3417    i = (c & 0xFF) - F_FN;
3418    if (i > n_fkey_tab)
3419	return(NULL);
3420    *len = fkey_tab[i].len;
3421    return(fkey_tab[i].str);
3422}
3423
3424static void
3425history_to_screen(scr_stat *scp)
3426{
3427    int i;
3428
3429    for (i=0; i<scp->ysize; i++)
3430	bcopy(scp->history + (((scp->history_pos - scp->history) +
3431	       scp->history_size-((i+1)*scp->xsize))%scp->history_size),
3432	       scp->scr_buf + (scp->xsize * (scp->ysize-1 - i)),
3433	       scp->xsize * sizeof(u_short));
3434    mark_all(scp);
3435}
3436
3437static int
3438history_up_line(scr_stat *scp)
3439{
3440    if (WRAPHIST(scp, scp->history_pos, -(scp->xsize*scp->ysize)) !=
3441	scp->history_head) {
3442	scp->history_pos = WRAPHIST(scp, scp->history_pos, -scp->xsize);
3443	history_to_screen(scp);
3444	return 0;
3445    }
3446    else
3447	return -1;
3448}
3449
3450static int
3451history_down_line(scr_stat *scp)
3452{
3453    if (scp->history_pos != scp->history_head) {
3454	scp->history_pos = WRAPHIST(scp, scp->history_pos, scp->xsize);
3455	history_to_screen(scp);
3456	return 0;
3457    }
3458    else
3459	return -1;
3460}
3461
3462/*
3463 * scgetc(flags) - get character from keyboard.
3464 * If flags & SCGETC_CN, then avoid harmful side effects.
3465 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3466 * return NOKEY if there is nothing there.
3467 */
3468static u_int
3469scgetc(u_int flags)
3470{
3471    struct key_t *key;
3472    u_char scancode, keycode;
3473    u_int state, action;
3474    int c;
3475    static u_char esc_flag = 0, compose = 0;
3476    static u_int chr = 0;
3477
3478next_code:
3479    /* first see if there is something in the keyboard port */
3480    if (flags & SCGETC_NONBLOCK) {
3481	c = read_kbd_data_no_wait(sc_kbdc);
3482	if (c == -1)
3483	    return(NOKEY);
3484    } else {
3485	do {
3486	    c = read_kbd_data(sc_kbdc);
3487	} while(c == -1);
3488    }
3489    scancode = (u_char)c;
3490
3491    /* make screensaver happy */
3492    if (!(scancode & 0x80))
3493	getmicroruntime(&scrn_time_stamp);
3494
3495    if (!(flags & SCGETC_CN)) {
3496	/* do the /dev/random device a favour */
3497	add_keyboard_randomness(scancode);
3498
3499	if (cur_console->status & KBD_RAW_MODE)
3500	    return scancode;
3501    }
3502
3503    keycode = scancode & 0x7F;
3504    switch (esc_flag) {
3505    case 0x00:      /* normal scancode */
3506	switch(scancode) {
3507	case 0xB8:  /* left alt (compose key) */
3508	    if (compose) {
3509		compose = 0;
3510		if (chr > 255) {
3511		    do_bell(cur_console,
3512			BELL_PITCH, BELL_DURATION);
3513		    chr = 0;
3514		}
3515	    }
3516	    break;
3517	case 0x38:
3518	    if (!compose) {
3519		compose = 1;
3520		chr = 0;
3521	    }
3522	    break;
3523	case 0xE0:
3524	case 0xE1:
3525	    esc_flag = scancode;
3526	    goto next_code;
3527	}
3528	break;
3529    case 0xE0:      /* 0xE0 prefix */
3530	esc_flag = 0;
3531	switch (keycode) {
3532	case 0x1C:  /* right enter key */
3533	    keycode = 0x59;
3534	    break;
3535	case 0x1D:  /* right ctrl key */
3536	    keycode = 0x5A;
3537	    break;
3538	case 0x35:  /* keypad divide key */
3539	    keycode = 0x5B;
3540	    break;
3541	case 0x37:  /* print scrn key */
3542	    keycode = 0x5C;
3543	    break;
3544	case 0x38:  /* right alt key (alt gr) */
3545	    keycode = 0x5D;
3546	    break;
3547	case 0x47:  /* grey home key */
3548	    keycode = 0x5E;
3549	    break;
3550	case 0x48:  /* grey up arrow key */
3551	    keycode = 0x5F;
3552	    break;
3553	case 0x49:  /* grey page up key */
3554	    keycode = 0x60;
3555	    break;
3556	case 0x4B:  /* grey left arrow key */
3557	    keycode = 0x61;
3558	    break;
3559	case 0x4D:  /* grey right arrow key */
3560	    keycode = 0x62;
3561	    break;
3562	case 0x4F:  /* grey end key */
3563	    keycode = 0x63;
3564	    break;
3565	case 0x50:  /* grey down arrow key */
3566	    keycode = 0x64;
3567	    break;
3568	case 0x51:  /* grey page down key */
3569	    keycode = 0x65;
3570	    break;
3571	case 0x52:  /* grey insert key */
3572	    keycode = 0x66;
3573	    break;
3574	case 0x53:  /* grey delete key */
3575	    keycode = 0x67;
3576	    break;
3577
3578	/* the following 3 are only used on the MS "Natural" keyboard */
3579	case 0x5b:  /* left Window key */
3580	    keycode = 0x69;
3581	    break;
3582	case 0x5c:  /* right Window key */
3583	    keycode = 0x6a;
3584	    break;
3585	case 0x5d:  /* menu key */
3586	    keycode = 0x6b;
3587	    break;
3588	default:    /* ignore everything else */
3589	    goto next_code;
3590	}
3591	break;
3592    case 0xE1:      /* 0xE1 prefix */
3593	esc_flag = 0;
3594	if (keycode == 0x1D)
3595	    esc_flag = 0x1D;
3596	goto next_code;
3597	/* NOT REACHED */
3598    case 0x1D:      /* pause / break */
3599	esc_flag = 0;
3600	if (keycode != 0x45)
3601	    goto next_code;
3602	keycode = 0x68;
3603	break;
3604    }
3605
3606    if (!(flags & SCGETC_CN) && (cur_console->status & KBD_CODE_MODE))
3607	return (keycode | (scancode & 0x80));
3608
3609    /* if scroll-lock pressed allow history browsing */
3610    if (cur_console->history && cur_console->status & SLKED) {
3611	int i;
3612
3613	cur_console->status &= ~CURSOR_ENABLED;
3614	if (!(cur_console->status & BUFFER_SAVED)) {
3615	    cur_console->status |= BUFFER_SAVED;
3616	    cur_console->history_save = cur_console->history_head;
3617
3618	    /* copy screen into top of history buffer */
3619	    for (i=0; i<cur_console->ysize; i++) {
3620		bcopy(cur_console->scr_buf + (cur_console->xsize * i),
3621		       cur_console->history_head,
3622		       cur_console->xsize * sizeof(u_short));
3623		cur_console->history_head += cur_console->xsize;
3624		if (cur_console->history_head + cur_console->xsize >
3625		    cur_console->history + cur_console->history_size)
3626		    cur_console->history_head=cur_console->history;
3627	    }
3628	    cur_console->history_pos = cur_console->history_head;
3629	    history_to_screen(cur_console);
3630	}
3631	switch (scancode) {
3632	case 0x47:  /* home key */
3633	    cur_console->history_pos = cur_console->history_head;
3634	    history_to_screen(cur_console);
3635	    goto next_code;
3636
3637	case 0x4F:  /* end key */
3638	    cur_console->history_pos =
3639		WRAPHIST(cur_console, cur_console->history_head,
3640			 cur_console->xsize*cur_console->ysize);
3641	    history_to_screen(cur_console);
3642	    goto next_code;
3643
3644	case 0x48:  /* up arrow key */
3645	    if (history_up_line(cur_console))
3646		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
3647	    goto next_code;
3648
3649	case 0x50:  /* down arrow key */
3650	    if (history_down_line(cur_console))
3651		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
3652	    goto next_code;
3653
3654	case 0x49:  /* page up key */
3655	    for (i=0; i<cur_console->ysize; i++)
3656	    if (history_up_line(cur_console)) {
3657		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
3658		break;
3659	    }
3660	    goto next_code;
3661
3662	case 0x51:  /* page down key */
3663	    for (i=0; i<cur_console->ysize; i++)
3664	    if (history_down_line(cur_console)) {
3665		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
3666		break;
3667	    }
3668	    goto next_code;
3669	}
3670    }
3671
3672    if (compose) {
3673	switch (scancode) {
3674	/* key pressed process it */
3675	case 0x47: case 0x48: case 0x49:    /* keypad 7,8,9 */
3676	    chr = (scancode - 0x40) + chr*10;
3677	    goto next_code;
3678	case 0x4B: case 0x4C: case 0x4D:    /* keypad 4,5,6 */
3679	    chr = (scancode - 0x47) + chr*10;
3680	    goto next_code;
3681	case 0x4F: case 0x50: case 0x51:    /* keypad 1,2,3 */
3682	    chr = (scancode - 0x4E) + chr*10;
3683	    goto next_code;
3684	case 0x52:              /* keypad 0 */
3685	    chr *= 10;
3686	    goto next_code;
3687
3688	/* key release, no interest here */
3689	case 0xC7: case 0xC8: case 0xC9:    /* keypad 7,8,9 */
3690	case 0xCB: case 0xCC: case 0xCD:    /* keypad 4,5,6 */
3691	case 0xCF: case 0xD0: case 0xD1:    /* keypad 1,2,3 */
3692	case 0xD2:              /* keypad 0 */
3693	    goto next_code;
3694
3695	case 0x38:              /* left alt key */
3696	    break;
3697	default:
3698	    if (chr) {
3699		compose = chr = 0;
3700		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
3701		goto next_code;
3702	    }
3703	    break;
3704	}
3705    }
3706
3707    state = (shfts ? 1 : 0 ) | (2 * (ctls ? 1 : 0)) | (4 * (alts ? 1 : 0));
3708    if ((!agrs && (cur_console->status & ALKED))
3709	|| (agrs && !(cur_console->status & ALKED)))
3710	keycode += ALTGR_OFFSET;
3711    key = &key_map.key[keycode];
3712    if ( ((key->flgs & FLAG_LOCK_C) && (cur_console->status & CLKED))
3713	 || ((key->flgs & FLAG_LOCK_N) && (cur_console->status & NLKED)) )
3714	state ^= 1;
3715
3716    /* Check for make/break */
3717    action = key->map[state];
3718    if (scancode & 0x80) {      /* key released */
3719	if (key->spcl & (0x80>>state)) {
3720	    switch (action) {
3721	    case LSH:
3722		shfts &= ~1;
3723		break;
3724	    case RSH:
3725		shfts &= ~2;
3726		break;
3727	    case LCTR:
3728		ctls &= ~1;
3729		break;
3730	    case RCTR:
3731		ctls &= ~2;
3732		break;
3733	    case LALT:
3734		alts &= ~1;
3735		break;
3736	    case RALT:
3737		alts &= ~2;
3738		break;
3739	    case NLK:
3740		nlkcnt = 0;
3741		break;
3742	    case CLK:
3743		clkcnt = 0;
3744		break;
3745	    case SLK:
3746		slkcnt = 0;
3747		break;
3748	    case ASH:
3749		agrs = 0;
3750		break;
3751	    case ALK:
3752		alkcnt = 0;
3753		break;
3754	    case META:
3755		metas = 0;
3756		break;
3757	    }
3758	}
3759	if (chr && !compose) {
3760	    action = chr;
3761	    chr = 0;
3762	    return(action);
3763	}
3764    } else {
3765	/* key pressed */
3766	if (key->spcl & (0x80>>state)) {
3767	    switch (action) {
3768	    /* LOCKING KEYS */
3769	    case NLK:
3770#ifdef SC_SPLASH_SCREEN
3771		toggle_splash_screen(cur_console); /* SOS XXX */
3772#endif
3773		if (!nlkcnt) {
3774		    nlkcnt++;
3775		    if (cur_console->status & NLKED)
3776			cur_console->status &= ~NLKED;
3777		    else
3778			cur_console->status |= NLKED;
3779		    update_leds(cur_console->status);
3780		}
3781		break;
3782	    case CLK:
3783		if (!clkcnt) {
3784		    clkcnt++;
3785		    if (cur_console->status & CLKED)
3786			cur_console->status &= ~CLKED;
3787		    else
3788			cur_console->status |= CLKED;
3789		    update_leds(cur_console->status);
3790		}
3791		break;
3792	    case SLK:
3793		if (!slkcnt) {
3794		    slkcnt++;
3795		    if (cur_console->status & SLKED) {
3796			cur_console->status &= ~SLKED;
3797			if (cur_console->status & BUFFER_SAVED){
3798			    int i;
3799			    u_short *ptr = cur_console->history_save;
3800
3801			    for (i=0; i<cur_console->ysize; i++) {
3802				bcopy(ptr,
3803				       cur_console->scr_buf +
3804				       (cur_console->xsize*i),
3805				       cur_console->xsize * sizeof(u_short));
3806				ptr += cur_console->xsize;
3807				if (ptr + cur_console->xsize >
3808				    cur_console->history +
3809				    cur_console->history_size)
3810				    ptr = cur_console->history;
3811			    }
3812			    cur_console->status &= ~BUFFER_SAVED;
3813			    cur_console->history_head=cur_console->history_save;
3814			    cur_console->status |= CURSOR_ENABLED;
3815			    mark_all(cur_console);
3816			}
3817			scstart(VIRTUAL_TTY(get_scr_num()));
3818		    }
3819		    else
3820			cur_console->status |= SLKED;
3821		    update_leds(cur_console->status);
3822		}
3823		break;
3824	    case ALK:
3825		if (!alkcnt) {
3826		    alkcnt++;
3827		    if (cur_console->status & ALKED)
3828			cur_console->status &= ~ALKED;
3829		    else
3830			cur_console->status |= ALKED;
3831		    update_leds(cur_console->status);
3832		}
3833		break;
3834
3835	    /* NON-LOCKING KEYS */
3836	    case NOP:
3837		break;
3838	    case SPSC:
3839#ifdef SC_SPLASH_SCREEN
3840		accents = 0;
3841		toggle_splash_screen(cur_console);
3842#endif
3843		break;
3844	    case RBT:
3845#ifndef SC_DISABLE_REBOOT
3846		accents = 0;
3847		shutdown_nice();
3848#endif
3849		break;
3850	    case SUSP:
3851#if NAPM > 0
3852		accents = 0;
3853		apm_suspend();
3854#endif
3855		break;
3856
3857	    case DBG:
3858#ifdef DDB          /* try to switch to console 0 */
3859		accents = 0;
3860		if (cur_console->smode.mode == VT_AUTO &&
3861		    console[0]->smode.mode == VT_AUTO)
3862		    switch_scr(cur_console, 0);
3863		Debugger("manual escape to debugger");
3864#else
3865		printf("No debugger in kernel\n");
3866#endif
3867		break;
3868	    case LSH:
3869		shfts |= 1;
3870		break;
3871	    case RSH:
3872		shfts |= 2;
3873		break;
3874	    case LCTR:
3875		ctls |= 1;
3876		break;
3877	    case RCTR:
3878		ctls |= 2;
3879		break;
3880	    case LALT:
3881		alts |= 1;
3882		break;
3883	    case RALT:
3884		alts |= 2;
3885		break;
3886	    case ASH:
3887		agrs = 1;
3888		break;
3889	    case META:
3890		metas = 1;
3891		break;
3892	    case NEXT:
3893		{
3894		int next, this = get_scr_num();
3895		accents = 0;
3896		for (next = this+1; next != this; next = (next+1)%MAXCONS) {
3897		    struct tty *tp = VIRTUAL_TTY(next);
3898		    if (tp->t_state & TS_ISOPEN) {
3899			switch_scr(cur_console, next);
3900			break;
3901		    }
3902		}
3903		}
3904		break;
3905	    case BTAB:
3906		accents = 0;
3907		return(BKEY);
3908	    default:
3909		if (action >= F_ACC && action <= L_ACC) {
3910		    /* turn it into an index */
3911		    action -= F_ACC - 1;
3912		    if ((action > accent_map.n_accs)
3913			|| (accent_map.acc[action - 1].accchar == 0)) {
3914			/*
3915			 * The index is out of range or pointing to an
3916			 * empty entry.
3917			 */
3918			accents = 0;
3919			do_bell(cur_console, BELL_PITCH, BELL_DURATION);
3920		    }
3921		    /*
3922		     * If the same accent key has been hit twice,
3923		     * produce the accent char itself.
3924		     */
3925		    if (action == accents) {
3926			action = accent_map.acc[accents - 1].accchar;
3927			accents = 0;
3928			if (metas)
3929			    action |= MKEY;
3930			return (action);
3931		    }
3932		    /* remember the index and wait for the next key stroke */
3933		    accents = action;
3934		    break;
3935		}
3936		if (accents > 0) {
3937		    accents = 0;
3938		    do_bell(cur_console, BELL_PITCH, BELL_DURATION);
3939		}
3940		if (action >= F_SCR && action <= L_SCR) {
3941		    switch_scr(cur_console, action - F_SCR);
3942		    break;
3943		}
3944		if (action >= F_FN && action <= L_FN)
3945		    action |= FKEY;
3946		return(action);
3947	    }
3948	}
3949	else {
3950	    if (accents) {
3951		struct acc_t *acc;
3952		int i;
3953
3954		acc = &accent_map.acc[accents - 1];
3955		accents = 0;
3956		/*
3957		 * If the accent key is followed by the space key,
3958		 * produce the accent char itself.
3959		 */
3960		if (action == ' ') {
3961		    action = acc->accchar;
3962		    if (metas)
3963			action |= MKEY;
3964		    return (action);
3965		}
3966		for (i = 0; i < NUM_ACCENTCHARS; ++i) {
3967		    if (acc->map[i][0] == 0)	/* end of the map entry */
3968			break;
3969		    if (acc->map[i][0] == action) {
3970			action = acc->map[i][1];
3971			if (metas)
3972			    action |= MKEY;
3973			return (action);
3974		    }
3975		}
3976		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
3977		goto next_code;
3978	    }
3979	    if (metas)
3980		action |= MKEY;
3981	    return(action);
3982	}
3983    }
3984    goto next_code;
3985}
3986
3987int
3988scmmap(dev_t dev, int offset, int nprot)
3989{
3990    if (offset > 0x20000 - PAGE_SIZE)
3991	return -1;
3992    return i386_btop((VIDEOMEM + offset));
3993}
3994
3995/*
3996 * Calculate hardware attributes word using logical attributes mask and
3997 * hardware colors
3998 */
3999
4000static int
4001mask2attr(struct term_stat *term)
4002{
4003    int attr, mask = term->attr_mask;
4004
4005    if (mask & REVERSE_ATTR) {
4006	attr = ((mask & FOREGROUND_CHANGED) ?
4007		((term->cur_color & 0xF000) >> 4) :
4008		(term->rev_color & 0x0F00)) |
4009	       ((mask & BACKGROUND_CHANGED) ?
4010		((term->cur_color & 0x0F00) << 4) :
4011		(term->rev_color & 0xF000));
4012    } else
4013	attr = term->cur_color;
4014
4015    /* XXX: underline mapping for Hercules adapter can be better */
4016    if (mask & (BOLD_ATTR | UNDERLINE_ATTR))
4017	attr ^= 0x0800;
4018    if (mask & BLINK_ATTR)
4019	attr ^= 0x8000;
4020
4021    return attr;
4022}
4023
4024static void
4025set_keyboard(int command, int data)
4026{
4027    int s;
4028
4029    if (sc_kbdc == NULL)
4030	return;
4031
4032    /* prevent the timeout routine from polling the keyboard */
4033    if (!kbdc_lock(sc_kbdc, TRUE))
4034	return;
4035
4036    /* disable the keyboard and mouse interrupt */
4037    s = spltty();
4038#if 0
4039    c = get_controller_command_byte(sc_kbdc);
4040    if ((c == -1)
4041	|| !set_controller_command_byte(sc_kbdc,
4042            kbdc_get_device_mask(sc_kbdc),
4043            KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
4044                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
4045	/* CONTROLLER ERROR */
4046        kbdc_lock(sc_kbdc, FALSE);
4047	splx(s);
4048	return;
4049    }
4050    /*
4051     * Now that the keyboard controller is told not to generate
4052     * the keyboard and mouse interrupts, call `splx()' to allow
4053     * the other tty interrupts. The clock interrupt may also occur,
4054     * but the timeout routine (`scrn_timer()') will be blocked
4055     * by the lock flag set via `kbdc_lock()'
4056     */
4057    splx(s);
4058#endif
4059
4060    if (send_kbd_command_and_data(sc_kbdc, command, data) != KBD_ACK)
4061        send_kbd_command(sc_kbdc, KBDC_ENABLE_KBD);
4062
4063#if 0
4064    /* restore the interrupts */
4065    if (!set_controller_command_byte(sc_kbdc,
4066            kbdc_get_device_mask(sc_kbdc),
4067	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
4068	/* CONTROLLER ERROR */
4069    }
4070#else
4071    splx(s);
4072#endif
4073    kbdc_lock(sc_kbdc, FALSE);
4074}
4075
4076static void
4077update_leds(int which)
4078{
4079    static u_char xlate_leds[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
4080
4081    /* replace CAPS led with ALTGR led for ALTGR keyboards */
4082    if (key_map.n_keys > ALTGR_OFFSET) {
4083	if (which & ALKED)
4084	    which |= CLKED;
4085	else
4086	    which &= ~CLKED;
4087    }
4088
4089    set_keyboard(KBDC_SET_LEDS, xlate_leds[which & LED_MASK]);
4090}
4091
4092void
4093set_mode(scr_stat *scp)
4094{
4095    char special_modetable[MODE_PARAM_SIZE];
4096    char *mp;
4097
4098    if (scp != cur_console)
4099	return;
4100
4101    /*
4102     * even if mode switching is disabled, we can change back
4103     * to the initial mode or the custom mode based on the initial
4104     * mode if we have saved register values upon start-up.
4105     */
4106    mp = get_mode_param(scp, scp->mode);
4107    if (mp == NULL)
4108	return;
4109    bcopy(mp, &special_modetable, sizeof(special_modetable));
4110
4111    /* setup video hardware for the given mode */
4112    switch (scp->mode) {
4113    case M_VGA_C80x60: case M_VGA_M80x60:
4114	special_modetable[2]  = 0x08;
4115	special_modetable[19] = 0x47;
4116	goto special_480l;
4117
4118    case M_VGA_C80x30: case M_VGA_M80x30:
4119	special_modetable[19] = 0x4f;
4120special_480l:
4121	special_modetable[9] |= 0xc0;
4122	special_modetable[16] = 0x08;
4123	special_modetable[17] = 0x3e;
4124	special_modetable[26] = 0xea;
4125	special_modetable[28] = 0xdf;
4126	special_modetable[31] = 0xe7;
4127	special_modetable[32] = 0x04;
4128	goto setup_mode;
4129
4130    case M_ENH_C80x43: case M_ENH_B80x43:
4131	special_modetable[28] = 87;
4132	goto special_80x50;
4133
4134    case M_VGA_C80x50: case M_VGA_M80x50:
4135special_80x50:
4136	special_modetable[2] = 8;
4137	special_modetable[19] = 7;
4138	goto setup_mode;
4139
4140    case M_VGA_C40x25: case M_VGA_C80x25:
4141    case M_VGA_M80x25:
4142    case M_B40x25:     case M_C40x25:
4143    case M_B80x25:     case M_C80x25:
4144    case M_ENH_B40x25: case M_ENH_C40x25:
4145    case M_ENH_B80x25: case M_ENH_C80x25:
4146    case M_EGAMONO80x25:
4147
4148setup_mode:
4149	set_vgaregs(special_modetable);
4150	scp->font_size = special_modetable[2];
4151
4152	/* set font type (size) */
4153	if (scp->font_size < 14) {
4154	    if (fonts_loaded & FONT_8)
4155		copy_font(LOAD, FONT_8, font_8);
4156	    outb(TSIDX, 0x03); outb(TSREG, 0x0A);   /* font 2 */
4157	} else if (scp->font_size >= 16) {
4158	    if (fonts_loaded & FONT_16)
4159		copy_font(LOAD, FONT_16, font_16);
4160	    outb(TSIDX, 0x03); outb(TSREG, 0x00);   /* font 0 */
4161	} else {
4162	    if (fonts_loaded & FONT_14)
4163		copy_font(LOAD, FONT_14, font_14);
4164	    outb(TSIDX, 0x03); outb(TSREG, 0x05);   /* font 1 */
4165	}
4166	if (flags & CHAR_CURSOR)
4167	    set_destructive_cursor(scp);
4168	mark_all(scp);
4169	break;
4170
4171    case M_VGA_MODEX:
4172	/* "unchain" the VGA mode */
4173	special_modetable[5-1+0x04] &= 0xf7;
4174	special_modetable[5-1+0x04] |= 0x04;
4175	/* turn off doubleword mode */
4176	special_modetable[10+0x14] &= 0xbf;
4177	/* turn off word adressing */
4178	special_modetable[10+0x17] |= 0x40;
4179	/* set logical screen width */
4180	special_modetable[10+0x13] = 80;
4181	/* set 240 lines */
4182	special_modetable[10+0x11] = 0x2c;
4183	special_modetable[10+0x06] = 0x0d;
4184	special_modetable[10+0x07] = 0x3e;
4185	special_modetable[10+0x10] = 0xea;
4186	special_modetable[10+0x11] = 0xac;
4187	special_modetable[10+0x12] = 0xdf;
4188	special_modetable[10+0x15] = 0xe7;
4189	special_modetable[10+0x16] = 0x06;
4190	/* set vertical sync polarity to reflect aspect ratio */
4191	special_modetable[9] = 0xe3;
4192	goto setup_grmode;
4193
4194    case M_BG320:     case M_CG320:     case M_BG640:
4195    case M_CG320_D:   case M_CG640_E:
4196    case M_CG640x350: case M_ENH_CG640:
4197    case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
4198
4199setup_grmode:
4200	set_vgaregs(special_modetable);
4201	scp->font_size = FONT_NONE;
4202	break;
4203
4204    default:
4205	/* call user defined function XXX */
4206	break;
4207    }
4208
4209    /* set border color for this (virtual) console */
4210    set_border(scp->border);
4211    return;
4212}
4213
4214void
4215set_border(u_char color)
4216{
4217    switch (crtc_type) {
4218    case KD_EGA:
4219    case KD_VGA:
4220        inb(crtc_addr + 6);		/* reset flip-flop */
4221        outb(ATC, 0x31); outb(ATC, color);
4222	break;
4223    case KD_CGA:
4224	outb(crtc_addr + 5, color & 0x0f); /* color select register */
4225	break;
4226    case KD_MONO:
4227    case KD_HERCULES:
4228    default:
4229	break;
4230    }
4231}
4232
4233static void
4234set_vgaregs(char *modetable)
4235{
4236    int i, s = splhigh();
4237
4238    outb(TSIDX, 0x00); outb(TSREG, 0x01);   	/* stop sequencer */
4239    outb(TSIDX, 0x07); outb(TSREG, 0x00);   	/* unlock registers */
4240    for (i=0; i<4; i++) {           		/* program sequencer */
4241	outb(TSIDX, i+1);
4242	outb(TSREG, modetable[i+5]);
4243    }
4244    outb(MISC, modetable[9]);       		/* set dot-clock */
4245    outb(TSIDX, 0x00); outb(TSREG, 0x03);   	/* start sequencer */
4246    outb(crtc_addr, 0x11);
4247    outb(crtc_addr+1, inb(crtc_addr+1) & 0x7F);
4248    for (i=0; i<25; i++) {          		/* program crtc */
4249	outb(crtc_addr, i);
4250	if (i == 14 || i == 15)     		/* no hardware cursor */
4251	    outb(crtc_addr+1, 0xff);
4252	else
4253	    outb(crtc_addr+1, modetable[i+10]);
4254    }
4255    inb(crtc_addr+6);           		/* reset flip-flop */
4256    for (i=0; i<20; i++) {          		/* program attribute ctrl */
4257	outb(ATC, i);
4258	outb(ATC, modetable[i+35]);
4259    }
4260    for (i=0; i<9; i++) {           		/* program graph data ctrl */
4261	outb(GDCIDX, i);
4262	outb(GDCREG, modetable[i+55]);
4263    }
4264    inb(crtc_addr+6);           		/* reset flip-flop */
4265    outb(ATC, 0x20);            		/* enable palette */
4266    splx(s);
4267}
4268
4269static void
4270read_vgaregs(char *buf)
4271{
4272    int i, j;
4273    int s;
4274
4275    bzero(buf, MODE_PARAM_SIZE);
4276
4277    s = splhigh();
4278
4279    outb(TSIDX, 0x00); outb(TSREG, 0x01);   	/* stop sequencer */
4280    outb(TSIDX, 0x07); outb(TSREG, 0x00);   	/* unlock registers */
4281    for (i=0, j=5; i<4; i++) {
4282	outb(TSIDX, i+1);
4283	buf[j++] = inb(TSREG);
4284    }
4285    buf[9] = inb(MISC + 10);      		/* dot-clock */
4286    outb(TSIDX, 0x00); outb(TSREG, 0x03);   	/* start sequencer */
4287
4288    for (i=0, j=10; i<25; i++) {       		/* crtc */
4289	outb(crtc_addr, i);
4290	buf[j++] = inb(crtc_addr+1);
4291    }
4292    for (i=0, j=35; i<20; i++) {          	/* attribute ctrl */
4293        inb(crtc_addr+6);           		/* reset flip-flop */
4294	outb(ATC, i);
4295	buf[j++] = inb(ATC + 1);
4296    }
4297    for (i=0, j=55; i<9; i++) {           	/* graph data ctrl */
4298	outb(GDCIDX, i);
4299	buf[j++] = inb(GDCREG);
4300    }
4301    inb(crtc_addr+6);           		/* reset flip-flop */
4302    outb(ATC, 0x20);            		/* enable palette */
4303
4304    buf[0] = *(char *)pa_to_va(0x44a);		/* COLS */
4305    buf[1] = *(char *)pa_to_va(0x484);		/* ROWS */
4306    buf[2] = *(char *)pa_to_va(0x485);		/* POINTS */
4307    buf[3] = *(char *)pa_to_va(0x44c);
4308    buf[4] = *(char *)pa_to_va(0x44d);
4309
4310    splx(s);
4311}
4312
4313static int
4314comp_vgaregs(u_char *buf1, u_char *buf2)
4315{
4316    static struct {
4317        u_char mask;
4318    } params[MODE_PARAM_SIZE] = {
4319	0xff, 0x00, 0xff, 		/* COLS, ROWS, POINTS */
4320	0xff, 0xff, 			/* page length */
4321	0xfe, 0xff, 0xff, 0xff,		/* sequencer registers */
4322	0xf3,				/* misc register */
4323	0xff, 0xff, 0xff, 0x7f, 0xff,	/* CRTC */
4324	0xff, 0xff, 0xff, 0x7f, 0xff,
4325	0x00, 0x00, 0x00, 0x00, 0x00,
4326	0x00, 0xff, 0x7f, 0xff, 0xff,
4327	0x7f, 0xff, 0xff, 0xef, 0xff,
4328	0xff, 0xff, 0xff, 0xff, 0xff,	/* attribute controller registers */
4329	0xff, 0xff, 0xff, 0xff, 0xff,
4330	0xff, 0xff, 0xff, 0xff, 0xff,
4331	0xff, 0xff, 0xff, 0xff, 0xf0,
4332	0xff, 0xff, 0xff, 0xff, 0xff,	/* GDC register */
4333	0xff, 0xff, 0xff, 0xff,
4334    };
4335    int identical = TRUE;
4336    int i;
4337
4338    for (i = 0; i < sizeof(params)/sizeof(params[0]); ++i) {
4339	if (params[i].mask == 0)	/* don't care */
4340	    continue;
4341	if ((buf1[i] & params[i].mask) != (buf2[i] & params[i].mask))
4342	    return COMP_DIFFERENT;
4343	if (buf1[i] != buf2[i])
4344	    identical = FALSE;
4345    }
4346    return (identical) ? COMP_IDENTICAL : COMP_SIMILAR;
4347
4348#if 0
4349    for(i = 0; i < 20; ++i) {
4350	if (*buf1++ != *buf2++)
4351	    return COMP_DIFFERENT;
4352    }
4353    buf1 += 2;  /* skip the cursor shape */
4354    buf2 += 2;
4355    for(i = 22; i < 24; ++i) {
4356	if (*buf1++ != *buf2++)
4357	    return COMP_DIFFERENT;
4358    }
4359    buf1 += 2;  /* skip the cursor position */
4360    buf2 += 2;
4361    for(i = 26; i < MODE_PARAM_SIZE; ++i) {
4362	if (*buf1++ != *buf2++)
4363	    return COMP_DIFFERENT;
4364    }
4365    return COMP_IDENTICAL;
4366#endif
4367}
4368
4369static void
4370dump_vgaregs(u_char *buf)
4371{
4372    int i;
4373
4374    for(i = 0; i < MODE_PARAM_SIZE;) {
4375	printf("%02x ", buf[i]);
4376	if ((++i % 16) == 0)
4377	    printf("\n");
4378    }
4379}
4380
4381static void
4382set_font_mode(u_char *buf)
4383{
4384    int s = splhigh();
4385
4386    font_loading_in_progress = TRUE;
4387
4388    /* save register values */
4389    outb(TSIDX, 0x02); buf[0] = inb(TSREG);
4390    outb(TSIDX, 0x04); buf[1] = inb(TSREG);
4391    outb(GDCIDX, 0x04); buf[2] = inb(GDCREG);
4392    outb(GDCIDX, 0x05); buf[3] = inb(GDCREG);
4393    outb(GDCIDX, 0x06); buf[4] = inb(GDCREG);
4394    inb(crtc_addr + 6);
4395    outb(ATC, 0x10); buf[5] = inb(ATC + 1);
4396
4397    /* setup vga for loading fonts */
4398    inb(crtc_addr+6);           		/* reset flip-flop */
4399    outb(ATC, 0x10); outb(ATC, buf[5] & ~0x01);
4400    inb(crtc_addr+6);               		/* reset flip-flop */
4401    outb(ATC, 0x20);            		/* enable palette */
4402
4403#if SLOW_VGA
4404    outb(TSIDX, 0x02); outb(TSREG, 0x04);
4405    outb(TSIDX, 0x04); outb(TSREG, 0x07);
4406    outb(GDCIDX, 0x04); outb(GDCREG, 0x02);
4407    outb(GDCIDX, 0x05); outb(GDCREG, 0x00);
4408    outb(GDCIDX, 0x06); outb(GDCREG, 0x04);
4409#else
4410    outw(TSIDX, 0x0402);
4411    outw(TSIDX, 0x0704);
4412    outw(GDCIDX, 0x0204);
4413    outw(GDCIDX, 0x0005);
4414    outw(GDCIDX, 0x0406);               /* addr = a0000, 64kb */
4415#endif
4416    splx(s);
4417}
4418
4419static void
4420set_normal_mode(u_char *buf)
4421{
4422    char *modetable;
4423    int s = splhigh();
4424
4425    /* setup vga for normal operation mode again */
4426    inb(crtc_addr+6);           		/* reset flip-flop */
4427    outb(ATC, 0x10); outb(ATC, buf[5]);
4428    inb(crtc_addr+6);               		/* reset flip-flop */
4429    outb(ATC, 0x20);            		/* enable palette */
4430
4431#if SLOW_VGA
4432    outb(TSIDX, 0x02); outb(TSREG, buf[0]);
4433    outb(TSIDX, 0x04); outb(TSREG, buf[1]);
4434    outb(GDCIDX, 0x04); outb(GDCREG, buf[2]);
4435    outb(GDCIDX, 0x05); outb(GDCREG, buf[3]);
4436    if (crtc_addr == MONO_BASE) {
4437	outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x08);
4438    } else {
4439	outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x0c);
4440    }
4441#else
4442    outw(TSIDX, 0x0002 | (buf[0] << 8));
4443    outw(TSIDX, 0x0004 | (buf[1] << 8));
4444    outw(GDCIDX, 0x0004 | (buf[2] << 8));
4445    outw(GDCIDX, 0x0005 | (buf[3] << 8));
4446    if (crtc_addr == MONO_BASE)
4447        outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x08)<<8));
4448    else
4449        outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x0c)<<8));
4450#endif
4451
4452    font_loading_in_progress = FALSE;
4453    splx(s);
4454}
4455
4456void
4457copy_font(int operation, int font_type, char* font_image)
4458{
4459    int ch, line, segment, fontsize;
4460    u_char buf[PARAM_BUFSIZE];
4461    u_char val;
4462
4463    switch (font_type) {
4464    default:
4465    case FONT_8:
4466	segment = 0x8000;
4467	fontsize = 8;
4468	break;
4469    case FONT_14:
4470	segment = 0x4000;
4471	fontsize = 14;
4472	break;
4473    case FONT_16:
4474	segment = 0x0000;
4475	fontsize = 16;
4476	break;
4477    }
4478    outb(TSIDX, 0x01); val = inb(TSREG);        /* disable screen */
4479    outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
4480    set_font_mode(buf);
4481    for (ch=0; ch < 256; ch++)
4482	for (line=0; line < fontsize; line++)
4483	if (operation)
4484	    *(char *)pa_to_va(VIDEOMEM+(segment)+(ch*32)+line) =
4485		    font_image[(ch*fontsize)+line];
4486	else
4487	    font_image[(ch*fontsize)+line] =
4488	    *(char *)pa_to_va(VIDEOMEM+(segment)+(ch*32)+line);
4489    set_normal_mode(buf);
4490    outb(TSIDX, 0x01); outb(TSREG, val & 0xDF); /* enable screen */
4491}
4492
4493static void
4494set_destructive_cursor(scr_stat *scp)
4495{
4496    u_char buf[PARAM_BUFSIZE];
4497    u_char cursor[32];
4498    caddr_t address;
4499    int i;
4500    char *font_buffer;
4501
4502    if (scp->font_size < 14) {
4503	font_buffer = font_8;
4504	address = (caddr_t)VIDEOMEM + 0x8000;
4505    }
4506    else if (scp->font_size >= 16) {
4507	font_buffer = font_16;
4508	address = (caddr_t)VIDEOMEM;
4509    }
4510    else {
4511	font_buffer = font_14;
4512	address = (caddr_t)VIDEOMEM + 0x4000;
4513    }
4514
4515    if (scp->status & MOUSE_VISIBLE) {
4516	if ((scp->cursor_saveunder & 0xff) == SC_MOUSE_CHAR)
4517    	    bcopy(&scp->mouse_cursor[0], cursor, scp->font_size);
4518	else if ((scp->cursor_saveunder & 0xff) == SC_MOUSE_CHAR + 1)
4519    	    bcopy(&scp->mouse_cursor[32], cursor, scp->font_size);
4520	else if ((scp->cursor_saveunder & 0xff) == SC_MOUSE_CHAR + 2)
4521    	    bcopy(&scp->mouse_cursor[64], cursor, scp->font_size);
4522	else if ((scp->cursor_saveunder & 0xff) == SC_MOUSE_CHAR + 3)
4523    	    bcopy(&scp->mouse_cursor[96], cursor, scp->font_size);
4524	else
4525	    bcopy(font_buffer+((scp->cursor_saveunder & 0xff)*scp->font_size),
4526 	       	   cursor, scp->font_size);
4527    }
4528    else
4529    	bcopy(font_buffer + ((scp->cursor_saveunder & 0xff) * scp->font_size),
4530 	       cursor, scp->font_size);
4531    for (i=0; i<32; i++)
4532	if ((i >= scp->cursor_start && i <= scp->cursor_end) ||
4533	    (scp->cursor_start >= scp->font_size && i == scp->font_size - 1))
4534	    cursor[i] |= 0xff;
4535#if 1
4536    while (!(inb(crtc_addr+6) & 0x08)) /* wait for vertical retrace */ ;
4537#endif
4538    set_font_mode(buf);
4539    generic_bcopy(cursor, (char *)pa_to_va(address) + DEAD_CHAR * 32, 32);
4540    set_normal_mode(buf);
4541}
4542
4543static void
4544set_mouse_pos(scr_stat *scp)
4545{
4546    static int last_xpos = -1, last_ypos = -1;
4547
4548    if (scp->mouse_xpos < 0)
4549	scp->mouse_xpos = 0;
4550    if (scp->mouse_ypos < 0)
4551	scp->mouse_ypos = 0;
4552    if (scp->status & UNKNOWN_MODE) {
4553        if (scp->mouse_xpos > scp->xpixel-1)
4554	    scp->mouse_xpos = scp->xpixel-1;
4555        if (scp->mouse_ypos > scp->ypixel-1)
4556	    scp->mouse_ypos = scp->ypixel-1;
4557	return;
4558    }
4559    if (scp->mouse_xpos > (scp->xsize*8)-1)
4560	scp->mouse_xpos = (scp->xsize*8)-1;
4561    if (scp->mouse_ypos > (scp->ysize*scp->font_size)-1)
4562	scp->mouse_ypos = (scp->ysize*scp->font_size)-1;
4563
4564    if (scp->mouse_xpos != last_xpos || scp->mouse_ypos != last_ypos) {
4565	scp->status |= MOUSE_MOVED;
4566
4567    	scp->mouse_pos = scp->scr_buf +
4568	    ((scp->mouse_ypos/scp->font_size)*scp->xsize + scp->mouse_xpos/8);
4569
4570	if ((scp->status & MOUSE_VISIBLE) && (scp->status & MOUSE_CUTTING))
4571	    mouse_cut(scp);
4572    }
4573}
4574
4575#define isspace(c)	(((c) & 0xff) == ' ')
4576
4577static int
4578skip_spc_right(scr_stat *scp, u_short *p)
4579{
4580    int i;
4581
4582    for (i = (p - scp->scr_buf) % scp->xsize; i < scp->xsize; ++i) {
4583	if (!isspace(*p))
4584	    break;
4585	++p;
4586    }
4587    return i;
4588}
4589
4590static int
4591skip_spc_left(scr_stat *scp, u_short *p)
4592{
4593    int i;
4594
4595    for (i = (p-- - scp->scr_buf) % scp->xsize - 1; i >= 0; --i) {
4596	if (!isspace(*p))
4597	    break;
4598	--p;
4599    }
4600    return i;
4601}
4602
4603static void
4604mouse_cut(scr_stat *scp)
4605{
4606    u_short *end;
4607    u_short *p;
4608    int i = 0;
4609    int j = 0;
4610
4611    scp->mouse_cut_end = (scp->mouse_pos >= scp->mouse_cut_start) ?
4612	scp->mouse_pos + 1 : scp->mouse_pos;
4613    end = (scp->mouse_cut_start > scp->mouse_cut_end) ?
4614	scp->mouse_cut_start : scp->mouse_cut_end;
4615    for (p = (scp->mouse_cut_start > scp->mouse_cut_end) ?
4616	    scp->mouse_cut_end : scp->mouse_cut_start; p < end; ++p) {
4617	cut_buffer[i] = *p & 0xff;
4618	/* remember the position of the last non-space char */
4619	if (!isspace(cut_buffer[i++]))
4620	    j = i;
4621	/* trim trailing blank when crossing lines */
4622	if (((p - scp->scr_buf) % scp->xsize) == (scp->xsize - 1)) {
4623	    cut_buffer[j++] = '\n';
4624	    i = j;
4625	}
4626    }
4627    cut_buffer[i] = '\0';
4628
4629    /* scan towards the end of the last line */
4630    --p;
4631    for (i = (p - scp->scr_buf) % scp->xsize; i < scp->xsize; ++i) {
4632	if (!isspace(*p))
4633	    break;
4634	++p;
4635    }
4636    /* if there is nothing but blank chars, trim them, but mark towards eol */
4637    if (i >= scp->xsize) {
4638	if (scp->mouse_cut_start > scp->mouse_cut_end)
4639	    scp->mouse_cut_start = p;
4640	else
4641	    scp->mouse_cut_end = p;
4642	cut_buffer[j++] = '\n';
4643	cut_buffer[j] = '\0';
4644    }
4645
4646    mark_for_update(scp, scp->mouse_cut_start - scp->scr_buf);
4647    mark_for_update(scp, scp->mouse_cut_end - scp->scr_buf);
4648}
4649
4650static void
4651mouse_cut_start(scr_stat *scp)
4652{
4653    int i;
4654
4655    if (scp->status & MOUSE_VISIBLE) {
4656	if (scp->mouse_pos == scp->mouse_cut_start &&
4657	    scp->mouse_cut_start == scp->mouse_cut_end - 1) {
4658	    cut_buffer[0] = '\0';
4659	    remove_cutmarking(scp);
4660	} else if (skip_spc_right(scp, scp->mouse_pos) >= scp->xsize) {
4661	    /* if the pointer is on trailing blank chars, mark towards eol */
4662	    i = skip_spc_left(scp, scp->mouse_pos) + 1;
4663	    scp->mouse_cut_start = scp->scr_buf +
4664	        ((scp->mouse_pos - scp->scr_buf) / scp->xsize) * scp->xsize + i;
4665	    scp->mouse_cut_end = scp->scr_buf +
4666	        ((scp->mouse_pos - scp->scr_buf) / scp->xsize + 1) * scp->xsize;
4667	    cut_buffer[0] = '\n';
4668	    cut_buffer[1] = '\0';
4669	    scp->status |= MOUSE_CUTTING;
4670	} else {
4671	    scp->mouse_cut_start = scp->mouse_pos;
4672	    scp->mouse_cut_end = scp->mouse_cut_start + 1;
4673	    cut_buffer[0] = *scp->mouse_cut_start & 0xff;
4674	    cut_buffer[1] = '\0';
4675	    scp->status |= MOUSE_CUTTING;
4676	}
4677    	mark_all(scp);
4678	/* delete all other screens cut markings */
4679	for (i=0; i<MAXCONS; i++) {
4680	    if (console[i] == NULL || console[i] == scp)
4681		continue;
4682	    remove_cutmarking(console[i]);
4683	}
4684    }
4685}
4686
4687static void
4688mouse_cut_end(scr_stat *scp)
4689{
4690    if (scp->status & MOUSE_VISIBLE) {
4691	scp->status &= ~MOUSE_CUTTING;
4692    }
4693}
4694
4695static void
4696mouse_cut_word(scr_stat *scp)
4697{
4698    u_short *p;
4699    u_short *sol;
4700    u_short *eol;
4701    int i;
4702
4703    /*
4704     * Because we don't have locale information in the kernel,
4705     * we only distinguish space char and non-space chars.  Punctuation
4706     * chars, symbols and other regular chars are all treated alike.
4707     */
4708    if (scp->status & MOUSE_VISIBLE) {
4709	sol = scp->scr_buf
4710	    + ((scp->mouse_pos - scp->scr_buf) / scp->xsize) * scp->xsize;
4711	eol = sol + scp->xsize;
4712	if (isspace(*scp->mouse_pos)) {
4713	    for (p = scp->mouse_pos; p >= sol; --p)
4714	        if (!isspace(*p))
4715		    break;
4716	    scp->mouse_cut_start = ++p;
4717	    for (p = scp->mouse_pos; p < eol; ++p)
4718	        if (!isspace(*p))
4719		    break;
4720	    scp->mouse_cut_end = p;
4721	} else {
4722	    for (p = scp->mouse_pos; p >= sol; --p)
4723	        if (isspace(*p))
4724		    break;
4725	    scp->mouse_cut_start = ++p;
4726	    for (p = scp->mouse_pos; p < eol; ++p)
4727	        if (isspace(*p))
4728		    break;
4729	    scp->mouse_cut_end = p;
4730	}
4731	for (i = 0, p = scp->mouse_cut_start; p < scp->mouse_cut_end; ++p)
4732	    cut_buffer[i++] = *p & 0xff;
4733	cut_buffer[i] = '\0';
4734	scp->status |= MOUSE_CUTTING;
4735    }
4736}
4737
4738static void
4739mouse_cut_line(scr_stat *scp)
4740{
4741    u_short *p;
4742    int i;
4743
4744    if (scp->status & MOUSE_VISIBLE) {
4745	scp->mouse_cut_start = scp->scr_buf
4746	    + ((scp->mouse_pos - scp->scr_buf) / scp->xsize) * scp->xsize;
4747	scp->mouse_cut_end = scp->mouse_cut_start + scp->xsize;
4748	for (i = 0, p = scp->mouse_cut_start; p < scp->mouse_cut_end; ++p)
4749	    cut_buffer[i++] = *p & 0xff;
4750	cut_buffer[i++] = '\n';
4751	cut_buffer[i] = '\0';
4752	scp->status |= MOUSE_CUTTING;
4753    }
4754}
4755
4756static void
4757mouse_cut_extend(scr_stat *scp)
4758{
4759    if ((scp->status & MOUSE_VISIBLE) && !(scp->status & MOUSE_CUTTING)
4760	&& (scp->mouse_cut_start != NULL)) {
4761	mouse_cut(scp);
4762	scp->status |= MOUSE_CUTTING;
4763    }
4764}
4765
4766static void
4767mouse_paste(scr_stat *scp)
4768{
4769    if (scp->status & MOUSE_VISIBLE) {
4770	struct tty *tp;
4771	u_char *ptr = cut_buffer;
4772
4773	tp = VIRTUAL_TTY(get_scr_num());
4774	while (*ptr)
4775	    (*linesw[tp->t_line].l_rint)(scr_rmap[*ptr++], tp);
4776    }
4777}
4778
4779static void
4780draw_mouse_image(scr_stat *scp)
4781{
4782    caddr_t address;
4783    int i;
4784    char *font_buffer;
4785    u_char buf[PARAM_BUFSIZE];
4786    u_short buffer[32];
4787    u_short xoffset, yoffset;
4788    u_short *crt_pos = Crtat + (scp->mouse_pos - scp->scr_buf);
4789    int font_size = scp->font_size;
4790
4791    if (font_size < 14) {
4792	font_buffer = font_8;
4793	address = (caddr_t)VIDEOMEM + 0x8000;
4794    }
4795    else if (font_size >= 16) {
4796	font_buffer = font_16;
4797	address = (caddr_t)VIDEOMEM;
4798    }
4799    else {
4800	font_buffer = font_14;
4801	address = (caddr_t)VIDEOMEM + 0x4000;
4802    }
4803    xoffset = scp->mouse_xpos % 8;
4804    yoffset = scp->mouse_ypos % font_size;
4805
4806    /* prepare mousepointer char's bitmaps */
4807    bcopy(font_buffer + ((*(scp->mouse_pos) & 0xff) * font_size),
4808	   &scp->mouse_cursor[0], font_size);
4809    bcopy(font_buffer + ((*(scp->mouse_pos+1) & 0xff) * font_size),
4810	   &scp->mouse_cursor[32], font_size);
4811    bcopy(font_buffer + ((*(scp->mouse_pos+scp->xsize) & 0xff) * font_size),
4812	   &scp->mouse_cursor[64], font_size);
4813    bcopy(font_buffer + ((*(scp->mouse_pos+scp->xsize+1) & 0xff) * font_size),
4814	   &scp->mouse_cursor[96], font_size);
4815    for (i=0; i<font_size; i++) {
4816	buffer[i] = scp->mouse_cursor[i]<<8 | scp->mouse_cursor[i+32];
4817	buffer[i+font_size]=scp->mouse_cursor[i+64]<<8|scp->mouse_cursor[i+96];
4818    }
4819
4820    /* now and-or in the mousepointer image */
4821    for (i=0; i<16; i++) {
4822	buffer[i+yoffset] =
4823	    ( buffer[i+yoffset] & ~(mouse_and_mask[i] >> xoffset))
4824	    | (mouse_or_mask[i] >> xoffset);
4825    }
4826    for (i=0; i<font_size; i++) {
4827	scp->mouse_cursor[i] = (buffer[i] & 0xff00) >> 8;
4828	scp->mouse_cursor[i+32] = buffer[i] & 0xff;
4829	scp->mouse_cursor[i+64] = (buffer[i+font_size] & 0xff00) >> 8;
4830	scp->mouse_cursor[i+96] = buffer[i+font_size] & 0xff;
4831    }
4832
4833    scp->mouse_oldpos = scp->mouse_pos;
4834
4835#if 1
4836    /* wait for vertical retrace to avoid jitter on some videocards */
4837    while (!(inb(crtc_addr+6) & 0x08)) /* idle */ ;
4838#endif
4839    set_font_mode(buf);
4840    generic_bcopy(scp->mouse_cursor, (char *)pa_to_va(address) + SC_MOUSE_CHAR * 32, 128);
4841    set_normal_mode(buf);
4842    *(crt_pos) = (*(scp->mouse_pos) & 0xff00) | SC_MOUSE_CHAR;
4843    *(crt_pos+scp->xsize) =
4844	(*(scp->mouse_pos + scp->xsize) & 0xff00) | (SC_MOUSE_CHAR + 2);
4845    if (scp->mouse_xpos < (scp->xsize-1)*8) {
4846    	*(crt_pos + 1) = (*(scp->mouse_pos + 1) & 0xff00) | (SC_MOUSE_CHAR + 1);
4847    	*(crt_pos+scp->xsize + 1) =
4848	    (*(scp->mouse_pos + scp->xsize + 1) & 0xff00) | (SC_MOUSE_CHAR + 3);
4849    }
4850    mark_for_update(scp, scp->mouse_pos - scp->scr_buf);
4851    mark_for_update(scp, scp->mouse_pos + scp->xsize + 1 - scp->scr_buf);
4852}
4853
4854static void
4855remove_mouse_image(scr_stat *scp)
4856{
4857    u_short *crt_pos = Crtat + (scp->mouse_oldpos - scp->scr_buf);
4858
4859    *(crt_pos) = *(scp->mouse_oldpos);
4860    *(crt_pos+1) = *(scp->mouse_oldpos+1);
4861    *(crt_pos+scp->xsize) = *(scp->mouse_oldpos+scp->xsize);
4862    *(crt_pos+scp->xsize+1) = *(scp->mouse_oldpos+scp->xsize+1);
4863    mark_for_update(scp, scp->mouse_oldpos - scp->scr_buf);
4864    mark_for_update(scp, scp->mouse_oldpos + scp->xsize + 1 - scp->scr_buf);
4865}
4866
4867static void
4868draw_cutmarking(scr_stat *scp)
4869{
4870    u_short *ptr;
4871    u_short och, nch;
4872
4873    for (ptr=scp->scr_buf; ptr<=(scp->scr_buf+(scp->xsize*scp->ysize)); ptr++) {
4874	nch = och = *(Crtat + (ptr - scp->scr_buf));
4875	/* are we outside the selected area ? */
4876	if ( ptr < (scp->mouse_cut_start > scp->mouse_cut_end ?
4877	            scp->mouse_cut_end : scp->mouse_cut_start) ||
4878	     ptr >= (scp->mouse_cut_start > scp->mouse_cut_end ?
4879	            scp->mouse_cut_start : scp->mouse_cut_end)) {
4880	    if (ptr != scp->cursor_pos)
4881		nch = (och & 0xff) | (*ptr & 0xff00);
4882	}
4883	else {
4884	    /* are we clear of the cursor image ? */
4885	    if (ptr != scp->cursor_pos)
4886		nch = (och & 0x88ff) | (*ptr & 0x7000)>>4 | (*ptr & 0x0700)<<4;
4887	    else {
4888		if (flags & CHAR_CURSOR)
4889		    nch = (och & 0x88ff)|(*ptr & 0x7000)>>4|(*ptr & 0x0700)<<4;
4890		else
4891		    if (!(flags & BLINK_CURSOR))
4892		        nch = (och & 0xff) | (*ptr & 0xff00);
4893	    }
4894	}
4895	if (nch != och)
4896	    *(Crtat + (ptr - scp->scr_buf)) = nch;
4897    }
4898}
4899
4900static void
4901remove_cutmarking(scr_stat *scp)
4902{
4903    scp->mouse_cut_start = scp->mouse_cut_end = NULL;
4904    scp->status &= ~MOUSE_CUTTING;
4905    mark_all(scp);
4906}
4907
4908static void
4909save_palette(void)
4910{
4911    int i;
4912
4913    outb(PALRADR, 0x00);
4914    for (i=0x00; i<0x300; i++)
4915	palette[i] = inb(PALDATA);
4916    inb(crtc_addr+6);           /* reset flip/flop */
4917}
4918
4919void
4920load_palette(char *palette)
4921{
4922    int i;
4923
4924    outb(PIXMASK, 0xFF);            /* no pixelmask */
4925    outb(PALWADR, 0x00);
4926    for (i=0x00; i<0x300; i++)
4927	 outb(PALDATA, palette[i]);
4928    inb(crtc_addr+6);           /* reset flip/flop */
4929    outb(ATC, 0x20);            /* enable palette */
4930}
4931
4932static void
4933do_bell(scr_stat *scp, int pitch, int duration)
4934{
4935    if (cold)
4936	return;
4937
4938    if (flags & VISUAL_BELL) {
4939	if (blink_in_progress)
4940	    return;
4941	blink_in_progress = 4;
4942	if (scp != cur_console)
4943	    blink_in_progress += 2;
4944	blink_screen(cur_console);
4945    } else {
4946	if (scp != cur_console)
4947	    pitch *= 2;
4948	sysbeep(pitch, duration);
4949    }
4950}
4951
4952static void
4953blink_screen(void *arg)
4954{
4955    scr_stat *scp = arg;
4956
4957    if ((scp->status & UNKNOWN_MODE) || (blink_in_progress <= 1)) {
4958	blink_in_progress = FALSE;
4959    	mark_all(scp);
4960	if (delayed_next_scr)
4961	    switch_scr(scp, delayed_next_scr - 1);
4962    }
4963    else {
4964	if (blink_in_progress & 1)
4965	    fillw(kernel_default.std_color | scr_map[0x20],
4966		  Crtat, scp->xsize * scp->ysize);
4967	else
4968	    fillw(kernel_default.rev_color | scr_map[0x20],
4969		  Crtat, scp->xsize * scp->ysize);
4970	blink_in_progress--;
4971	timeout(blink_screen, scp, hz / 10);
4972    }
4973}
4974
4975void
4976sc_bcopy(u_short *p, int from, int to, int mark)
4977{
4978    if (!vesa_mode) {
4979	generic_bcopy(p+from, Crtat+from, (to-from+1)*sizeof (u_short));
4980    } else if (vesa_mode == 0x102) {
4981	u_char *d, *e;
4982	int i,j;
4983
4984	if (mark)
4985		mark = 255;
4986	d = (u_char *)Crtat;
4987	d += 10 + 6*16*100 + (from%80) + 16*100*(from/80);
4988	for (i = from ; i <= to ; i++) {
4989	    e = d;
4990	    for (j = 0 ; j < 16; j++) {
4991	        *e = mark^font_16[(p[i]&0xff)*16+j];
4992		e+=100;
4993	    }
4994	    d++;
4995	    if ((i % 80) == 79)
4996		d += 20 + 15*100;
4997	}
4998    }
4999}
5000
5001#ifdef SC_SPLASH_SCREEN
5002static void
5003toggle_splash_screen(scr_stat *scp)
5004{
5005    static int toggle = 0;
5006    static u_char save_mode;
5007    int s;
5008
5009    if (video_mode_ptr == NULL)
5010	return;
5011
5012    s = splhigh();
5013    if (toggle) {
5014	scp->mode = save_mode;
5015	scp->status &= ~UNKNOWN_MODE;
5016	set_mode(scp);
5017	load_palette(palette);
5018	toggle = 0;
5019    }
5020    else {
5021	save_mode = scp->mode;
5022	scp->mode = M_VGA_CG320;
5023	scp->status |= UNKNOWN_MODE;
5024	set_mode(scp);
5025	/* load image */
5026	toggle = 1;
5027    }
5028    splx(s);
5029}
5030#endif
5031#endif /* NSC */
5032