Add Syscall: Exit, PutChar, GetCar, PutString, GetString
This commit is contained in:
parent
6f405265a5
commit
80fc250109
15 changed files with 359 additions and 17 deletions
66
code/userprog/consoledriver.cc
Normal file
66
code/userprog/consoledriver.cc
Normal file
|
@ -0,0 +1,66 @@
|
|||
#ifdef CHANGED
|
||||
#include "copyright.h"
|
||||
#include "system.h"
|
||||
#include "consoledriver.h"
|
||||
#include "synch.h"
|
||||
|
||||
#define STRINGBUFFER 8
|
||||
|
||||
static Semaphore *readAvail;
|
||||
static Semaphore *writeDone;
|
||||
|
||||
static void ReadAvailHandler(void *arg) { (void) arg; readAvail->V(); }
|
||||
static void WriteDoneHandler(void *arg) { (void) arg; writeDone->V(); }
|
||||
|
||||
ConsoleDriver::ConsoleDriver(const char *in, const char *out)
|
||||
{
|
||||
readAvail = new Semaphore("read avail", 0);
|
||||
writeDone = new Semaphore("write done", 0);
|
||||
console = new Console (in, out, ReadAvailHandler, WriteDoneHandler, NULL);
|
||||
}
|
||||
|
||||
ConsoleDriver::~ConsoleDriver()
|
||||
{
|
||||
delete console;
|
||||
delete writeDone;
|
||||
delete readAvail;
|
||||
}
|
||||
|
||||
void ConsoleDriver::PutChar( int ch)
|
||||
{
|
||||
console->TX(ch);
|
||||
writeDone->P();
|
||||
}
|
||||
|
||||
int ConsoleDriver::GetChar()
|
||||
{
|
||||
readAvail->P();
|
||||
int ch = console->RX();
|
||||
return ch;
|
||||
}
|
||||
|
||||
void ConsoleDriver::PutString(const char s[MAX_STRING_SIZE])
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < MAX_STRING_SIZE; i++){
|
||||
if (*(s+i) =='\0'){
|
||||
return;
|
||||
}
|
||||
PutChar(*(s+i));
|
||||
}
|
||||
}
|
||||
|
||||
void ConsoleDriver::GetString(char * s, int n)
|
||||
{
|
||||
int i;
|
||||
for ( i = 0; i < n - 1; i++){
|
||||
char c = GetChar();
|
||||
if ( c == '\0' || c == '\n' || c == EOF) {
|
||||
break;
|
||||
}
|
||||
*(s+i) = c;
|
||||
}
|
||||
*(s+i) = '\0';
|
||||
}
|
||||
|
||||
#endif // CHANGED
|
Loading…
Add table
Add a link
Reference in a new issue