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