xref: /original-bsd/sys/i386/isa/pccons.c (revision 4ddaeef2)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * William Jolitz.
7  *
8  * Added support for ibmpc term type and improved keyboard support. -Don Ahn
9  *
10  * %sccs.include.redist.c%
11  *
12  *	@(#)pccons.c	5.9 (Berkeley) 05/12/91
13  */
14 
15 /*
16  * code to work keyboard & display for PC-style console
17  */
18 #include "param.h"
19 #include "conf.h"
20 #include "ioctl.h"
21 #include "proc.h"
22 #include "user.h"
23 #include "tty.h"
24 #include "uio.h"
25 #include "i386/isa/isa_device.h"
26 #include "callout.h"
27 #include "systm.h"
28 #include "kernel.h"
29 #include "syslog.h"
30 #include "i386/isa/icu.h"
31 #include "i386/i386/cons.h"
32 
33 struct	tty pccons;
34 
35 struct	pcconsoftc {
36 	char	cs_flags;
37 #define	CSF_ACTIVE	0x1	/* timeout active */
38 #define	CSF_POLLING	0x2	/* polling for input */
39 	char	cs_lastc;	/* last char sent */
40 	int	cs_timo;	/* timeouts since interrupt */
41 	u_long	cs_wedgecnt;	/* times restarted */
42 } pcconsoftc;
43 
44 int pcprobe(), pcattach();
45 
46 struct	isa_driver pcdriver = {
47 	pcprobe, pcattach, "pc",
48 };
49 
50 #define	COL		80
51 #define	ROW		25
52 #define	CHR		2
53 #define MONO_BASE	0x3B4
54 #define MONO_BUF	0xfe0B0000
55 #define CGA_BASE	0x3D4
56 #define CGA_BUF		0xfe0B8000
57 #define IOPHYSMEM	0xA0000
58 
59 u_char	color = 0xe ;
60 static unsigned int addr_6845 = MONO_BASE;
61 u_short *Crtat = (u_short *)MONO_BUF;
62 static openf;
63 
64 /*
65  * We check the console periodically to make sure
66  * that it hasn't wedged.  Unfortunately, if an XOFF
67  * is typed on the console, that can't be distinguished
68  * from more catastrophic failure.
69  */
70 #define	CN_TIMERVAL	(hz)		/* frequency at which to check cons */
71 #define	CN_TIMO		(2*60)		/* intervals to allow for output char */
72 
73 int	pcstart();
74 int	pcparam();
75 int	ttrstrt();
76 char	partab[];
77 
78 /*
79  * Wait for CP to accept last CP command sent
80  * before setting up next command.
81  */
82 #define	waitforlast(timo) { \
83 	if (pclast) { \
84 		(timo) = 10000; \
85 		do \
86 			uncache((char *)&pclast->cp_unit); \
87 		while ((pclast->cp_unit&CPTAKE) == 0 && --(timo)); \
88 	} \
89 }
90 
91 u_char inb();
92 
93 pcprobe(dev)
94 struct isa_device *dev;
95 {
96 	u_char c;
97 	int again = 0;
98 
99 	/* Enable interrupts and keyboard controller */
100 	while (inb(0x64)&2); outb(0x64,0x60);
101 	while (inb(0x64)&2); outb(0x60,0x4D);
102 
103 	/* Start keyboard stuff RESET */
104 	while (inb(0x64)&2);	/* wait input ready */
105 	outb(0x60,0xFF);	/* RESET */
106 	while((c=inb(0x60))!=0xFA) {
107 		if ((c == 0xFE) ||  (c == 0xFF)) {
108 			if(!again)printf("KEYBOARD disconnected: RECONNECT \n");
109 			while (inb(0x64)&2);	/* wait input ready */
110 			outb(0x60,0xFF);	/* RESET */
111 			again = 1;
112 		}
113 	}
114 	/* pick up keyboard reset return code */
115 	while((c=inb(0x60))!=0xAA);
116 	return 1;
117 }
118 
119 pcattach(dev)
120 struct isa_device *dev;
121 {
122 	u_short *cp = Crtat + (CGA_BUF-MONO_BUF)/CHR;
123 	u_short was;
124 
125 	/* Crtat initialized to point to MONO buffer   */
126 	/* if not present change to CGA_BUF offset     */
127 	/* ONLY ADD the difference since locore.s adds */
128 	/* in the remapped offset at the right time    */
129 
130 	was = *Crtat;
131 	*Crtat = (u_short) 0xA55A;
132 	if (*Crtat != 0xA55A)
133 		printf("<mono>");
134 	else	printf("<color>");
135 	*Crtat = was;
136 	cursor();
137 }
138 
139 /* ARGSUSED */
140 #ifdef __STDC__
141 pcopen(dev_t dev, int flag, int mode, struct proc *p)
142 #else
143 pcopen(dev, flag, mode, p)
144 	dev_t dev;
145 	int flag, mode;
146 	struct proc *p;
147 #endif
148 {
149 	register struct tty *tp;
150 
151 	tp = &pccons;
152 	tp->t_oproc = pcstart;
153 	tp->t_param = pcparam;
154 	tp->t_dev = dev;
155 	openf++;
156 	if ((tp->t_state & TS_ISOPEN) == 0) {
157 		tp->t_state |= TS_WOPEN;
158 		ttychars(tp);
159 		tp->t_iflag = TTYDEF_IFLAG;
160 		tp->t_oflag = TTYDEF_OFLAG;
161 		tp->t_cflag = TTYDEF_CFLAG;
162 		tp->t_lflag = TTYDEF_LFLAG;
163 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
164 		pcparam(tp, &tp->t_termios);
165 		ttsetwater(tp);
166 	} else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
167 		return (EBUSY);
168 	tp->t_state |= TS_CARR_ON;
169 	return ((*linesw[tp->t_line].l_open)(dev, tp));
170 }
171 
172 pcclose(dev, flag)
173 	dev_t dev;
174 {
175 	(*linesw[pccons.t_line].l_close)(&pccons);
176 	ttyclose(&pccons);
177 	return(0);
178 }
179 
180 /*ARGSUSED*/
181 pcread(dev, uio, flag)
182 	dev_t dev;
183 	struct uio *uio;
184 {
185 	return ((*linesw[pccons.t_line].l_read)(&pccons, uio, flag));
186 }
187 
188 /*ARGSUSED*/
189 pcwrite(dev, uio, flag)
190 	dev_t dev;
191 	struct uio *uio;
192 {
193 	return ((*linesw[pccons.t_line].l_write)(&pccons, uio, flag));
194 }
195 
196 /*
197  * Got a console receive interrupt -
198  * the console processor wants to give us a character.
199  * Catch the character, and see who it goes to.
200  */
201 pcrint(dev, irq, cpl)
202 	dev_t dev;
203 {
204 	int c;
205 
206 	c = sgetc(1);
207 	if (c&0x100) return;
208 	if (pcconsoftc.cs_flags&CSF_POLLING)
209 		return;
210 #ifdef KDB
211 	if (kdbrintr(c, &pccons))
212 		return;
213 #endif
214 	(*linesw[pccons.t_line].l_rint)(c&0xff, &pccons);
215 }
216 
217 pcioctl(dev, cmd, data, flag)
218 	dev_t dev;
219 	caddr_t data;
220 {
221 	register struct tty *tp = &pccons;
222 	register error;
223 
224 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
225 	if (error >= 0)
226 		return (error);
227 	error = ttioctl(tp, cmd, data, flag);
228 	if (error >= 0)
229 		return (error);
230 	return (ENOTTY);
231 }
232 
233 int	pcconsintr = 1;
234 /*
235  * Got a console transmission interrupt -
236  * the console processor wants another character.
237  */
238 pcxint(dev)
239 	dev_t dev;
240 {
241 	register struct tty *tp;
242 	register int unit;
243 
244 	if (!pcconsintr)
245 		return;
246 	pccons.t_state &= ~TS_BUSY;
247 	pcconsoftc.cs_timo = 0;
248 	if (pccons.t_line)
249 		(*linesw[pccons.t_line].l_start)(&pccons);
250 	else
251 		pcstart(&pccons);
252 }
253 
254 pcstart(tp)
255 	register struct tty *tp;
256 {
257 	register c, s;
258 
259 	s = spltty();
260 	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
261 		goto out;
262 	do {
263 	if (tp->t_outq.c_cc <= tp->t_lowat) {
264 		if (tp->t_state&TS_ASLEEP) {
265 			tp->t_state &= ~TS_ASLEEP;
266 			wakeup((caddr_t)&tp->t_outq);
267 		}
268 		if (tp->t_wsel) {
269 			selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
270 			tp->t_wsel = 0;
271 			tp->t_state &= ~TS_WCOLL;
272 		}
273 	}
274 	if (tp->t_outq.c_cc == 0)
275 		goto out;
276 	c = getc(&tp->t_outq);
277 	splx(s);
278 	sput(c,0x7);
279 	s = spltty();
280 	} while(1);
281 out:
282 	splx(s);
283 }
284 
285 pccnprobe(cp)
286 	struct consdev *cp;
287 {
288 	int maj;
289 	extern int pcopen();
290 
291 	/* locate the major number */
292 	for (maj = 0; maj < nchrdev; maj++)
293 		if (cdevsw[maj].d_open == pcopen)
294 			break;
295 
296 	/* initialize required fields */
297 	cp->cn_dev = makedev(maj, 0);
298 	cp->cn_tp = &pccons;
299 	cp->cn_pri = CN_INTERNAL;
300 }
301 
302 /* ARGSUSED */
303 pccninit(cp)
304 	struct consdev *cp;
305 {
306 	/*
307 	 * For now, don't screw with it.
308 	 */
309 	/* crtat = 0; */
310 }
311 
312 static __color;
313 
314 /* ARGSUSED */
315 pccnputc(dev, c)
316 	dev_t dev;
317 	char c;
318 {
319 	int clr = __color;
320 
321 	if (clr == 0)
322 		clr = 0x30;
323 	else
324 		clr |= 0x60;
325 	if (c == '\n')
326 		sput('\r', clr);
327 	sput(c, clr);
328 }
329 
330 /*
331  * Print a character on console.
332  */
333 pcputchar(c, tp)
334 	char c;
335 	register struct tty *tp;
336 {
337 	sput(c,0x2);
338 	if (c=='\n') getchar();
339 }
340 
341 
342 /* ARGSUSED */
343 pccngetc(dev)
344 	dev_t dev;
345 {
346 	register int c, s;
347 
348 	s = spltty();		/* block pcrint while we poll */
349 	c = sgetc(0);
350 	if (c == '\r') c = '\n';
351 	splx(s);
352 	return (c);
353 }
354 
355 pcgetchar(tp)
356 	register struct tty *tp;
357 {
358 	int c;
359 
360 	c = sgetc(0);
361 	return (c&0xff);
362 }
363 
364 /*
365  * Set line parameters
366  */
367 pcparam(tp, t)
368 	register struct tty *tp;
369 	register struct termios *t;
370 {
371 	register int cflag = t->c_cflag;
372         /* and copy to tty */
373         tp->t_ispeed = t->c_ispeed;
374         tp->t_ospeed = t->c_ospeed;
375         tp->t_cflag = cflag;
376 
377 	return(0);
378 }
379 
380 #ifdef KDB
381 /*
382  * Turn input polling on/off (used by debugger).
383  */
384 pcpoll(onoff)
385 	int onoff;
386 {
387 }
388 #endif
389 
390 extern int hz;
391 
392 static beeping;
393 sysbeepstop()
394 {
395 	/* disable counter 2 */
396 	outb(0x61,inb(0x61)&0xFC);
397 	beeping = 0;
398 }
399 
400 sysbeep()
401 {
402 
403 	/* enable counter 2 */
404 	outb(0x61,inb(0x61)|3);
405 	/* set command for counter 2, 2 byte write */
406 	outb(0x43,0xB6);
407 	/* send 0x637 for 750 HZ */
408 	outb(0x42,0x37);
409 	outb(0x42,0x06);
410 	if(!beeping)timeout(sysbeepstop,0,hz/4);
411 	beeping = 1;
412 }
413 
414 /* cursor() sets an offset (0-1999) into the 80x25 text area    */
415 
416 static u_short *crtat = 0;
417 char bg_at = 0x0f;
418 char so_at = 0x70;
419 
420 cursor()
421 { 	int pos = crtat - Crtat;
422 
423 	outb(addr_6845,14);
424 	outb(addr_6845+1,pos >> 8);
425 	outb(addr_6845,15);
426 	outb(addr_6845+1,pos&0xff);
427 	timeout(cursor,0,hz/10);
428 }
429 
430 u_char shfts, ctls, alts, caps, num, stp, scroll;
431 
432 /*
433  * Compensate for abysmally stupid frame buffer aribitration with macro
434  */
435 #define	wrtchar(c) { do *crtat = (c); while ((c) != *crtat); crtat++; row++; }
436 
437 /* sput has support for emulation of the 'ibmpc' termcap entry. */
438 /* This is a bare-bones implementation of a bare-bones entry    */
439 /* One modification: Change li#24 to li#25 to reflect 25 lines  */
440 
441 sput(c, ca)
442 u_char c, ca;
443 {
444 
445 	static int esc,ebrac,eparm,cx,cy,row,so;
446 
447 	if (crtat == 0) {
448 		u_short *cp = Crtat + (CGA_BUF-MONO_BUF)/CHR, was;
449 		unsigned cursorat;
450 
451 		/* Crtat initialized to point to MONO buffer   */
452 		/* if not present change to CGA_BUF offset     */
453 		/* ONLY ADD the difference since locore.s adds */
454 		/* in the remapped offset at the right time    */
455 
456 		was = *cp;
457 		*cp = (u_short) 0xA55A;
458 		if (*cp != 0xA55A) {
459 			addr_6845 = MONO_BASE;
460 		} else {
461 			*cp = was;
462 			addr_6845 = CGA_BASE;
463 			Crtat = Crtat + (CGA_BUF-MONO_BUF)/CHR;
464 		}
465 		/* Extract cursor location */
466 		outb(addr_6845,14);
467 		cursorat = inb(addr_6845+1)<<8 ;
468 		outb(addr_6845,15);
469 		cursorat |= inb(addr_6845+1);
470 
471 		crtat = Crtat + cursorat;
472 		fillw((bg_at<<8)|' ', crtat, COL*ROW-cursorat);
473 	}
474 	switch(c) {
475 	case 0x1B:
476 		esc = 1; ebrac = 0; eparm = 0;
477 		break;
478 
479 	case '\t':
480 		do {
481 			wrtchar((ca<<8)| ' ');
482 		} while (row % 8);
483 		break;
484 
485 	case '\010':
486 		crtat--; row--;
487 		if (row < 0) row += COL;  /* non-destructive backspace */
488 		break;
489 
490 	case '\r':
491 		crtat -= row ; row = 0;
492 		break;
493 
494 	case '\n':
495 		crtat += COL ;
496 		break;
497 
498 	default:
499 		if (esc) {
500 			if (ebrac) {
501 				switch(c) {
502 				case 'm': /* no support for standout */
503 					if (!cx) so = 0;
504 					else so = 1;
505 					esc = 0; ebrac = 0; eparm = 0;
506 					break;
507 				case 'A': /* back one row */
508 					crtat -= COL;
509 					esc = 0; ebrac = 0; eparm = 0;
510 					break;
511 				case 'B': /* down one row */
512 					crtat += COL;
513 					esc = 0; ebrac = 0; eparm = 0;
514 					break;
515 				case 'C': /* right cursor */
516 					crtat++; row++;
517 					esc = 0; ebrac = 0; eparm = 0;
518 					break;
519 				case 'J': /* Clear to end of display */
520 					fillw((bg_at<<8)+' ', crtat,
521 						Crtat+COL*ROW-crtat);
522 					esc = 0; ebrac = 0; eparm = 0;
523 					break;
524 				case 'K': /* Clear to EOL */
525 					fillw((bg_at<<8)+' ', crtat,
526 						COL-(crtat-Crtat)%COL);
527 					esc = 0; ebrac = 0; eparm = 0;
528 					break;
529 				case 'H': /* Cursor move */
530 					if ((!cx)||(!cy)) {
531 						crtat = Crtat;
532 						row = 0;
533 					} else {
534 						crtat = Crtat+(cx-1)*COL+cy-1;
535 						row = cy-1;
536 					}
537 					esc = 0; ebrac = 0; eparm = 0;
538 					break;
539 				case ';': /* Switch params in cursor def */
540 					eparm = 1;
541 					return;
542 				default: /* Only numbers valid here */
543 					if ((c >= '0')&&(c <= '9')) {
544 						if (eparm) {
545 							cy *= 10;
546 							cy += c - '0';
547 						} else {
548 							cx *= 10;
549 							cx += c - '0';
550 						}
551 					} else {
552 						esc = 0; ebrac = 0; eparm = 0;
553 					}
554 					return;
555 				}
556 				break;
557 			} else if (c == 'c') { /* Clear screen & home */
558 				fillw((bg_at<<8)+' ', Crtat,COL*ROW);
559 				crtat = Crtat; row = 0;
560 				esc = 0; ebrac = 0; eparm = 0;
561 			} else if (c == '[') { /* Start ESC [ sequence */
562 				ebrac = 1; cx = 0; cy = 0; eparm = 0;
563 			} else { /* Invalid, clear state */
564 				 esc = 0; ebrac = 0; eparm = 0;
565 			}
566 		} else {
567 			if (c == 7)
568 				sysbeep();
569 			/* Print only printables */
570 			else /*if (c >= ' ') */ {
571 				if (so) {
572 					wrtchar((so_at<<8)| c);
573 				} else {
574 					wrtchar((ca<<8)| c);
575 				}
576 				if (row >= COL) row = 0;
577 				break ;
578 			}
579 		}
580 	}
581 	if (crtat >= Crtat+COL*(ROW)) { /* scroll check */
582 		if (openf) do sgetc(1); while (scroll);
583 		bcopy(Crtat+COL,Crtat,COL*(ROW-1)*CHR);
584 		fillw ((bg_at<<8) + ' ', Crtat+COL*(ROW-1),COL) ;
585 		crtat -= COL ;
586 	}
587 }
588 
589 #define	L		0x0001	/* locking function */
590 #define	SHF		0x0002	/* keyboard shift */
591 #define	ALT		0x0004	/* alternate shift -- alternate chars */
592 #define	NUM		0x0008	/* numeric shift  cursors vs. numeric */
593 #define	CTL		0x0010	/* control shift  -- allows ctl function */
594 #define	CPS		0x0020	/* caps shift -- swaps case of letter */
595 #define	ASCII		0x0040	/* ascii code for this key */
596 #define	STP		0x0080	/* stop output */
597 #define	FUNC		0x0100	/* function key */
598 #define	SCROLL		0x0200	/* scroll lock key */
599 
600 unsigned	__debug = 0; /*0xffe */;
601 u_short action[] = {
602 0,     ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,		/* scan  0- 7 */
603 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,		/* scan  8-15 */
604 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,		/* scan 16-23 */
605 ASCII, ASCII, ASCII, ASCII, ASCII,   CTL, ASCII, ASCII,		/* scan 24-31 */
606 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,		/* scan 32-39 */
607 ASCII, ASCII, SHF  , ASCII, ASCII, ASCII, ASCII, ASCII,		/* scan 40-47 */
608 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,  SHF,  ASCII,		/* scan 48-55 */
609   ALT, ASCII, CPS  , FUNC , FUNC , FUNC , FUNC , FUNC ,		/* scan 56-63 */
610 FUNC , FUNC , FUNC , FUNC , FUNC , NUM, SCROLL, ASCII,		/* scan 64-71 */
611 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,		/* scan 72-79 */
612 ASCII, ASCII, ASCII, ASCII,     0,     0,     0,     0,		/* scan 80-87 */
613 0,0,0,0,0,0,0,0,
614 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
615 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,	} ;
616 
617 u_char unshift[] = {	/* no shift */
618 0,     033  , '1'  , '2'  , '3'  , '4'  , '5'  , '6'  ,		/* scan  0- 7 */
619 '7'  , '8'  , '9'  , '0'  , '-'  , '='  , 0177 ,'\t'  ,		/* scan  8-15 */
620 
621 'q'  , 'w'  , 'e'  , 'r'  , 't'  , 'y'  , 'u'  , 'i'  ,		/* scan 16-23 */
622 'o'  , 'p'  , '['  , ']'  , '\r' , CTL  , 'a'  , 's'  ,		/* scan 24-31 */
623 
624 'd'  , 'f'  , 'g'  , 'h'  , 'j'  , 'k'  , 'l'  , ';'  ,		/* scan 32-39 */
625 '\'' , '`'  , SHF  , '\\' , 'z'  , 'x'  , 'c'  , 'v'  ,		/* scan 40-47 */
626 
627 'b'  , 'n'  , 'm'  , ','  , '.'  , '/'  , SHF  ,   '*',		/* scan 48-55 */
628 ALT  , ' '  , CPS,     1,     2,    3 ,     4,     5,		/* scan 56-63 */
629 
630     6,     7,     8,     9,    10, NUM, STP,   '7',		/* scan 64-71 */
631   '8',   '9',   '-',   '4',   '5',   '6',   '+',   '1',		/* scan 72-79 */
632 
633   '2',   '3',   '0',   '.',     0,     0,     0,     0,		/* scan 80-87 */
634 0,0,0,0,0,0,0,0,
635 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
636 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,	} ;
637 
638 u_char shift[] = {	/* shift shift */
639 0,     033  , '!'  , '@'  , '#'  , '$'  , '%'  , '^'  ,		/* scan  0- 7 */
640 '&'  , '*'  , '('  , ')'  , '_'  , '+'  , 0177 ,'\t'  ,		/* scan  8-15 */
641 'Q'  , 'W'  , 'E'  , 'R'  , 'T'  , 'Y'  , 'U'  , 'I'  ,		/* scan 16-23 */
642 'O'  , 'P'  , '{'  , '}'  , '\r' , CTL  , 'A'  , 'S'  ,		/* scan 24-31 */
643 'D'  , 'F'  , 'G'  , 'H'  , 'J'  , 'K'  , 'L'  , ':'  ,		/* scan 32-39 */
644 '"'  , '~'  , SHF  , '|'  , 'Z'  , 'X'  , 'C'  , 'V'  ,		/* scan 40-47 */
645 'B'  , 'N'  , 'M'  , '<'  , '>'  , '?'  , SHF  ,   '*',		/* scan 48-55 */
646 ALT  , ' '  , CPS,     0,     0, ' '  ,     0,     0,		/* scan 56-63 */
647     0,     0,     0,     0,     0, NUM, STP,   '7',		/* scan 64-71 */
648   '8',   '9',   '-',   '4',   '5',   '6',   '+',   '1',		/* scan 72-79 */
649   '2',   '3',   '0',   '.',     0,     0,     0,     0,		/* scan 80-87 */
650 0,0,0,0,0,0,0,0,
651 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
652 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,	} ;
653 
654 u_char ctl[] = {	/* CTL shift */
655 0,     033  , '!'  , 000  , '#'  , '$'  , '%'  , 036  ,		/* scan  0- 7 */
656 '&'  , '*'  , '('  , ')'  , 037  , '+'  , 034  ,'\177',		/* scan  8-15 */
657 021  , 027  , 005  , 022  , 024  , 031  , 025  , 011  ,		/* scan 16-23 */
658 017  , 020  , 033  , 035  , '\r' , CTL  , 001  , 023  ,		/* scan 24-31 */
659 004  , 006  , 007  , 010  , 012  , 013  , 014  , ';'  ,		/* scan 32-39 */
660 '\'' , '`'  , SHF  , 034  , 032  , 030  , 003  , 026  ,		/* scan 40-47 */
661 002  , 016  , 015  , '<'  , '>'  , '?'  , SHF  ,   '*',		/* scan 48-55 */
662 ALT  , ' '  , CPS,     0,     0, ' '  ,     0,     0,		/* scan 56-63 */
663 CPS,     0,     0,     0,     0,     0,     0,     0,		/* scan 64-71 */
664     0,     0,     0,     0,     0,     0,     0,     0,		/* scan 72-79 */
665     0,     0,     0,     0,     0,     0,     0,     0,		/* scan 80-87 */
666     0,     0,   033, '7'  , '4'  , '1'  ,     0, NUM,		/* scan 88-95 */
667 '8'  , '5'  , '2'  ,     0, STP, '9'  , '6'  , '3'  ,		/*scan  96-103*/
668 '.'  ,     0, '*'  , '-'  , '+'  ,     0,     0,     0,		/*scan 104-111*/
669 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,	} ;
670 
671 #ifdef notdef
672 struct key {
673 	u_short action;		/* how this key functions */
674 	char	ascii[8];	/* ascii result character indexed by shifts */
675 };
676 #endif
677 
678 
679 #define	KBSTAT		0x64	/* kbd status port */
680 #define	KBS_INP_BUF_FUL	0x02	/* kbd char ready */
681 #define	KBDATA		0x60	/* kbd data port */
682 #define	KBSTATUSPORT	0x61	/* kbd status */
683 
684 update_led()
685 {
686 	while (inb(0x64)&2);	/* wait input ready */
687 	outb(0x60,0xED);	/* LED Command */
688 	while (inb(0x64)&2);	/* wait input ready */
689 	outb(0x60,scroll | 2*num | 4*caps);
690 }
691 
692 reset_cpu() {
693 	while(1) {
694 		while (inb(0x64)&2);	/* wait input ready */
695 		outb(0x64,0xFE);	/* Reset Command */
696 		DELAY(4000000);
697 		while (inb(0x64)&2);	/* wait input ready */
698 		outb(0x64,0xFF);	/* Keyboard Reset Command */
699 	}
700 	/* NOTREACHED */
701 }
702 
703 /*
704 sgetc(noblock) : get a character from the keyboard. If noblock = 0 wait until
705 a key is gotten.  Otherwise return a 0x100 (256).
706 */
707 int sgetc(noblock)
708 {
709 	u_char dt; unsigned key;
710 loop:
711 	/* First see if there is something in the keyboard port */
712 	if (inb(KBSTAT)&1) dt = inb(KBDATA);
713 	else { if (noblock) return (0x100); else goto loop; }
714 
715 	/* Check for cntl-alt-del */
716 	if ((dt == 83)&&ctls&&alts) _exit();
717 
718 	/* Check for make/break */
719 	if (dt & 0x80) {
720 		/* break */
721 		dt = dt & 0x7f ;
722 		switch (action[dt]) {
723 		case SHF: shfts = 0; break;
724 		case ALT: alts = 0; break;
725 		case CTL: ctls = 0; break;
726 		case FUNC:
727 			/* Toggle debug flags */
728 			key = unshift[dt];
729 			if(__debug & (1<<key)) __debug &= ~(1<<key) ;
730 			else __debug |= (1<<key) ;
731 			break;
732 		}
733 	} else {
734 		/* make */
735 		dt = dt & 0x7f ;
736 		switch (action[dt]) {
737 		/* LOCKING KEYS */
738 		case NUM: num ^= 1; update_led(); break;
739 		case CPS: caps ^= 1; update_led(); break;
740 		case SCROLL: scroll ^= 1; update_led(); break;
741 		case STP: stp ^= 1; if(stp) goto loop; break;
742 
743 		/* NON-LOCKING KEYS */
744 		case SHF: shfts = 1; break;
745 		case ALT: alts = 1; break;
746 		case CTL: ctls = 1; break;
747 		case ASCII:
748 			if (shfts) dt = shift[dt];
749 			else if (ctls) dt = ctl[dt];
750 			else dt = unshift[dt];
751 			if (caps && (dt >= 'a' && dt <= 'z')) dt -= 'a' - 'A';
752 			return(dt);
753 		}
754 	}
755 	if (noblock) return (0x100); else goto loop;
756 }
757 
758 pg(p,q,r,s,t,u,v,w,x,y,z) char *p; {
759 	printf(p,q,r,s,t,u,v,w,x,y,z);
760 	printf("\n");
761 	return(getchar());
762 }
763 
764 /* special characters */
765 #define bs	8
766 #define lf	10
767 #define cr	13
768 #define cntlc	3
769 #define del	0177
770 #define cntld	4
771 
772 getchar()
773 {
774 	register char thechar;
775 	register delay;
776 	int x;
777 
778 	pcconsoftc.cs_flags |= CSF_POLLING;
779 	x=splhigh();
780 	sput('>',0x6);
781 	/*while (1) {*/
782 		thechar = (char) sgetc(0);
783 		pcconsoftc.cs_flags &= ~CSF_POLLING;
784 		splx(x);
785 		switch (thechar) {
786 		    default: if (thechar >= ' ')
787 			     	sput(thechar,0x6);
788 			     return(thechar);
789 		    case cr:
790 		    case lf: sput(cr,0x6);
791 			     sput(lf,0x6);
792 			     return(lf);
793 		    case bs:
794 		    case del:
795 			     sput(bs,0x6);
796 			     sput(' ',0x6);
797 			     sput(bs,0x6);
798 			     return(thechar);
799 		    /*case cntlc:
800 			     sput('^',0xe) ; sput('C',0xe) ; sput('\r',0xe) ; sput('\n',0xe) ;
801 			     _exit(-2) ; */
802 		    case cntld:
803 			     sput('^',0x6) ; sput('D',0x6) ; sput('\r',0x6) ; sput('\n',0x6) ;
804 			     return(0);
805 		}
806 	/*}*/
807 }
808 
809 #include "machine/dbg.h"
810 #include "machine/stdarg.h"
811 static nrow;
812 
813 void
814 #ifdef __STDC__
815 dprintf(unsigned flgs, const char *fmt, ...)
816 #else
817 dprintf(flgs, fmt /*, va_alist */)
818         char *fmt;
819 	unsigned flgs;
820 #endif
821 {	extern unsigned __debug;
822 	va_list ap;
823 
824 	if((flgs&__debug) > DPAUSE) {
825 		__color = ffs(flgs&__debug)+1;
826 		va_start(ap,fmt);
827 		kprintf(fmt, 1, (struct tty *)0, ap);
828 		va_end(ap);
829 	if (flgs&DPAUSE || nrow%24 == 23) {
830 		int x;
831 		x = splhigh();
832 		if (nrow%24 == 23) nrow = 0;
833 		sgetc(0);
834 		splx(x);
835 	}
836 	}
837 	__color = 0;
838 }
839 
840 consinit() {}
841