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*52659Storek * @(#)pccons.c 5.12 (Berkeley) 02/25/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 21749592Swilliam pcioctl(dev, cmd, data, flag) 21841053Swilliam dev_t dev; 21949573Swilliam caddr_t data; 22041053Swilliam { 22149592Swilliam register struct tty *tp = &pccons; 22241053Swilliam register error; 22341053Swilliam 22449573Swilliam error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag); 22541053Swilliam if (error >= 0) 22649573Swilliam return (error); 22749573Swilliam error = ttioctl(tp, cmd, data, flag); 22849573Swilliam if (error >= 0) 22949573Swilliam return (error); 23049573Swilliam return (ENOTTY); 23141053Swilliam } 23241053Swilliam 23349592Swilliam int pcconsintr = 1; 23441053Swilliam /* 23541053Swilliam * Got a console transmission interrupt - 23641053Swilliam * the console processor wants another character. 23741053Swilliam */ 23849592Swilliam pcxint(dev) 23941053Swilliam dev_t dev; 24041053Swilliam { 24141053Swilliam register struct tty *tp; 24241053Swilliam register int unit; 24341053Swilliam 24449592Swilliam if (!pcconsintr) 24541053Swilliam return; 24649592Swilliam pccons.t_state &= ~TS_BUSY; 24749592Swilliam pcconsoftc.cs_timo = 0; 24849592Swilliam if (pccons.t_line) 24949592Swilliam (*linesw[pccons.t_line].l_start)(&pccons); 25041053Swilliam else 25149592Swilliam pcstart(&pccons); 25241053Swilliam } 25341053Swilliam 25449592Swilliam pcstart(tp) 25541053Swilliam register struct tty *tp; 25641053Swilliam { 25741053Swilliam register c, s; 25841053Swilliam 25941053Swilliam s = spltty(); 26041053Swilliam if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP)) 26141053Swilliam goto out; 26249573Swilliam do { 263*52659Storek if (tp->t_outq.c_cc <= tp->t_lowat) { 264*52659Storek if (tp->t_state&TS_ASLEEP) { 265*52659Storek tp->t_state &= ~TS_ASLEEP; 266*52659Storek wakeup((caddr_t)&tp->t_outq); 267*52659Storek } 268*52659Storek selwakeup(&tp->t_wsel); 26941053Swilliam } 270*52659Storek if (tp->t_outq.c_cc == 0) 271*52659Storek goto out; 272*52659Storek c = getc(&tp->t_outq); 273*52659Storek splx(s); 274*52659Storek sput(c, 0x7); 275*52659Storek s = spltty(); 27649573Swilliam } while(1); 27741053Swilliam out: 27841053Swilliam splx(s); 27941053Swilliam } 28041053Swilliam 28149592Swilliam pccnprobe(cp) 28249592Swilliam struct consdev *cp; 28349592Swilliam { 28449592Swilliam int maj; 28549592Swilliam extern int pcopen(); 28649592Swilliam 28749592Swilliam /* locate the major number */ 28849592Swilliam for (maj = 0; maj < nchrdev; maj++) 28949592Swilliam if (cdevsw[maj].d_open == pcopen) 29049592Swilliam break; 29149592Swilliam 29249592Swilliam /* initialize required fields */ 29349592Swilliam cp->cn_dev = makedev(maj, 0); 29449592Swilliam cp->cn_tp = &pccons; 29549592Swilliam cp->cn_pri = CN_INTERNAL; 29649592Swilliam } 29749592Swilliam 29849592Swilliam /* ARGSUSED */ 29949592Swilliam pccninit(cp) 30049592Swilliam struct consdev *cp; 30149592Swilliam { 30249592Swilliam /* 30349592Swilliam * For now, don't screw with it. 30449592Swilliam */ 30549592Swilliam /* crtat = 0; */ 30649592Swilliam } 30749592Swilliam 30849573Swilliam static __color; 30949573Swilliam 31049592Swilliam /* ARGSUSED */ 31149592Swilliam pccnputc(dev, c) 31249592Swilliam dev_t dev; 31341053Swilliam char c; 31449592Swilliam { 31549592Swilliam int clr = __color; 31649592Swilliam 31749592Swilliam if (clr == 0) 31849592Swilliam clr = 0x30; 31949592Swilliam else 32049592Swilliam clr |= 0x60; 32141053Swilliam if (c == '\n') 32249592Swilliam sput('\r', clr); 32349573Swilliam sput(c, clr); 32441053Swilliam } 32541053Swilliam 32641053Swilliam /* 32741053Swilliam * Print a character on console. 32841053Swilliam */ 32949592Swilliam pcputchar(c, tp) 33041053Swilliam char c; 33141053Swilliam register struct tty *tp; 33241053Swilliam { 33343589Sdonahn sput(c,0x2); 33441053Swilliam if (c=='\n') getchar(); 33541053Swilliam } 33641053Swilliam 33741053Swilliam 33849592Swilliam /* ARGSUSED */ 33949592Swilliam pccngetc(dev) 34049592Swilliam dev_t dev; 34141053Swilliam { 34241053Swilliam register int c, s; 34341053Swilliam 34449592Swilliam s = spltty(); /* block pcrint while we poll */ 34543589Sdonahn c = sgetc(0); 34643589Sdonahn if (c == '\r') c = '\n'; 34741053Swilliam splx(s); 34841053Swilliam return (c); 34941053Swilliam } 35041053Swilliam 35149592Swilliam pcgetchar(tp) 35241053Swilliam register struct tty *tp; 35341053Swilliam { 35441053Swilliam int c; 35541053Swilliam 35641053Swilliam c = sgetc(0); 35741053Swilliam return (c&0xff); 35841053Swilliam } 35941053Swilliam 36041053Swilliam /* 36141053Swilliam * Set line parameters 36241053Swilliam */ 36349592Swilliam pcparam(tp, t) 36441053Swilliam register struct tty *tp; 36549573Swilliam register struct termios *t; 36641053Swilliam { 36749573Swilliam register int cflag = t->c_cflag; 36849573Swilliam /* and copy to tty */ 36949573Swilliam tp->t_ispeed = t->c_ispeed; 37049573Swilliam tp->t_ospeed = t->c_ospeed; 37149573Swilliam tp->t_cflag = cflag; 37249573Swilliam 37349573Swilliam return(0); 37441053Swilliam } 37541053Swilliam 37641053Swilliam #ifdef KDB 37741053Swilliam /* 37841053Swilliam * Turn input polling on/off (used by debugger). 37941053Swilliam */ 38049592Swilliam pcpoll(onoff) 38141053Swilliam int onoff; 38241053Swilliam { 38341053Swilliam } 38441053Swilliam #endif 38541053Swilliam 38643589Sdonahn extern int hz; 38743589Sdonahn 38849573Swilliam static beeping; 38943589Sdonahn sysbeepstop() 39043589Sdonahn { 39143589Sdonahn /* disable counter 2 */ 39243589Sdonahn outb(0x61,inb(0x61)&0xFC); 39349573Swilliam beeping = 0; 39443589Sdonahn } 39543589Sdonahn 39643589Sdonahn sysbeep() 39743589Sdonahn { 39849573Swilliam 39943589Sdonahn /* enable counter 2 */ 40043589Sdonahn outb(0x61,inb(0x61)|3); 40143589Sdonahn /* set command for counter 2, 2 byte write */ 40243589Sdonahn outb(0x43,0xB6); 40343589Sdonahn /* send 0x637 for 750 HZ */ 40443589Sdonahn outb(0x42,0x37); 40543589Sdonahn outb(0x42,0x06); 40649573Swilliam if(!beeping)timeout(sysbeepstop,0,hz/4); 40749573Swilliam beeping = 1; 40843589Sdonahn } 40943589Sdonahn 41045535Sbill /* cursor() sets an offset (0-1999) into the 80x25 text area */ 41141053Swilliam 41245535Sbill static u_short *crtat = 0; 41345535Sbill char bg_at = 0x0f; 41445535Sbill char so_at = 0x70; 41541053Swilliam 41645535Sbill cursor() 41745535Sbill { int pos = crtat - Crtat; 41841053Swilliam 41943589Sdonahn outb(addr_6845,14); 42043589Sdonahn outb(addr_6845+1,pos >> 8); 42143589Sdonahn outb(addr_6845,15); 42243589Sdonahn outb(addr_6845+1,pos&0xff); 42349573Swilliam timeout(cursor,0,hz/10); 42443589Sdonahn } 42543589Sdonahn 42649573Swilliam u_char shfts, ctls, alts, caps, num, stp, scroll; 42749573Swilliam 42849573Swilliam /* 42949573Swilliam * Compensate for abysmally stupid frame buffer aribitration with macro 43049573Swilliam */ 43149573Swilliam #define wrtchar(c) { do *crtat = (c); while ((c) != *crtat); crtat++; row++; } 43249573Swilliam 43343589Sdonahn /* sput has support for emulation of the 'ibmpc' termcap entry. */ 43443589Sdonahn /* This is a bare-bones implementation of a bare-bones entry */ 43543589Sdonahn /* One modification: Change li#24 to li#25 to reflect 25 lines */ 43643589Sdonahn 43743589Sdonahn sput(c, ca) 43843589Sdonahn u_char c, ca; 43943589Sdonahn { 44043589Sdonahn 44143589Sdonahn static int esc,ebrac,eparm,cx,cy,row,so; 44243589Sdonahn 44341053Swilliam if (crtat == 0) { 44445535Sbill u_short *cp = Crtat + (CGA_BUF-MONO_BUF)/CHR, was; 44545535Sbill unsigned cursorat; 44643589Sdonahn 44743589Sdonahn /* Crtat initialized to point to MONO buffer */ 44843589Sdonahn /* if not present change to CGA_BUF offset */ 44943589Sdonahn /* ONLY ADD the difference since locore.s adds */ 45043589Sdonahn /* in the remapped offset at the right time */ 45143589Sdonahn 45245535Sbill was = *cp; 45345535Sbill *cp = (u_short) 0xA55A; 45445535Sbill if (*cp != 0xA55A) { 45545535Sbill addr_6845 = MONO_BASE; 45645535Sbill } else { 45745535Sbill *cp = was; 45845535Sbill addr_6845 = CGA_BASE; 45943589Sdonahn Crtat = Crtat + (CGA_BUF-MONO_BUF)/CHR; 46045535Sbill } 46145535Sbill /* Extract cursor location */ 46245535Sbill outb(addr_6845,14); 46345535Sbill cursorat = inb(addr_6845+1)<<8 ; 46445535Sbill outb(addr_6845,15); 46545535Sbill cursorat |= inb(addr_6845+1); 46645535Sbill 46745535Sbill crtat = Crtat + cursorat; 46845535Sbill fillw((bg_at<<8)|' ', crtat, COL*ROW-cursorat); 46941053Swilliam } 47041053Swilliam switch(c) { 47143589Sdonahn case 0x1B: 47243589Sdonahn esc = 1; ebrac = 0; eparm = 0; 47343589Sdonahn break; 47441053Swilliam 47541053Swilliam case '\t': 47641053Swilliam do { 47749573Swilliam wrtchar((ca<<8)| ' '); 47849573Swilliam } while (row % 8); 47941053Swilliam break; 48041053Swilliam 48141053Swilliam case '\010': 48241053Swilliam crtat--; row--; 48343589Sdonahn if (row < 0) row += COL; /* non-destructive backspace */ 48441053Swilliam break; 48541053Swilliam 48641053Swilliam case '\r': 48743589Sdonahn crtat -= row ; row = 0; 48841053Swilliam break; 48941053Swilliam 49041053Swilliam case '\n': 49141053Swilliam crtat += COL ; 49241053Swilliam break; 49341053Swilliam 49441053Swilliam default: 49543589Sdonahn if (esc) { 49643589Sdonahn if (ebrac) { 49743589Sdonahn switch(c) { 49843589Sdonahn case 'm': /* no support for standout */ 49943589Sdonahn if (!cx) so = 0; 50043589Sdonahn else so = 1; 50143589Sdonahn esc = 0; ebrac = 0; eparm = 0; 50243589Sdonahn break; 50343589Sdonahn case 'A': /* back one row */ 50443589Sdonahn crtat -= COL; 50543589Sdonahn esc = 0; ebrac = 0; eparm = 0; 50643589Sdonahn break; 50743589Sdonahn case 'B': /* down one row */ 50843589Sdonahn crtat += COL; 50943589Sdonahn esc = 0; ebrac = 0; eparm = 0; 51043589Sdonahn break; 51143589Sdonahn case 'C': /* right cursor */ 51243589Sdonahn crtat++; row++; 51343589Sdonahn esc = 0; ebrac = 0; eparm = 0; 51443589Sdonahn break; 51543589Sdonahn case 'J': /* Clear to end of display */ 51645535Sbill fillw((bg_at<<8)+' ', crtat, 51745535Sbill Crtat+COL*ROW-crtat); 51843589Sdonahn esc = 0; ebrac = 0; eparm = 0; 51943589Sdonahn break; 52043589Sdonahn case 'K': /* Clear to EOL */ 52149573Swilliam fillw((bg_at<<8)+' ', crtat, 52249573Swilliam COL-(crtat-Crtat)%COL); 52343589Sdonahn esc = 0; ebrac = 0; eparm = 0; 52443589Sdonahn break; 52543589Sdonahn case 'H': /* Cursor move */ 52643589Sdonahn if ((!cx)||(!cy)) { 52743589Sdonahn crtat = Crtat; 52843589Sdonahn row = 0; 52943589Sdonahn } else { 53043589Sdonahn crtat = Crtat+(cx-1)*COL+cy-1; 53143589Sdonahn row = cy-1; 53243589Sdonahn } 53343589Sdonahn esc = 0; ebrac = 0; eparm = 0; 53443589Sdonahn break; 53543589Sdonahn case ';': /* Switch params in cursor def */ 53643589Sdonahn eparm = 1; 53743589Sdonahn return; 53843589Sdonahn default: /* Only numbers valid here */ 53943589Sdonahn if ((c >= '0')&&(c <= '9')) { 54043589Sdonahn if (eparm) { 54143589Sdonahn cy *= 10; 54243589Sdonahn cy += c - '0'; 54343589Sdonahn } else { 54443589Sdonahn cx *= 10; 54543589Sdonahn cx += c - '0'; 54643589Sdonahn } 54743589Sdonahn } else { 54843589Sdonahn esc = 0; ebrac = 0; eparm = 0; 54943589Sdonahn } 55043589Sdonahn return; 55143589Sdonahn } 55243589Sdonahn break; 55343589Sdonahn } else if (c == 'c') { /* Clear screen & home */ 55445535Sbill fillw((bg_at<<8)+' ', Crtat,COL*ROW); 55543589Sdonahn crtat = Crtat; row = 0; 55643589Sdonahn esc = 0; ebrac = 0; eparm = 0; 55745535Sbill } else if (c == '[') { /* Start ESC [ sequence */ 55843589Sdonahn ebrac = 1; cx = 0; cy = 0; eparm = 0; 55945535Sbill } else { /* Invalid, clear state */ 56043589Sdonahn esc = 0; ebrac = 0; eparm = 0; 56143589Sdonahn } 56243589Sdonahn } else { 56349573Swilliam if (c == 7) 56443589Sdonahn sysbeep(); 56543589Sdonahn /* Print only printables */ 56645535Sbill else /*if (c >= ' ') */ { 56743589Sdonahn if (so) { 56849573Swilliam wrtchar((so_at<<8)| c); 56943589Sdonahn } else { 57049573Swilliam wrtchar((ca<<8)| c); 57143589Sdonahn } 57243589Sdonahn if (row >= COL) row = 0; 57343589Sdonahn break ; 57443589Sdonahn } 57541053Swilliam } 57641053Swilliam } 57743589Sdonahn if (crtat >= Crtat+COL*(ROW)) { /* scroll check */ 57849573Swilliam if (openf) do sgetc(1); while (scroll); 57943589Sdonahn bcopy(Crtat+COL,Crtat,COL*(ROW-1)*CHR); 58049573Swilliam fillw ((bg_at<<8) + ' ', Crtat+COL*(ROW-1),COL) ; 58143589Sdonahn crtat -= COL ; 58243589Sdonahn } 58341053Swilliam } 58443589Sdonahn 58541053Swilliam #define L 0x0001 /* locking function */ 58641053Swilliam #define SHF 0x0002 /* keyboard shift */ 58741053Swilliam #define ALT 0x0004 /* alternate shift -- alternate chars */ 58841053Swilliam #define NUM 0x0008 /* numeric shift cursors vs. numeric */ 58941053Swilliam #define CTL 0x0010 /* control shift -- allows ctl function */ 59041053Swilliam #define CPS 0x0020 /* caps shift -- swaps case of letter */ 59141053Swilliam #define ASCII 0x0040 /* ascii code for this key */ 59241053Swilliam #define STP 0x0080 /* stop output */ 59341053Swilliam #define FUNC 0x0100 /* function key */ 59443589Sdonahn #define SCROLL 0x0200 /* scroll lock key */ 59541053Swilliam 59649695Swilliam unsigned __debug = 0; /*0xffe */; 59741053Swilliam u_short action[] = { 59841053Swilliam 0, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 0- 7 */ 59941053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 8-15 */ 60041053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 16-23 */ 60141053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, CTL, ASCII, ASCII, /* scan 24-31 */ 60241053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 32-39 */ 60341053Swilliam ASCII, ASCII, SHF , ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 40-47 */ 60441053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, SHF, ASCII, /* scan 48-55 */ 60543589Sdonahn ALT, ASCII, CPS , FUNC , FUNC , FUNC , FUNC , FUNC , /* scan 56-63 */ 60649573Swilliam FUNC , FUNC , FUNC , FUNC , FUNC , NUM, SCROLL, ASCII, /* scan 64-71 */ 60741053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 72-79 */ 60841053Swilliam ASCII, ASCII, ASCII, ASCII, 0, 0, 0, 0, /* scan 80-87 */ 60941053Swilliam 0,0,0,0,0,0,0,0, 61041053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 61141053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 61241053Swilliam 61341053Swilliam u_char unshift[] = { /* no shift */ 61441053Swilliam 0, 033 , '1' , '2' , '3' , '4' , '5' , '6' , /* scan 0- 7 */ 61541053Swilliam '7' , '8' , '9' , '0' , '-' , '=' , 0177 ,'\t' , /* scan 8-15 */ 61641053Swilliam 61741053Swilliam 'q' , 'w' , 'e' , 'r' , 't' , 'y' , 'u' , 'i' , /* scan 16-23 */ 61841053Swilliam 'o' , 'p' , '[' , ']' , '\r' , CTL , 'a' , 's' , /* scan 24-31 */ 61941053Swilliam 62041053Swilliam 'd' , 'f' , 'g' , 'h' , 'j' , 'k' , 'l' , ';' , /* scan 32-39 */ 62141053Swilliam '\'' , '`' , SHF , '\\' , 'z' , 'x' , 'c' , 'v' , /* scan 40-47 */ 62241053Swilliam 62341053Swilliam 'b' , 'n' , 'm' , ',' , '.' , '/' , SHF , '*', /* scan 48-55 */ 62443589Sdonahn ALT , ' ' , CPS, 1, 2, 3 , 4, 5, /* scan 56-63 */ 62541053Swilliam 62643589Sdonahn 6, 7, 8, 9, 10, NUM, STP, '7', /* scan 64-71 */ 62741053Swilliam '8', '9', '-', '4', '5', '6', '+', '1', /* scan 72-79 */ 62841053Swilliam 62941053Swilliam '2', '3', '0', '.', 0, 0, 0, 0, /* scan 80-87 */ 63041053Swilliam 0,0,0,0,0,0,0,0, 63141053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 63241053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 63341053Swilliam 63441053Swilliam u_char shift[] = { /* shift shift */ 63541053Swilliam 0, 033 , '!' , '@' , '#' , '$' , '%' , '^' , /* scan 0- 7 */ 63641053Swilliam '&' , '*' , '(' , ')' , '_' , '+' , 0177 ,'\t' , /* scan 8-15 */ 63741053Swilliam 'Q' , 'W' , 'E' , 'R' , 'T' , 'Y' , 'U' , 'I' , /* scan 16-23 */ 63841053Swilliam 'O' , 'P' , '{' , '}' , '\r' , CTL , 'A' , 'S' , /* scan 24-31 */ 63941053Swilliam 'D' , 'F' , 'G' , 'H' , 'J' , 'K' , 'L' , ':' , /* scan 32-39 */ 64041053Swilliam '"' , '~' , SHF , '|' , 'Z' , 'X' , 'C' , 'V' , /* scan 40-47 */ 64141053Swilliam 'B' , 'N' , 'M' , '<' , '>' , '?' , SHF , '*', /* scan 48-55 */ 64243589Sdonahn ALT , ' ' , CPS, 0, 0, ' ' , 0, 0, /* scan 56-63 */ 64343589Sdonahn 0, 0, 0, 0, 0, NUM, STP, '7', /* scan 64-71 */ 64441053Swilliam '8', '9', '-', '4', '5', '6', '+', '1', /* scan 72-79 */ 64541053Swilliam '2', '3', '0', '.', 0, 0, 0, 0, /* scan 80-87 */ 64641053Swilliam 0,0,0,0,0,0,0,0, 64741053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 64841053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 64941053Swilliam 65041053Swilliam u_char ctl[] = { /* CTL shift */ 65141053Swilliam 0, 033 , '!' , 000 , '#' , '$' , '%' , 036 , /* scan 0- 7 */ 65241053Swilliam '&' , '*' , '(' , ')' , 037 , '+' , 034 ,'\177', /* scan 8-15 */ 65341053Swilliam 021 , 027 , 005 , 022 , 024 , 031 , 025 , 011 , /* scan 16-23 */ 65441053Swilliam 017 , 020 , 033 , 035 , '\r' , CTL , 001 , 023 , /* scan 24-31 */ 65541053Swilliam 004 , 006 , 007 , 010 , 012 , 013 , 014 , ';' , /* scan 32-39 */ 65641053Swilliam '\'' , '`' , SHF , 034 , 032 , 030 , 003 , 026 , /* scan 40-47 */ 65741053Swilliam 002 , 016 , 015 , '<' , '>' , '?' , SHF , '*', /* scan 48-55 */ 65843589Sdonahn ALT , ' ' , CPS, 0, 0, ' ' , 0, 0, /* scan 56-63 */ 65943589Sdonahn CPS, 0, 0, 0, 0, 0, 0, 0, /* scan 64-71 */ 66041053Swilliam 0, 0, 0, 0, 0, 0, 0, 0, /* scan 72-79 */ 66141053Swilliam 0, 0, 0, 0, 0, 0, 0, 0, /* scan 80-87 */ 66243589Sdonahn 0, 0, 033, '7' , '4' , '1' , 0, NUM, /* scan 88-95 */ 66343589Sdonahn '8' , '5' , '2' , 0, STP, '9' , '6' , '3' , /*scan 96-103*/ 66441053Swilliam '.' , 0, '*' , '-' , '+' , 0, 0, 0, /*scan 104-111*/ 66541053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 66641053Swilliam 66741053Swilliam #ifdef notdef 66841053Swilliam struct key { 66941053Swilliam u_short action; /* how this key functions */ 67041053Swilliam char ascii[8]; /* ascii result character indexed by shifts */ 67141053Swilliam }; 67241053Swilliam #endif 67341053Swilliam 67441053Swilliam 67543589Sdonahn #define KBSTAT 0x64 /* kbd status port */ 67643589Sdonahn #define KBS_INP_BUF_FUL 0x02 /* kbd char ready */ 67743589Sdonahn #define KBDATA 0x60 /* kbd data port */ 67841053Swilliam #define KBSTATUSPORT 0x61 /* kbd status */ 67941053Swilliam 68043589Sdonahn update_led() 68143589Sdonahn { 68243589Sdonahn while (inb(0x64)&2); /* wait input ready */ 68343589Sdonahn outb(0x60,0xED); /* LED Command */ 68443589Sdonahn while (inb(0x64)&2); /* wait input ready */ 68543589Sdonahn outb(0x60,scroll | 2*num | 4*caps); 68643589Sdonahn } 68741053Swilliam 68845535Sbill reset_cpu() { 68945535Sbill while(1) { 69045535Sbill while (inb(0x64)&2); /* wait input ready */ 69145535Sbill outb(0x64,0xFE); /* Reset Command */ 69245535Sbill DELAY(4000000); 69345535Sbill while (inb(0x64)&2); /* wait input ready */ 69445535Sbill outb(0x64,0xFF); /* Keyboard Reset Command */ 69545535Sbill } 69645535Sbill /* NOTREACHED */ 69745535Sbill } 69845535Sbill 69943589Sdonahn /* 70043589Sdonahn sgetc(noblock) : get a character from the keyboard. If noblock = 0 wait until 70143589Sdonahn a key is gotten. Otherwise return a 0x100 (256). 70243589Sdonahn */ 70343589Sdonahn int sgetc(noblock) 70443589Sdonahn { 70543589Sdonahn u_char dt; unsigned key; 70641053Swilliam loop: 70743589Sdonahn /* First see if there is something in the keyboard port */ 70843589Sdonahn if (inb(KBSTAT)&1) dt = inb(KBDATA); 70943589Sdonahn else { if (noblock) return (0x100); else goto loop; } 71041053Swilliam 71143589Sdonahn /* Check for cntl-alt-del */ 71243589Sdonahn if ((dt == 83)&&ctls&&alts) _exit(); 71341053Swilliam 71443589Sdonahn /* Check for make/break */ 71543589Sdonahn if (dt & 0x80) { 71643589Sdonahn /* break */ 71743589Sdonahn dt = dt & 0x7f ; 71843589Sdonahn switch (action[dt]) { 71943589Sdonahn case SHF: shfts = 0; break; 72043589Sdonahn case ALT: alts = 0; break; 72143589Sdonahn case CTL: ctls = 0; break; 72243589Sdonahn case FUNC: 72343589Sdonahn /* Toggle debug flags */ 72443589Sdonahn key = unshift[dt]; 72543589Sdonahn if(__debug & (1<<key)) __debug &= ~(1<<key) ; 72643589Sdonahn else __debug |= (1<<key) ; 72743589Sdonahn break; 72841053Swilliam } 72943589Sdonahn } else { 73043589Sdonahn /* make */ 73143589Sdonahn dt = dt & 0x7f ; 73243589Sdonahn switch (action[dt]) { 73343589Sdonahn /* LOCKING KEYS */ 73443589Sdonahn case NUM: num ^= 1; update_led(); break; 73543589Sdonahn case CPS: caps ^= 1; update_led(); break; 73643589Sdonahn case SCROLL: scroll ^= 1; update_led(); break; 73743589Sdonahn case STP: stp ^= 1; if(stp) goto loop; break; 73843589Sdonahn 73943589Sdonahn /* NON-LOCKING KEYS */ 74043589Sdonahn case SHF: shfts = 1; break; 74143589Sdonahn case ALT: alts = 1; break; 74243589Sdonahn case CTL: ctls = 1; break; 74343589Sdonahn case ASCII: 74443589Sdonahn if (shfts) dt = shift[dt]; 74543589Sdonahn else if (ctls) dt = ctl[dt]; 74643589Sdonahn else dt = unshift[dt]; 74743589Sdonahn if (caps && (dt >= 'a' && dt <= 'z')) dt -= 'a' - 'A'; 74843589Sdonahn return(dt); 74943589Sdonahn } 75041053Swilliam } 75143589Sdonahn if (noblock) return (0x100); else goto loop; 75241053Swilliam } 75341053Swilliam 75441053Swilliam pg(p,q,r,s,t,u,v,w,x,y,z) char *p; { 75541053Swilliam printf(p,q,r,s,t,u,v,w,x,y,z); 75641053Swilliam printf("\n"); 75741053Swilliam return(getchar()); 75841053Swilliam } 75941053Swilliam 76041053Swilliam /* special characters */ 76141053Swilliam #define bs 8 76241053Swilliam #define lf 10 76341053Swilliam #define cr 13 76441053Swilliam #define cntlc 3 76541053Swilliam #define del 0177 76641053Swilliam #define cntld 4 76741053Swilliam 76841053Swilliam getchar() 76941053Swilliam { 77041053Swilliam register char thechar; 77141053Swilliam register delay; 77241053Swilliam int x; 77341053Swilliam 77449592Swilliam pcconsoftc.cs_flags |= CSF_POLLING; 77541053Swilliam x=splhigh(); 77643589Sdonahn sput('>',0x6); 77745535Sbill /*while (1) {*/ 77841053Swilliam thechar = (char) sgetc(0); 77949592Swilliam pcconsoftc.cs_flags &= ~CSF_POLLING; 78043589Sdonahn splx(x); 78141053Swilliam switch (thechar) { 78241053Swilliam default: if (thechar >= ' ') 78343589Sdonahn sput(thechar,0x6); 78441053Swilliam return(thechar); 78541053Swilliam case cr: 78643589Sdonahn case lf: sput(cr,0x6); 78743589Sdonahn sput(lf,0x6); 78841053Swilliam return(lf); 78941053Swilliam case bs: 79041053Swilliam case del: 79143589Sdonahn sput(bs,0x6); 79243589Sdonahn sput(' ',0x6); 79343589Sdonahn sput(bs,0x6); 79441053Swilliam return(thechar); 79543589Sdonahn /*case cntlc: 79641053Swilliam sput('^',0xe) ; sput('C',0xe) ; sput('\r',0xe) ; sput('\n',0xe) ; 79743589Sdonahn _exit(-2) ; */ 79841053Swilliam case cntld: 79943589Sdonahn sput('^',0x6) ; sput('D',0x6) ; sput('\r',0x6) ; sput('\n',0x6) ; 80041053Swilliam return(0); 80141053Swilliam } 80245535Sbill /*}*/ 80341053Swilliam } 80449573Swilliam 80549573Swilliam #include "machine/dbg.h" 80649573Swilliam #include "machine/stdarg.h" 80749573Swilliam static nrow; 80849573Swilliam 80949573Swilliam void 81049573Swilliam #ifdef __STDC__ 81149573Swilliam dprintf(unsigned flgs, const char *fmt, ...) 81249573Swilliam #else 81349573Swilliam dprintf(flgs, fmt /*, va_alist */) 81449573Swilliam char *fmt; 81549573Swilliam unsigned flgs; 81649573Swilliam #endif 81749573Swilliam { extern unsigned __debug; 81849573Swilliam va_list ap; 81949573Swilliam 82049573Swilliam if((flgs&__debug) > DPAUSE) { 82149573Swilliam __color = ffs(flgs&__debug)+1; 82249573Swilliam va_start(ap,fmt); 82349573Swilliam kprintf(fmt, 1, (struct tty *)0, ap); 82449573Swilliam va_end(ap); 82549573Swilliam if (flgs&DPAUSE || nrow%24 == 23) { 82649573Swilliam int x; 82749573Swilliam x = splhigh(); 82849573Swilliam if (nrow%24 == 23) nrow = 0; 82949573Swilliam sgetc(0); 83049573Swilliam splx(x); 83149573Swilliam } 83249573Swilliam } 83349573Swilliam __color = 0; 83449573Swilliam } 83549573Swilliam 83649573Swilliam consinit() {} 837