diff options
Diffstat (limited to 'console.c')
-rw-r--r-- | console.c | 229 |
1 files changed, 113 insertions, 116 deletions
@@ -7,69 +7,22 @@ #include "param.h" #include "traps.h" #include "spinlock.h" -#include "dev.h" +#include "fs.h" +#include "file.h" #include "mmu.h" #include "proc.h" #include "x86.h" -#define CRTPORT 0x3d4 -#define BACKSPACE 0x100 +static void consputc(int); -static ushort *crt = (ushort*)0xb8000; // CGA memory +static int panicked = 0; static struct { struct spinlock lock; int locking; } cons; -static int panicked = 0; - static void -cgaputc(int c) -{ - int pos; - - // Cursor position: col + 80*row. - outb(CRTPORT, 14); - pos = inb(CRTPORT+1) << 8; - outb(CRTPORT, 15); - pos |= inb(CRTPORT+1); - - if(c == '\n') - pos += 80 - pos%80; - else if(c == BACKSPACE){ - if(pos > 0) - crt[--pos] = ' ' | 0x0700; - } else - crt[pos++] = (c&0xff) | 0x0700; // black on white - - if((pos/80) >= 24){ // Scroll up. - memmove(crt, crt+80, sizeof(crt[0])*23*80); - pos -= 80; - memset(crt+pos, 0, sizeof(crt[0])*(24*80 - pos)); - } - - outb(CRTPORT, 14); - outb(CRTPORT+1, pos>>8); - outb(CRTPORT, 15); - outb(CRTPORT+1, pos); - crt[pos] = ' ' | 0x0700; -} - -void -consputc(int c) -{ - if(panicked){ - cli(); - for(;;) - ; - } - - uartputc(c); - cgaputc(c); -} - -void printint(int xx, int base, int sgn) { static char digits[] = "0123456789abcdef"; @@ -79,10 +32,9 @@ printint(int xx, int base, int sgn) if(sgn && xx < 0){ neg = 1; - x = 0 - xx; - } else { + x = -xx; + } else x = xx; - } do{ buf[i++] = digits[x % base]; @@ -94,6 +46,7 @@ printint(int xx, int base, int sgn) consputc(buf[i]); } +//PAGEBREAK: 50 // Print to the console. only understands %d, %x, %p, %s. void cprintf(char *fmt, ...) @@ -108,42 +61,35 @@ cprintf(char *fmt, ...) argp = (uint*)(void*)&fmt + 1; state = 0; - for(i = 0; fmt[i]; i++){ - c = fmt[i] & 0xff; - switch(state){ - case 0: - if(c == '%') - state = '%'; - else - consputc(c); + for(i = 0; (c = fmt[i] & 0xff) != 0; i++){ + if(c != '%'){ + consputc(c); + continue; + } + c = fmt[++i] & 0xff; + if(c == 0) + break; + switch(c){ + case 'd': + printint(*argp++, 10, 1); + break; + case 'x': + case 'p': + printint(*argp++, 16, 0); + break; + case 's': + if((s = (char*)*argp++) == 0) + s = "(null)"; + for(; *s; s++) + consputc(*s); break; - case '%': - switch(c){ - case 'd': - printint(*argp++, 10, 1); - break; - case 'x': - case 'p': - printint(*argp++, 16, 0); - break; - case 's': - s = (char*)*argp++; - if(s == 0) - s = "(null)"; - for(; *s; s++) - consputc(*s); - break; - case '%': - consputc('%'); - break; - default: - // Print unknown % sequence to draw attention. - consputc('%'); - consputc(c); - break; - } - state = 0; + consputc('%'); + break; + default: + // Print unknown % sequence to draw attention. + consputc('%'); + consputc(c); break; } } @@ -152,21 +98,76 @@ cprintf(char *fmt, ...) release(&cons.lock); } -int -consolewrite(struct inode *ip, char *buf, int n) +void +panic(char *s) { int i; + uint pcs[10]; + + cli(); + cons.locking = 0; + cprintf("cpu%d: panic: ", cpu()); + cprintf(s); + cprintf("\n"); + getcallerpcs(&s, pcs); + for(i=0; i<10; i++) + cprintf(" %p", pcs[i]); + panicked = 1; // freeze other CPU + for(;;) + ; +} - iunlock(ip); - acquire(&cons.lock); - for(i = 0; i < n; i++) - consputc(buf[i] & 0xff); - release(&cons.lock); - ilock(ip); +//PAGEBREAK: 50 +#define BACKSPACE 0x100 +#define CRTPORT 0x3d4 +static ushort *crt = (ushort*)0xb8000; // CGA memory - return n; +static void +cgaputc(int c) +{ + int pos; + + // Cursor position: col + 80*row. + outb(CRTPORT, 14); + pos = inb(CRTPORT+1) << 8; + outb(CRTPORT, 15); + pos |= inb(CRTPORT+1); + + if(c == '\n') + pos += 80 - pos%80; + else if(c == BACKSPACE){ + if(pos > 0) + crt[--pos] = ' ' | 0x0700; + } else + crt[pos++] = (c&0xff) | 0x0700; // black on white + + if((pos/80) >= 24){ // Scroll up. + memmove(crt, crt+80, sizeof(crt[0])*23*80); + pos -= 80; + memset(crt+pos, 0, sizeof(crt[0])*(24*80 - pos)); + } + + outb(CRTPORT, 14); + outb(CRTPORT+1, pos>>8); + outb(CRTPORT, 15); + outb(CRTPORT+1, pos); + crt[pos] = ' ' | 0x0700; +} + +void +consputc(int c) +{ + if(panicked){ + cli(); + for(;;) + ; + } + + uartputc(c); + cgaputc(c); } +//PAGEBREAK: 50 #define INPUT_BUF 128 struct { struct spinlock lock; @@ -255,6 +256,21 @@ consoleread(struct inode *ip, char *dst, int n) return target - n; } +int +consolewrite(struct inode *ip, char *buf, int n) +{ + int i; + + iunlock(ip); + acquire(&cons.lock); + for(i = 0; i < n; i++) + consputc(buf[i] & 0xff); + release(&cons.lock); + ilock(ip); + + return n; +} + void consoleinit(void) { @@ -269,22 +285,3 @@ consoleinit(void) ioapicenable(IRQ_KBD, 0); } -void -panic(char *s) -{ - int i; - uint pcs[10]; - - cli(); - cons.locking = 0; - cprintf("cpu%d: panic: ", cpu()); - cprintf(s); - cprintf("\n"); - getcallerpcs(&s, pcs); - for(i=0; i<10; i++) - cprintf(" %p", pcs[i]); - panicked = 1; // freeze other CPU - for(;;) - ; -} - |