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*53643Sbostic * @(#)pccons.c 5.14 (Berkeley) 05/20/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 214*53643Sbostic if (openf) /* 386bsd */ 215*53643Sbostic (*linesw[pccons.t_line].l_rint)(c&0xff, &pccons); 21641053Swilliam } 21741053Swilliam 21852691Ssklower pcioctl(dev, cmd, data, flag, p) 21941053Swilliam dev_t dev; 22052691Ssklower int cmd, flag; 22149573Swilliam caddr_t data; 22252691Ssklower struct proc *p; 22341053Swilliam { 22449592Swilliam register struct tty *tp = &pccons; 22541053Swilliam register error; 22641053Swilliam 22752691Ssklower error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 22841053Swilliam if (error >= 0) 22949573Swilliam return (error); 23049573Swilliam error = ttioctl(tp, cmd, data, flag); 23149573Swilliam if (error >= 0) 23249573Swilliam return (error); 23349573Swilliam return (ENOTTY); 23441053Swilliam } 23541053Swilliam 23649592Swilliam int pcconsintr = 1; 23741053Swilliam /* 23841053Swilliam * Got a console transmission interrupt - 23941053Swilliam * the console processor wants another character. 24041053Swilliam */ 24149592Swilliam pcxint(dev) 24241053Swilliam dev_t dev; 24341053Swilliam { 24441053Swilliam register struct tty *tp; 24541053Swilliam register int unit; 24641053Swilliam 24749592Swilliam if (!pcconsintr) 24841053Swilliam return; 24949592Swilliam pccons.t_state &= ~TS_BUSY; 25049592Swilliam pcconsoftc.cs_timo = 0; 25149592Swilliam if (pccons.t_line) 25249592Swilliam (*linesw[pccons.t_line].l_start)(&pccons); 25341053Swilliam else 25449592Swilliam pcstart(&pccons); 25541053Swilliam } 25641053Swilliam 25749592Swilliam pcstart(tp) 25841053Swilliam register struct tty *tp; 25941053Swilliam { 26041053Swilliam register c, s; 26141053Swilliam 26241053Swilliam s = spltty(); 26341053Swilliam if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP)) 26441053Swilliam goto out; 26549573Swilliam do { 26652659Storek if (tp->t_outq.c_cc <= tp->t_lowat) { 26752659Storek if (tp->t_state&TS_ASLEEP) { 26852659Storek tp->t_state &= ~TS_ASLEEP; 26952659Storek wakeup((caddr_t)&tp->t_outq); 27052659Storek } 27152659Storek selwakeup(&tp->t_wsel); 27241053Swilliam } 27352659Storek if (tp->t_outq.c_cc == 0) 27452659Storek goto out; 27552659Storek c = getc(&tp->t_outq); 27652659Storek splx(s); 27752659Storek sput(c, 0x7); 27852659Storek s = spltty(); 27949573Swilliam } while(1); 28041053Swilliam out: 28141053Swilliam splx(s); 28241053Swilliam } 28341053Swilliam 28449592Swilliam pccnprobe(cp) 28549592Swilliam struct consdev *cp; 28649592Swilliam { 28749592Swilliam int maj; 28849592Swilliam extern int pcopen(); 28949592Swilliam 29049592Swilliam /* locate the major number */ 29149592Swilliam for (maj = 0; maj < nchrdev; maj++) 29249592Swilliam if (cdevsw[maj].d_open == pcopen) 29349592Swilliam break; 29449592Swilliam 29549592Swilliam /* initialize required fields */ 29649592Swilliam cp->cn_dev = makedev(maj, 0); 29749592Swilliam cp->cn_tp = &pccons; 29849592Swilliam cp->cn_pri = CN_INTERNAL; 29949592Swilliam } 30049592Swilliam 30149592Swilliam /* ARGSUSED */ 30249592Swilliam pccninit(cp) 30349592Swilliam struct consdev *cp; 30449592Swilliam { 30549592Swilliam /* 30649592Swilliam * For now, don't screw with it. 30749592Swilliam */ 30849592Swilliam /* crtat = 0; */ 30949592Swilliam } 31049592Swilliam 31149573Swilliam static __color; 31249573Swilliam 31349592Swilliam /* ARGSUSED */ 31449592Swilliam pccnputc(dev, c) 31549592Swilliam dev_t dev; 31641053Swilliam char c; 31749592Swilliam { 31849592Swilliam int clr = __color; 31949592Swilliam 32049592Swilliam if (clr == 0) 32149592Swilliam clr = 0x30; 32249592Swilliam else 32349592Swilliam clr |= 0x60; 32441053Swilliam if (c == '\n') 32549592Swilliam sput('\r', clr); 32649573Swilliam sput(c, clr); 32741053Swilliam } 32841053Swilliam 32941053Swilliam /* 33041053Swilliam * Print a character on console. 33141053Swilliam */ 33249592Swilliam pcputchar(c, tp) 33341053Swilliam char c; 33441053Swilliam register struct tty *tp; 33541053Swilliam { 33643589Sdonahn sput(c,0x2); 33741053Swilliam if (c=='\n') getchar(); 33841053Swilliam } 33941053Swilliam 34041053Swilliam 34149592Swilliam /* ARGSUSED */ 34249592Swilliam pccngetc(dev) 34349592Swilliam dev_t dev; 34441053Swilliam { 34541053Swilliam register int c, s; 34641053Swilliam 34749592Swilliam s = spltty(); /* block pcrint while we poll */ 34843589Sdonahn c = sgetc(0); 34943589Sdonahn if (c == '\r') c = '\n'; 35041053Swilliam splx(s); 35141053Swilliam return (c); 35241053Swilliam } 35341053Swilliam 35449592Swilliam pcgetchar(tp) 35541053Swilliam register struct tty *tp; 35641053Swilliam { 35741053Swilliam int c; 35841053Swilliam 35941053Swilliam c = sgetc(0); 36041053Swilliam return (c&0xff); 36141053Swilliam } 36241053Swilliam 36341053Swilliam /* 36441053Swilliam * Set line parameters 36541053Swilliam */ 36649592Swilliam pcparam(tp, t) 36741053Swilliam register struct tty *tp; 36849573Swilliam register struct termios *t; 36941053Swilliam { 37049573Swilliam register int cflag = t->c_cflag; 37149573Swilliam /* and copy to tty */ 37249573Swilliam tp->t_ispeed = t->c_ispeed; 37349573Swilliam tp->t_ospeed = t->c_ospeed; 37449573Swilliam tp->t_cflag = cflag; 37549573Swilliam 37649573Swilliam return(0); 37741053Swilliam } 37841053Swilliam 37941053Swilliam #ifdef KDB 38041053Swilliam /* 38141053Swilliam * Turn input polling on/off (used by debugger). 38241053Swilliam */ 38349592Swilliam pcpoll(onoff) 38441053Swilliam int onoff; 38541053Swilliam { 38641053Swilliam } 38741053Swilliam #endif 38841053Swilliam 38943589Sdonahn extern int hz; 39043589Sdonahn 39149573Swilliam static beeping; 39243589Sdonahn sysbeepstop() 39343589Sdonahn { 39443589Sdonahn /* disable counter 2 */ 39543589Sdonahn outb(0x61,inb(0x61)&0xFC); 39649573Swilliam beeping = 0; 39743589Sdonahn } 39843589Sdonahn 39943589Sdonahn sysbeep() 40043589Sdonahn { 40149573Swilliam 40243589Sdonahn /* enable counter 2 */ 40343589Sdonahn outb(0x61,inb(0x61)|3); 40443589Sdonahn /* set command for counter 2, 2 byte write */ 40543589Sdonahn outb(0x43,0xB6); 40643589Sdonahn /* send 0x637 for 750 HZ */ 40743589Sdonahn outb(0x42,0x37); 40843589Sdonahn outb(0x42,0x06); 40949573Swilliam if(!beeping)timeout(sysbeepstop,0,hz/4); 41049573Swilliam beeping = 1; 41143589Sdonahn } 41243589Sdonahn 41345535Sbill /* cursor() sets an offset (0-1999) into the 80x25 text area */ 41441053Swilliam 41545535Sbill static u_short *crtat = 0; 41645535Sbill char bg_at = 0x0f; 41745535Sbill char so_at = 0x70; 41841053Swilliam 41945535Sbill cursor() 42045535Sbill { int pos = crtat - Crtat; 42141053Swilliam 42243589Sdonahn outb(addr_6845,14); 42343589Sdonahn outb(addr_6845+1,pos >> 8); 42443589Sdonahn outb(addr_6845,15); 42543589Sdonahn outb(addr_6845+1,pos&0xff); 42649573Swilliam timeout(cursor,0,hz/10); 42743589Sdonahn } 42843589Sdonahn 42949573Swilliam u_char shfts, ctls, alts, caps, num, stp, scroll; 43049573Swilliam 43149573Swilliam /* 43249573Swilliam * Compensate for abysmally stupid frame buffer aribitration with macro 43349573Swilliam */ 43449573Swilliam #define wrtchar(c) { do *crtat = (c); while ((c) != *crtat); crtat++; row++; } 43549573Swilliam 43643589Sdonahn /* sput has support for emulation of the 'ibmpc' termcap entry. */ 43743589Sdonahn /* This is a bare-bones implementation of a bare-bones entry */ 43843589Sdonahn /* One modification: Change li#24 to li#25 to reflect 25 lines */ 43943589Sdonahn 44043589Sdonahn sput(c, ca) 44143589Sdonahn u_char c, ca; 44243589Sdonahn { 44343589Sdonahn 44443589Sdonahn static int esc,ebrac,eparm,cx,cy,row,so; 44543589Sdonahn 44641053Swilliam if (crtat == 0) { 44745535Sbill u_short *cp = Crtat + (CGA_BUF-MONO_BUF)/CHR, was; 44845535Sbill unsigned cursorat; 44943589Sdonahn 45043589Sdonahn /* Crtat initialized to point to MONO buffer */ 45143589Sdonahn /* if not present change to CGA_BUF offset */ 45243589Sdonahn /* ONLY ADD the difference since locore.s adds */ 45343589Sdonahn /* in the remapped offset at the right time */ 45443589Sdonahn 45545535Sbill was = *cp; 45645535Sbill *cp = (u_short) 0xA55A; 45745535Sbill if (*cp != 0xA55A) { 45845535Sbill addr_6845 = MONO_BASE; 45945535Sbill } else { 46045535Sbill *cp = was; 46145535Sbill addr_6845 = CGA_BASE; 46243589Sdonahn Crtat = Crtat + (CGA_BUF-MONO_BUF)/CHR; 46345535Sbill } 46445535Sbill /* Extract cursor location */ 46545535Sbill outb(addr_6845,14); 46645535Sbill cursorat = inb(addr_6845+1)<<8 ; 46745535Sbill outb(addr_6845,15); 46845535Sbill cursorat |= inb(addr_6845+1); 46945535Sbill 47045535Sbill crtat = Crtat + cursorat; 47145535Sbill fillw((bg_at<<8)|' ', crtat, COL*ROW-cursorat); 47241053Swilliam } 47341053Swilliam switch(c) { 47443589Sdonahn case 0x1B: 47543589Sdonahn esc = 1; ebrac = 0; eparm = 0; 47643589Sdonahn break; 47741053Swilliam 47841053Swilliam case '\t': 47941053Swilliam do { 48049573Swilliam wrtchar((ca<<8)| ' '); 48149573Swilliam } while (row % 8); 48241053Swilliam break; 48341053Swilliam 48441053Swilliam case '\010': 48541053Swilliam crtat--; row--; 48643589Sdonahn if (row < 0) row += COL; /* non-destructive backspace */ 48741053Swilliam break; 48841053Swilliam 48941053Swilliam case '\r': 49043589Sdonahn crtat -= row ; row = 0; 49141053Swilliam break; 49241053Swilliam 49341053Swilliam case '\n': 49441053Swilliam crtat += COL ; 49541053Swilliam break; 49641053Swilliam 49741053Swilliam default: 49843589Sdonahn if (esc) { 49943589Sdonahn if (ebrac) { 50043589Sdonahn switch(c) { 50143589Sdonahn case 'm': /* no support for standout */ 50243589Sdonahn if (!cx) so = 0; 50343589Sdonahn else so = 1; 50443589Sdonahn esc = 0; ebrac = 0; eparm = 0; 50543589Sdonahn break; 50643589Sdonahn case 'A': /* back one row */ 50743589Sdonahn crtat -= COL; 50843589Sdonahn esc = 0; ebrac = 0; eparm = 0; 50943589Sdonahn break; 51043589Sdonahn case 'B': /* down one row */ 51143589Sdonahn crtat += COL; 51243589Sdonahn esc = 0; ebrac = 0; eparm = 0; 51343589Sdonahn break; 51443589Sdonahn case 'C': /* right cursor */ 51543589Sdonahn crtat++; row++; 51643589Sdonahn esc = 0; ebrac = 0; eparm = 0; 51743589Sdonahn break; 51843589Sdonahn case 'J': /* Clear to end of display */ 51945535Sbill fillw((bg_at<<8)+' ', crtat, 52045535Sbill Crtat+COL*ROW-crtat); 52143589Sdonahn esc = 0; ebrac = 0; eparm = 0; 52243589Sdonahn break; 52343589Sdonahn case 'K': /* Clear to EOL */ 52449573Swilliam fillw((bg_at<<8)+' ', crtat, 52549573Swilliam COL-(crtat-Crtat)%COL); 52643589Sdonahn esc = 0; ebrac = 0; eparm = 0; 52743589Sdonahn break; 52843589Sdonahn case 'H': /* Cursor move */ 52943589Sdonahn if ((!cx)||(!cy)) { 53043589Sdonahn crtat = Crtat; 53143589Sdonahn row = 0; 53243589Sdonahn } else { 53343589Sdonahn crtat = Crtat+(cx-1)*COL+cy-1; 53443589Sdonahn row = cy-1; 53543589Sdonahn } 53643589Sdonahn esc = 0; ebrac = 0; eparm = 0; 53743589Sdonahn break; 53843589Sdonahn case ';': /* Switch params in cursor def */ 53943589Sdonahn eparm = 1; 54043589Sdonahn return; 54143589Sdonahn default: /* Only numbers valid here */ 54243589Sdonahn if ((c >= '0')&&(c <= '9')) { 54343589Sdonahn if (eparm) { 54443589Sdonahn cy *= 10; 54543589Sdonahn cy += c - '0'; 54643589Sdonahn } else { 54743589Sdonahn cx *= 10; 54843589Sdonahn cx += c - '0'; 54943589Sdonahn } 55043589Sdonahn } else { 55143589Sdonahn esc = 0; ebrac = 0; eparm = 0; 55243589Sdonahn } 55343589Sdonahn return; 55443589Sdonahn } 55543589Sdonahn break; 55643589Sdonahn } else if (c == 'c') { /* Clear screen & home */ 55745535Sbill fillw((bg_at<<8)+' ', Crtat,COL*ROW); 55843589Sdonahn crtat = Crtat; row = 0; 55943589Sdonahn esc = 0; ebrac = 0; eparm = 0; 56045535Sbill } else if (c == '[') { /* Start ESC [ sequence */ 56143589Sdonahn ebrac = 1; cx = 0; cy = 0; eparm = 0; 56245535Sbill } else { /* Invalid, clear state */ 56343589Sdonahn esc = 0; ebrac = 0; eparm = 0; 56443589Sdonahn } 56543589Sdonahn } else { 56649573Swilliam if (c == 7) 56743589Sdonahn sysbeep(); 56843589Sdonahn /* Print only printables */ 56945535Sbill else /*if (c >= ' ') */ { 57043589Sdonahn if (so) { 57149573Swilliam wrtchar((so_at<<8)| c); 57243589Sdonahn } else { 57349573Swilliam wrtchar((ca<<8)| c); 57443589Sdonahn } 57543589Sdonahn if (row >= COL) row = 0; 57643589Sdonahn break ; 57743589Sdonahn } 57841053Swilliam } 57941053Swilliam } 58043589Sdonahn if (crtat >= Crtat+COL*(ROW)) { /* scroll check */ 58149573Swilliam if (openf) do sgetc(1); while (scroll); 58243589Sdonahn bcopy(Crtat+COL,Crtat,COL*(ROW-1)*CHR); 58349573Swilliam fillw ((bg_at<<8) + ' ', Crtat+COL*(ROW-1),COL) ; 58443589Sdonahn crtat -= COL ; 58543589Sdonahn } 58641053Swilliam } 58743589Sdonahn 58841053Swilliam #define L 0x0001 /* locking function */ 58941053Swilliam #define SHF 0x0002 /* keyboard shift */ 59041053Swilliam #define ALT 0x0004 /* alternate shift -- alternate chars */ 59141053Swilliam #define NUM 0x0008 /* numeric shift cursors vs. numeric */ 59241053Swilliam #define CTL 0x0010 /* control shift -- allows ctl function */ 59341053Swilliam #define CPS 0x0020 /* caps shift -- swaps case of letter */ 59441053Swilliam #define ASCII 0x0040 /* ascii code for this key */ 59541053Swilliam #define STP 0x0080 /* stop output */ 59641053Swilliam #define FUNC 0x0100 /* function key */ 59743589Sdonahn #define SCROLL 0x0200 /* scroll lock key */ 59841053Swilliam 59949695Swilliam unsigned __debug = 0; /*0xffe */; 60041053Swilliam u_short action[] = { 60141053Swilliam 0, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 0- 7 */ 60241053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 8-15 */ 60341053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 16-23 */ 60441053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, CTL, ASCII, ASCII, /* scan 24-31 */ 60541053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 32-39 */ 60641053Swilliam ASCII, ASCII, SHF , ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 40-47 */ 60741053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, SHF, ASCII, /* scan 48-55 */ 60843589Sdonahn ALT, ASCII, CPS , FUNC , FUNC , FUNC , FUNC , FUNC , /* scan 56-63 */ 60949573Swilliam FUNC , FUNC , FUNC , FUNC , FUNC , NUM, SCROLL, ASCII, /* scan 64-71 */ 61041053Swilliam ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 72-79 */ 61141053Swilliam ASCII, ASCII, ASCII, ASCII, 0, 0, 0, 0, /* scan 80-87 */ 61241053Swilliam 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 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 61541053Swilliam 61641053Swilliam u_char unshift[] = { /* no shift */ 61741053Swilliam 0, 033 , '1' , '2' , '3' , '4' , '5' , '6' , /* scan 0- 7 */ 61841053Swilliam '7' , '8' , '9' , '0' , '-' , '=' , 0177 ,'\t' , /* scan 8-15 */ 61941053Swilliam 62041053Swilliam 'q' , 'w' , 'e' , 'r' , 't' , 'y' , 'u' , 'i' , /* scan 16-23 */ 62141053Swilliam 'o' , 'p' , '[' , ']' , '\r' , CTL , 'a' , 's' , /* scan 24-31 */ 62241053Swilliam 62341053Swilliam 'd' , 'f' , 'g' , 'h' , 'j' , 'k' , 'l' , ';' , /* scan 32-39 */ 62441053Swilliam '\'' , '`' , SHF , '\\' , 'z' , 'x' , 'c' , 'v' , /* scan 40-47 */ 62541053Swilliam 62641053Swilliam 'b' , 'n' , 'm' , ',' , '.' , '/' , SHF , '*', /* scan 48-55 */ 62743589Sdonahn ALT , ' ' , CPS, 1, 2, 3 , 4, 5, /* scan 56-63 */ 62841053Swilliam 62943589Sdonahn 6, 7, 8, 9, 10, NUM, STP, '7', /* scan 64-71 */ 63041053Swilliam '8', '9', '-', '4', '5', '6', '+', '1', /* scan 72-79 */ 63141053Swilliam 63241053Swilliam '2', '3', '0', '.', 0, 0, 0, 0, /* scan 80-87 */ 63341053Swilliam 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 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 63641053Swilliam 63741053Swilliam u_char shift[] = { /* shift shift */ 63841053Swilliam 0, 033 , '!' , '@' , '#' , '$' , '%' , '^' , /* scan 0- 7 */ 63941053Swilliam '&' , '*' , '(' , ')' , '_' , '+' , 0177 ,'\t' , /* scan 8-15 */ 64041053Swilliam 'Q' , 'W' , 'E' , 'R' , 'T' , 'Y' , 'U' , 'I' , /* scan 16-23 */ 64141053Swilliam 'O' , 'P' , '{' , '}' , '\r' , CTL , 'A' , 'S' , /* scan 24-31 */ 64241053Swilliam 'D' , 'F' , 'G' , 'H' , 'J' , 'K' , 'L' , ':' , /* scan 32-39 */ 64341053Swilliam '"' , '~' , SHF , '|' , 'Z' , 'X' , 'C' , 'V' , /* scan 40-47 */ 64441053Swilliam 'B' , 'N' , 'M' , '<' , '>' , '?' , SHF , '*', /* scan 48-55 */ 64543589Sdonahn ALT , ' ' , CPS, 0, 0, ' ' , 0, 0, /* scan 56-63 */ 64643589Sdonahn 0, 0, 0, 0, 0, NUM, STP, '7', /* scan 64-71 */ 64741053Swilliam '8', '9', '-', '4', '5', '6', '+', '1', /* scan 72-79 */ 64841053Swilliam '2', '3', '0', '.', 0, 0, 0, 0, /* scan 80-87 */ 64941053Swilliam 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 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 65241053Swilliam 65341053Swilliam u_char ctl[] = { /* CTL shift */ 65441053Swilliam 0, 033 , '!' , 000 , '#' , '$' , '%' , 036 , /* scan 0- 7 */ 65541053Swilliam '&' , '*' , '(' , ')' , 037 , '+' , 034 ,'\177', /* scan 8-15 */ 65641053Swilliam 021 , 027 , 005 , 022 , 024 , 031 , 025 , 011 , /* scan 16-23 */ 65741053Swilliam 017 , 020 , 033 , 035 , '\r' , CTL , 001 , 023 , /* scan 24-31 */ 65841053Swilliam 004 , 006 , 007 , 010 , 012 , 013 , 014 , ';' , /* scan 32-39 */ 65941053Swilliam '\'' , '`' , SHF , 034 , 032 , 030 , 003 , 026 , /* scan 40-47 */ 66041053Swilliam 002 , 016 , 015 , '<' , '>' , '?' , SHF , '*', /* scan 48-55 */ 66143589Sdonahn ALT , ' ' , CPS, 0, 0, ' ' , 0, 0, /* scan 56-63 */ 66243589Sdonahn CPS, 0, 0, 0, 0, 0, 0, 0, /* scan 64-71 */ 66341053Swilliam 0, 0, 0, 0, 0, 0, 0, 0, /* scan 72-79 */ 66441053Swilliam 0, 0, 0, 0, 0, 0, 0, 0, /* scan 80-87 */ 66543589Sdonahn 0, 0, 033, '7' , '4' , '1' , 0, NUM, /* scan 88-95 */ 66643589Sdonahn '8' , '5' , '2' , 0, STP, '9' , '6' , '3' , /*scan 96-103*/ 66741053Swilliam '.' , 0, '*' , '-' , '+' , 0, 0, 0, /*scan 104-111*/ 66841053Swilliam 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 66941053Swilliam 67041053Swilliam #ifdef notdef 67141053Swilliam struct key { 67241053Swilliam u_short action; /* how this key functions */ 67341053Swilliam char ascii[8]; /* ascii result character indexed by shifts */ 67441053Swilliam }; 67541053Swilliam #endif 67641053Swilliam 67741053Swilliam 67843589Sdonahn #define KBSTAT 0x64 /* kbd status port */ 67943589Sdonahn #define KBS_INP_BUF_FUL 0x02 /* kbd char ready */ 68043589Sdonahn #define KBDATA 0x60 /* kbd data port */ 68141053Swilliam #define KBSTATUSPORT 0x61 /* kbd status */ 68241053Swilliam 68343589Sdonahn update_led() 68443589Sdonahn { 68543589Sdonahn while (inb(0x64)&2); /* wait input ready */ 68643589Sdonahn outb(0x60,0xED); /* LED Command */ 68743589Sdonahn while (inb(0x64)&2); /* wait input ready */ 68843589Sdonahn outb(0x60,scroll | 2*num | 4*caps); 68943589Sdonahn } 69041053Swilliam 69145535Sbill reset_cpu() { 69245535Sbill while(1) { 69345535Sbill while (inb(0x64)&2); /* wait input ready */ 69445535Sbill outb(0x64,0xFE); /* Reset Command */ 69545535Sbill DELAY(4000000); 69645535Sbill while (inb(0x64)&2); /* wait input ready */ 69745535Sbill outb(0x64,0xFF); /* Keyboard Reset Command */ 69845535Sbill } 69945535Sbill /* NOTREACHED */ 70045535Sbill } 70145535Sbill 70243589Sdonahn /* 70343589Sdonahn sgetc(noblock) : get a character from the keyboard. If noblock = 0 wait until 70443589Sdonahn a key is gotten. Otherwise return a 0x100 (256). 70543589Sdonahn */ 70643589Sdonahn int sgetc(noblock) 70743589Sdonahn { 70843589Sdonahn u_char dt; unsigned key; 70941053Swilliam loop: 71043589Sdonahn /* First see if there is something in the keyboard port */ 71143589Sdonahn if (inb(KBSTAT)&1) dt = inb(KBDATA); 71243589Sdonahn else { if (noblock) return (0x100); else goto loop; } 71341053Swilliam 71443589Sdonahn /* Check for cntl-alt-del */ 71543589Sdonahn if ((dt == 83)&&ctls&&alts) _exit(); 71641053Swilliam 71743589Sdonahn /* Check for make/break */ 71843589Sdonahn if (dt & 0x80) { 71943589Sdonahn /* break */ 72043589Sdonahn dt = dt & 0x7f ; 72143589Sdonahn switch (action[dt]) { 72243589Sdonahn case SHF: shfts = 0; break; 72343589Sdonahn case ALT: alts = 0; break; 72443589Sdonahn case CTL: ctls = 0; break; 72543589Sdonahn case FUNC: 72643589Sdonahn /* Toggle debug flags */ 72743589Sdonahn key = unshift[dt]; 72843589Sdonahn if(__debug & (1<<key)) __debug &= ~(1<<key) ; 72943589Sdonahn else __debug |= (1<<key) ; 73043589Sdonahn break; 73141053Swilliam } 73243589Sdonahn } else { 73343589Sdonahn /* make */ 73443589Sdonahn dt = dt & 0x7f ; 73543589Sdonahn switch (action[dt]) { 73643589Sdonahn /* LOCKING KEYS */ 73743589Sdonahn case NUM: num ^= 1; update_led(); break; 73843589Sdonahn case CPS: caps ^= 1; update_led(); break; 73943589Sdonahn case SCROLL: scroll ^= 1; update_led(); break; 74043589Sdonahn case STP: stp ^= 1; if(stp) goto loop; break; 74143589Sdonahn 74243589Sdonahn /* NON-LOCKING KEYS */ 74343589Sdonahn case SHF: shfts = 1; break; 74443589Sdonahn case ALT: alts = 1; break; 74543589Sdonahn case CTL: ctls = 1; break; 74643589Sdonahn case ASCII: 74743589Sdonahn if (shfts) dt = shift[dt]; 74843589Sdonahn else if (ctls) dt = ctl[dt]; 74943589Sdonahn else dt = unshift[dt]; 75043589Sdonahn if (caps && (dt >= 'a' && dt <= 'z')) dt -= 'a' - 'A'; 75143589Sdonahn return(dt); 75243589Sdonahn } 75341053Swilliam } 75443589Sdonahn if (noblock) return (0x100); else goto loop; 75541053Swilliam } 75641053Swilliam 75741053Swilliam pg(p,q,r,s,t,u,v,w,x,y,z) char *p; { 75841053Swilliam printf(p,q,r,s,t,u,v,w,x,y,z); 75941053Swilliam printf("\n"); 76041053Swilliam return(getchar()); 76141053Swilliam } 76241053Swilliam 76341053Swilliam /* special characters */ 76441053Swilliam #define bs 8 76541053Swilliam #define lf 10 76641053Swilliam #define cr 13 76741053Swilliam #define cntlc 3 76841053Swilliam #define del 0177 76941053Swilliam #define cntld 4 77041053Swilliam 77141053Swilliam getchar() 77241053Swilliam { 77341053Swilliam register char thechar; 77441053Swilliam register delay; 77541053Swilliam int x; 77641053Swilliam 77749592Swilliam pcconsoftc.cs_flags |= CSF_POLLING; 77841053Swilliam x=splhigh(); 77943589Sdonahn sput('>',0x6); 78045535Sbill /*while (1) {*/ 78141053Swilliam thechar = (char) sgetc(0); 78249592Swilliam pcconsoftc.cs_flags &= ~CSF_POLLING; 78343589Sdonahn splx(x); 78441053Swilliam switch (thechar) { 78541053Swilliam default: if (thechar >= ' ') 78643589Sdonahn sput(thechar,0x6); 78741053Swilliam return(thechar); 78841053Swilliam case cr: 78943589Sdonahn case lf: sput(cr,0x6); 79043589Sdonahn sput(lf,0x6); 79141053Swilliam return(lf); 79241053Swilliam case bs: 79341053Swilliam case del: 79443589Sdonahn sput(bs,0x6); 79543589Sdonahn sput(' ',0x6); 79643589Sdonahn sput(bs,0x6); 79741053Swilliam return(thechar); 79843589Sdonahn /*case cntlc: 79941053Swilliam sput('^',0xe) ; sput('C',0xe) ; sput('\r',0xe) ; sput('\n',0xe) ; 80043589Sdonahn _exit(-2) ; */ 80141053Swilliam case cntld: 80243589Sdonahn sput('^',0x6) ; sput('D',0x6) ; sput('\r',0x6) ; sput('\n',0x6) ; 80341053Swilliam return(0); 80441053Swilliam } 80545535Sbill /*}*/ 80641053Swilliam } 80749573Swilliam 80849573Swilliam #include "machine/dbg.h" 80949573Swilliam #include "machine/stdarg.h" 81049573Swilliam static nrow; 81149573Swilliam 81249573Swilliam void 81349573Swilliam #ifdef __STDC__ 81449573Swilliam dprintf(unsigned flgs, const char *fmt, ...) 81549573Swilliam #else 81649573Swilliam dprintf(flgs, fmt /*, va_alist */) 81749573Swilliam char *fmt; 81849573Swilliam unsigned flgs; 81949573Swilliam #endif 82049573Swilliam { extern unsigned __debug; 82149573Swilliam va_list ap; 82249573Swilliam 82349573Swilliam if((flgs&__debug) > DPAUSE) { 82449573Swilliam __color = ffs(flgs&__debug)+1; 82549573Swilliam va_start(ap,fmt); 82649573Swilliam kprintf(fmt, 1, (struct tty *)0, ap); 82749573Swilliam va_end(ap); 82849573Swilliam if (flgs&DPAUSE || nrow%24 == 23) { 82949573Swilliam int x; 83049573Swilliam x = splhigh(); 83149573Swilliam if (nrow%24 == 23) nrow = 0; 83249573Swilliam sgetc(0); 83349573Swilliam splx(x); 83449573Swilliam } 83549573Swilliam } 83649573Swilliam __color = 0; 83749573Swilliam } 83849573Swilliam 83949573Swilliam consinit() {} 840