Added code to get the consoles to be split into their own

Fixed issue where exiting a shell would not close the parent console properly
Changed the way the consoles are linked to the processes by making it a pointer to the console
Added all the console changes for the above
Added a slow maze drawing program similar to the c64 version
This commit is contained in:
iDunnoDev
2022-12-14 01:04:42 +00:00
committed by iDunnoDev
parent 5b21654261
commit 924d90c066
8 changed files with 454 additions and 248 deletions

View File

@ -1,6 +1,7 @@
{ {
"files.associations": { "files.associations": {
"new": "c", "new": "c",
"*.tcc": "c" "*.tcc": "c",
"cstdlib": "c"
} }
} }

View File

@ -200,6 +200,7 @@ UPROGS=\
_argtest\ _argtest\
_screen\ _screen\
_cls\ _cls\
_maze\
fs.img: mkfs $(UPROGS) fs.img: mkfs $(UPROGS)
./mkfs fs.img $(UPROGS) ./mkfs fs.img $(UPROGS)
@ -223,8 +224,8 @@ QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
then echo "-gdb tcp::$(GDBPORT)"; \ then echo "-gdb tcp::$(GDBPORT)"; \
else echo "-s -p $(GDBPORT)"; fi) else echo "-s -p $(GDBPORT)"; fi)
ifndef CPUS ifndef CPUS
CPUS := 2 #CPUS := 2
#CPUS := 1 CPUS := 1 # For debugging
endif endif
QEMUOPTS = -drive file=fs.img,index=1,media=disk,format=raw -drive file=xv6.img,index=0,media=disk,format=raw -smp $(CPUS) -m 512 $(QEMUEXTRA) QEMUOPTS = -drive file=fs.img,index=1,media=disk,format=raw -drive file=xv6.img,index=0,media=disk,format=raw -smp $(CPUS) -m 512 $(QEMUEXTRA)

594
console.c
View File

@ -17,31 +17,35 @@
#define INPUT_BUF 128 #define INPUT_BUF 128
struct kbdbuffer { struct kbdbuffer
{
char buf[INPUT_BUF]; char buf[INPUT_BUF];
uint r; // Read index uint r; // Read index
uint w; // Write index uint w; // Write index
uint e; // Edit index uint e; // Edit index
}; };
struct kbdbuffer inputBuffer; struct kbdbuffer inputBuffer;
struct kbdbuffer * input = 0; struct kbdbuffer *input = 0;
struct vconsole { struct vconsole
{
uint consoleindex;
struct spinlock lock; struct spinlock lock;
struct proc* processowner; struct proc *processowner;
ushort screenbuffer[SCRWIDTH * SCRHEIGHT]; ushort screenbuffer[SCRWIDTH * SCRHEIGHT];
struct kbdbuffer keybuffer; struct kbdbuffer keybuffer;
int pos; int pos;
int active; int active;
int inuse;
}; };
struct vconsole consoles[MAXVCONSOLES]; struct vconsole consoles[MAXVCONSOLES];
struct vconsole* currentconsole = 0; struct vconsole *currentconsole = 0;
static uint currentconsoleindex = 0; //static uint currentconsoleindex = 0;
#define C(x) ((x) - '@') // Control-x #define C(x) ((x) - '@') // Control-x
void clearconsole(ushort *bufferin); void clearconsole(ushort *bufferin);
void loadscreenbuffer(ushort *bufferin); void loadscreenbuffer(ushort *bufferin);
@ -52,97 +56,114 @@ static void consputc(int);
static int panicked = 0; static int panicked = 0;
static struct { static struct
{
struct spinlock lock; struct spinlock lock;
int locking; int locking;
} cons; } cons;
static void printint(int xx, int base, int sign) { static void printint(int xx, int base, int sign)
{
static char digits[] = "0123456789abcdef"; static char digits[] = "0123456789abcdef";
char buf[16]; char buf[16];
int i; int i;
uint x; uint x;
if (sign && (sign = xx < 0)) { if (sign && (sign = xx < 0))
{
x = -xx; x = -xx;
} }
else { else
{
x = xx; x = xx;
} }
i = 0; i = 0;
do { do
{
buf[i++] = digits[x % base]; buf[i++] = digits[x % base];
} } while ((x /= base) != 0);
while ((x /= base) != 0);
if (sign) { if (sign)
{
buf[i++] = '-'; buf[i++] = '-';
} }
while (--i >= 0) { while (--i >= 0)
{
consputc(buf[i]); consputc(buf[i]);
} }
} }
// Print to the console. only understands %d, %x, %p, %s. // Print to the console. only understands %d, %x, %p, %s.
void cprintf(char *fmt, ...) { void cprintf(char *fmt, ...)
{
int i, c, locking; int i, c, locking;
uint *argp; uint *argp;
char *s; char *s;
locking = cons.locking; locking = cons.locking;
if (locking) { if (locking)
{
acquire(&cons.lock); acquire(&cons.lock);
} }
if (fmt == 0) { if (fmt == 0)
{
panic("null fmt"); panic("null fmt");
} }
argp = (uint*)(void*)(&fmt + 1); argp = (uint *)(void *)(&fmt + 1);
for (i = 0; (c = fmt[i] & 0xff) != 0; i++) { for (i = 0; (c = fmt[i] & 0xff) != 0; i++)
if (c != '%') { {
if (c != '%')
{
consputc(c); consputc(c);
continue; continue;
} }
c = fmt[++i] & 0xff; c = fmt[++i] & 0xff;
if (c == 0) { if (c == 0)
{
break; break;
} }
switch (c) { switch (c)
case 'd': {
printint(*argp++, 10, 1); case 'd':
break; printint(*argp++, 10, 1);
case 'x': break;
case 'p': case 'x':
printint(*argp++, 16, 0); case 'p':
break; printint(*argp++, 16, 0);
case 's': break;
if ((s = (char*)*argp++) == 0) { case 's':
s = "(null)"; if ((s = (char *)*argp++) == 0)
} {
for (; *s; s++) { s = "(null)";
consputc(*s); }
} for (; *s; s++)
break; {
case '%': consputc(*s);
consputc('%'); }
break; break;
default: case '%':
// Print unknown % sequence to draw attention. consputc('%');
consputc('%'); break;
consputc(c); default:
break; // Print unknown % sequence to draw attention.
consputc('%');
consputc(c);
break;
} }
} }
if (locking) { if (locking)
{
release(&cons.lock); release(&cons.lock);
} }
} }
void panic(char *s) { void panic(char *s)
{
int i; int i;
uint pcs[10]; uint pcs[10];
@ -153,32 +174,64 @@ void panic(char *s) {
cprintf(s); cprintf(s);
cprintf("\n"); cprintf("\n");
getcallerpcs(&s, pcs); getcallerpcs(&s, pcs);
for (i = 0; i < 10; i++) { for (i = 0; i < 10; i++)
{
cprintf(" %p", pcs[i]); cprintf(" %p", pcs[i]);
} }
panicked = 1; // freeze other CPU panicked = 1; // freeze other CPU
for (;;) { for (;;)
{
; ;
} }
} }
// function that checks and returns a valid vconsole pointer, since i was repeating the same checks i moved it
// into its own function to keep things looking clean
struct vconsole* getvalidprocessconsoleptr(void)
{
struct vconsole* inconsoleptr = 0;
// Check if a process has actually been created otherwise use the base console
if (myproc() == 0x0)
{
inconsoleptr = currentconsole;
if (inconsoleptr == 0)
{
// fall back to the base console if the process and currentconsole has no console assigned for some reason
inconsoleptr = &consoles[0];
}
}
else
{
inconsoleptr = myproc()->consoleptr;
if (inconsoleptr == 0)
{
// fall back to the console set in currentconsole
inconsoleptr = currentconsole;
if (inconsoleptr == 0)
{
// fall back to the base console if the process and currentconsole has no console assigned for some reason
inconsoleptr = &consoles[0];
}
}
}
return inconsoleptr;
}
#define BACKSPACE 0x100 #define BACKSPACE 0x100
#define CRTPORT 0x3d4 #define CRTPORT 0x3d4
#define TITLEOFF (SCRWIDTH * 1) // Size of the offset we need for the title bar #define TITLEOFF (SCRWIDTH * 1) // Size of the offset we need for the title bar
static ushort *crt = (ushort*)P2V(0xb8000); // CGA memory static ushort *crt = (ushort *)P2V(0xb8000); // CGA memory
static void cgaputc(int c) { static void cgaputc(int c)
{
int pos; int pos;
int consoleindex = 0; struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
// Check if a process has actually been created otherwise use the base console
if (myproc() != 0x0) ushort *currentbuffer = inconsoleptr->screenbuffer;
{
consoleindex = myproc()->consoleIndex;
}
ushort* currentbuffer = consoles[consoleindex].screenbuffer;
if (consoleindex == currentconsoleindex) if (inconsoleptr->inuse)
{ {
// Cursor position: col + 80*row. // Cursor position: col + 80*row.
outb(CRTPORT, 14); outb(CRTPORT, 14);
@ -188,45 +241,50 @@ static void cgaputc(int c) {
} }
else else
{ {
pos = consoles[consoleindex].pos; pos = inconsoleptr->pos;
} }
if (c == '\n') { if (c == '\n')
{
pos += SCRWIDTH - pos % SCRWIDTH; pos += SCRWIDTH - pos % SCRWIDTH;
} }
else if (c == BACKSPACE) { else if (c == BACKSPACE)
if (pos > (TITLEOFF)) { {
if (pos > (TITLEOFF))
{
currentbuffer[pos] = 0; // Clear the character from the buffer currentbuffer[pos] = 0; // Clear the character from the buffer
--pos; --pos;
} }
} }
else { else
//int posp = pos; {
if (consoleindex == currentconsoleindex) // int posp = pos;
if (inconsoleptr->inuse)
{ {
crt[pos] = (c & 0xff) | 0x0700; // black on white crt[pos] = (c & 0xff) | 0x0700; // black on white
} }
currentbuffer[pos] = (c & 0xff) | 0x0700; // black on white currentbuffer[pos] = (c & 0xff) | 0x0700; // black on white
pos++; pos++;
} }
if (pos < TITLEOFF || pos > SCRHEIGHT * SCRWIDTH) { if (pos < TITLEOFF || pos > SCRHEIGHT * SCRWIDTH)
{
panic("pos under/overflow"); panic("pos under/overflow");
} }
if ((pos / 80) >= 24) { // Scroll up. if ((pos / 80) >= 24)
memmove(currentbuffer + TITLEOFF, currentbuffer + (SCRWIDTH + TITLEOFF), sizeof(crt[0]) * (SCRHEIGHT - 1) * SCRWIDTH); { // Scroll up.
memmove(currentbuffer + TITLEOFF, currentbuffer + (SCRWIDTH + TITLEOFF), sizeof(crt[0]) * (SCRHEIGHT - 1) * SCRWIDTH);
pos -= 80; pos -= 80;
memset(currentbuffer + pos, 0, sizeof(crt[0]) * (SCRHEIGHT * SCRWIDTH - pos)); memset(currentbuffer + pos, 0, sizeof(crt[0]) * (SCRHEIGHT * SCRWIDTH - pos));
if (consoleindex == currentconsoleindex) if (inconsoleptr->inuse)
{ {
memmove(crt + TITLEOFF, crt + (SCRWIDTH + TITLEOFF), sizeof(crt[0]) * (SCRHEIGHT - 1) * SCRWIDTH); memmove(crt + TITLEOFF, crt + (SCRWIDTH + TITLEOFF), sizeof(crt[0]) * (SCRHEIGHT - 1) * SCRWIDTH);
memset(crt + pos, 0, sizeof(crt[0]) * (SCRHEIGHT * SCRWIDTH - pos)); memset(crt + pos, 0, sizeof(crt[0]) * (SCRHEIGHT * SCRWIDTH - pos));
} }
} }
consoles[consoleindex].pos = pos; inconsoleptr->pos = pos;
if (consoleindex == currentconsoleindex) if (inconsoleptr->inuse)
{ {
outb(CRTPORT, 14); outb(CRTPORT, 14);
outb(CRTPORT + 1, pos >> 8); outb(CRTPORT + 1, pos >> 8);
@ -236,32 +294,40 @@ static void cgaputc(int c) {
} }
} }
void consputc(int c) { void consputc(int c)
if (panicked) { {
if (panicked)
{
cli(); cli();
for (;;) { for (;;)
{
; ;
} }
} }
if (c == BACKSPACE) { if (c == BACKSPACE)
{
uartputc('\b'); uartputc('\b');
uartputc(' '); uartputc(' ');
uartputc('\b'); uartputc('\b');
} }
else { else
{
uartputc(c); uartputc(c);
} }
cgaputc(c); cgaputc(c);
} }
int consoleget(void) { int consoleget(void)
{
int c; int c;
acquire(&cons.lock); acquire(&cons.lock);
while ((c = kbdgetc()) <= 0) { while ((c = kbdgetc()) <= 0)
if (c == 0) { {
if (c == 0)
{
c = kbdgetc(); c = kbdgetc();
} }
} }
@ -271,108 +337,132 @@ int consoleget(void) {
return c; return c;
} }
void consoleintr(int (*getc)(void)) { void consoleintr(int (*getc)(void))
{
int c, doprocdump = 0, doconsoleswitch = 0, doconsolehome = 0; int c, doprocdump = 0, doconsoleswitch = 0, doconsolehome = 0;
acquire(&cons.lock); acquire(&cons.lock);
while ((c = getc()) >= 0) { while ((c = getc()) >= 0)
switch (c) { {
case C('P'): // Process listing. switch (c)
// procdump() locks cons.lock indirectly; invoke later {
doprocdump = 1; case C('P'): // Process listing.
break; // procdump() locks cons.lock indirectly; invoke later
case C('U'): // Kill line. doprocdump = 1;
while (input->e != input->w && break;
input->buf[(input->e - 1) % INPUT_BUF] != '\n') { case C('U'): // Kill line.
input->e--; while (input->e != input->w &&
consputc(BACKSPACE); input->buf[(input->e - 1) % INPUT_BUF] != '\n')
{
input->e--;
consputc(BACKSPACE);
}
break;
case C('H'):
case '\x7f': // Backspace
if (input->e != input->w)
{
input->e--;
consputc(BACKSPACE);
}
break;
case C('T'):
doconsoleswitch = 1;
break;
case C('K'):
doconsolehome = 1;
break;
default:
if (c != 0 && input->e - input->r < INPUT_BUF)
{
c = (c == '\r') ? '\n' : c;
input->buf[input->e++ % INPUT_BUF] = c;
consputc(c);
if (c == '\n' || c == C('D') || input->e == input->r + INPUT_BUF)
{
input->w = input->e;
wakeup(&(input->r));
} }
break; }
case C('H'): break;
case '\x7f': // Backspace
if (input->e != input->w) {
input->e--;
consputc(BACKSPACE);
}
break;
case C('T'):
doconsoleswitch = 1;
break;
case C('K'):
doconsolehome = 1;
break;
default:
if (c != 0 && input->e - input->r < INPUT_BUF) {
c = (c == '\r') ? '\n' : c;
input->buf[input->e++ % INPUT_BUF] = c;
consputc(c);
if (c == '\n' || c == C('D') || input->e == input->r + INPUT_BUF) {
input->w = input->e;
wakeup(&(input->r));
}
}
break;
} }
} }
release(&cons.lock); release(&cons.lock);
if (doprocdump) { if (doprocdump)
procdump(); // now call procdump() wo. cons.lock held {
procdump(); // now call procdump() wo. cons.lock held
} }
if (doconsolehome) { if (doconsolehome)
if (currentconsoleindex != 0) {
if (currentconsole->consoleindex != 0)
{ {
switchtoconsole(0); switchtoconsole(0);
} }
} }
if (doconsoleswitch) if (doconsoleswitch)
{ {
int toconsole = 0; struct vconsole* toconsole = 0;
for (int i = (currentconsoleindex + 1); i < MAXVCONSOLES; i++) acquire(&cons.lock);
for (int i = (currentconsole->consoleindex + 1); i < MAXVCONSOLES; i++)
{ {
if (consoles[i].active) if (consoles[i].active)
{ {
toconsole = i; toconsole = &consoles[i];
break; break;
} }
} }
if (toconsole != currentconsoleindex) if (toconsole == 0)
{
toconsole = getbaseconsoleptr();
}
release(&cons.lock);
if (!toconsole->inuse)
{ {
switchtoconsole(toconsole); switchtoconsole(toconsole);
} }
} }
} }
int consoleread(struct inode *ip, char *dst, int n) { int consoleread(struct inode *ip, char *dst, int n)
{
uint target; uint target;
int c; int c;
struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
struct kbdbuffer* consolekbdbuffer = &inconsoleptr->keybuffer;
iunlock(ip); iunlock(ip);
target = n; target = n;
acquire(&cons.lock); acquire(&cons.lock);
while (n > 0) { while (n > 0)
while (input->r == input->w) { {
if (myproc()->killed) { while (consolekbdbuffer->r == consolekbdbuffer->w)
release(&cons.lock); {
ilock(ip); if (myproc()->killed)
return -1; {
release(&cons.lock);
ilock(ip);
return -1;
}
sleep(&(consolekbdbuffer->r), &cons.lock);
} }
sleep(&(input->r), &cons.lock); c = consolekbdbuffer->buf[consolekbdbuffer->r++ % INPUT_BUF];
} if (c == C('D'))
c = input->buf[input->r++ % INPUT_BUF]; { // EOF
if (c == C('D')) { // EOF if (n < target)
if (n < target) { {
// Save ^D for next time, to make sure // Save ^D for next time, to make sure
// caller gets a 0-byte result. // caller gets a 0-byte result.
input->r--; consolekbdbuffer->r--;
}
break;
} }
break; *dst++ = c;
} --n;
*dst++ = c; if (c == '\n')
--n; {
if (c == '\n') { break;
break; }
}
} }
release(&cons.lock); release(&cons.lock);
ilock(ip); ilock(ip);
@ -380,14 +470,17 @@ int consoleread(struct inode *ip, char *dst, int n) {
return target - n; return target - n;
} }
int consolewrite(struct inode *ip, char *buf, int n) { int consolewrite(struct inode *ip, char *buf, int n)
{
int i; int i;
//struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
iunlock(ip); iunlock(ip);
acquire(&cons.lock); acquire(&cons.lock);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++)
consputc(buf[i] & 0xff); {
} consputc(buf[i] & 0xff);
}
release(&cons.lock); release(&cons.lock);
ilock(ip); ilock(ip);
@ -413,8 +506,13 @@ void testfillbuffer(ushort *bufferin)
} }
} }
struct vconsole* getbaseconsoleptr(void)
{
return &consoles[0];
}
void clearconsole(ushort *bufferin) void clearconsole(ushort *bufferin)
{ {
// Flood the screen buffer with blank spaces // Flood the screen buffer with blank spaces
memset(bufferin, 0, sizeof(bufferin[0]) * SCRHEIGHT * SCRWIDTH); memset(bufferin, 0, sizeof(bufferin[0]) * SCRHEIGHT * SCRWIDTH);
} }
@ -427,53 +525,59 @@ void loadscreenbuffer(ushort *bufferin)
void savescreenbuffer(ushort *bufferin) void savescreenbuffer(ushort *bufferin)
{ {
// Copy the memory from the console buffer to the crt buffer // Copy the memory from the console buffer to the crt buffer
memmove(bufferin, crt, sizeof(crt[0]) * SCRHEIGHT * SCRWIDTH); memmove(bufferin, crt, sizeof(crt[0]) * SCRHEIGHT * SCRWIDTH);
} }
void clearscreen(void) void clearscreen(void)
{ {
//cprintf("process id: %d", myproc()->consoleIndex); // cprintf("process id: %d", myproc()->consoleIndex);
//return; // return;
//cprintf("size of buffer item: %d\n", sizeof(consoles[currentconsoleindex].screenbuffer[0]) * (SCRHEIGHT * SCRWIDTH)); // cprintf("size of buffer item: %d\n", sizeof(consoles[currentconsoleindex].screenbuffer[0]) * (SCRHEIGHT * SCRWIDTH));
//cprintf("size of crt item: %d\n", sizeof(crt[0]) * (SCRHEIGHT * SCRWIDTH)); // cprintf("size of crt item: %d\n", sizeof(crt[0]) * (SCRHEIGHT * SCRWIDTH));
//return; // return;
int consoleindex = 0;
// Check if a process has actually been created otherwise use the base console // Check if a process has actually been created otherwise use the base console
if (myproc() != 0x0) struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
{
consoleindex = myproc()->consoleIndex;
}
if (consoleindex == currentconsoleindex)
{
acquire(&cons.lock);
int pos = TITLEOFF;
//testfillbuffer(consoles[currentconsoleindex].screenbuffer);
clearconsole(crt);
//loadscreenbuffer(consoles[currentconsoleindex].screenbuffer);
release(&cons.lock);
currentconsole->pos = pos; acquire(&cons.lock);
int pos = TITLEOFF;
clearconsole(inconsoleptr->screenbuffer);
inconsoleptr->pos = pos;
release(&cons.lock);
if (inconsoleptr->inuse)
{
acquire(&cons.lock);
loadscreenbuffer(inconsoleptr->screenbuffer);
release(&cons.lock);
outb(CRTPORT, 14); outb(CRTPORT, 14);
outb(CRTPORT + 1, pos >> 8); outb(CRTPORT + 1, pos >> 8);
outb(CRTPORT, 15); outb(CRTPORT, 15);
outb(CRTPORT + 1, pos); outb(CRTPORT + 1, pos);
} }
else
{
}
} }
void consoleinit(void) { void consoleinit(void)
initlock(&cons.lock, "console"); {
initlock(&cons.lock, "console");
// Initialise pointer to point to our console input buffer
currentconsole = &consoles[currentconsoleindex]; for (int i = 0; i < MAXVCONSOLES; i++)
{
consoles[i].consoleindex = i;
consoles[i].processowner = 0;
consoles[i].pos = 0;
consoles[i].active = 0;
consoles[i].inuse = 0;
//initlock(&consoles[i].lock, "vconsole" + i);
}
// Initialise pointer to point to our console input buffer
currentconsole = &consoles[0];
currentconsole->active = 1; currentconsole->active = 1;
initlock(&currentconsole->lock, "vconsole0"); currentconsole->inuse = 1;
input = &currentconsole->keybuffer; input = &currentconsole->keybuffer;
devsw[CONSOLE].write = consolewrite; devsw[CONSOLE].write = consolewrite;
@ -486,23 +590,34 @@ void consoleinit(void) {
ioapicenable(IRQ_KBD, 0); ioapicenable(IRQ_KBD, 0);
} }
int newconsole(void) struct vconsole* newconsole(void)
{ {
int result = 0; struct vconsole* result = 0;
acquire(&cons.lock);
for (int i = 1; i < MAXVCONSOLES; i++) for (int i = 1; i < MAXVCONSOLES; i++)
{ {
if (!consoles[i].active) if (!consoles[i].active)
{ {
result = i; consoles[i].processowner = myproc();
result = &consoles[i];
break; break;
} }
} }
release(&cons.lock);
return result; return result;
} }
int switchtoconsole(int consoleindex) void releaseallconsoles(void)
{
for (int i = 0; i < MAXVCONSOLES; i++)
{
consoles[i].inuse = 0;
}
}
int switchtoconsole(struct vconsole* consoleptr)
{ {
int pos; int pos;
@ -511,25 +626,26 @@ int switchtoconsole(int consoleindex)
outb(CRTPORT, 15); outb(CRTPORT, 15);
pos |= inb(CRTPORT + 1);*/ pos |= inb(CRTPORT + 1);*/
//consoles[currentconsoleindex].pos = pos; // consoles[currentconsoleindex].pos = pos;
//savescreenbuffer(consoles[currentconsoleindex].screenbuffer); // savescreenbuffer(consoles[currentconsoleindex].screenbuffer);
//acquire(&currentconsole->lock); // acquire(&currentconsole->lock);
//sleep(&(currentconsole), &currentconsole->lock); // sleep(&(currentconsole), &currentconsole->lock);
acquire(&cons.lock); acquire(&cons.lock);
currentconsoleindex = consoleindex; releaseallconsoles();
currentconsole = &consoles[currentconsoleindex]; currentconsole = consoleptr;
currentconsole->inuse = 1;
input = &currentconsole->keybuffer; input = &currentconsole->keybuffer;
//ioapicenable(IRQ_KBD, 0); // ioapicenable(IRQ_KBD, 0);
loadscreenbuffer(currentconsole->screenbuffer); loadscreenbuffer(currentconsole->screenbuffer);
release(&cons.lock); release(&cons.lock);
if (!currentconsole->active) if (!currentconsole->active)
{ {
clearscreen(); clearscreen();
cprintf("Welcome to Console: %d\n", currentconsoleindex); cprintf("Welcome to Console: %d\n", currentconsole->consoleindex);
currentconsole->active = 1; currentconsole->active = 1;
} }
else else
@ -540,21 +656,41 @@ int switchtoconsole(int consoleindex)
outb(CRTPORT + 1, pos >> 8); outb(CRTPORT + 1, pos >> 8);
outb(CRTPORT, 15); outb(CRTPORT, 15);
outb(CRTPORT + 1, pos); outb(CRTPORT + 1, pos);
} }
//wakeup(myproc()->chan); // wakeup(myproc()->chan);
return 0; return 0;
} }
int closeconsole(int consoleindex) int closeconsole(void)
{ {
clearconsole(currentconsole->screenbuffer); struct vconsole* consoleptr = myproc()->consoleptr;
currentconsole->active = 0;
switchtoconsole(0); cprintf("Console Owner PID: %d\n", consoleptr->processowner->pid);
if (myproc() == consoleptr->processowner)
{
clearconsole(consoleptr->screenbuffer);
consoleptr->active = 0;
if (consoleptr->inuse)
{
consoleptr->inuse = 0;
switchtoconsole(&consoles[0]);
}
}
return 0; return 0;
} }
int getcurrentconsoleindex(void) int getcurrentconsoleindex(void)
{ {
return currentconsoleindex; for (int i = 0; i < MAXVCONSOLES; i++)
{
if (consoles[i].inuse)
{
return consoles[i].consoleindex;
}
}
return -1;
} }

7
defs.h
View File

@ -22,11 +22,12 @@ void cprintf(char*, ...);
void consoleintr(int (*)(void)); void consoleintr(int (*)(void));
int consoleget(void); int consoleget(void);
void panic(char*) __attribute__((noreturn)); void panic(char*) __attribute__((noreturn));
int newconsole(void); struct vconsole* newconsole(void);
int switchtoconsole(int consoleindex); int switchtoconsole(struct vconsole*);
int closeconsole(int consoleindex); int closeconsole(void);
int getcurrentconsoleindex(void); int getcurrentconsoleindex(void);
void clearscreen(void); void clearscreen(void);
struct vconsole* getbaseconsoleptr(void);
// exec.c // exec.c
int exec(char*, char**); int exec(char*, char**);

66
maze.c Normal file
View File

@ -0,0 +1,66 @@
#include "types.h"
#include "user.h"
uint rngnumber(void)
{
// Take from http://stackoverflow.com/questions/1167253/implementation-of-rand
static unsigned int z1 = 12345, z2 = 12345, z3 = 12345, z4 = 12345;
unsigned int b;
b = ((z1 << 6) ^ z1) >> 13;
z1 = ((z1 & 4294967294U) << 18) ^ b;
b = ((z2 << 2) ^ z2) >> 27;
z2 = ((z2 & 4294967288U) << 2) ^ b;
b = ((z3 << 13) ^ z3) >> 21;
z3 = ((z3 & 4294967280U) << 7) ^ b;
b = ((z4 << 3) ^ z4) >> 12;
z4 = ((z4 & 4294967168U) << 13) ^ b;
return (z1 ^ z2 ^ z3 ^ z4) / 2;
}
int rngrange(int start, int end)
{
if (end < start)
{
int swtmp = start;
start = end;
end = swtmp;
}
int range = end - start + 1;
return rngnumber() % (range) + start;
}
int main(int argc, char *argv[]) {
int lines = 1000;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-l") == 0) {
lines = atoi(argv[i + 1]);
}
}
cls();
printf(1, "Start Maze\n");
for (int y = 0; y < lines; y++)
{
for (int x = 0; x < 79; x++)
{
int rndnum = rngrange(189, 197);
switch (rndnum)
{
case 189:
rndnum = 179;
break;
case 190:
rndnum = 180;
break;
}
printf(1, "%c", rndnum);
}
printf(1, "\n");
sleep(10);
}
exit();
}

13
proc.c
View File

@ -112,7 +112,7 @@ static struct proc* allocproc(void) {
p->context = (struct context*)sp; p->context = (struct context*)sp;
memset(p->context, 0, sizeof *p->context); memset(p->context, 0, sizeof *p->context);
p->context->eip = (uint)forkret; p->context->eip = (uint)forkret;
p->consoleIndex = 0; p->consoleptr = getbaseconsoleptr();
return p; return p;
} }
@ -142,7 +142,7 @@ void userinit(void) {
safestrcpy(p->name, "initcode", sizeof(p->name)); safestrcpy(p->name, "initcode", sizeof(p->name));
p->cwd = namei("/"); p->cwd = namei("/");
p->consoleIndex = 0; p->consoleptr = getbaseconsoleptr();
safestrcpy(p->title, "Shell (Root)", sizeof(p->title)); safestrcpy(p->title, "Shell (Root)", sizeof(p->title));
// this assignment to p->state lets other cores // this assignment to p->state lets other cores
@ -219,7 +219,7 @@ int fork(void) {
acquire(&ptable.lock); acquire(&ptable.lock);
np->state = RUNNABLE; np->state = RUNNABLE;
np->consoleIndex = curproc->consoleIndex; np->consoleptr = curproc->consoleptr;
release(&ptable.lock); release(&ptable.lock);
@ -260,16 +260,16 @@ void exit(void) {
for (p = ptable.proc; p < &ptable.proc[NPROC]; p++) { for (p = ptable.proc; p < &ptable.proc[NPROC]; p++) {
if (p->parent == curproc) { if (p->parent == curproc) {
p->parent = initproc; p->parent = initproc;
p->consoleIndex = 0; p->consoleptr = getbaseconsoleptr();
if (p->state == ZOMBIE) { if (p->state == ZOMBIE) {
wakeup1(initproc); wakeup1(initproc);
} }
} }
} }
if (curproc->consoleIndex != 0 && curproc->consoleIndex == getcurrentconsoleindex()) if (curproc->consoleptr != getbaseconsoleptr())
{ {
closeconsole(curproc->consoleIndex); closeconsole();
} }
// Jump into the scheduler, never to return. // Jump into the scheduler, never to return.
@ -305,6 +305,7 @@ int wait(void) {
p->name[0] = 0; p->name[0] = 0;
p->killed = 0; p->killed = 0;
p->state = UNUSED; p->state = UNUSED;
p->consoleptr = 0;
release(&ptable.lock); release(&ptable.lock);
return pid; return pid;
} }

2
proc.h
View File

@ -48,7 +48,7 @@ struct proc {
struct file *ofile[NOFILE]; // Open files struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory struct inode *cwd; // Current directory
char name[16]; // Process name (debugging) char name[16]; // Process name (debugging)
uint consoleIndex; struct vconsole* consoleptr;
char title[20]; char title[20];
}; };

View File

@ -93,12 +93,12 @@ int sys_uptime(void)
} }
int sys_greeting(void) int sys_greeting(void)
{ {
cprintf("Hello again\n"); cprintf("Hello again\n");
cprintf("Using Console: %d\n", getcurrentconsoleindex()); cprintf("Using Console: %d\n", getcurrentconsoleindex());
cprintf("Current PID: %d\n", myproc()->pid); cprintf("Current PID: %d\n", myproc()->pid);
cprintf("Current Parent PID: %d\n", myproc()->parent->pid); cprintf("Current Parent PID: %d\n", myproc()->parent->pid);
cprintf("Process Console: %d\n", myproc()->consoleIndex); //cprintf("Process Console: %d\n", myproc()->consoleptr->consoleindex);
return 0; return 0;
} }
@ -129,11 +129,11 @@ int sys_screen(void)
{ {
struct proc *curproc = myproc(); struct proc *curproc = myproc();
int result = 0; int result = 0;
int consoleindex = -1; struct vconsole* consoleptr = 0;
if ((consoleindex = newconsole()) != 0) if ((consoleptr = newconsole()) != 0)
{ {
curproc->consoleIndex = consoleindex; curproc->consoleptr = consoleptr;
switchtoconsole(consoleindex); switchtoconsole(consoleptr);
result = 1; result = 1;
} }
return result; return result;