xv6-cs450/console.c

158 lines
2.7 KiB
C
Raw Normal View History

#include "types.h"
#include "x86.h"
2006-06-12 17:22:12 +02:00
#include "defs.h"
#include "spinlock.h"
2006-07-29 11:35:02 +02:00
struct spinlock console_lock = { "console" };
2006-07-17 03:25:22 +02:00
int panicked = 0;
int use_console_lock = 0;
2006-06-12 17:22:12 +02:00
2006-06-22 17:51:57 +02:00
/*
* copy console output to parallel port, which you can tell
* .bochsrc to copy to the stdout:
* parport1: enabled=1, file="/dev/stdout"
*/
static void
lpt_putc(int c)
{
int i;
for (i = 0; !(inb(0x378+1) & 0x80) && i < 12800; i++)
;
outb(0x378+0, c);
outb(0x378+2, 0x08|0x04|0x01);
outb(0x378+2, 0x08);
}
static void
cons_putc(int c)
2006-06-12 17:22:12 +02:00
{
int crtport = 0x3d4; // io port of CGA
2006-07-20 11:07:53 +02:00
ushort *crt = (ushort *) 0xB8000; // base of CGA memory
2006-06-12 17:22:12 +02:00
int ind;
2006-07-17 03:25:22 +02:00
if(panicked){
cli();
2006-07-17 03:25:22 +02:00
for(;;)
;
}
2006-06-22 17:51:57 +02:00
lpt_putc(c);
2006-06-12 17:22:12 +02:00
// cursor position, 16 bits, col + 80*row
outb(crtport, 14);
ind = inb(crtport + 1) << 8;
outb(crtport, 15);
ind |= inb(crtport + 1);
c &= 0xff;
if(c == '\n'){
ind -= (ind % 80);
ind += 80;
} else {
c |= 0x0700; // black on white
crt[ind] = c;
ind++;
2006-06-12 17:22:12 +02:00
}
if((ind / 80) >= 24){
// scroll up
memmove(crt, crt + 80, sizeof(crt[0]) * (23 * 80));
2006-06-12 17:22:12 +02:00
ind -= 80;
memset(crt + ind, 0, sizeof(crt[0]) * ((24 * 80) - ind));
}
outb(crtport, 14);
outb(crtport + 1, ind >> 8);
outb(crtport, 15);
outb(crtport + 1, ind);
}
2006-06-12 17:22:12 +02:00
void
printint(int xx, int base, int sgn)
{
char buf[16];
char digits[] = "0123456789ABCDEF";
int i = 0, neg = 0;
uint x;
2006-06-12 17:22:12 +02:00
if(sgn && xx < 0){
neg = 1;
x = 0 - xx;
} else {
x = xx;
}
do {
buf[i++] = digits[x % base];
} while((x /= base) != 0);
if(neg)
buf[i++] = '-';
while(--i >= 0)
cons_putc(buf[i]);
2006-06-12 17:22:12 +02:00
}
/*
2006-07-16 18:00:03 +02:00
* print to the console. only understands %d, %x, %p, %s.
2006-06-12 17:22:12 +02:00
*/
void
cprintf(char *fmt, ...)
{
int i, state = 0, c;
uint *ap = (uint *)(void*)&fmt + 1;
2006-06-12 17:22:12 +02:00
if(use_console_lock)
acquire(&console_lock);
2006-06-12 17:22:12 +02:00
for(i = 0; fmt[i]; i++){
c = fmt[i] & 0xff;
if(state == 0){
if(c == '%'){
state = '%';
} else {
cons_putc(c);
2006-06-12 17:22:12 +02:00
}
} else if(state == '%'){
if(c == 'd'){
printint(*ap, 10, 1);
ap++;
2006-07-11 03:07:40 +02:00
} else if(c == 'x' || c == 'p'){
2006-06-12 17:22:12 +02:00
printint(*ap, 16, 0);
ap++;
2006-07-16 18:00:03 +02:00
} else if(c == 's'){
char *s = (char*)*ap;
ap++;
while(*s != 0){
cons_putc(*s);
2006-07-16 18:00:03 +02:00
s++;
}
2006-06-12 17:22:12 +02:00
} else if(c == '%'){
cons_putc(c);
2006-07-16 18:00:03 +02:00
} else {
// Unknown % sequence. Print it to draw attention.
cons_putc('%');
cons_putc(c);
2006-06-12 17:22:12 +02:00
}
state = 0;
}
}
if(use_console_lock)
release(&console_lock);
2006-06-12 17:22:12 +02:00
}
void
panic(char *s)
{
__asm __volatile("cli");
use_console_lock = 0;
cprintf("panic: ");
2006-06-12 17:22:12 +02:00
cprintf(s, 0);
cprintf("\n", 0);
2006-07-17 03:25:22 +02:00
panicked = 1; // freeze other CPU
for(;;)
2006-06-12 17:22:12 +02:00
;
}