xref: /csrg-svn/sys/i386/isa/pccons.c (revision 52691)
141053Swilliam /*-
241053Swilliam  * Copyright (c) 1990 The Regents of the University of California.
341053Swilliam  * All rights reserved.
441053Swilliam  *
541053Swilliam  * This code is derived from software contributed to Berkeley by
649828Sbostic  * William Jolitz and Don Ahn.
741053Swilliam  *
849573Swilliam  * %sccs.include.redist.c%
943589Sdonahn  *
10*52691Ssklower  *	@(#)pccons.c	5.13 (Berkeley) 02/28/92
1141053Swilliam  */
1241053Swilliam 
1341053Swilliam /*
1449592Swilliam  * code to work keyboard & display for PC-style console
1541053Swilliam  */
1641053Swilliam #include "param.h"
1741053Swilliam #include "conf.h"
1841053Swilliam #include "ioctl.h"
1949573Swilliam #include "proc.h"
2041053Swilliam #include "user.h"
2141053Swilliam #include "tty.h"
2241053Swilliam #include "uio.h"
2349573Swilliam #include "i386/isa/isa_device.h"
2441053Swilliam #include "callout.h"
2541053Swilliam #include "systm.h"
2641053Swilliam #include "kernel.h"
2741053Swilliam #include "syslog.h"
2849573Swilliam #include "i386/isa/icu.h"
2949592Swilliam #include "i386/i386/cons.h"
3041053Swilliam 
3149592Swilliam struct	tty pccons;
3241053Swilliam 
3349592Swilliam struct	pcconsoftc {
3441053Swilliam 	char	cs_flags;
3541053Swilliam #define	CSF_ACTIVE	0x1	/* timeout active */
3641053Swilliam #define	CSF_POLLING	0x2	/* polling for input */
3741053Swilliam 	char	cs_lastc;	/* last char sent */
3841053Swilliam 	int	cs_timo;	/* timeouts since interrupt */
3941053Swilliam 	u_long	cs_wedgecnt;	/* times restarted */
4049592Swilliam } pcconsoftc;
4141053Swilliam 
4249592Swilliam int pcprobe(), pcattach();
4343589Sdonahn 
4449592Swilliam struct	isa_driver pcdriver = {
4549592Swilliam 	pcprobe, pcattach, "pc",
4643589Sdonahn };
4743589Sdonahn 
4845535Sbill #define	COL		80
4945535Sbill #define	ROW		25
5045535Sbill #define	CHR		2
5145535Sbill #define MONO_BASE	0x3B4
5249573Swilliam #define MONO_BUF	0xfe0B0000
5345535Sbill #define CGA_BASE	0x3D4
5449573Swilliam #define CGA_BUF		0xfe0B8000
5545535Sbill #define IOPHYSMEM	0xA0000
5645535Sbill 
5745535Sbill u_char	color = 0xe ;
5845535Sbill static unsigned int addr_6845 = MONO_BASE;
5945535Sbill u_short *Crtat = (u_short *)MONO_BUF;
6049573Swilliam static openf;
6145535Sbill 
6249592Swilliam /*
6349592Swilliam  * We check the console periodically to make sure
6449592Swilliam  * that it hasn't wedged.  Unfortunately, if an XOFF
6549592Swilliam  * is typed on the console, that can't be distinguished
6649592Swilliam  * from more catastrophic failure.
6749592Swilliam  */
6849592Swilliam #define	CN_TIMERVAL	(hz)		/* frequency at which to check cons */
6949592Swilliam #define	CN_TIMO		(2*60)		/* intervals to allow for output char */
7049592Swilliam 
7149592Swilliam int	pcstart();
7249592Swilliam int	pcparam();
7341053Swilliam int	ttrstrt();
7441053Swilliam char	partab[];
7549592Swilliam 
7649592Swilliam /*
7749592Swilliam  * Wait for CP to accept last CP command sent
7849592Swilliam  * before setting up next command.
7949592Swilliam  */
8049592Swilliam #define	waitforlast(timo) { \
8149592Swilliam 	if (pclast) { \
8249592Swilliam 		(timo) = 10000; \
8349592Swilliam 		do \
8449592Swilliam 			uncache((char *)&pclast->cp_unit); \
8549592Swilliam 		while ((pclast->cp_unit&CPTAKE) == 0 && --(timo)); \
8649592Swilliam 	} \
8749592Swilliam }
8849592Swilliam 
8941053Swilliam u_char inb();
9041053Swilliam 
9149592Swilliam pcprobe(dev)
9245535Sbill struct isa_device *dev;
9343589Sdonahn {
9443589Sdonahn 	u_char c;
9543589Sdonahn 	int again = 0;
9643589Sdonahn 
9743589Sdonahn 	/* Enable interrupts and keyboard controller */
9843589Sdonahn 	while (inb(0x64)&2); outb(0x64,0x60);
9943589Sdonahn 	while (inb(0x64)&2); outb(0x60,0x4D);
10043589Sdonahn 
10143589Sdonahn 	/* Start keyboard stuff RESET */
10243589Sdonahn 	while (inb(0x64)&2);	/* wait input ready */
10343589Sdonahn 	outb(0x60,0xFF);	/* RESET */
10443589Sdonahn 	while((c=inb(0x60))!=0xFA) {
10543589Sdonahn 		if ((c == 0xFE) ||  (c == 0xFF)) {
10643589Sdonahn 			if(!again)printf("KEYBOARD disconnected: RECONNECT \n");
10743589Sdonahn 			while (inb(0x64)&2);	/* wait input ready */
10843589Sdonahn 			outb(0x60,0xFF);	/* RESET */
10943589Sdonahn 			again = 1;
11043589Sdonahn 		}
11143589Sdonahn 	}
11243589Sdonahn 	/* pick up keyboard reset return code */
11349573Swilliam 	while((c=inb(0x60))!=0xAA);
11443589Sdonahn 	return 1;
11543589Sdonahn }
11643589Sdonahn 
11749592Swilliam pcattach(dev)
11845535Sbill struct isa_device *dev;
11943589Sdonahn {
12045535Sbill 	u_short *cp = Crtat + (CGA_BUF-MONO_BUF)/CHR;
12145535Sbill 	u_short was;
12245535Sbill 
12345535Sbill 	/* Crtat initialized to point to MONO buffer   */
12445535Sbill 	/* if not present change to CGA_BUF offset     */
12545535Sbill 	/* ONLY ADD the difference since locore.s adds */
12645535Sbill 	/* in the remapped offset at the right time    */
12745535Sbill 
12845535Sbill 	was = *Crtat;
12945535Sbill 	*Crtat = (u_short) 0xA55A;
13045535Sbill 	if (*Crtat != 0xA55A)
13145535Sbill 		printf("<mono>");
13245535Sbill 	else	printf("<color>");
13345535Sbill 	*Crtat = was;
13449573Swilliam 	cursor();
13543589Sdonahn }
13643589Sdonahn 
13749573Swilliam /* ARGSUSED */
13849573Swilliam #ifdef __STDC__
13949592Swilliam pcopen(dev_t dev, int flag, int mode, struct proc *p)
14049573Swilliam #else
14149592Swilliam pcopen(dev, flag, mode, p)
14241053Swilliam 	dev_t dev;
14349573Swilliam 	int flag, mode;
14449573Swilliam 	struct proc *p;
14549573Swilliam #endif
14641053Swilliam {
14741053Swilliam 	register struct tty *tp;
14841053Swilliam 
14949592Swilliam 	tp = &pccons;
15049592Swilliam 	tp->t_oproc = pcstart;
15149592Swilliam 	tp->t_param = pcparam;
15249573Swilliam 	tp->t_dev = dev;
15349573Swilliam 	openf++;
15449573Swilliam 	if ((tp->t_state & TS_ISOPEN) == 0) {
15549573Swilliam 		tp->t_state |= TS_WOPEN;
15641053Swilliam 		ttychars(tp);
15749573Swilliam 		tp->t_iflag = TTYDEF_IFLAG;
15849573Swilliam 		tp->t_oflag = TTYDEF_OFLAG;
15949573Swilliam 		tp->t_cflag = TTYDEF_CFLAG;
16049573Swilliam 		tp->t_lflag = TTYDEF_LFLAG;
16149573Swilliam 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
16249592Swilliam 		pcparam(tp, &tp->t_termios);
16349573Swilliam 		ttsetwater(tp);
16449573Swilliam 	} else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
16549573Swilliam 		return (EBUSY);
16649573Swilliam 	tp->t_state |= TS_CARR_ON;
16741053Swilliam 	return ((*linesw[tp->t_line].l_open)(dev, tp));
16841053Swilliam }
16941053Swilliam 
17049751Smarc pcclose(dev, flag, mode, p)
17141053Swilliam 	dev_t dev;
17249751Smarc 	int flag, mode;
17349751Smarc 	struct proc *p;
17441053Swilliam {
17549751Smarc 	(*linesw[pccons.t_line].l_close)(&pccons, flag);
17649592Swilliam 	ttyclose(&pccons);
17749573Swilliam 	return(0);
17841053Swilliam }
17941053Swilliam 
18041053Swilliam /*ARGSUSED*/
18149592Swilliam pcread(dev, uio, flag)
18241053Swilliam 	dev_t dev;
18341053Swilliam 	struct uio *uio;
18441053Swilliam {
18549592Swilliam 	return ((*linesw[pccons.t_line].l_read)(&pccons, uio, flag));
18641053Swilliam }
18741053Swilliam 
18841053Swilliam /*ARGSUSED*/
18949592Swilliam pcwrite(dev, uio, flag)
19041053Swilliam 	dev_t dev;
19141053Swilliam 	struct uio *uio;
19241053Swilliam {
19349592Swilliam 	return ((*linesw[pccons.t_line].l_write)(&pccons, uio, flag));
19441053Swilliam }
19541053Swilliam 
19641053Swilliam /*
19741053Swilliam  * Got a console receive interrupt -
19841053Swilliam  * the console processor wants to give us a character.
19941053Swilliam  * Catch the character, and see who it goes to.
20041053Swilliam  */
20149592Swilliam pcrint(dev, irq, cpl)
20241053Swilliam 	dev_t dev;
20341053Swilliam {
20441053Swilliam 	int c;
20541053Swilliam 
20641053Swilliam 	c = sgetc(1);
20745535Sbill 	if (c&0x100) return;
20849592Swilliam 	if (pcconsoftc.cs_flags&CSF_POLLING)
20941053Swilliam 		return;
21041053Swilliam #ifdef KDB
21149592Swilliam 	if (kdbrintr(c, &pccons))
21241053Swilliam 		return;
21341053Swilliam #endif
21449592Swilliam 	(*linesw[pccons.t_line].l_rint)(c&0xff, &pccons);
21541053Swilliam }
21641053Swilliam 
217*52691Ssklower pcioctl(dev, cmd, data, flag, p)
21841053Swilliam 	dev_t dev;
219*52691Ssklower 	int cmd, flag;
22049573Swilliam 	caddr_t data;
221*52691Ssklower 	struct proc *p;
22241053Swilliam {
22349592Swilliam 	register struct tty *tp = &pccons;
22441053Swilliam 	register error;
22541053Swilliam 
226*52691Ssklower 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
22741053Swilliam 	if (error >= 0)
22849573Swilliam 		return (error);
22949573Swilliam 	error = ttioctl(tp, cmd, data, flag);
23049573Swilliam 	if (error >= 0)
23149573Swilliam 		return (error);
23249573Swilliam 	return (ENOTTY);
23341053Swilliam }
23441053Swilliam 
23549592Swilliam int	pcconsintr = 1;
23641053Swilliam /*
23741053Swilliam  * Got a console transmission interrupt -
23841053Swilliam  * the console processor wants another character.
23941053Swilliam  */
24049592Swilliam pcxint(dev)
24141053Swilliam 	dev_t dev;
24241053Swilliam {
24341053Swilliam 	register struct tty *tp;
24441053Swilliam 	register int unit;
24541053Swilliam 
24649592Swilliam 	if (!pcconsintr)
24741053Swilliam 		return;
24849592Swilliam 	pccons.t_state &= ~TS_BUSY;
24949592Swilliam 	pcconsoftc.cs_timo = 0;
25049592Swilliam 	if (pccons.t_line)
25149592Swilliam 		(*linesw[pccons.t_line].l_start)(&pccons);
25241053Swilliam 	else
25349592Swilliam 		pcstart(&pccons);
25441053Swilliam }
25541053Swilliam 
25649592Swilliam pcstart(tp)
25741053Swilliam 	register struct tty *tp;
25841053Swilliam {
25941053Swilliam 	register c, s;
26041053Swilliam 
26141053Swilliam 	s = spltty();
26241053Swilliam 	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
26341053Swilliam 		goto out;
26449573Swilliam 	do {
26552659Storek 		if (tp->t_outq.c_cc <= tp->t_lowat) {
26652659Storek 			if (tp->t_state&TS_ASLEEP) {
26752659Storek 				tp->t_state &= ~TS_ASLEEP;
26852659Storek 				wakeup((caddr_t)&tp->t_outq);
26952659Storek 			}
27052659Storek 			selwakeup(&tp->t_wsel);
27141053Swilliam 		}
27252659Storek 		if (tp->t_outq.c_cc == 0)
27352659Storek 			goto out;
27452659Storek 		c = getc(&tp->t_outq);
27552659Storek 		splx(s);
27652659Storek 		sput(c, 0x7);
27752659Storek 		s = spltty();
27849573Swilliam 	} while(1);
27941053Swilliam out:
28041053Swilliam 	splx(s);
28141053Swilliam }
28241053Swilliam 
28349592Swilliam pccnprobe(cp)
28449592Swilliam 	struct consdev *cp;
28549592Swilliam {
28649592Swilliam 	int maj;
28749592Swilliam 	extern int pcopen();
28849592Swilliam 
28949592Swilliam 	/* locate the major number */
29049592Swilliam 	for (maj = 0; maj < nchrdev; maj++)
29149592Swilliam 		if (cdevsw[maj].d_open == pcopen)
29249592Swilliam 			break;
29349592Swilliam 
29449592Swilliam 	/* initialize required fields */
29549592Swilliam 	cp->cn_dev = makedev(maj, 0);
29649592Swilliam 	cp->cn_tp = &pccons;
29749592Swilliam 	cp->cn_pri = CN_INTERNAL;
29849592Swilliam }
29949592Swilliam 
30049592Swilliam /* ARGSUSED */
30149592Swilliam pccninit(cp)
30249592Swilliam 	struct consdev *cp;
30349592Swilliam {
30449592Swilliam 	/*
30549592Swilliam 	 * For now, don't screw with it.
30649592Swilliam 	 */
30749592Swilliam 	/* crtat = 0; */
30849592Swilliam }
30949592Swilliam 
31049573Swilliam static __color;
31149573Swilliam 
31249592Swilliam /* ARGSUSED */
31349592Swilliam pccnputc(dev, c)
31449592Swilliam 	dev_t dev;
31541053Swilliam 	char c;
31649592Swilliam {
31749592Swilliam 	int clr = __color;
31849592Swilliam 
31949592Swilliam 	if (clr == 0)
32049592Swilliam 		clr = 0x30;
32149592Swilliam 	else
32249592Swilliam 		clr |= 0x60;
32341053Swilliam 	if (c == '\n')
32449592Swilliam 		sput('\r', clr);
32549573Swilliam 	sput(c, clr);
32641053Swilliam }
32741053Swilliam 
32841053Swilliam /*
32941053Swilliam  * Print a character on console.
33041053Swilliam  */
33149592Swilliam pcputchar(c, tp)
33241053Swilliam 	char c;
33341053Swilliam 	register struct tty *tp;
33441053Swilliam {
33543589Sdonahn 	sput(c,0x2);
33641053Swilliam 	if (c=='\n') getchar();
33741053Swilliam }
33841053Swilliam 
33941053Swilliam 
34049592Swilliam /* ARGSUSED */
34149592Swilliam pccngetc(dev)
34249592Swilliam 	dev_t dev;
34341053Swilliam {
34441053Swilliam 	register int c, s;
34541053Swilliam 
34649592Swilliam 	s = spltty();		/* block pcrint while we poll */
34743589Sdonahn 	c = sgetc(0);
34843589Sdonahn 	if (c == '\r') c = '\n';
34941053Swilliam 	splx(s);
35041053Swilliam 	return (c);
35141053Swilliam }
35241053Swilliam 
35349592Swilliam pcgetchar(tp)
35441053Swilliam 	register struct tty *tp;
35541053Swilliam {
35641053Swilliam 	int c;
35741053Swilliam 
35841053Swilliam 	c = sgetc(0);
35941053Swilliam 	return (c&0xff);
36041053Swilliam }
36141053Swilliam 
36241053Swilliam /*
36341053Swilliam  * Set line parameters
36441053Swilliam  */
36549592Swilliam pcparam(tp, t)
36641053Swilliam 	register struct tty *tp;
36749573Swilliam 	register struct termios *t;
36841053Swilliam {
36949573Swilliam 	register int cflag = t->c_cflag;
37049573Swilliam         /* and copy to tty */
37149573Swilliam         tp->t_ispeed = t->c_ispeed;
37249573Swilliam         tp->t_ospeed = t->c_ospeed;
37349573Swilliam         tp->t_cflag = cflag;
37449573Swilliam 
37549573Swilliam 	return(0);
37641053Swilliam }
37741053Swilliam 
37841053Swilliam #ifdef KDB
37941053Swilliam /*
38041053Swilliam  * Turn input polling on/off (used by debugger).
38141053Swilliam  */
38249592Swilliam pcpoll(onoff)
38341053Swilliam 	int onoff;
38441053Swilliam {
38541053Swilliam }
38641053Swilliam #endif
38741053Swilliam 
38843589Sdonahn extern int hz;
38943589Sdonahn 
39049573Swilliam static beeping;
39143589Sdonahn sysbeepstop()
39243589Sdonahn {
39343589Sdonahn 	/* disable counter 2 */
39443589Sdonahn 	outb(0x61,inb(0x61)&0xFC);
39549573Swilliam 	beeping = 0;
39643589Sdonahn }
39743589Sdonahn 
39843589Sdonahn sysbeep()
39943589Sdonahn {
40049573Swilliam 
40143589Sdonahn 	/* enable counter 2 */
40243589Sdonahn 	outb(0x61,inb(0x61)|3);
40343589Sdonahn 	/* set command for counter 2, 2 byte write */
40443589Sdonahn 	outb(0x43,0xB6);
40543589Sdonahn 	/* send 0x637 for 750 HZ */
40643589Sdonahn 	outb(0x42,0x37);
40743589Sdonahn 	outb(0x42,0x06);
40849573Swilliam 	if(!beeping)timeout(sysbeepstop,0,hz/4);
40949573Swilliam 	beeping = 1;
41043589Sdonahn }
41143589Sdonahn 
41245535Sbill /* cursor() sets an offset (0-1999) into the 80x25 text area    */
41341053Swilliam 
41445535Sbill static u_short *crtat = 0;
41545535Sbill char bg_at = 0x0f;
41645535Sbill char so_at = 0x70;
41741053Swilliam 
41845535Sbill cursor()
41945535Sbill { 	int pos = crtat - Crtat;
42041053Swilliam 
42143589Sdonahn 	outb(addr_6845,14);
42243589Sdonahn 	outb(addr_6845+1,pos >> 8);
42343589Sdonahn 	outb(addr_6845,15);
42443589Sdonahn 	outb(addr_6845+1,pos&0xff);
42549573Swilliam 	timeout(cursor,0,hz/10);
42643589Sdonahn }
42743589Sdonahn 
42849573Swilliam u_char shfts, ctls, alts, caps, num, stp, scroll;
42949573Swilliam 
43049573Swilliam /*
43149573Swilliam  * Compensate for abysmally stupid frame buffer aribitration with macro
43249573Swilliam  */
43349573Swilliam #define	wrtchar(c) { do *crtat = (c); while ((c) != *crtat); crtat++; row++; }
43449573Swilliam 
43543589Sdonahn /* sput has support for emulation of the 'ibmpc' termcap entry. */
43643589Sdonahn /* This is a bare-bones implementation of a bare-bones entry    */
43743589Sdonahn /* One modification: Change li#24 to li#25 to reflect 25 lines  */
43843589Sdonahn 
43943589Sdonahn sput(c, ca)
44043589Sdonahn u_char c, ca;
44143589Sdonahn {
44243589Sdonahn 
44343589Sdonahn 	static int esc,ebrac,eparm,cx,cy,row,so;
44443589Sdonahn 
44541053Swilliam 	if (crtat == 0) {
44645535Sbill 		u_short *cp = Crtat + (CGA_BUF-MONO_BUF)/CHR, was;
44745535Sbill 		unsigned cursorat;
44843589Sdonahn 
44943589Sdonahn 		/* Crtat initialized to point to MONO buffer   */
45043589Sdonahn 		/* if not present change to CGA_BUF offset     */
45143589Sdonahn 		/* ONLY ADD the difference since locore.s adds */
45243589Sdonahn 		/* in the remapped offset at the right time    */
45343589Sdonahn 
45445535Sbill 		was = *cp;
45545535Sbill 		*cp = (u_short) 0xA55A;
45645535Sbill 		if (*cp != 0xA55A) {
45745535Sbill 			addr_6845 = MONO_BASE;
45845535Sbill 		} else {
45945535Sbill 			*cp = was;
46045535Sbill 			addr_6845 = CGA_BASE;
46143589Sdonahn 			Crtat = Crtat + (CGA_BUF-MONO_BUF)/CHR;
46245535Sbill 		}
46345535Sbill 		/* Extract cursor location */
46445535Sbill 		outb(addr_6845,14);
46545535Sbill 		cursorat = inb(addr_6845+1)<<8 ;
46645535Sbill 		outb(addr_6845,15);
46745535Sbill 		cursorat |= inb(addr_6845+1);
46845535Sbill 
46945535Sbill 		crtat = Crtat + cursorat;
47045535Sbill 		fillw((bg_at<<8)|' ', crtat, COL*ROW-cursorat);
47141053Swilliam 	}
47241053Swilliam 	switch(c) {
47343589Sdonahn 	case 0x1B:
47443589Sdonahn 		esc = 1; ebrac = 0; eparm = 0;
47543589Sdonahn 		break;
47641053Swilliam 
47741053Swilliam 	case '\t':
47841053Swilliam 		do {
47949573Swilliam 			wrtchar((ca<<8)| ' ');
48049573Swilliam 		} while (row % 8);
48141053Swilliam 		break;
48241053Swilliam 
48341053Swilliam 	case '\010':
48441053Swilliam 		crtat--; row--;
48543589Sdonahn 		if (row < 0) row += COL;  /* non-destructive backspace */
48641053Swilliam 		break;
48741053Swilliam 
48841053Swilliam 	case '\r':
48943589Sdonahn 		crtat -= row ; row = 0;
49041053Swilliam 		break;
49141053Swilliam 
49241053Swilliam 	case '\n':
49341053Swilliam 		crtat += COL ;
49441053Swilliam 		break;
49541053Swilliam 
49641053Swilliam 	default:
49743589Sdonahn 		if (esc) {
49843589Sdonahn 			if (ebrac) {
49943589Sdonahn 				switch(c) {
50043589Sdonahn 				case 'm': /* no support for standout */
50143589Sdonahn 					if (!cx) so = 0;
50243589Sdonahn 					else so = 1;
50343589Sdonahn 					esc = 0; ebrac = 0; eparm = 0;
50443589Sdonahn 					break;
50543589Sdonahn 				case 'A': /* back one row */
50643589Sdonahn 					crtat -= COL;
50743589Sdonahn 					esc = 0; ebrac = 0; eparm = 0;
50843589Sdonahn 					break;
50943589Sdonahn 				case 'B': /* down one row */
51043589Sdonahn 					crtat += COL;
51143589Sdonahn 					esc = 0; ebrac = 0; eparm = 0;
51243589Sdonahn 					break;
51343589Sdonahn 				case 'C': /* right cursor */
51443589Sdonahn 					crtat++; row++;
51543589Sdonahn 					esc = 0; ebrac = 0; eparm = 0;
51643589Sdonahn 					break;
51743589Sdonahn 				case 'J': /* Clear to end of display */
51845535Sbill 					fillw((bg_at<<8)+' ', crtat,
51945535Sbill 						Crtat+COL*ROW-crtat);
52043589Sdonahn 					esc = 0; ebrac = 0; eparm = 0;
52143589Sdonahn 					break;
52243589Sdonahn 				case 'K': /* Clear to EOL */
52349573Swilliam 					fillw((bg_at<<8)+' ', crtat,
52449573Swilliam 						COL-(crtat-Crtat)%COL);
52543589Sdonahn 					esc = 0; ebrac = 0; eparm = 0;
52643589Sdonahn 					break;
52743589Sdonahn 				case 'H': /* Cursor move */
52843589Sdonahn 					if ((!cx)||(!cy)) {
52943589Sdonahn 						crtat = Crtat;
53043589Sdonahn 						row = 0;
53143589Sdonahn 					} else {
53243589Sdonahn 						crtat = Crtat+(cx-1)*COL+cy-1;
53343589Sdonahn 						row = cy-1;
53443589Sdonahn 					}
53543589Sdonahn 					esc = 0; ebrac = 0; eparm = 0;
53643589Sdonahn 					break;
53743589Sdonahn 				case ';': /* Switch params in cursor def */
53843589Sdonahn 					eparm = 1;
53943589Sdonahn 					return;
54043589Sdonahn 				default: /* Only numbers valid here */
54143589Sdonahn 					if ((c >= '0')&&(c <= '9')) {
54243589Sdonahn 						if (eparm) {
54343589Sdonahn 							cy *= 10;
54443589Sdonahn 							cy += c - '0';
54543589Sdonahn 						} else {
54643589Sdonahn 							cx *= 10;
54743589Sdonahn 							cx += c - '0';
54843589Sdonahn 						}
54943589Sdonahn 					} else {
55043589Sdonahn 						esc = 0; ebrac = 0; eparm = 0;
55143589Sdonahn 					}
55243589Sdonahn 					return;
55343589Sdonahn 				}
55443589Sdonahn 				break;
55543589Sdonahn 			} else if (c == 'c') { /* Clear screen & home */
55645535Sbill 				fillw((bg_at<<8)+' ', Crtat,COL*ROW);
55743589Sdonahn 				crtat = Crtat; row = 0;
55843589Sdonahn 				esc = 0; ebrac = 0; eparm = 0;
55945535Sbill 			} else if (c == '[') { /* Start ESC [ sequence */
56043589Sdonahn 				ebrac = 1; cx = 0; cy = 0; eparm = 0;
56145535Sbill 			} else { /* Invalid, clear state */
56243589Sdonahn 				 esc = 0; ebrac = 0; eparm = 0;
56343589Sdonahn 			}
56443589Sdonahn 		} else {
56549573Swilliam 			if (c == 7)
56643589Sdonahn 				sysbeep();
56743589Sdonahn 			/* Print only printables */
56845535Sbill 			else /*if (c >= ' ') */ {
56943589Sdonahn 				if (so) {
57049573Swilliam 					wrtchar((so_at<<8)| c);
57143589Sdonahn 				} else {
57249573Swilliam 					wrtchar((ca<<8)| c);
57343589Sdonahn 				}
57443589Sdonahn 				if (row >= COL) row = 0;
57543589Sdonahn 				break ;
57643589Sdonahn 			}
57741053Swilliam 		}
57841053Swilliam 	}
57943589Sdonahn 	if (crtat >= Crtat+COL*(ROW)) { /* scroll check */
58049573Swilliam 		if (openf) do sgetc(1); while (scroll);
58143589Sdonahn 		bcopy(Crtat+COL,Crtat,COL*(ROW-1)*CHR);
58249573Swilliam 		fillw ((bg_at<<8) + ' ', Crtat+COL*(ROW-1),COL) ;
58343589Sdonahn 		crtat -= COL ;
58443589Sdonahn 	}
58541053Swilliam }
58643589Sdonahn 
58741053Swilliam #define	L		0x0001	/* locking function */
58841053Swilliam #define	SHF		0x0002	/* keyboard shift */
58941053Swilliam #define	ALT		0x0004	/* alternate shift -- alternate chars */
59041053Swilliam #define	NUM		0x0008	/* numeric shift  cursors vs. numeric */
59141053Swilliam #define	CTL		0x0010	/* control shift  -- allows ctl function */
59241053Swilliam #define	CPS		0x0020	/* caps shift -- swaps case of letter */
59341053Swilliam #define	ASCII		0x0040	/* ascii code for this key */
59441053Swilliam #define	STP		0x0080	/* stop output */
59541053Swilliam #define	FUNC		0x0100	/* function key */
59643589Sdonahn #define	SCROLL		0x0200	/* scroll lock key */
59741053Swilliam 
59849695Swilliam unsigned	__debug = 0; /*0xffe */;
59941053Swilliam u_short action[] = {
60041053Swilliam 0,     ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,		/* scan  0- 7 */
60141053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,		/* scan  8-15 */
60241053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,		/* scan 16-23 */
60341053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII,   CTL, ASCII, ASCII,		/* scan 24-31 */
60441053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,		/* scan 32-39 */
60541053Swilliam ASCII, ASCII, SHF  , ASCII, ASCII, ASCII, ASCII, ASCII,		/* scan 40-47 */
60641053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,  SHF,  ASCII,		/* scan 48-55 */
60743589Sdonahn   ALT, ASCII, CPS  , FUNC , FUNC , FUNC , FUNC , FUNC ,		/* scan 56-63 */
60849573Swilliam FUNC , FUNC , FUNC , FUNC , FUNC , NUM, SCROLL, ASCII,		/* scan 64-71 */
60941053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,		/* scan 72-79 */
61041053Swilliam ASCII, ASCII, ASCII, ASCII,     0,     0,     0,     0,		/* scan 80-87 */
61141053Swilliam 0,0,0,0,0,0,0,0,
61241053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
61341053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,	} ;
61441053Swilliam 
61541053Swilliam u_char unshift[] = {	/* no shift */
61641053Swilliam 0,     033  , '1'  , '2'  , '3'  , '4'  , '5'  , '6'  ,		/* scan  0- 7 */
61741053Swilliam '7'  , '8'  , '9'  , '0'  , '-'  , '='  , 0177 ,'\t'  ,		/* scan  8-15 */
61841053Swilliam 
61941053Swilliam 'q'  , 'w'  , 'e'  , 'r'  , 't'  , 'y'  , 'u'  , 'i'  ,		/* scan 16-23 */
62041053Swilliam 'o'  , 'p'  , '['  , ']'  , '\r' , CTL  , 'a'  , 's'  ,		/* scan 24-31 */
62141053Swilliam 
62241053Swilliam 'd'  , 'f'  , 'g'  , 'h'  , 'j'  , 'k'  , 'l'  , ';'  ,		/* scan 32-39 */
62341053Swilliam '\'' , '`'  , SHF  , '\\' , 'z'  , 'x'  , 'c'  , 'v'  ,		/* scan 40-47 */
62441053Swilliam 
62541053Swilliam 'b'  , 'n'  , 'm'  , ','  , '.'  , '/'  , SHF  ,   '*',		/* scan 48-55 */
62643589Sdonahn ALT  , ' '  , CPS,     1,     2,    3 ,     4,     5,		/* scan 56-63 */
62741053Swilliam 
62843589Sdonahn     6,     7,     8,     9,    10, NUM, STP,   '7',		/* scan 64-71 */
62941053Swilliam   '8',   '9',   '-',   '4',   '5',   '6',   '+',   '1',		/* scan 72-79 */
63041053Swilliam 
63141053Swilliam   '2',   '3',   '0',   '.',     0,     0,     0,     0,		/* scan 80-87 */
63241053Swilliam 0,0,0,0,0,0,0,0,
63341053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
63441053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,	} ;
63541053Swilliam 
63641053Swilliam u_char shift[] = {	/* shift shift */
63741053Swilliam 0,     033  , '!'  , '@'  , '#'  , '$'  , '%'  , '^'  ,		/* scan  0- 7 */
63841053Swilliam '&'  , '*'  , '('  , ')'  , '_'  , '+'  , 0177 ,'\t'  ,		/* scan  8-15 */
63941053Swilliam 'Q'  , 'W'  , 'E'  , 'R'  , 'T'  , 'Y'  , 'U'  , 'I'  ,		/* scan 16-23 */
64041053Swilliam 'O'  , 'P'  , '{'  , '}'  , '\r' , CTL  , 'A'  , 'S'  ,		/* scan 24-31 */
64141053Swilliam 'D'  , 'F'  , 'G'  , 'H'  , 'J'  , 'K'  , 'L'  , ':'  ,		/* scan 32-39 */
64241053Swilliam '"'  , '~'  , SHF  , '|'  , 'Z'  , 'X'  , 'C'  , 'V'  ,		/* scan 40-47 */
64341053Swilliam 'B'  , 'N'  , 'M'  , '<'  , '>'  , '?'  , SHF  ,   '*',		/* scan 48-55 */
64443589Sdonahn ALT  , ' '  , CPS,     0,     0, ' '  ,     0,     0,		/* scan 56-63 */
64543589Sdonahn     0,     0,     0,     0,     0, NUM, STP,   '7',		/* scan 64-71 */
64641053Swilliam   '8',   '9',   '-',   '4',   '5',   '6',   '+',   '1',		/* scan 72-79 */
64741053Swilliam   '2',   '3',   '0',   '.',     0,     0,     0,     0,		/* scan 80-87 */
64841053Swilliam 0,0,0,0,0,0,0,0,
64941053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
65041053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,	} ;
65141053Swilliam 
65241053Swilliam u_char ctl[] = {	/* CTL shift */
65341053Swilliam 0,     033  , '!'  , 000  , '#'  , '$'  , '%'  , 036  ,		/* scan  0- 7 */
65441053Swilliam '&'  , '*'  , '('  , ')'  , 037  , '+'  , 034  ,'\177',		/* scan  8-15 */
65541053Swilliam 021  , 027  , 005  , 022  , 024  , 031  , 025  , 011  ,		/* scan 16-23 */
65641053Swilliam 017  , 020  , 033  , 035  , '\r' , CTL  , 001  , 023  ,		/* scan 24-31 */
65741053Swilliam 004  , 006  , 007  , 010  , 012  , 013  , 014  , ';'  ,		/* scan 32-39 */
65841053Swilliam '\'' , '`'  , SHF  , 034  , 032  , 030  , 003  , 026  ,		/* scan 40-47 */
65941053Swilliam 002  , 016  , 015  , '<'  , '>'  , '?'  , SHF  ,   '*',		/* scan 48-55 */
66043589Sdonahn ALT  , ' '  , CPS,     0,     0, ' '  ,     0,     0,		/* scan 56-63 */
66143589Sdonahn CPS,     0,     0,     0,     0,     0,     0,     0,		/* scan 64-71 */
66241053Swilliam     0,     0,     0,     0,     0,     0,     0,     0,		/* scan 72-79 */
66341053Swilliam     0,     0,     0,     0,     0,     0,     0,     0,		/* scan 80-87 */
66443589Sdonahn     0,     0,   033, '7'  , '4'  , '1'  ,     0, NUM,		/* scan 88-95 */
66543589Sdonahn '8'  , '5'  , '2'  ,     0, STP, '9'  , '6'  , '3'  ,		/*scan  96-103*/
66641053Swilliam '.'  ,     0, '*'  , '-'  , '+'  ,     0,     0,     0,		/*scan 104-111*/
66741053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,	} ;
66841053Swilliam 
66941053Swilliam #ifdef notdef
67041053Swilliam struct key {
67141053Swilliam 	u_short action;		/* how this key functions */
67241053Swilliam 	char	ascii[8];	/* ascii result character indexed by shifts */
67341053Swilliam };
67441053Swilliam #endif
67541053Swilliam 
67641053Swilliam 
67743589Sdonahn #define	KBSTAT		0x64	/* kbd status port */
67843589Sdonahn #define	KBS_INP_BUF_FUL	0x02	/* kbd char ready */
67943589Sdonahn #define	KBDATA		0x60	/* kbd data port */
68041053Swilliam #define	KBSTATUSPORT	0x61	/* kbd status */
68141053Swilliam 
68243589Sdonahn update_led()
68343589Sdonahn {
68443589Sdonahn 	while (inb(0x64)&2);	/* wait input ready */
68543589Sdonahn 	outb(0x60,0xED);	/* LED Command */
68643589Sdonahn 	while (inb(0x64)&2);	/* wait input ready */
68743589Sdonahn 	outb(0x60,scroll | 2*num | 4*caps);
68843589Sdonahn }
68941053Swilliam 
69045535Sbill reset_cpu() {
69145535Sbill 	while(1) {
69245535Sbill 		while (inb(0x64)&2);	/* wait input ready */
69345535Sbill 		outb(0x64,0xFE);	/* Reset Command */
69445535Sbill 		DELAY(4000000);
69545535Sbill 		while (inb(0x64)&2);	/* wait input ready */
69645535Sbill 		outb(0x64,0xFF);	/* Keyboard Reset Command */
69745535Sbill 	}
69845535Sbill 	/* NOTREACHED */
69945535Sbill }
70045535Sbill 
70143589Sdonahn /*
70243589Sdonahn sgetc(noblock) : get a character from the keyboard. If noblock = 0 wait until
70343589Sdonahn a key is gotten.  Otherwise return a 0x100 (256).
70443589Sdonahn */
70543589Sdonahn int sgetc(noblock)
70643589Sdonahn {
70743589Sdonahn 	u_char dt; unsigned key;
70841053Swilliam loop:
70943589Sdonahn 	/* First see if there is something in the keyboard port */
71043589Sdonahn 	if (inb(KBSTAT)&1) dt = inb(KBDATA);
71143589Sdonahn 	else { if (noblock) return (0x100); else goto loop; }
71241053Swilliam 
71343589Sdonahn 	/* Check for cntl-alt-del */
71443589Sdonahn 	if ((dt == 83)&&ctls&&alts) _exit();
71541053Swilliam 
71643589Sdonahn 	/* Check for make/break */
71743589Sdonahn 	if (dt & 0x80) {
71843589Sdonahn 		/* break */
71943589Sdonahn 		dt = dt & 0x7f ;
72043589Sdonahn 		switch (action[dt]) {
72143589Sdonahn 		case SHF: shfts = 0; break;
72243589Sdonahn 		case ALT: alts = 0; break;
72343589Sdonahn 		case CTL: ctls = 0; break;
72443589Sdonahn 		case FUNC:
72543589Sdonahn 			/* Toggle debug flags */
72643589Sdonahn 			key = unshift[dt];
72743589Sdonahn 			if(__debug & (1<<key)) __debug &= ~(1<<key) ;
72843589Sdonahn 			else __debug |= (1<<key) ;
72943589Sdonahn 			break;
73041053Swilliam 		}
73143589Sdonahn 	} else {
73243589Sdonahn 		/* make */
73343589Sdonahn 		dt = dt & 0x7f ;
73443589Sdonahn 		switch (action[dt]) {
73543589Sdonahn 		/* LOCKING KEYS */
73643589Sdonahn 		case NUM: num ^= 1; update_led(); break;
73743589Sdonahn 		case CPS: caps ^= 1; update_led(); break;
73843589Sdonahn 		case SCROLL: scroll ^= 1; update_led(); break;
73943589Sdonahn 		case STP: stp ^= 1; if(stp) goto loop; break;
74043589Sdonahn 
74143589Sdonahn 		/* NON-LOCKING KEYS */
74243589Sdonahn 		case SHF: shfts = 1; break;
74343589Sdonahn 		case ALT: alts = 1; break;
74443589Sdonahn 		case CTL: ctls = 1; break;
74543589Sdonahn 		case ASCII:
74643589Sdonahn 			if (shfts) dt = shift[dt];
74743589Sdonahn 			else if (ctls) dt = ctl[dt];
74843589Sdonahn 			else dt = unshift[dt];
74943589Sdonahn 			if (caps && (dt >= 'a' && dt <= 'z')) dt -= 'a' - 'A';
75043589Sdonahn 			return(dt);
75143589Sdonahn 		}
75241053Swilliam 	}
75343589Sdonahn 	if (noblock) return (0x100); else goto loop;
75441053Swilliam }
75541053Swilliam 
75641053Swilliam pg(p,q,r,s,t,u,v,w,x,y,z) char *p; {
75741053Swilliam 	printf(p,q,r,s,t,u,v,w,x,y,z);
75841053Swilliam 	printf("\n");
75941053Swilliam 	return(getchar());
76041053Swilliam }
76141053Swilliam 
76241053Swilliam /* special characters */
76341053Swilliam #define bs	8
76441053Swilliam #define lf	10
76541053Swilliam #define cr	13
76641053Swilliam #define cntlc	3
76741053Swilliam #define del	0177
76841053Swilliam #define cntld	4
76941053Swilliam 
77041053Swilliam getchar()
77141053Swilliam {
77241053Swilliam 	register char thechar;
77341053Swilliam 	register delay;
77441053Swilliam 	int x;
77541053Swilliam 
77649592Swilliam 	pcconsoftc.cs_flags |= CSF_POLLING;
77741053Swilliam 	x=splhigh();
77843589Sdonahn 	sput('>',0x6);
77945535Sbill 	/*while (1) {*/
78041053Swilliam 		thechar = (char) sgetc(0);
78149592Swilliam 		pcconsoftc.cs_flags &= ~CSF_POLLING;
78243589Sdonahn 		splx(x);
78341053Swilliam 		switch (thechar) {
78441053Swilliam 		    default: if (thechar >= ' ')
78543589Sdonahn 			     	sput(thechar,0x6);
78641053Swilliam 			     return(thechar);
78741053Swilliam 		    case cr:
78843589Sdonahn 		    case lf: sput(cr,0x6);
78943589Sdonahn 			     sput(lf,0x6);
79041053Swilliam 			     return(lf);
79141053Swilliam 		    case bs:
79241053Swilliam 		    case del:
79343589Sdonahn 			     sput(bs,0x6);
79443589Sdonahn 			     sput(' ',0x6);
79543589Sdonahn 			     sput(bs,0x6);
79641053Swilliam 			     return(thechar);
79743589Sdonahn 		    /*case cntlc:
79841053Swilliam 			     sput('^',0xe) ; sput('C',0xe) ; sput('\r',0xe) ; sput('\n',0xe) ;
79943589Sdonahn 			     _exit(-2) ; */
80041053Swilliam 		    case cntld:
80143589Sdonahn 			     sput('^',0x6) ; sput('D',0x6) ; sput('\r',0x6) ; sput('\n',0x6) ;
80241053Swilliam 			     return(0);
80341053Swilliam 		}
80445535Sbill 	/*}*/
80541053Swilliam }
80649573Swilliam 
80749573Swilliam #include "machine/dbg.h"
80849573Swilliam #include "machine/stdarg.h"
80949573Swilliam static nrow;
81049573Swilliam 
81149573Swilliam void
81249573Swilliam #ifdef __STDC__
81349573Swilliam dprintf(unsigned flgs, const char *fmt, ...)
81449573Swilliam #else
81549573Swilliam dprintf(flgs, fmt /*, va_alist */)
81649573Swilliam         char *fmt;
81749573Swilliam 	unsigned flgs;
81849573Swilliam #endif
81949573Swilliam {	extern unsigned __debug;
82049573Swilliam 	va_list ap;
82149573Swilliam 
82249573Swilliam 	if((flgs&__debug) > DPAUSE) {
82349573Swilliam 		__color = ffs(flgs&__debug)+1;
82449573Swilliam 		va_start(ap,fmt);
82549573Swilliam 		kprintf(fmt, 1, (struct tty *)0, ap);
82649573Swilliam 		va_end(ap);
82749573Swilliam 	if (flgs&DPAUSE || nrow%24 == 23) {
82849573Swilliam 		int x;
82949573Swilliam 		x = splhigh();
83049573Swilliam 		if (nrow%24 == 23) nrow = 0;
83149573Swilliam 		sgetc(0);
83249573Swilliam 		splx(x);
83349573Swilliam 	}
83449573Swilliam 	}
83549573Swilliam 	__color = 0;
83649573Swilliam }
83749573Swilliam 
83849573Swilliam consinit() {}
839